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