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.api.DocumentModel;
18 import org.nuxeo.ecm.core.api.DocumentModelList;
19 import org.nuxeo.ecm.core.api.repository.RepositoryManager;
20 import org.nuxeo.ecm.core.model.Repository;
21 import org.nuxeo.osgi.application.FrameworkBootstrap;
22 import org.nuxeo.runtime.api.Framework;
23 import org.nuxeo.ecm.core.storage.sql.ra.ConnectionFactoryImpl;
24 import org.nuxeo.ecm.core.storage.sql.ra.ManagedConnectionFactoryImpl;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class NuxeoConnectorEmbedded {
31 <host>127.0.0.1</host>
32 <port>62474</port> <!-- java -->
34 private Logger logger = LoggerFactory.getLogger(NuxeoConnectorEmbedded.class);
36 public final static String NUXEO_CLIENT_DIR = JEEServerDeployment.NUXEO_CLIENT_DIR;
37 public final static String NUXEO_SERVER_DIR = JEEServerDeployment.NUXEO_SERVER_DIR;
38 private final static String ERROR_CONNECTOR_NOT_INITIALIZED = "NuxeoConnector is not initialized!";
40 private final static String CSPACE_NUXEO_HOME = "CSPACE_NUXEO_HOME";
42 private static final NuxeoConnectorEmbedded self = new NuxeoConnectorEmbedded();
43 private NuxeoClientEmbedded client;
44 private volatile boolean initialized = false; // use volatile for lazy
47 public FrameworkBootstrap fb;
48 private ServletContext servletContext;
49 private RepositoryClientConfigType repositoryClientConfig;
51 private NuxeoConnectorEmbedded() {
55 public final static NuxeoConnectorEmbedded getInstance() {
59 private String getNuxeoServerPath(String serverRootPath) throws IOException {
62 // Look for the CSPACE_NUXEO_HOME environment variable that might contain the fully qualified path of the
63 // Nuxeo EP configuration directory.
65 String prop = System.getenv(CSPACE_NUXEO_HOME);
66 if (prop != null && !prop.isEmpty()) {
70 // Could not find the 'CSPACE_NUXEO_HOME' environment variable, so using the default location instead.
72 result = serverRootPath + "/" + NUXEO_SERVER_DIR;
78 private File getNuxeoServerDir(String serverRootPath) throws IOException {
82 String path = getNuxeoServerPath(serverRootPath);
84 File temp = new File(path);
85 if (temp.exists() == true) {
88 errMsg = "The Nuxeo EP configuration directory is missing or inaccessible at: '" + path + "'.";
94 path = path != null ? path : "<empty>";
95 errMsg = "Unknown error trying to find Nuxeo configuration: '" +
96 CSPACE_NUXEO_HOME + "' = " +
99 throw new IOException(errMsg);
106 // Start/boot the Nuxeo EP server instance
108 private void startNuxeoEP(String serverRootPath) throws Exception {
109 File nuxeoHomeDir = getNuxeoServerDir(serverRootPath);
111 if (logger.isInfoEnabled() == true) {
112 logger.info("Starting Nuxeo EP server from configuration at: "
113 + nuxeoHomeDir.getCanonicalPath());
116 fb = new FrameworkBootstrap(NuxeoConnectorEmbedded.class.getClassLoader(),
123 * release releases resources occupied by Nuxeo remoting client runtime
125 * @throws java.lang.Exception
127 public void release() throws Exception {
128 if (initialized == true) {
130 client.tryDisconnect();
131 } catch (Exception e) {
132 logger.error("Failed to disconnect Nuxeo connection.", e);
138 public void initialize(String serverRootPath,
139 RepositoryClientConfigType repositoryClientConfig,
140 ServletContext servletContext) throws Exception {
141 if (initialized == false) {
142 synchronized (this) {
143 if (initialized == false) {
144 this.servletContext = servletContext;
145 this.repositoryClientConfig = repositoryClientConfig;
146 startNuxeoEP(serverRootPath);
147 client = NuxeoClientEmbedded.getInstance();
155 * This is a debug-only method. Use to look at runtime values of Nuxeo internal
158 public String getDatabaseName(String repoName) {
159 String result = null;
161 // TODO: Remove after CSPACE-6375 issue is resolved.
163 // List<RepositoryDescriptor> repositoryDescriptorList = this.getRepositoryDescriptor(repoName);
164 // } catch (Exception e1) {
165 // // TODO Auto-generated catch block
166 // e1.printStackTrace();
169 Repository repository = null;
171 repository = this.lookupRepository(repoName);
172 } catch (Exception e) {
173 // TODO Auto-generated catch block
177 ConnectionFactoryImpl connectionFactory = (ConnectionFactoryImpl)repository;
178 ManagedConnectionFactoryImpl managedConnectionFactory = connectionFactory.getManagedConnectionFactory();
179 String property = managedConnectionFactory.getProperty();
185 * releaseRepositorySession releases given repository session
188 * @throws java.lang.Exception
190 public void releaseRepositorySession(CoreSessionInterface repoSession)
192 if (repoSession != null) {
193 getClient().releaseRepository(repoSession);
195 if (logger.isDebugEnabled()) {
196 logger.debug("releaseRepositorySession() released repository session");
202 * getRepositorySession get session to default repository
204 * @return RepositoryInstance
205 * @throws java.lang.Exception
207 public CoreSessionInterface getRepositorySession(RepositoryDomainType repoDomain) throws Exception {
208 CoreSessionInterface repoSession = getClient().openRepository(repoDomain);
210 if (logger.isDebugEnabled() && repoSession != null) {
211 logger.debug("getRepositorySession() opened repository session");
212 String repoName = repoDomain.getRepositoryName();
213 String databaseName = this.getDatabaseName(repoName); // For debugging purposes only
219 public Repository lookupRepository(String name) throws Exception {
221 RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
224 // needed by glassfish
225 repo = (Repository) new InitialContext().lookup("NXRepository/" + name);
226 } catch (NamingException e) {
229 repo = (Repository) new InitialContext().lookup("java:NXRepository/"
231 } catch (NamingException ee) {
232 repo = (Repository) repositoryManager.getRepository(
238 throw new IllegalArgumentException("Repository not found: " + name);
244 // TODO: Remove after CSPACE-6375 issue is resolved.
245 // public List<RepositoryDescriptor> getRepositoryDescriptor(String name) throws Exception {
246 // RepositoryDescriptor repo = null;
247 // RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
248 // Iterable<RepositoryDescriptor> descriptorsList = repositoryManager.getDescriptors();
249 // for (RepositoryDescriptor descriptor : descriptorsList) {
250 // String homeDir = descriptor.getHomeDirectory();
251 // String config = descriptor.getConfigurationFile();
252 // RepositoryFactory factor = descriptor.getFactory();
259 * getClient get Nuxeo client for accessing Nuxeo services remotely using
260 * Nuxeo Java (EJB) Remote APIS
262 * @return NuxeoClient
263 * @throws java.lang.Exception
265 public NuxeoClientEmbedded getClient() throws Exception {
266 if (initialized == true) {
270 // Nuxeo connection was not initialized
272 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
273 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
276 void releaseClient() throws Exception {
277 if (initialized == true) {
281 // Nuxeo connection was not initialized
283 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
284 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
289 * retrieveWorkspaceIds retrieves all workspace ids from default repository
292 * a repository domain for a given tenant - see the tenant bindings XML file for details
294 * @throws java.lang.Exception
296 public Hashtable<String, String> retrieveWorkspaceIds(RepositoryDomainType repoDomain)
298 CoreSessionInterface repoSession = null;
299 Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
301 repoSession = getRepositorySession(repoDomain);
302 DocumentModel rootDoc = repoSession.getRootDocument();
303 DocumentModelList rootChildrenList = repoSession.getChildren(rootDoc.getRef());
304 Iterator<DocumentModel> diter = rootChildrenList.iterator();
305 while (diter.hasNext()) {
306 DocumentModel domain = diter.next();
307 String domainPath = "/" + repoDomain.getStorageName();
308 if (!domain.getPathAsString().equalsIgnoreCase(domainPath)) {
309 continue; // If it's not our domain folder/directory then skip it
311 if (logger.isDebugEnabled()) {
312 logger.debug("domain=" + domain.toString());
314 DocumentModelList domainChildrenList = repoSession.getChildren(domain.getRef());
315 Iterator<DocumentModel> witer = domainChildrenList.iterator();
316 while (witer.hasNext()) {
317 DocumentModel childNode = witer.next();
318 if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) {
319 DocumentModelList workspaceList = repoSession
320 .getChildren(childNode.getRef());
321 Iterator<DocumentModel> wsiter = workspaceList
323 while (wsiter.hasNext()) {
324 DocumentModel workspace = wsiter.next();
325 if (logger.isDebugEnabled()) {
326 logger.debug("workspace name="
327 + workspace.getName() + " id="
328 + workspace.getId());
330 workspaceIds.put(workspace.getName().toLowerCase(),
336 } catch (Exception e) {
337 if (logger.isDebugEnabled()) {
338 logger.debug("retrieveWorkspaceIds() caught exception ", e);
342 if (repoSession != null) {
343 releaseRepositorySession(repoSession);