1 package org.collectionspace.services.nuxeo.client.java;
\r
4 import java.io.IOException;
\r
5 import java.util.Hashtable;
\r
6 import java.util.Iterator;
\r
8 import javax.naming.InitialContext;
\r
9 import javax.naming.NamingException;
\r
10 import javax.servlet.ServletContext;
\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 import org.nuxeo.ecm.core.NXCore;
\r
17 import org.nuxeo.ecm.core.api.DocumentModel;
\r
18 import org.nuxeo.ecm.core.api.DocumentModelList;
\r
19 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
\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
27 import org.slf4j.Logger;
\r
28 import org.slf4j.LoggerFactory;
\r
30 public class NuxeoConnectorEmbedded {
\r
32 <host>127.0.0.1</host>
\r
33 <port>62474</port> <!-- java -->
\r
35 private Logger logger = LoggerFactory.getLogger(NuxeoConnectorEmbedded.class);
\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
41 private final static String CSPACE_JEESERVER_HOME = "CSPACE_CONTAINER";
\r
42 private final static String CSPACE_NUXEO_HOME = "CSPACE_NUXEO_HOME";
\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
50 private RepositoryClientConfigType repositoryClientConfig;
\r
51 public FrameworkBootstrap fb;
\r
53 private NuxeoConnectorEmbedded() {
\r
56 public final static NuxeoConnectorEmbedded getInstance() {
\r
60 private String getNuxeoServerPath(String serverRootPath) throws IOException {
\r
61 String result = null;
\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
66 String prop = System.getenv(CSPACE_NUXEO_HOME);
\r
67 if (prop != null && !prop.isEmpty()) {
\r
71 // Could not find the 'CSPACE_NUXEO_HOME' environment variable, so using the default location instead.
\r
73 result = serverRootPath + "/" + NUXEO_SERVER_DIR;
\r
79 private File getNuxeoServerDir(String serverRootPath) throws IOException {
\r
81 String errMsg = null;
\r
83 String path = getNuxeoServerPath(serverRootPath);
\r
85 File temp = new File(path);
\r
86 if (temp.exists() == true) {
\r
89 errMsg = "The Nuxeo EP configuration directory is missing or inaccessible at: '" + path + "'.";
\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
100 throw new IOException(errMsg);
\r
107 // Start/boot the Nuxeo EP server instance
\r
109 private void startNuxeoEP(String serverRootPath) throws Exception {
\r
110 File nuxeoHomeDir = getNuxeoServerDir(serverRootPath);
\r
112 if (logger.isInfoEnabled() == true) {
\r
113 logger.info("Starting Nuxeo EP server from configuration at: "
\r
114 + nuxeoHomeDir.getCanonicalPath());
\r
117 fb = new FrameworkBootstrap(NuxeoConnectorEmbedded.class.getClassLoader(),
\r
124 * release releases resources occupied by Nuxeo remoting client runtime
\r
126 * @throws java.lang.Exception
\r
128 public void release() throws Exception {
\r
129 if (initialized == true) {
\r
131 client.tryDisconnect();
\r
132 } catch (Exception e) {
\r
133 logger.error("Failed to disconnect Nuxeo connection.", e);
\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 = new NuxeoClientEmbedded();
\r
149 initialized = true;
\r
155 public String getDatabaseName(String repoName) {
\r
156 String result = null;
\r
159 this.getRepositoryDescriptor(repoName);
\r
160 } catch (Exception e1) {
\r
161 // TODO Auto-generated catch block
\r
162 e1.printStackTrace();
\r
165 Repository repository = null;
\r
167 repository = this.lookupRepository(repoName);
\r
168 } catch (Exception e) {
\r
169 // TODO Auto-generated catch block
\r
170 e.printStackTrace();
\r
173 ConnectionFactoryImpl connectionFactory = (ConnectionFactoryImpl)repository;
\r
174 ManagedConnectionFactoryImpl managedConnectionFactory = connectionFactory.getManagedConnectionFactory();
\r
175 String serverUrl = managedConnectionFactory.getServerURL();
\r
181 * releaseRepositorySession releases given repository session
\r
183 * @param repoSession
\r
184 * @throws java.lang.Exception
\r
186 public void releaseRepositorySession(RepositoryInstance repoSession)
\r
188 if (repoSession != null) {
\r
189 getClient().releaseRepository(repoSession);
\r
191 if (logger.isDebugEnabled()) {
\r
192 logger.debug("releaseRepositorySession() released repository session");
\r
198 * getRepositorySession get session to default repository
\r
200 * @return RepositoryInstance
\r
201 * @throws java.lang.Exception
\r
203 public RepositoryInstance getRepositorySession(RepositoryDomainType repoDomain) throws Exception {
\r
204 RepositoryInstance repoSession = getClient().openRepository(repoDomain);
\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
212 return repoSession;
\r
215 public Repository lookupRepository(String name) throws Exception {
\r
218 // needed by glassfish
\r
219 repo = (Repository) new InitialContext().lookup("NXRepository/"
\r
221 } catch (NamingException e) {
\r
224 repo = (Repository) new InitialContext().lookup("java:NXRepository/"
\r
226 } catch (NamingException ee) {
\r
227 repo = (Repository) NXCore.getRepositoryService().getRepositoryManager().getRepository(
\r
231 if (repo == null) {
\r
232 throw new IllegalArgumentException("Repository not found: " + name);
\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
250 * getClient get Nuxeo client for accessing Nuxeo services remotely using
\r
251 * Nuxeo Java (EJB) Remote APIS
\r
253 * @return NuxeoClient
\r
254 * @throws java.lang.Exception
\r
256 public NuxeoClientEmbedded getClient() throws Exception {
\r
257 if (initialized == true) {
\r
258 if (client.isConnected()) {
\r
263 // Nuxeo connection was not initialized
\r
265 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
266 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
269 void releaseClient() throws Exception {
\r
270 if (initialized == true) {
\r
274 // Nuxeo connection was not initialized
\r
276 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
277 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
282 * retrieveWorkspaceIds retrieves all workspace ids from default repository
\r
284 * @param repoDomain
\r
285 * a repository domain for a given tenant - see the tenant bindings XML file for details
\r
287 * @throws java.lang.Exception
\r
289 public Hashtable<String, String> retrieveWorkspaceIds(RepositoryDomainType repoDomain)
\r
291 RepositoryInstance repoSession = null;
\r
292 Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
\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
304 if (logger.isDebugEnabled()) {
\r
305 logger.debug("domain=" + domain.toString());
\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
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
323 workspaceIds.put(workspace.getName().toLowerCase(),
\r
324 workspace.getId());
\r
329 } catch (Exception e) {
\r
330 if (logger.isDebugEnabled()) {
\r
331 logger.debug("retrieveWorkspaceIds() caught exception ", e);
\r
335 if (repoSession != null) {
\r
336 releaseRepositorySession(repoSession);
\r
340 return workspaceIds;
\r