1 package org.collectionspace.services.nuxeo.client.java;
\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
9 import javax.naming.InitialContext;
\r
10 import javax.naming.NamingException;
\r
11 import javax.servlet.ServletContext;
\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
28 import org.slf4j.Logger;
\r
29 import org.slf4j.LoggerFactory;
\r
31 public class NuxeoConnectorEmbedded {
\r
33 <host>127.0.0.1</host>
\r
34 <port>62474</port> <!-- java -->
\r
36 private Logger logger = LoggerFactory.getLogger(NuxeoConnectorEmbedded.class);
\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
43 private static final String HOST = "127.0.0.1";
\r
44 private static final int PORT = 62474;
\r
46 private final static String CSPACE_JEESERVER_HOME = "CSPACE_CONTAINER";
\r
47 private final static String CSPACE_NUXEO_HOME = "CSPACE_NUXEO_HOME";
\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
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
58 private RepositoryClientConfigType repositoryClientConfig;
\r
59 public FrameworkBootstrap fb;
\r
61 private NuxeoConnectorEmbedded() {
\r
64 public final static NuxeoConnectorEmbedded getInstance() {
\r
68 private String getClientUserName() {
\r
69 String username = System.getenv(NUXEO_CLIENT_USERNAME);
\r
70 if (username == null) {
\r
71 username = "Administrator";
\r
76 private String getClientPassword() {
\r
77 String password = System.getenv(NUXEO_CLIENT_PASSWORD);
\r
78 if (password == null) {
\r
79 password = "Administrator";
\r
84 private String getServerRootPath() {
\r
85 String result = null;
\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
95 private String getNuxeoServerPath(String serverRootPath) throws IOException {
\r
96 String result = null;
\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
101 String prop = System.getenv(CSPACE_NUXEO_HOME);
\r
102 if (prop != null && !prop.isEmpty()) {
\r
106 // Could not find the 'CSPACE_NUXEO_HOME' environment variable, so using the default location instead.
\r
108 result = serverRootPath + "/" + NUXEO_SERVER_DIR;
\r
114 private File getNuxeoServerDir(String serverRootPath) throws IOException {
\r
115 File result = null;
\r
116 String errMsg = null;
\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
124 errMsg = "The Nuxeo EP configuration directory is missing or inaccessible at: '" + path + "'.";
\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
135 throw new IOException(errMsg);
\r
142 // Start/boot the Nuxeo EP server instance
\r
144 private void startNuxeoEP(String serverRootPath) throws Exception {
\r
145 File nuxeoHomeDir = getNuxeoServerDir(serverRootPath);
\r
147 if (logger.isInfoEnabled() == true) {
\r
148 logger.info("Starting Nuxeo EP server from configuration at: "
\r
149 + nuxeoHomeDir.getCanonicalPath());
\r
152 fb = new FrameworkBootstrap(NuxeoConnectorEmbedded.class.getClassLoader(),
\r
159 * release releases resources occupied by Nuxeo remoting client runtime
\r
161 * @throws java.lang.Exception
\r
163 public void release() throws Exception {
\r
164 if (initialized == true) {
\r
166 client.tryDisconnect();
\r
167 } catch (Exception e) {
\r
168 logger.error("Failed to disconnect Nuxeo connection.", e);
\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
190 public String getDatabaseName(String repoName) {
\r
191 String result = null;
\r
194 this.getRepositoryDescriptor(repoName);
\r
195 } catch (Exception e1) {
\r
196 // TODO Auto-generated catch block
\r
197 e1.printStackTrace();
\r
200 Repository repository = null;
\r
202 repository = this.lookupRepository(repoName);
\r
203 } catch (Exception e) {
\r
204 // TODO Auto-generated catch block
\r
205 e.printStackTrace();
\r
207 ConnectionFactoryImpl connectionFactory = (ConnectionFactoryImpl)repository;
\r
208 ManagedConnectionFactoryImpl managedConnectionFactory = connectionFactory.getManagedConnectionFactory();
\r
209 String serverUrl = managedConnectionFactory.getServerURL();
\r
215 * releaseRepositorySession releases given repository session
\r
217 * @param repoSession
\r
218 * @throws java.lang.Exception
\r
220 public void releaseRepositorySession(RepositoryInstance repoSession)
\r
222 if (repoSession != null) {
\r
223 getClient().releaseRepository(repoSession);
\r
225 if (logger.isDebugEnabled()) {
\r
226 logger.debug("releaseRepositorySession() released repository session");
\r
232 * getRepositorySession get session to default repository
\r
234 * @return RepositoryInstance
\r
235 * @throws java.lang.Exception
\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
244 return repoSession;
\r
247 public Repository lookupRepository(String name) throws Exception {
\r
250 // needed by glassfish
\r
251 repo = (Repository) new InitialContext().lookup("NXRepository/"
\r
253 } catch (NamingException e) {
\r
256 repo = (Repository) new InitialContext().lookup("java:NXRepository/"
\r
258 } catch (NamingException ee) {
\r
259 repo = (Repository) NXCore.getRepositoryService().getRepositoryManager().getRepository(
\r
263 if (repo == null) {
\r
264 throw new IllegalArgumentException("Repository not found: " + name);
\r
269 public RepositoryDescriptor getRepositoryDescriptor(String name) throws Exception {
\r
270 RepositoryDescriptor repo = null;
\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
283 * getClient get Nuxeo client for accessing Nuxeo services remotely using
\r
284 * Nuxeo Java (EJB) Remote APIS
\r
286 * @return NuxeoClient
\r
287 * @throws java.lang.Exception
\r
289 public NuxeoClientEmbedded getClient() throws Exception {
\r
290 if (initialized == true) {
\r
291 if (client.isConnected()) {
\r
295 client.forceConnect(this.HOST,
\r
297 if (logger.isDebugEnabled()) {
\r
298 logger.debug("getClient(): connection successful port="
\r
305 // Nuxeo connection was not initialized
\r
307 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
308 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
311 void releaseClient() throws Exception {
\r
312 if (initialized == true) {
\r
313 // client.logout();
\r
316 // Nuxeo connection was not initialized
\r
318 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
319 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
324 * retrieveWorkspaceIds retrieves all workspace ids from default repository
\r
326 * @param repoDomain
\r
327 * a repository domain for a given tenant - see the tenant bindings XML file for details
\r
329 * @throws java.lang.Exception
\r
331 public Hashtable<String, String> retrieveWorkspaceIds(RepositoryDomainType repoDomain)
\r
333 RepositoryInstance repoSession = null;
\r
334 Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
\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
346 if (logger.isDebugEnabled()) {
\r
347 logger.debug("domain=" + domain.toString());
\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
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
365 workspaceIds.put(workspace.getName().toLowerCase(),
\r
366 workspace.getId());
\r
371 } catch (Exception e) {
\r
372 if (logger.isDebugEnabled()) {
\r
373 logger.debug("retrieveWorkspaceIds() caught exception ", e);
\r
377 if (repoSession != null) {
\r
378 releaseRepositorySession(repoSession);
\r
381 return workspaceIds;
\r
385 private void loadBundles() throws Exception {
\r