1 package org.collectionspace.services.nuxeo.client.java;
4 import java.io.IOException;
5 import java.util.Hashtable;
6 import java.util.Iterator;
8 import javax.naming.InitialContext;
9 import javax.naming.NamingException;
10 import javax.servlet.ServletContext;
12 import org.collectionspace.services.common.api.JEEServerDeployment;
13 import org.collectionspace.services.config.RepositoryClientConfigType;
14 import org.collectionspace.services.config.tenant.RepositoryDomainType;
15 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
17 import org.nuxeo.ecm.core.NXCore;
18 import org.nuxeo.ecm.core.api.DocumentModel;
19 import org.nuxeo.ecm.core.api.DocumentModelList;
20 import org.nuxeo.ecm.core.model.Repository;
21 import org.nuxeo.osgi.application.FrameworkBootstrap;
22 import org.nuxeo.ecm.core.repository.RepositoryDescriptor;
23 import org.nuxeo.ecm.core.repository.RepositoryFactory;
24 import org.nuxeo.ecm.core.storage.sql.ra.ConnectionFactoryImpl;
25 import org.nuxeo.ecm.core.storage.sql.ra.ManagedConnectionFactoryImpl;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class NuxeoConnectorEmbedded {
32 <host>127.0.0.1</host>
33 <port>62474</port> <!-- java -->
35 private Logger logger = LoggerFactory.getLogger(NuxeoConnectorEmbedded.class);
37 public final static String NUXEO_CLIENT_DIR = JEEServerDeployment.NUXEO_CLIENT_DIR;
38 public final static String NUXEO_SERVER_DIR = JEEServerDeployment.NUXEO_SERVER_DIR;
39 private final static String ERROR_CONNECTOR_NOT_INITIALIZED = "NuxeoConnector is not initialized!";
41 private final static String CSPACE_JEESERVER_HOME = "CSPACE_CONTAINER";
42 private final static String CSPACE_NUXEO_HOME = "CSPACE_NUXEO_HOME";
44 private static final NuxeoConnectorEmbedded self = new NuxeoConnectorEmbedded();
45 private NuxeoClientEmbedded client;
46 private ServletContext servletContext = null;
47 private volatile boolean initialized = false; // use volatile for lazy
50 private RepositoryClientConfigType repositoryClientConfig;
51 public FrameworkBootstrap fb;
53 private NuxeoConnectorEmbedded() {
56 public final static NuxeoConnectorEmbedded getInstance() {
60 private String getNuxeoServerPath(String serverRootPath) throws IOException {
63 // Look for the CSPACE_NUXEO_HOME environment variable that might contain the fully qualified path of the
64 // Nuxeo EP configuration directory.
66 String prop = System.getenv(CSPACE_NUXEO_HOME);
67 if (prop != null && !prop.isEmpty()) {
71 // Could not find the 'CSPACE_NUXEO_HOME' environment variable, so using the default location instead.
73 result = serverRootPath + "/" + NUXEO_SERVER_DIR;
79 private File getNuxeoServerDir(String serverRootPath) throws IOException {
83 String path = getNuxeoServerPath(serverRootPath);
85 File temp = new File(path);
86 if (temp.exists() == true) {
89 errMsg = "The Nuxeo EP configuration directory is missing or inaccessible at: '" + path + "'.";
95 path = path != null ? path : "<empty>";
96 errMsg = "Unknown error trying to find Nuxeo configuration: '" +
97 CSPACE_NUXEO_HOME + "' = " +
100 throw new IOException(errMsg);
107 // Start/boot the Nuxeo EP server instance
109 private void startNuxeoEP(String serverRootPath) throws Exception {
110 File nuxeoHomeDir = getNuxeoServerDir(serverRootPath);
112 if (logger.isInfoEnabled() == true) {
113 logger.info("Starting Nuxeo EP server from configuration at: "
114 + nuxeoHomeDir.getCanonicalPath());
117 fb = new FrameworkBootstrap(NuxeoConnectorEmbedded.class.getClassLoader(),
124 * release releases resources occupied by Nuxeo remoting client runtime
126 * @throws java.lang.Exception
128 public void release() throws Exception {
129 if (initialized == true) {
131 client.tryDisconnect();
132 } catch (Exception e) {
133 logger.error("Failed to disconnect Nuxeo connection.", e);
139 public void initialize(String serverRootPath,
140 RepositoryClientConfigType repositoryClientConfig,
141 ServletContext servletContext) throws Exception {
142 if (initialized == false) {
143 synchronized (this) {
144 if (initialized == false) {
145 this.servletContext = servletContext;
146 this.repositoryClientConfig = repositoryClientConfig;
147 startNuxeoEP(serverRootPath);
148 client = NuxeoClientEmbedded.getInstance();
155 public String getDatabaseName(String repoName) {
156 String result = null;
159 this.getRepositoryDescriptor(repoName);
160 } catch (Exception e1) {
161 // TODO Auto-generated catch block
162 e1.printStackTrace();
165 Repository repository = null;
167 repository = this.lookupRepository(repoName);
168 } catch (Exception e) {
169 // TODO Auto-generated catch block
173 ConnectionFactoryImpl connectionFactory = (ConnectionFactoryImpl)repository;
174 ManagedConnectionFactoryImpl managedConnectionFactory = connectionFactory.getManagedConnectionFactory();
175 String serverUrl = managedConnectionFactory.getServerURL();
181 * releaseRepositorySession releases given repository session
184 * @throws java.lang.Exception
186 public void releaseRepositorySession(RepositoryInstanceInterface repoSession)
188 if (repoSession != null) {
189 getClient().releaseRepository(repoSession);
191 if (logger.isDebugEnabled()) {
192 logger.debug("releaseRepositorySession() released repository session");
198 * getRepositorySession get session to default repository
200 * @return RepositoryInstance
201 * @throws java.lang.Exception
203 public RepositoryInstanceInterface getRepositorySession(RepositoryDomainType repoDomain) throws Exception {
204 RepositoryInstanceInterface repoSession = getClient().openRepository(repoDomain);
206 if (logger.isDebugEnabled() && repoSession != null) {
207 logger.debug("getRepositorySession() opened repository session");
208 String repoName = repoDomain.getRepositoryName();
209 String databaseName = this.getDatabaseName(repoName); // For debugging purposes only
215 public Repository lookupRepository(String name) throws Exception {
218 // needed by glassfish
219 repo = (Repository) new InitialContext().lookup("NXRepository/"
221 } catch (NamingException e) {
224 repo = (Repository) new InitialContext().lookup("java:NXRepository/"
226 } catch (NamingException ee) {
227 repo = (Repository) NXCore.getRepositoryService().getRepositoryManager().getRepository(
232 throw new IllegalArgumentException("Repository not found: " + name);
237 public RepositoryDescriptor getRepositoryDescriptor(String name) throws Exception {
238 RepositoryDescriptor repo = null;
239 Iterable<RepositoryDescriptor> descriptorsList = NXCore.getRepositoryService().getRepositoryManager().getDescriptors();
240 for (RepositoryDescriptor descriptor : descriptorsList) {
241 String homeDir = descriptor.getHomeDirectory();
242 String config = descriptor.getConfigurationFile();
243 RepositoryFactory factor = descriptor.getFactory();
250 * getClient get Nuxeo client for accessing Nuxeo services remotely using
251 * Nuxeo Java (EJB) Remote APIS
253 * @return NuxeoClient
254 * @throws java.lang.Exception
256 public NuxeoClientEmbedded getClient() throws Exception {
257 if (initialized == true) {
258 if (client.isConnected()) {
263 // Nuxeo connection was not initialized
265 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
266 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
269 void releaseClient() throws Exception {
270 if (initialized == true) {
274 // Nuxeo connection was not initialized
276 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
277 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
282 * retrieveWorkspaceIds retrieves all workspace ids from default repository
285 * a repository domain for a given tenant - see the tenant bindings XML file for details
287 * @throws java.lang.Exception
289 public Hashtable<String, String> retrieveWorkspaceIds(RepositoryDomainType repoDomain)
291 RepositoryInstanceInterface repoSession = null;
292 Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
294 repoSession = getRepositorySession(repoDomain);
295 DocumentModel rootDoc = repoSession.getRootDocument();
296 DocumentModelList rootChildrenList = repoSession.getChildren(rootDoc.getRef());
297 Iterator<DocumentModel> diter = rootChildrenList.iterator();
298 while (diter.hasNext()) {
299 DocumentModel domain = diter.next();
300 String domainPath = "/" + repoDomain.getStorageName();
301 if (!domain.getPathAsString().equalsIgnoreCase(domainPath)) {
302 continue; // If it's not our domain folder/directory then skip it
304 if (logger.isDebugEnabled()) {
305 logger.debug("domain=" + domain.toString());
307 DocumentModelList domainChildrenList = repoSession.getChildren(domain.getRef());
308 Iterator<DocumentModel> witer = domainChildrenList.iterator();
309 while (witer.hasNext()) {
310 DocumentModel childNode = witer.next();
311 if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) {
312 DocumentModelList workspaceList = repoSession
313 .getChildren(childNode.getRef());
314 Iterator<DocumentModel> wsiter = workspaceList
316 while (wsiter.hasNext()) {
317 DocumentModel workspace = wsiter.next();
318 if (logger.isDebugEnabled()) {
319 logger.debug("workspace name="
320 + workspace.getName() + " id="
321 + workspace.getId());
323 workspaceIds.put(workspace.getName().toLowerCase(),
329 } catch (Exception e) {
330 if (logger.isDebugEnabled()) {
331 logger.debug("retrieveWorkspaceIds() caught exception ", e);
335 if (repoSession != null) {
336 releaseRepositorySession(repoSession);