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.servlet.ServletContext;
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 import org.nuxeo.ecm.core.api.CoreInstance;
15 import org.nuxeo.ecm.core.api.CoreSession;
16 import org.nuxeo.ecm.core.api.DocumentModel;
17 import org.nuxeo.ecm.core.api.DocumentModelList;
18 import org.nuxeo.ecm.core.api.SystemPrincipal;
19 import org.nuxeo.ecm.core.api.repository.Repository;
20 import org.nuxeo.ecm.core.api.repository.RepositoryManager;
21 import org.nuxeo.osgi.application.FrameworkBootstrap;
22 import org.nuxeo.runtime.api.Framework;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class NuxeoConnectorEmbedded {
28 <host>127.0.0.1</host>
29 <port>62474</port> <!-- java -->
31 private Logger logger = LoggerFactory.getLogger(NuxeoConnectorEmbedded.class);
33 public final static String NUXEO_CLIENT_DIR = JEEServerDeployment.NUXEO_CLIENT_DIR;
34 public final static String NUXEO_SERVER_DIR = JEEServerDeployment.NUXEO_SERVER_DIR;
35 private final static String ERROR_CONNECTOR_NOT_INITIALIZED = "NuxeoConnector is not initialized!";
37 private final static String CSPACE_NUXEO_HOME = "CSPACE_NUXEO_HOME";
39 private static final NuxeoConnectorEmbedded self = new NuxeoConnectorEmbedded();
40 private NuxeoClientEmbedded client;
41 private volatile boolean initialized = false; // use volatile for lazy
44 public FrameworkBootstrap fb;
45 private ServletContext servletContext;
46 private RepositoryClientConfigType repositoryClientConfig;
48 private NuxeoConnectorEmbedded() {
52 public final static NuxeoConnectorEmbedded getInstance() {
56 private String getNuxeoServerPath(String serverRootPath) throws IOException {
59 // Look for the CSPACE_NUXEO_HOME environment variable that might contain the fully qualified path of the
60 // Nuxeo EP configuration directory.
62 String prop = System.getenv(CSPACE_NUXEO_HOME);
63 if (prop != null && !prop.isEmpty()) {
67 // Could not find the 'CSPACE_NUXEO_HOME' environment variable, so using the default location instead.
69 result = serverRootPath + "/" + NUXEO_SERVER_DIR;
75 private File getNuxeoServerDir(String serverRootPath) throws IOException {
79 String path = getNuxeoServerPath(serverRootPath);
81 File temp = new File(path);
82 if (temp.exists() == true) {
85 errMsg = "The Nuxeo EP configuration directory is missing or inaccessible at: '" + path + "'.";
91 path = path != null ? path : "<empty>";
92 errMsg = "Unknown error trying to find Nuxeo configuration: '" +
93 CSPACE_NUXEO_HOME + "' = " +
96 throw new IOException(errMsg);
103 // Start/boot the Nuxeo EP server instance
105 private void startNuxeoEP(String serverRootPath) throws Exception {
106 File nuxeoHomeDir = getNuxeoServerDir(serverRootPath);
108 if (logger.isInfoEnabled() == true) {
109 logger.info("Starting Nuxeo EP server from configuration at: "
110 + nuxeoHomeDir.getCanonicalPath());
113 fb = new FrameworkBootstrap(NuxeoConnectorEmbedded.class.getClassLoader(),
115 fb.setHostName("Tomcat");
116 fb.setHostVersion("7.0.64"); //FIXME: Should not be hard coded.
121 // Test to see if we can connect to the default repository
122 CoreSession coreSession = null;
124 Repository defaultRepo = Framework.getService(RepositoryManager.class).getDefaultRepository();
125 coreSession = CoreInstance.openCoreSession(defaultRepo.getName(), new SystemPrincipal(null));
126 } catch (Throwable t) {
127 logger.error(t.getMessage());
128 throw new RuntimeException("Could not start the Nuxeo EP Framework", t);
130 if (coreSession != null) {
131 CoreInstance.closeCoreSession(coreSession);
137 * release releases resources occupied by Nuxeo remoting client runtime
139 * @throws java.lang.Exception
141 public void release() throws Exception {
142 if (initialized == true) {
144 client.tryDisconnect();
145 } catch (Exception e) {
146 logger.error("Failed to disconnect Nuxeo connection.", e);
152 public void initialize(String serverRootPath,
153 RepositoryClientConfigType repositoryClientConfig,
154 ServletContext servletContext) throws Exception {
155 if (initialized == false) {
156 synchronized (this) {
157 if (initialized == false) {
158 this.servletContext = servletContext;
159 this.repositoryClientConfig = repositoryClientConfig;
160 startNuxeoEP(serverRootPath);
161 client = NuxeoClientEmbedded.getInstance();
169 * releaseRepositorySession releases given repository session
172 * @throws java.lang.Exception
174 public void releaseRepositorySession(CoreSessionInterface repoSession)
176 if (repoSession != null) {
177 getClient().releaseRepository(repoSession);
179 if (logger.isDebugEnabled()) {
180 logger.debug("releaseRepositorySession() released repository session");
186 * getRepositorySession get session to default repository
188 * @return RepositoryInstance
189 * @throws java.lang.Exception
191 public CoreSessionInterface getRepositorySession(RepositoryDomainType repoDomain) throws Exception {
192 CoreSessionInterface repoSession = getClient().openRepository(repoDomain);
194 if (logger.isDebugEnabled() && repoSession != null) {
195 String repoName = repoDomain.getRepositoryName();
196 logger.debug("getRepositorySession() opened repository session on: %s repo",
197 repoName != null ? repoName : "unknown");
204 // TODO: Remove after CSPACE-6375 issue is resolved.
205 // public List<RepositoryDescriptor> getRepositoryDescriptor(String name) throws Exception {
206 // RepositoryDescriptor repo = null;
207 // RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
208 // Iterable<RepositoryDescriptor> descriptorsList = repositoryManager.getDescriptors();
209 // for (RepositoryDescriptor descriptor : descriptorsList) {
210 // String homeDir = descriptor.getHomeDirectory();
211 // String config = descriptor.getConfigurationFile();
212 // RepositoryFactory factor = descriptor.getFactory();
219 * getClient get Nuxeo client for accessing Nuxeo services remotely using
220 * Nuxeo Java (EJB) Remote APIS
222 * @return NuxeoClient
223 * @throws java.lang.Exception
225 public NuxeoClientEmbedded getClient() throws Exception {
226 if (initialized == true) {
230 // Nuxeo connection was not initialized
232 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
233 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
236 void releaseClient() throws Exception {
237 if (initialized == true) {
241 // Nuxeo connection was not initialized
243 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
244 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
249 * retrieveWorkspaceIds retrieves all workspace ids from default repository
252 * a repository domain for a given tenant - see the tenant bindings XML file for details
254 * @throws java.lang.Exception
256 public Hashtable<String, String> retrieveWorkspaceIds(RepositoryDomainType repoDomain)
258 CoreSessionInterface repoSession = null;
259 Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
261 repoSession = getRepositorySession(repoDomain);
262 DocumentModel rootDoc = repoSession.getRootDocument();
263 DocumentModelList rootChildrenList = repoSession.getChildren(rootDoc.getRef());
264 Iterator<DocumentModel> diter = rootChildrenList.iterator();
265 while (diter.hasNext()) {
266 DocumentModel domain = diter.next();
267 String domainPath = "/" + repoDomain.getStorageName();
268 if (!domain.getPathAsString().equalsIgnoreCase(domainPath)) {
269 continue; // If it's not our domain folder/directory then skip it
271 if (logger.isDebugEnabled()) {
272 logger.debug("domain=" + domain.toString());
274 DocumentModelList domainChildrenList = repoSession.getChildren(domain.getRef());
275 Iterator<DocumentModel> witer = domainChildrenList.iterator();
276 while (witer.hasNext()) {
277 DocumentModel childNode = witer.next();
278 if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) {
279 DocumentModelList workspaceList = repoSession
280 .getChildren(childNode.getRef());
281 Iterator<DocumentModel> wsiter = workspaceList
283 while (wsiter.hasNext()) {
284 DocumentModel workspace = wsiter.next();
285 if (logger.isDebugEnabled()) {
286 logger.debug("workspace name="
287 + workspace.getName() + " id="
288 + workspace.getId());
290 workspaceIds.put(workspace.getName().toLowerCase(),
296 } catch (Exception e) {
297 if (logger.isDebugEnabled()) {
298 logger.debug("retrieveWorkspaceIds() caught exception ", e);
302 if (repoSession != null) {
303 releaseRepositorySession(repoSession);