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;
15 import org.nuxeo.ecm.core.api.DocumentModel;
16 import org.nuxeo.ecm.core.api.DocumentModelList;
17 import org.nuxeo.osgi.application.FrameworkBootstrap;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public class NuxeoConnectorEmbedded {
24 <host>127.0.0.1</host>
25 <port>62474</port> <!-- java -->
27 private Logger logger = LoggerFactory.getLogger(NuxeoConnectorEmbedded.class);
29 public final static String NUXEO_CLIENT_DIR = JEEServerDeployment.NUXEO_CLIENT_DIR;
30 public final static String NUXEO_SERVER_DIR = JEEServerDeployment.NUXEO_SERVER_DIR;
31 private final static String ERROR_CONNECTOR_NOT_INITIALIZED = "NuxeoConnector is not initialized!";
33 private final static String CSPACE_NUXEO_HOME = "CSPACE_NUXEO_HOME";
35 private static final NuxeoConnectorEmbedded self = new NuxeoConnectorEmbedded();
36 private NuxeoClientEmbedded client;
37 private volatile boolean initialized = false; // use volatile for lazy
40 public FrameworkBootstrap fb;
41 private ServletContext servletContext;
42 private RepositoryClientConfigType repositoryClientConfig;
44 private NuxeoConnectorEmbedded() {
48 public final static NuxeoConnectorEmbedded getInstance() {
52 private String getNuxeoServerPath(String serverRootPath) throws IOException {
55 // Look for the CSPACE_NUXEO_HOME environment variable that might contain the fully qualified path of the
56 // Nuxeo EP configuration directory.
58 String prop = System.getenv(CSPACE_NUXEO_HOME);
59 if (prop != null && !prop.isEmpty()) {
63 // Could not find the 'CSPACE_NUXEO_HOME' environment variable, so using the default location instead.
65 result = serverRootPath + "/" + NUXEO_SERVER_DIR;
71 private File getNuxeoServerDir(String serverRootPath) throws IOException {
75 String path = getNuxeoServerPath(serverRootPath);
77 File temp = new File(path);
78 if (temp.exists() == true) {
81 errMsg = "The Nuxeo EP configuration directory is missing or inaccessible at: '" + path + "'.";
87 path = path != null ? path : "<empty>";
88 errMsg = "Unknown error trying to find Nuxeo configuration: '" +
89 CSPACE_NUXEO_HOME + "' = " +
92 throw new IOException(errMsg);
99 // Start/boot the Nuxeo EP server instance
101 private void startNuxeoEP(String serverRootPath) throws Exception {
102 File nuxeoHomeDir = getNuxeoServerDir(serverRootPath);
104 if (logger.isInfoEnabled() == true) {
105 logger.info("Starting Nuxeo EP server from configuration at: "
106 + nuxeoHomeDir.getCanonicalPath());
109 fb = new FrameworkBootstrap(NuxeoConnectorEmbedded.class.getClassLoader(),
111 fb.setHostName("Tomcat");
112 fb.setHostVersion("7.0.57"); //FIXME: Should not be hard coded.
119 * release releases resources occupied by Nuxeo remoting client runtime
121 * @throws java.lang.Exception
123 public void release() throws Exception {
124 if (initialized == true) {
126 client.tryDisconnect();
127 } catch (Exception e) {
128 logger.error("Failed to disconnect Nuxeo connection.", e);
134 public void initialize(String serverRootPath,
135 RepositoryClientConfigType repositoryClientConfig,
136 ServletContext servletContext) throws Exception {
137 if (initialized == false) {
138 synchronized (this) {
139 if (initialized == false) {
140 this.servletContext = servletContext;
141 this.repositoryClientConfig = repositoryClientConfig;
142 startNuxeoEP(serverRootPath);
143 client = NuxeoClientEmbedded.getInstance();
151 * releaseRepositorySession releases given repository session
154 * @throws java.lang.Exception
156 public void releaseRepositorySession(CoreSessionInterface repoSession)
158 if (repoSession != null) {
159 getClient().releaseRepository(repoSession);
161 if (logger.isDebugEnabled()) {
162 logger.debug("releaseRepositorySession() released repository session");
168 * getRepositorySession get session to default repository
170 * @return RepositoryInstance
171 * @throws java.lang.Exception
173 public CoreSessionInterface getRepositorySession(RepositoryDomainType repoDomain) throws Exception {
174 CoreSessionInterface repoSession = getClient().openRepository(repoDomain);
176 if (logger.isDebugEnabled() && repoSession != null) {
177 String repoName = repoDomain.getRepositoryName();
178 logger.debug("getRepositorySession() opened repository session on: %s repo",
179 repoName != null ? repoName : "unknown");
186 // TODO: Remove after CSPACE-6375 issue is resolved.
187 // public List<RepositoryDescriptor> getRepositoryDescriptor(String name) throws Exception {
188 // RepositoryDescriptor repo = null;
189 // RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
190 // Iterable<RepositoryDescriptor> descriptorsList = repositoryManager.getDescriptors();
191 // for (RepositoryDescriptor descriptor : descriptorsList) {
192 // String homeDir = descriptor.getHomeDirectory();
193 // String config = descriptor.getConfigurationFile();
194 // RepositoryFactory factor = descriptor.getFactory();
201 * getClient get Nuxeo client for accessing Nuxeo services remotely using
202 * Nuxeo Java (EJB) Remote APIS
204 * @return NuxeoClient
205 * @throws java.lang.Exception
207 public NuxeoClientEmbedded getClient() throws Exception {
208 if (initialized == true) {
212 // Nuxeo connection was not initialized
214 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
215 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
218 void releaseClient() throws Exception {
219 if (initialized == true) {
223 // Nuxeo connection was not initialized
225 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
226 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
231 * retrieveWorkspaceIds retrieves all workspace ids from default repository
234 * a repository domain for a given tenant - see the tenant bindings XML file for details
236 * @throws java.lang.Exception
238 public Hashtable<String, String> retrieveWorkspaceIds(RepositoryDomainType repoDomain)
240 CoreSessionInterface repoSession = null;
241 Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
243 repoSession = getRepositorySession(repoDomain);
244 DocumentModel rootDoc = repoSession.getRootDocument();
245 DocumentModelList rootChildrenList = repoSession.getChildren(rootDoc.getRef());
246 Iterator<DocumentModel> diter = rootChildrenList.iterator();
247 while (diter.hasNext()) {
248 DocumentModel domain = diter.next();
249 String domainPath = "/" + repoDomain.getStorageName();
250 if (!domain.getPathAsString().equalsIgnoreCase(domainPath)) {
251 continue; // If it's not our domain folder/directory then skip it
253 if (logger.isDebugEnabled()) {
254 logger.debug("domain=" + domain.toString());
256 DocumentModelList domainChildrenList = repoSession.getChildren(domain.getRef());
257 Iterator<DocumentModel> witer = domainChildrenList.iterator();
258 while (witer.hasNext()) {
259 DocumentModel childNode = witer.next();
260 if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) {
261 DocumentModelList workspaceList = repoSession
262 .getChildren(childNode.getRef());
263 Iterator<DocumentModel> wsiter = workspaceList
265 while (wsiter.hasNext()) {
266 DocumentModel workspace = wsiter.next();
267 if (logger.isDebugEnabled()) {
268 logger.debug("workspace name="
269 + workspace.getName() + " id="
270 + workspace.getId());
272 workspaceIds.put(workspace.getName().toLowerCase(),
278 } catch (Exception e) {
279 if (logger.isDebugEnabled()) {
280 logger.debug("retrieveWorkspaceIds() caught exception ", e);
284 if (repoSession != null) {
285 releaseRepositorySession(repoSession);