1 package org.collectionspace.services.nuxeo.client.java;
\r
4 import java.io.IOException;
\r
5 import java.util.Collection;
\r
6 import java.util.Hashtable;
\r
7 import java.util.Iterator;
\r
9 import javax.servlet.ServletConfig;
\r
10 import javax.servlet.ServletContext;
\r
12 //import org.collectionspace.services.common.RepositoryClientConfigType;
\r
13 import org.collectionspace.services.common.RepositoryClientConfigType;
\r
14 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
\r
15 import org.nuxeo.ecm.core.api.CoreInstance;
\r
16 import org.nuxeo.ecm.core.api.DocumentModel;
\r
17 import org.nuxeo.ecm.core.api.DocumentModelList;
\r
18 import org.nuxeo.ecm.core.api.repository.Repository;
\r
19 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
\r
20 import org.nuxeo.ecm.core.api.repository.RepositoryManager;
\r
21 import org.nuxeo.ecm.core.client.DefaultLoginHandler;
\r
22 import org.nuxeo.ecm.core.client.NuxeoApp;
\r
23 //import org.nuxeo.ecm.core.client.NuxeoClient;
\r
24 import org.nuxeo.osgi.application.FrameworkBootstrap;
\r
25 import org.nuxeo.runtime.api.Framework;
\r
26 import org.slf4j.Logger;
\r
27 import org.slf4j.LoggerFactory;
\r
29 public class NuxeoConnectorEmbedded {
\r
31 <host>127.0.0.1</host>
\r
32 <port>62474</port> <!-- java -->
\r
34 private Logger logger = LoggerFactory.getLogger(NuxeoConnectorEmbedded.class);
\r
36 public final static String NUXEO_CLIENT_DIR = "nuxeo-client";
\r
37 public final static String NUXEO_SERVER_DIR = "nuxeo-server";
\r
38 private final static String ERROR_CONNECTOR_NOT_INITIALIZED = "NuxeoConnector is not initialized!";
\r
41 private static final String HOST = "127.0.0.1";
\r
42 private static final int PORT = 62474;
\r
44 private final static String CSPACE_JEESERVER_HOME = "CSPACE_CONTAINER";
\r
45 private final static String CSPACE_NUXEO_HOME = "CSPACE_NUXEO_HOME";
\r
47 private static final String NUXEO_CLIENT_USERNAME = "NUXEO_CLIENT_USERNAME";
\r
48 private static final String NUXEO_CLIENT_PASSWORD = "NUXEO_CLIENT_PASSWORD";
\r
50 private static final NuxeoConnectorEmbedded self = new NuxeoConnectorEmbedded();
\r
51 private NuxeoClientEmbedded client;
\r
52 private ServletContext servletContext = null;
\r
53 private volatile boolean initialized = false; // use volatile for lazy
\r
54 // initialization in
\r
56 private RepositoryClientConfigType repositoryClientConfig;
\r
57 public FrameworkBootstrap fb;
\r
59 private NuxeoConnectorEmbedded() {
\r
62 public final static NuxeoConnectorEmbedded getInstance() {
\r
66 private String getClientUserName() {
\r
67 String username = System.getenv(NUXEO_CLIENT_USERNAME);
\r
68 if (username == null) {
\r
69 username = "Administrator";
\r
74 private String getClientPassword() {
\r
75 String password = System.getenv(NUXEO_CLIENT_PASSWORD);
\r
76 if (password == null) {
\r
77 password = "Administrator";
\r
82 private String getServerRootPath() {
\r
83 String result = null;
\r
85 String prop = System.getenv(CSPACE_JEESERVER_HOME);
\r
86 if (prop == null || prop.isEmpty()) {
\r
87 logger.error("The following CollectionSpace services' environment variable needs to be set: " + CSPACE_JEESERVER_HOME);
\r
93 private String getNuxeoServerPath(String serverRootPath) throws IOException {
\r
94 String result = null;
\r
96 // Look for the CSPACE_NUXEO_HOME environment variable that might contain the fully qualified path of the
\r
97 // Nuxeo EP configuration directory.
\r
99 String prop = System.getenv(CSPACE_NUXEO_HOME);
\r
100 if (prop != null && !prop.isEmpty()) {
\r
104 // Could not find the 'CSPACE_NUXEO_HOME' environment variable, so using the default location instead.
\r
106 result = serverRootPath + "/" + NUXEO_SERVER_DIR;
\r
112 private File getNuxeoServerDir(String serverRootPath) throws IOException {
\r
113 File result = null;
\r
114 String errMsg = null;
\r
116 String path = getNuxeoServerPath(serverRootPath);
\r
117 if (path != null) {
\r
118 File temp = new File(path);
\r
119 if (temp.exists() == true) {
\r
122 errMsg = "The Nuxeo EP configuration directory is missing or inaccessible at: '" + path + "'.";
\r
126 if (result == null) {
\r
127 if (errMsg == null) {
\r
128 path = path != null ? path : "<empty>";
\r
129 errMsg = "Unknown error trying to find Nuxeo configuration: '" +
\r
130 CSPACE_NUXEO_HOME + "' = " +
\r
133 throw new IOException(errMsg);
\r
140 // Start/boot the Nuxeo EP server instance
\r
142 private void startNuxeoEP(String serverRootPath) throws Exception {
\r
143 File nuxeoHomeDir = getNuxeoServerDir(serverRootPath);
\r
145 if (logger.isInfoEnabled() == true) {
\r
146 logger.info("Starting Nuxeo EP server from configuration at: "
\r
147 + nuxeoHomeDir.getCanonicalPath());
\r
150 fb = new FrameworkBootstrap(NuxeoConnectorEmbedded.class.getClassLoader(),
\r
157 * release releases resources occupied by Nuxeo remoting client runtime
\r
159 * @throws java.lang.Exception
\r
161 public void release() throws Exception {
\r
162 if (initialized == true) {
\r
164 client.tryDisconnect();
\r
165 } catch (Exception e) {
\r
166 logger.error("Failed to disconnect Nuxeo connection.", e);
\r
172 public void initialize(String serverRootPath,
\r
173 RepositoryClientConfigType repositoryClientConfig,
\r
174 ServletContext servletContext) throws Exception {
\r
175 if (initialized == false) {
\r
176 synchronized (this) {
\r
177 if (initialized == false) {
\r
178 this.servletContext = servletContext;
\r
179 this.repositoryClientConfig = repositoryClientConfig;
\r
180 startNuxeoEP(serverRootPath);
\r
181 client = new NuxeoClientEmbedded();
\r
182 initialized = true;
\r
189 * releaseRepositorySession releases given repository session
\r
191 * @param repoSession
\r
192 * @throws java.lang.Exception
\r
194 public void releaseRepositorySession(RepositoryInstance repoSession)
\r
196 if (repoSession != null) {
\r
197 getClient().releaseRepository(repoSession);
\r
199 if (logger.isDebugEnabled()) {
\r
200 logger.debug("releaseRepositorySession() released repository session");
\r
206 * getRepositorySession get session to default repository
\r
208 * @return RepositoryInstance
\r
209 * @throws java.lang.Exception
\r
211 public RepositoryInstance getRepositorySession() throws Exception {
\r
212 RepositoryInstance repoSession = getClient().openRepository();
\r
213 if (logger.isDebugEnabled()) {
\r
214 logger.debug("getRepositorySession() opened repository session");
\r
216 return repoSession;
\r
220 * getClient get Nuxeo client for accessing Nuxeo services remotely using
\r
221 * Nuxeo Java (EJB) Remote APIS
\r
223 * @return NuxeoClient
\r
224 * @throws java.lang.Exception
\r
226 public NuxeoClientEmbedded getClient() throws Exception {
\r
227 if (initialized == true) {
\r
228 if (client.isConnected()) {
\r
232 client.forceConnect(this.HOST,
\r
234 if (logger.isDebugEnabled()) {
\r
235 logger.debug("getClient(): connection successful port="
\r
242 // Nuxeo connection was not initialized
\r
244 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
245 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
248 void releaseClient() throws Exception {
\r
249 if (initialized == true) {
\r
250 // client.logout();
\r
253 // Nuxeo connection was not initialized
\r
255 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
256 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
261 * retrieveWorkspaceIds retrieves all workspace ids from default repository
\r
263 * @param tenantDomain
\r
264 * domain representing tenant
\r
266 * @throws java.lang.Exception
\r
268 public Hashtable<String, String> retrieveWorkspaceIds(String tenantDomain)
\r
270 RepositoryInstance repoSession = null;
\r
271 Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
\r
273 repoSession = getRepositorySession();
\r
274 DocumentModel rootDoc = repoSession.getRootDocument();
\r
275 DocumentModelList rootChildrenList = repoSession
\r
276 .getChildren(rootDoc.getRef());
\r
277 Iterator<DocumentModel> diter = rootChildrenList.iterator();
\r
278 while (diter.hasNext()) {
\r
279 DocumentModel domain = diter.next();
\r
280 String domainPath = "/" + tenantDomain;
\r
281 if (!domain.getPathAsString().equalsIgnoreCase(domainPath)) {
\r
284 if (logger.isDebugEnabled()) {
\r
285 logger.debug("domain=" + domain.toString());
\r
287 DocumentModelList domainChildrenList = repoSession
\r
288 .getChildren(domain.getRef());
\r
289 Iterator<DocumentModel> witer = domainChildrenList.iterator();
\r
290 while (witer.hasNext()) {
\r
291 DocumentModel childNode = witer.next();
\r
292 if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) {
\r
293 DocumentModelList workspaceList = repoSession
\r
294 .getChildren(childNode.getRef());
\r
295 Iterator<DocumentModel> wsiter = workspaceList
\r
297 while (wsiter.hasNext()) {
\r
298 DocumentModel workspace = wsiter.next();
\r
299 if (logger.isDebugEnabled()) {
\r
300 logger.debug("workspace name="
\r
301 + workspace.getName() + " id="
\r
302 + workspace.getId());
\r
304 workspaceIds.put(workspace.getName().toLowerCase(),
\r
305 workspace.getId());
\r
310 } catch (Exception e) {
\r
311 if (logger.isDebugEnabled()) {
\r
312 logger.debug("retrieveWorkspaceIds() caught exception ", e);
\r
316 if (repoSession != null) {
\r
317 releaseRepositorySession(repoSession);
\r
320 return workspaceIds;
\r
324 private void loadBundles() throws Exception {
\r