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.servlet.ServletContext;
\r
10 import org.collectionspace.services.config.RepositoryClientConfigType;
\r
11 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
\r
12 import org.nuxeo.ecm.core.api.DocumentModel;
\r
13 import org.nuxeo.ecm.core.api.DocumentModelList;
\r
14 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
\r
15 import org.nuxeo.osgi.application.FrameworkBootstrap;
\r
16 import org.slf4j.Logger;
\r
17 import org.slf4j.LoggerFactory;
\r
19 public class NuxeoConnectorEmbedded {
\r
21 <host>127.0.0.1</host>
\r
22 <port>62474</port> <!-- java -->
\r
24 private Logger logger = LoggerFactory.getLogger(NuxeoConnectorEmbedded.class);
\r
26 public final static String NUXEO_CLIENT_DIR = "nuxeo-client";
\r
27 public final static String NUXEO_SERVER_DIR = "nuxeo-server";
\r
28 private final static String ERROR_CONNECTOR_NOT_INITIALIZED = "NuxeoConnector is not initialized!";
\r
31 private static final String HOST = "127.0.0.1";
\r
32 private static final int PORT = 62474;
\r
34 private final static String CSPACE_JEESERVER_HOME = "CSPACE_CONTAINER";
\r
35 private final static String CSPACE_NUXEO_HOME = "CSPACE_NUXEO_HOME";
\r
37 private static final String NUXEO_CLIENT_USERNAME = "NUXEO_CLIENT_USERNAME";
\r
38 private static final String NUXEO_CLIENT_PASSWORD = "NUXEO_CLIENT_PASSWORD";
\r
40 private static final NuxeoConnectorEmbedded self = new NuxeoConnectorEmbedded();
\r
41 private NuxeoClientEmbedded client;
\r
42 private ServletContext servletContext = null;
\r
43 private volatile boolean initialized = false; // use volatile for lazy
\r
44 // initialization in
\r
46 private RepositoryClientConfigType repositoryClientConfig;
\r
47 public FrameworkBootstrap fb;
\r
49 private NuxeoConnectorEmbedded() {
\r
52 public final static NuxeoConnectorEmbedded getInstance() {
\r
56 private String getClientUserName() {
\r
57 String username = System.getenv(NUXEO_CLIENT_USERNAME);
\r
58 if (username == null) {
\r
59 username = "Administrator";
\r
64 private String getClientPassword() {
\r
65 String password = System.getenv(NUXEO_CLIENT_PASSWORD);
\r
66 if (password == null) {
\r
67 password = "Administrator";
\r
72 private String getServerRootPath() {
\r
73 String result = null;
\r
75 String prop = System.getenv(CSPACE_JEESERVER_HOME);
\r
76 if (prop == null || prop.isEmpty()) {
\r
77 logger.error("The following CollectionSpace services' environment variable needs to be set: " + CSPACE_JEESERVER_HOME);
\r
83 private String getNuxeoServerPath(String serverRootPath) throws IOException {
\r
84 String result = null;
\r
86 // Look for the CSPACE_NUXEO_HOME environment variable that might contain the fully qualified path of the
\r
87 // Nuxeo EP configuration directory.
\r
89 String prop = System.getenv(CSPACE_NUXEO_HOME);
\r
90 if (prop != null && !prop.isEmpty()) {
\r
94 // Could not find the 'CSPACE_NUXEO_HOME' environment variable, so using the default location instead.
\r
96 result = serverRootPath + "/" + NUXEO_SERVER_DIR;
\r
102 private File getNuxeoServerDir(String serverRootPath) throws IOException {
\r
103 File result = null;
\r
104 String errMsg = null;
\r
106 String path = getNuxeoServerPath(serverRootPath);
\r
107 if (path != null) {
\r
108 File temp = new File(path);
\r
109 if (temp.exists() == true) {
\r
112 errMsg = "The Nuxeo EP configuration directory is missing or inaccessible at: '" + path + "'.";
\r
116 if (result == null) {
\r
117 if (errMsg == null) {
\r
118 path = path != null ? path : "<empty>";
\r
119 errMsg = "Unknown error trying to find Nuxeo configuration: '" +
\r
120 CSPACE_NUXEO_HOME + "' = " +
\r
123 throw new IOException(errMsg);
\r
130 // Start/boot the Nuxeo EP server instance
\r
132 private void startNuxeoEP(String serverRootPath) throws Exception {
\r
133 File nuxeoHomeDir = getNuxeoServerDir(serverRootPath);
\r
135 if (logger.isInfoEnabled() == true) {
\r
136 logger.info("Starting Nuxeo EP server from configuration at: "
\r
137 + nuxeoHomeDir.getCanonicalPath());
\r
140 fb = new FrameworkBootstrap(NuxeoConnectorEmbedded.class.getClassLoader(),
\r
147 * release releases resources occupied by Nuxeo remoting client runtime
\r
149 * @throws java.lang.Exception
\r
151 public void release() throws Exception {
\r
152 if (initialized == true) {
\r
154 client.tryDisconnect();
\r
155 } catch (Exception e) {
\r
156 logger.error("Failed to disconnect Nuxeo connection.", e);
\r
162 public void initialize(String serverRootPath,
\r
163 RepositoryClientConfigType repositoryClientConfig,
\r
164 ServletContext servletContext) throws Exception {
\r
165 if (initialized == false) {
\r
166 synchronized (this) {
\r
167 if (initialized == false) {
\r
168 this.servletContext = servletContext;
\r
169 this.repositoryClientConfig = repositoryClientConfig;
\r
170 startNuxeoEP(serverRootPath);
\r
171 client = new NuxeoClientEmbedded();
\r
172 initialized = true;
\r
179 * releaseRepositorySession releases given repository session
\r
181 * @param repoSession
\r
182 * @throws java.lang.Exception
\r
184 public void releaseRepositorySession(RepositoryInstance repoSession)
\r
186 if (repoSession != null) {
\r
187 getClient().releaseRepository(repoSession);
\r
189 if (logger.isDebugEnabled()) {
\r
190 logger.debug("releaseRepositorySession() released repository session");
\r
196 * getRepositorySession get session to default repository
\r
198 * @return RepositoryInstance
\r
199 * @throws java.lang.Exception
\r
201 public RepositoryInstance getRepositorySession() throws Exception {
\r
202 RepositoryInstance repoSession = getClient().openRepository();
\r
203 if (logger.isDebugEnabled()) {
\r
204 logger.debug("getRepositorySession() opened repository session");
\r
206 return repoSession;
\r
210 * getClient get Nuxeo client for accessing Nuxeo services remotely using
\r
211 * Nuxeo Java (EJB) Remote APIS
\r
213 * @return NuxeoClient
\r
214 * @throws java.lang.Exception
\r
216 public NuxeoClientEmbedded getClient() throws Exception {
\r
217 if (initialized == true) {
\r
218 if (client.isConnected()) {
\r
222 client.forceConnect(this.HOST,
\r
224 if (logger.isDebugEnabled()) {
\r
225 logger.debug("getClient(): connection successful port="
\r
232 // Nuxeo connection was not initialized
\r
234 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
235 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
238 void releaseClient() throws Exception {
\r
239 if (initialized == true) {
\r
240 // client.logout();
\r
243 // Nuxeo connection was not initialized
\r
245 logger.error(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
246 throw new IllegalStateException(ERROR_CONNECTOR_NOT_INITIALIZED);
\r
251 * retrieveWorkspaceIds retrieves all workspace ids from default repository
\r
253 * @param tenantDomain
\r
254 * domain representing tenant
\r
256 * @throws java.lang.Exception
\r
258 public Hashtable<String, String> retrieveWorkspaceIds(String tenantDomain)
\r
260 RepositoryInstance repoSession = null;
\r
261 Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
\r
263 repoSession = getRepositorySession();
\r
264 DocumentModel rootDoc = repoSession.getRootDocument();
\r
265 DocumentModelList rootChildrenList = repoSession
\r
266 .getChildren(rootDoc.getRef());
\r
267 Iterator<DocumentModel> diter = rootChildrenList.iterator();
\r
268 while (diter.hasNext()) {
\r
269 DocumentModel domain = diter.next();
\r
270 String domainPath = "/" + tenantDomain;
\r
271 if (!domain.getPathAsString().equalsIgnoreCase(domainPath)) {
\r
274 if (logger.isDebugEnabled()) {
\r
275 logger.debug("domain=" + domain.toString());
\r
277 DocumentModelList domainChildrenList = repoSession
\r
278 .getChildren(domain.getRef());
\r
279 Iterator<DocumentModel> witer = domainChildrenList.iterator();
\r
280 while (witer.hasNext()) {
\r
281 DocumentModel childNode = witer.next();
\r
282 if (NuxeoUtils.Workspaces.equalsIgnoreCase(childNode.getName())) {
\r
283 DocumentModelList workspaceList = repoSession
\r
284 .getChildren(childNode.getRef());
\r
285 Iterator<DocumentModel> wsiter = workspaceList
\r
287 while (wsiter.hasNext()) {
\r
288 DocumentModel workspace = wsiter.next();
\r
289 if (logger.isDebugEnabled()) {
\r
290 logger.debug("workspace name="
\r
291 + workspace.getName() + " id="
\r
292 + workspace.getId());
\r
294 workspaceIds.put(workspace.getName().toLowerCase(),
\r
295 workspace.getId());
\r
300 } catch (Exception e) {
\r
301 if (logger.isDebugEnabled()) {
\r
302 logger.debug("retrieveWorkspaceIds() caught exception ", e);
\r
306 if (repoSession != null) {
\r
307 releaseRepositorySession(repoSession);
\r
310 return workspaceIds;
\r
314 private void loadBundles() throws Exception {
\r