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