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