]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
bda350a7981737c6d04370f87c2dcfe79e18299f
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.nuxeo.client.java;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.util.Hashtable;\r
6 import java.util.Iterator;\r
7 \r
8 import javax.servlet.ServletContext;\r
9 \r
10 import org.collectionspace.services.config.RepositoryClientConfigType;\r
11 import org.collectionspace.services.nuxeo.util.NuxeoUtils;\r
12 import org.nuxeo.ecm.core.api.DocumentModel;\r
13 import org.nuxeo.ecm.core.api.DocumentModelList;\r
14 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;\r
15 import org.nuxeo.osgi.application.FrameworkBootstrap;\r
16 import org.slf4j.Logger;\r
17 import org.slf4j.LoggerFactory;\r
18 \r
19 public class NuxeoConnectorEmbedded {\r
20         /*\r
21     <host>127.0.0.1</host>\r
22     <port>62474</port> <!-- java -->\r
23  */\r
24         private Logger logger = LoggerFactory.getLogger(NuxeoConnectorEmbedded.class);\r
25 \r
26         public final static String NUXEO_CLIENT_DIR = "nuxeo-client";\r
27         public final static String NUXEO_SERVER_DIR = "nuxeo-server";\r
28         private final static String ERROR_CONNECTOR_NOT_INITIALIZED = "NuxeoConnector is not initialized!";\r
29 \r
30 \r
31         private static final String HOST = "127.0.0.1";\r
32         private static final int PORT = 62474;\r
33                 \r
34         private final static String CSPACE_JEESERVER_HOME = "CSPACE_CONTAINER";\r
35         private final static String CSPACE_NUXEO_HOME = "CSPACE_NUXEO_HOME";\r
36         \r
37         private static final String NUXEO_CLIENT_USERNAME = "NUXEO_CLIENT_USERNAME";\r
38         private static final String NUXEO_CLIENT_PASSWORD = "NUXEO_CLIENT_PASSWORD";\r
39 \r
40         private static final NuxeoConnectorEmbedded self = new NuxeoConnectorEmbedded();\r
41         private NuxeoClientEmbedded client;\r
42         private ServletContext servletContext = null;\r
43         private volatile boolean initialized = false; // use volatile for lazy\r
44                                                                                                         // initialization in\r
45                                                                                                         // singleton\r
46         private RepositoryClientConfigType repositoryClientConfig;\r
47         public FrameworkBootstrap fb;\r
48 \r
49         private NuxeoConnectorEmbedded() {\r
50         }\r
51 \r
52         public final static NuxeoConnectorEmbedded getInstance() {\r
53                 return self;\r
54         }\r
55 \r
56         private String getClientUserName() {\r
57                 String username = System.getenv(NUXEO_CLIENT_USERNAME);\r
58                 if (username == null) {\r
59                         username = "Administrator";\r
60                 }\r
61                 return username;\r
62         }\r
63 \r
64         private String getClientPassword() {\r
65                 String password = System.getenv(NUXEO_CLIENT_PASSWORD);\r
66                 if (password == null) {\r
67                         password = "Administrator";\r
68                 }\r
69                 return password;\r
70         }\r
71         \r
72         private String getServerRootPath() {\r
73                 String result = null;\r
74                 \r
75                 String prop = System.getenv(CSPACE_JEESERVER_HOME);\r
76                 if (prop == null || prop.isEmpty()) {\r
77                         logger.error("The following CollectionSpace services' environment variable needs to be set: " + CSPACE_JEESERVER_HOME);\r
78                 }\r
79                 \r
80                 return result;\r
81         }\r
82         \r
83         private String getNuxeoServerPath(String serverRootPath) throws IOException {\r
84                 String result = null;\r
85                 //\r
86                 // Look for the CSPACE_NUXEO_HOME environment variable that might contain the fully qualified path of the\r
87                 // Nuxeo EP configuration directory.\r
88                 //\r
89                 String prop = System.getenv(CSPACE_NUXEO_HOME);\r
90                 if (prop != null && !prop.isEmpty()) {\r
91                         result = prop;\r
92                 } else {\r
93                         //\r
94                         // Could not find the 'CSPACE_NUXEO_HOME' environment variable, so using the default location instead.\r
95                         //\r
96                         result = serverRootPath + "/" + NUXEO_SERVER_DIR;\r
97                 }\r
98                 \r
99                 return result;\r
100         }\r
101         \r
102         private File getNuxeoServerDir(String serverRootPath) throws IOException {\r
103                 File result = null;\r
104                 String errMsg = null;\r
105                 \r
106                 String path = getNuxeoServerPath(serverRootPath);\r
107                 if (path != null) {\r
108                         File temp = new File(path);\r
109                         if (temp.exists() == true) {\r
110                                 result = temp;\r
111                         } else {\r
112                                 errMsg = "The Nuxeo EP configuration directory is missing or inaccessible at: '" + path + "'.";\r
113                         }\r
114                 }\r
115                 \r
116                 if (result == null) {\r
117                         if (errMsg == null) {\r
118                                 path = path != null ? path : "<empty>";\r
119                                 errMsg = "Unknown error trying to find Nuxeo configuration: '" +\r
120                                                 CSPACE_NUXEO_HOME + "' = " +\r
121                                                 path;\r
122                         }\r
123                         throw new IOException(errMsg);\r
124                 }\r
125                 \r
126                 return result;\r
127         }\r
128 \r
129         //\r
130         // Start/boot the Nuxeo EP server instance\r
131         //\r
132         private void startNuxeoEP(String serverRootPath) throws Exception {\r
133                 File nuxeoHomeDir = getNuxeoServerDir(serverRootPath);\r
134 \r
135                 if (logger.isInfoEnabled() == true) {\r
136                         logger.info("Starting Nuxeo EP server from configuration at: "\r
137                                         + nuxeoHomeDir.getCanonicalPath());\r
138                 }\r
139                 \r
140                 fb = new FrameworkBootstrap(NuxeoConnectorEmbedded.class.getClassLoader(),\r
141                                 nuxeoHomeDir);\r
142                 fb.initialize();\r
143                 fb.start();\r
144         }\r
145 \r
146         /**\r
147          * release releases resources occupied by Nuxeo remoting client runtime\r
148          * \r
149          * @throws java.lang.Exception\r
150          */\r
151         public void release() throws Exception {\r
152                 if (initialized == true) {\r
153                         try {\r
154                                 client.tryDisconnect();\r
155                         } catch (Exception e) {\r
156                                 logger.error("Failed to disconnect Nuxeo connection.", e);\r
157                                 throw e;\r
158                         }\r
159                 }\r
160         }\r
161 \r
162         public void initialize(String serverRootPath,\r
163                         RepositoryClientConfigType repositoryClientConfig,\r
164                         ServletContext servletContext) throws Exception {\r
165                 if (initialized == false) {\r
166                         synchronized (this) {\r
167                                 if (initialized == false) {\r
168                                         this.servletContext = servletContext;\r
169                                         this.repositoryClientConfig = repositoryClientConfig;\r
170                                         startNuxeoEP(serverRootPath);\r
171                                         client = new NuxeoClientEmbedded();\r
172                                         initialized = true;\r
173                                 }\r
174                         }\r
175                 }\r
176         }\r
177 \r
178         /**\r
179          * releaseRepositorySession releases given repository session\r
180          * \r
181          * @param repoSession\r
182          * @throws java.lang.Exception\r
183          */\r
184         public void releaseRepositorySession(RepositoryInstance repoSession)\r
185                         throws Exception {\r
186                 if (repoSession != null) {\r
187                         getClient().releaseRepository(repoSession);\r
188 \r
189                         if (logger.isDebugEnabled()) {\r
190                                 logger.debug("releaseRepositorySession() released repository session");\r
191                         }\r
192                 }\r
193         }\r
194 \r
195         /**\r
196          * getRepositorySession get session to default repository\r
197          * \r
198          * @return RepositoryInstance\r
199          * @throws java.lang.Exception\r
200          */\r
201         public RepositoryInstance getRepositorySession() throws Exception {\r
202                 RepositoryInstance repoSession = getClient().openRepository();\r
203                 if (logger.isDebugEnabled()) {\r
204                         logger.debug("getRepositorySession() opened repository session");\r
205                 }\r
206                 return repoSession;\r
207         }\r
208 \r
209         /**\r
210          * getClient get Nuxeo client for accessing Nuxeo services remotely using\r
211          * Nuxeo Java (EJB) Remote APIS\r
212          * \r
213          * @return NuxeoClient\r
214          * @throws java.lang.Exception\r
215          */\r
216         public NuxeoClientEmbedded getClient() throws Exception {\r
217                 if (initialized == true) {\r
218                         if (client.isConnected()) {\r
219 //                              client.login();\r
220                                 return client;\r
221                         } else {\r
222                                 client.forceConnect(this.HOST,\r
223                                                 this.PORT);\r
224                                 if (logger.isDebugEnabled()) {\r
225                                         logger.debug("getClient(): connection successful port="\r
226                                                         + this.PORT);\r
227                                 }\r
228                                 return client;\r
229                         }\r
230                 }\r
231                 //\r
232                 // Nuxeo connection was not initialized\r
233                 //\r
234                 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);\r
235                 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);\r
236         }\r
237         \r
238         void releaseClient() throws Exception {\r
239                 if (initialized == true) {\r
240 //                      client.logout();\r
241                 } else {\r
242                         //\r
243                         // Nuxeo connection was not initialized\r
244                         //\r
245                         logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);\r
246                         throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);\r
247                 }\r
248         }\r
249 \r
250         /**\r
251          * retrieveWorkspaceIds retrieves all workspace ids from default repository\r
252          * \r
253          * @param tenantDomain\r
254          *            domain representing tenant\r
255          * @return\r
256          * @throws java.lang.Exception\r
257          */\r
258         public Hashtable<String, String> retrieveWorkspaceIds(String tenantDomain)\r
259                         throws Exception {\r
260                 RepositoryInstance repoSession = null;\r
261                 Hashtable<String, String> workspaceIds = new Hashtable<String, String>();\r
262                 try {\r
263                         repoSession = getRepositorySession();\r
264                         DocumentModel rootDoc = repoSession.getRootDocument();\r
265                         DocumentModelList rootChildrenList = repoSession\r
266                                         .getChildren(rootDoc.getRef());\r
267                         Iterator<DocumentModel> diter = rootChildrenList.iterator();\r
268                         while (diter.hasNext()) {\r
269                                 DocumentModel domain = diter.next();\r
270                                 String domainPath = "/" + tenantDomain;\r
271                                 if (!domain.getPathAsString().equalsIgnoreCase(domainPath)) {\r
272                                         continue;\r
273                                 }\r
274                                 if (logger.isDebugEnabled()) {\r
275                                         logger.debug("domain=" + domain.toString());\r
276                                 }\r
277                                 DocumentModelList domainChildrenList = repoSession\r
278                                                 .getChildren(domain.getRef());\r
279                                 Iterator<DocumentModel> witer = domainChildrenList.iterator();\r
280                                 while (witer.hasNext()) {\r
281                                         DocumentModel childNode = witer.next();\r
282                                         if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) { \r
283                                                 DocumentModelList workspaceList = repoSession\r
284                                                                 .getChildren(childNode.getRef());\r
285                                                 Iterator<DocumentModel> wsiter = workspaceList\r
286                                                                 .iterator();\r
287                                                 while (wsiter.hasNext()) {\r
288                                                         DocumentModel workspace = wsiter.next();\r
289                                                         if (logger.isDebugEnabled()) {\r
290                                                                 logger.debug("workspace name="\r
291                                                                                 + workspace.getName() + " id="\r
292                                                                                 + workspace.getId());\r
293                                                         }\r
294                                                         workspaceIds.put(workspace.getName().toLowerCase(),\r
295                                                                         workspace.getId());\r
296                                                 }\r
297                                         }\r
298                                 }\r
299                         }\r
300                 } catch (Exception e) {\r
301                         if (logger.isDebugEnabled()) {\r
302                                 logger.debug("retrieveWorkspaceIds() caught exception ", e);\r
303                         }\r
304                         throw e;\r
305                 } finally {\r
306                         if (repoSession != null) {\r
307                                 releaseRepositorySession(repoSession);\r
308                         }\r
309                 }\r
310                 return workspaceIds;\r
311         }\r
312         \r
313         @Deprecated\r
314     private void loadBundles() throws Exception {\r
315     }\r
316         \r
317 }\r