]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f8f462342dff75253f4bbb5bd15b5c4cdf5ed633
[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.Collection;\r
6 import java.util.Hashtable;\r
7 import java.util.Iterator;\r
8 \r
9 import javax.naming.InitialContext;\r
10 import javax.naming.NamingException;\r
11 import javax.servlet.ServletContext;\r
12 \r
13 import org.collectionspace.services.common.api.JEEServerDeployment;\r
14 import org.collectionspace.services.config.RepositoryClientConfigType;\r
15 import org.collectionspace.services.config.tenant.RepositoryDomainType;\r
16 import org.collectionspace.services.nuxeo.util.NuxeoUtils;\r
17 import org.nuxeo.ecm.core.NXCore;\r
18 import org.nuxeo.ecm.core.api.DocumentModel;\r
19 import org.nuxeo.ecm.core.api.DocumentModelList;\r
20 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;\r
21 import org.nuxeo.ecm.core.model.Repository;\r
22 import org.nuxeo.osgi.application.FrameworkBootstrap;\r
23 import org.nuxeo.ecm.core.repository.RepositoryDescriptor;\r
24 import org.nuxeo.ecm.core.repository.RepositoryFactory;\r
25 import org.nuxeo.ecm.core.storage.sql.ra.ConnectionFactoryImpl;\r
26 import org.nuxeo.ecm.core.storage.sql.ra.ManagedConnectionFactoryImpl;\r
27 \r
28 import org.slf4j.Logger;\r
29 import org.slf4j.LoggerFactory;\r
30 \r
31 public class NuxeoConnectorEmbedded {\r
32         /*\r
33     <host>127.0.0.1</host>\r
34     <port>62474</port> <!-- java -->\r
35  */\r
36         private Logger logger = LoggerFactory.getLogger(NuxeoConnectorEmbedded.class);\r
37 \r
38         public final static String NUXEO_CLIENT_DIR = JEEServerDeployment.NUXEO_CLIENT_DIR;\r
39         public final static String NUXEO_SERVER_DIR = JEEServerDeployment.NUXEO_SERVER_DIR;\r
40         private final static String ERROR_CONNECTOR_NOT_INITIALIZED = "NuxeoConnector is not initialized!";\r
41 \r
42 \r
43         private static final String HOST = "127.0.0.1";\r
44         private static final int PORT = 62474;\r
45                 \r
46         private final static String CSPACE_JEESERVER_HOME = "CSPACE_CONTAINER";\r
47         private final static String CSPACE_NUXEO_HOME = "CSPACE_NUXEO_HOME";\r
48         \r
49         private static final String NUXEO_CLIENT_USERNAME = "NUXEO_CLIENT_USERNAME";\r
50         private static final String NUXEO_CLIENT_PASSWORD = "NUXEO_CLIENT_PASSWORD";\r
51 \r
52         private static final NuxeoConnectorEmbedded self = new NuxeoConnectorEmbedded();\r
53         private NuxeoClientEmbedded client;\r
54         private ServletContext servletContext = null;\r
55         private volatile boolean initialized = false; // use volatile for lazy\r
56                                                                                                         // initialization in\r
57                                                                                                         // singleton\r
58         private RepositoryClientConfigType repositoryClientConfig;\r
59         public FrameworkBootstrap fb;\r
60 \r
61         private NuxeoConnectorEmbedded() {\r
62         }\r
63 \r
64         public final static NuxeoConnectorEmbedded getInstance() {\r
65                 return self;\r
66         }\r
67 \r
68         private String getClientUserName() {\r
69                 String username = System.getenv(NUXEO_CLIENT_USERNAME);\r
70                 if (username == null) {\r
71                         username = "Administrator";\r
72                 }\r
73                 return username;\r
74         }\r
75 \r
76         private String getClientPassword() {\r
77                 String password = System.getenv(NUXEO_CLIENT_PASSWORD);\r
78                 if (password == null) {\r
79                         password = "Administrator";\r
80                 }\r
81                 return password;\r
82         }\r
83         \r
84         private String getServerRootPath() {\r
85                 String result = null;\r
86                 \r
87                 String prop = System.getenv(CSPACE_JEESERVER_HOME);\r
88                 if (prop == null || prop.isEmpty()) {\r
89                         logger.error("The following CollectionSpace services' environment variable needs to be set: " + CSPACE_JEESERVER_HOME);\r
90                 }\r
91                 \r
92                 return result;\r
93         }\r
94         \r
95         private String getNuxeoServerPath(String serverRootPath) throws IOException {\r
96                 String result = null;\r
97                 //\r
98                 // Look for the CSPACE_NUXEO_HOME environment variable that might contain the fully qualified path of the\r
99                 // Nuxeo EP configuration directory.\r
100                 //\r
101                 String prop = System.getenv(CSPACE_NUXEO_HOME);\r
102                 if (prop != null && !prop.isEmpty()) {\r
103                         result = prop;\r
104                 } else {\r
105                         //\r
106                         // Could not find the 'CSPACE_NUXEO_HOME' environment variable, so using the default location instead.\r
107                         //\r
108                         result = serverRootPath + "/" + NUXEO_SERVER_DIR;\r
109                 }\r
110                 \r
111                 return result;\r
112         }\r
113         \r
114         private File getNuxeoServerDir(String serverRootPath) throws IOException {\r
115                 File result = null;\r
116                 String errMsg = null;\r
117                 \r
118                 String path = getNuxeoServerPath(serverRootPath);\r
119                 if (path != null) {\r
120                         File temp = new File(path);\r
121                         if (temp.exists() == true) {\r
122                                 result = temp;\r
123                         } else {\r
124                                 errMsg = "The Nuxeo EP configuration directory is missing or inaccessible at: '" + path + "'.";\r
125                         }\r
126                 }\r
127                 \r
128                 if (result == null) {\r
129                         if (errMsg == null) {\r
130                                 path = path != null ? path : "<empty>";\r
131                                 errMsg = "Unknown error trying to find Nuxeo configuration: '" +\r
132                                                 CSPACE_NUXEO_HOME + "' = " +\r
133                                                 path;\r
134                         }\r
135                         throw new IOException(errMsg);\r
136                 }\r
137                 \r
138                 return result;\r
139         }\r
140 \r
141         //\r
142         // Start/boot the Nuxeo EP server instance\r
143         //\r
144         private void startNuxeoEP(String serverRootPath) throws Exception {\r
145                 File nuxeoHomeDir = getNuxeoServerDir(serverRootPath);\r
146 \r
147                 if (logger.isInfoEnabled() == true) {\r
148                         logger.info("Starting Nuxeo EP server from configuration at: "\r
149                                         + nuxeoHomeDir.getCanonicalPath());\r
150                 }\r
151                 \r
152                 fb = new FrameworkBootstrap(NuxeoConnectorEmbedded.class.getClassLoader(),\r
153                                 nuxeoHomeDir);\r
154                 fb.initialize();\r
155                 fb.start();\r
156         }\r
157 \r
158         /**\r
159          * release releases resources occupied by Nuxeo remoting client runtime\r
160          * \r
161          * @throws java.lang.Exception\r
162          */\r
163         public void release() throws Exception {\r
164                 if (initialized == true) {\r
165                         try {\r
166                                 client.tryDisconnect();\r
167                         } catch (Exception e) {\r
168                                 logger.error("Failed to disconnect Nuxeo connection.", e);\r
169                                 throw e;\r
170                         }\r
171                 }\r
172         }\r
173 \r
174         public void initialize(String serverRootPath,\r
175                         RepositoryClientConfigType repositoryClientConfig,\r
176                         ServletContext servletContext) throws Exception {\r
177                 if (initialized == false) {\r
178                         synchronized (this) {\r
179                                 if (initialized == false) {\r
180                                         this.servletContext = servletContext;\r
181                                         this.repositoryClientConfig = repositoryClientConfig;\r
182                                         startNuxeoEP(serverRootPath);\r
183                                         client = new NuxeoClientEmbedded();\r
184                                         initialized = true;\r
185                                 }\r
186                         }\r
187                 }\r
188         }\r
189         \r
190         public String getDatabaseName(String repoName) {\r
191                 String result = null;\r
192                 \r
193                 try {\r
194                         this.getRepositoryDescriptor(repoName);\r
195                 } catch (Exception e1) {\r
196                         // TODO Auto-generated catch block\r
197                         e1.printStackTrace();\r
198                 }\r
199                 \r
200                 Repository repository = null;\r
201                 try {\r
202                         repository = this.lookupRepository(repoName);\r
203                 } catch (Exception e) {\r
204                         // TODO Auto-generated catch block\r
205                         e.printStackTrace();\r
206                 }\r
207                 ConnectionFactoryImpl connectionFactory = (ConnectionFactoryImpl)repository;\r
208                 ManagedConnectionFactoryImpl managedConnectionFactory = connectionFactory.getManagedConnectionFactory();\r
209                 String serverUrl = managedConnectionFactory.getServerURL();\r
210                 \r
211                 return result;\r
212         }\r
213 \r
214         /**\r
215          * releaseRepositorySession releases given repository session\r
216          * \r
217          * @param repoSession\r
218          * @throws java.lang.Exception\r
219          */\r
220         public void releaseRepositorySession(RepositoryInstance repoSession)\r
221                         throws Exception {\r
222                 if (repoSession != null) {\r
223                         getClient().releaseRepository(repoSession);\r
224 \r
225                         if (logger.isDebugEnabled()) {\r
226                                 logger.debug("releaseRepositorySession() released repository session");\r
227                         }\r
228                 }\r
229         }\r
230 \r
231         /**\r
232          * getRepositorySession get session to default repository\r
233          * \r
234          * @return RepositoryInstance\r
235          * @throws java.lang.Exception\r
236          */\r
237         public RepositoryInstance getRepositorySession(RepositoryDomainType repoDomain) throws Exception {\r
238                 RepositoryInstance repoSession = getClient().openRepository(repoDomain);\r
239                 if (logger.isDebugEnabled() && repoSession != null) {\r
240                         logger.debug("getRepositorySession() opened repository session");\r
241                         String repoName = repoDomain.getRepositoryName();\r
242                         String databaseName = this.getDatabaseName(repoName); // For debugging purposes only\r
243                 }\r
244                 return repoSession;\r
245         }\r
246 \r
247     public Repository lookupRepository(String name) throws Exception {\r
248         Repository repo;\r
249         try {\r
250             // needed by glassfish\r
251             repo = (Repository) new InitialContext().lookup("NXRepository/"\r
252                     + name);\r
253         } catch (NamingException e) {\r
254             try {\r
255                 // needed by jboss\r
256                 repo = (Repository) new InitialContext().lookup("java:NXRepository/"\r
257                         + name);\r
258             } catch (NamingException ee) {\r
259                 repo = (Repository) NXCore.getRepositoryService().getRepositoryManager().getRepository(\r
260                         name);\r
261             }\r
262         }\r
263         if (repo == null) {\r
264             throw new IllegalArgumentException("Repository not found: " + name);\r
265         }\r
266         return repo;\r
267     }\r
268     \r
269     public RepositoryDescriptor getRepositoryDescriptor(String name) throws Exception {\r
270         RepositoryDescriptor repo = null;\r
271 \r
272         Iterable<RepositoryDescriptor> descriptorsList = NXCore.getRepositoryService().getRepositoryManager().getDescriptors();\r
273         for (RepositoryDescriptor descriptor : descriptorsList) {\r
274                 String homeDir = descriptor.getHomeDirectory();\r
275                 String config = descriptor.getConfigurationFile();\r
276                 RepositoryFactory factor = descriptor.getFactory();\r
277         }\r
278 \r
279         return repo;\r
280     }\r
281         \r
282         /**\r
283          * getClient get Nuxeo client for accessing Nuxeo services remotely using\r
284          * Nuxeo Java (EJB) Remote APIS\r
285          * \r
286          * @return NuxeoClient\r
287          * @throws java.lang.Exception\r
288          */\r
289         public NuxeoClientEmbedded getClient() throws Exception {\r
290                 if (initialized == true) {\r
291                         if (client.isConnected()) {\r
292 //                              client.login();\r
293                                 return client;\r
294                         } else {\r
295                                 client.forceConnect(this.HOST,\r
296                                                 this.PORT);\r
297                                 if (logger.isDebugEnabled()) {\r
298                                         logger.debug("getClient(): connection successful port="\r
299                                                         + this.PORT);\r
300                                 }\r
301                                 return client;\r
302                         }\r
303                 }\r
304                 //\r
305                 // Nuxeo connection was not initialized\r
306                 //\r
307                 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);\r
308                 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);\r
309         }\r
310         \r
311         void releaseClient() throws Exception {\r
312                 if (initialized == true) {\r
313 //                      client.logout();\r
314                 } else {\r
315                         //\r
316                         // Nuxeo connection was not initialized\r
317                         //\r
318                         logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);\r
319                         throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);\r
320                 }\r
321         }\r
322 \r
323         /**\r
324          * retrieveWorkspaceIds retrieves all workspace ids from default repository\r
325          * \r
326          * @param repoDomain\r
327          *            a repository domain for a given tenant - see the tenant bindings XML file for details\r
328          * @return\r
329          * @throws java.lang.Exception\r
330          */\r
331         public Hashtable<String, String> retrieveWorkspaceIds(RepositoryDomainType repoDomain)\r
332                         throws Exception {\r
333                 RepositoryInstance repoSession = null;\r
334                 Hashtable<String, String> workspaceIds = new Hashtable<String, String>();\r
335                 try {\r
336                         repoSession = getRepositorySession(repoDomain);\r
337                         DocumentModel rootDoc = repoSession.getRootDocument();\r
338                         DocumentModelList rootChildrenList = repoSession.getChildren(rootDoc.getRef());\r
339                         Iterator<DocumentModel> diter = rootChildrenList.iterator();\r
340                         while (diter.hasNext()) {\r
341                                 DocumentModel domain = diter.next();\r
342                                 String domainPath = "/" + repoDomain.getStorageName();\r
343                                 if (!domain.getPathAsString().equalsIgnoreCase(domainPath)) {\r
344                                         continue; // If it's not our domain folder/directory then skip it\r
345                                 }\r
346                                 if (logger.isDebugEnabled()) {\r
347                                         logger.debug("domain=" + domain.toString());\r
348                                 }\r
349                                 DocumentModelList domainChildrenList = repoSession.getChildren(domain.getRef());\r
350                                 Iterator<DocumentModel> witer = domainChildrenList.iterator();\r
351                                 while (witer.hasNext()) {\r
352                                         DocumentModel childNode = witer.next();\r
353                                         if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) { \r
354                                                 DocumentModelList workspaceList = repoSession\r
355                                                                 .getChildren(childNode.getRef());\r
356                                                 Iterator<DocumentModel> wsiter = workspaceList\r
357                                                                 .iterator();\r
358                                                 while (wsiter.hasNext()) {\r
359                                                         DocumentModel workspace = wsiter.next();\r
360                                                         if (logger.isDebugEnabled()) {\r
361                                                                 logger.debug("workspace name="\r
362                                                                                 + workspace.getName() + " id="\r
363                                                                                 + workspace.getId());\r
364                                                         }\r
365                                                         workspaceIds.put(workspace.getName().toLowerCase(),\r
366                                                                         workspace.getId());\r
367                                                 }\r
368                                         }\r
369                                 }\r
370                         }\r
371                 } catch (Exception e) {\r
372                         if (logger.isDebugEnabled()) {\r
373                                 logger.debug("retrieveWorkspaceIds() caught exception ", e);\r
374                         }\r
375                         throw e;\r
376                 } finally {\r
377                         if (repoSession != null) {\r
378                                 releaseRepositorySession(repoSession);\r
379                         }\r
380                 }\r
381                 return workspaceIds;\r
382         }\r
383         \r
384         @Deprecated\r
385     private void loadBundles() throws Exception {\r
386     }\r
387         \r
388 }\r