From 2f89ab02c3d51f4c592055d97e9c97bde1664caa Mon Sep 17 00:00:00 2001 From: Sanjay Dalal Date: Thu, 21 May 2009 21:29:03 +0000 Subject: [PATCH] CSPACE-59, CSPACE-60, added common service layer initialization (reading config, initializing nuxeo client runtime, etc.). also communicates with nuxeo repository to retrieve all workspace ids used by services deployed. requires fix in nuxeo-runtime. fixed bug introduced in collectionobject documenttype from the earlier build. --- JaxRsServiceProvider/pom.xml | 64 +++++- .../src/main/resources/nuxeo.properties | 2 - .../src/main/webapp/WEB-INF/web.xml | 83 ++++--- nbproject/private/private.xml | 4 + .../hello/services/MultischemaResource.java | 1 - .../client/CollectionSpaceClient.java | 2 +- .../pom.xml | 14 +- services/collectionobject/pom.xml | 62 ++++++ .../CollectionObjectServiceNuxeoImpl.java | 3 +- services/common/pom.xml | 152 ++++++++++++- .../common/src/main/config/service-config.xml | 33 +++ .../common/ServiceContextListener.java | 31 +++ .../services/common/ServiceMain.java | 137 ++++++++++++ .../CollectionSpaceServiceNuxeoImpl.java | 45 +++- .../services/nuxeo/NuxeoConnector.java | 205 ++++++++++++++++++ .../services/nuxeo/NuxeoUtils.java | 72 ++++++ services/common/src/main/resources/common.xsd | 51 +++++ 17 files changed, 891 insertions(+), 70 deletions(-) delete mode 100644 JaxRsServiceProvider/src/main/resources/nuxeo.properties create mode 100644 services/collectionobject/pom.xml create mode 100644 services/common/src/main/config/service-config.xml create mode 100644 services/common/src/main/java/org/collectionspace/services/common/ServiceContextListener.java create mode 100644 services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java create mode 100644 services/common/src/main/java/org/collectionspace/services/nuxeo/NuxeoConnector.java create mode 100644 services/common/src/main/java/org/collectionspace/services/nuxeo/NuxeoUtils.java create mode 100644 services/common/src/main/resources/common.xsd diff --git a/JaxRsServiceProvider/pom.xml b/JaxRsServiceProvider/pom.xml index e59c41b15..684c2df8a 100644 --- a/JaxRsServiceProvider/pom.xml +++ b/JaxRsServiceProvider/pom.xml @@ -68,19 +68,77 @@ - + + jboss + jboss-remoting + 2.2.2.SP8 + provided + + + org.nuxeo.common + nuxeo-common + ${nuxeo.version.1.5} + provided + + + org.nuxeo.runtime + nuxeo-runtime + ${nuxeo.version.1.5} + + + org.nuxeo.ecm.core + nuxeo-core-api + ${nuxeo.version.1.5} + + + org.nuxeo.ecm.core + nuxeo-core-client + ${nuxeo.version.1.5} + + + org.nuxeo.ecm.core + nuxeo-core-query + ${nuxeo.version.1.5} + + + org.nuxeo.ecm.core + nuxeo-core-schema + ${nuxeo.version.1.5} + + + org.nuxeo.ecm.core + nuxeo-core-io + ${nuxeo.version.1.5} + + + org.nuxeo.ecm.core + nuxeo-core-facade + ${nuxeo.version.1.5} + + + org.nuxeo.runtime + nuxeo-runtime-osgi + ${nuxeo.version.1.5} + + + org.osgi + osgi-core + 4.1 + + - collectionspace-services-jaxrs-provider + CollectionSpace org.codehaus.mojo jboss-maven-plugin ${jboss.dir} + 8180 @@ -90,7 +148,7 @@ undeploy - ${basedir}/target/collectionspace-services.war + ${basedir}/target/CollectionSpace.war diff --git a/JaxRsServiceProvider/src/main/resources/nuxeo.properties b/JaxRsServiceProvider/src/main/resources/nuxeo.properties deleted file mode 100644 index 3c9d5afb5..000000000 --- a/JaxRsServiceProvider/src/main/resources/nuxeo.properties +++ /dev/null @@ -1,2 +0,0 @@ -org.nuxeo.runtime.server.enabled=false -org.nuxeo.runtime.streaming.isServer=false diff --git a/JaxRsServiceProvider/src/main/webapp/WEB-INF/web.xml b/JaxRsServiceProvider/src/main/webapp/WEB-INF/web.xml index 6777d8f8e..29379ffcb 100644 --- a/JaxRsServiceProvider/src/main/webapp/WEB-INF/web.xml +++ b/JaxRsServiceProvider/src/main/webapp/WEB-INF/web.xml @@ -1,34 +1,49 @@ - - - - CollectionSpace - - - javax.ws.rs.Application - org.collectionspace.services.jaxrs.CollectionSpaceJaxRsApplication - - - - resteasy.servlet.mapping.prefix - /nuxeo-rest - - - - - org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap - - - - - Resteasy - - org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher - - - - - Resteasy - /nuxeo-rest/* - - - + + + + + CollectionSpace + + + javax.ws.rs.Application + org.collectionspace.services.jaxrs.CollectionSpaceJaxRsApplication + + + + resteasy.servlet.mapping.prefix + /nuxeo-rest + + + + + + org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap + + + + + + org.collectionspace.services.common.ServiceContextListener + + + + + Resteasy + + org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher + + + + + Resteasy + /nuxeo-rest/* + + + diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index cc2c0e57c..392f2c6e0 100644 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -1,4 +1,8 @@ + + file:/C:/dev/src/cs/src/services/trunk/build.xml + file:/C:/dev/src/cs/src/services/trunk/pom.xml + diff --git a/sandbox/sanjay/prototypes/jaxrs-prototype/jaxrs-nuxeo-javaapi-service/src/main/java/org/collectionspace/hello/services/MultischemaResource.java b/sandbox/sanjay/prototypes/jaxrs-prototype/jaxrs-nuxeo-javaapi-service/src/main/java/org/collectionspace/hello/services/MultischemaResource.java index 48fdb8da1..0fe6f8cc6 100644 --- a/sandbox/sanjay/prototypes/jaxrs-prototype/jaxrs-nuxeo-javaapi-service/src/main/java/org/collectionspace/hello/services/MultischemaResource.java +++ b/sandbox/sanjay/prototypes/jaxrs-prototype/jaxrs-nuxeo-javaapi-service/src/main/java/org/collectionspace/hello/services/MultischemaResource.java @@ -31,7 +31,6 @@ import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataOutput; import org.nuxeo.common.utils.IdUtils; -import org.nuxeo.ecm.core.api.CoreSession; import org.nuxeo.ecm.core.api.DocumentModel; diff --git a/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java b/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java index 9b41c3fe8..43d363504 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java +++ b/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java @@ -11,7 +11,7 @@ public abstract class CollectionSpaceClient { private static final String SCHEME = "http"; private static final String HOST = "localhost"; - private static final String PORT = "8080"; + private static final String PORT = "8180"; private static final String URI = "/CollectionSpace/nuxeo-rest"; private static final String URL = SCHEME + "://" + HOST + ":" + diff --git a/services/collectionobject/3rdparty/nuxeo-platform-cs-collectionobject/pom.xml b/services/collectionobject/3rdparty/nuxeo-platform-cs-collectionobject/pom.xml index d5eb52d94..56fc1f11d 100644 --- a/services/collectionobject/3rdparty/nuxeo-platform-cs-collectionobject/pom.xml +++ b/services/collectionobject/3rdparty/nuxeo-platform-cs-collectionobject/pom.xml @@ -3,15 +3,15 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 - - org.collectionspace.services.collectionobject.3rdparty - org.collectionspace.services - 1.0 - + + org.nuxeo.ecm.platform + nuxeo-platform-parent + 5.2-SNAPSHOT + - org.collectionspace.services.collectionobject.doctype + nuxeo-platform-cs-collectionobject jar - services.collectionobject.doctype + nuxeo-platform-cs-collectionobject CollectionObject Nuxeo document type diff --git a/services/collectionobject/pom.xml b/services/collectionobject/pom.xml new file mode 100644 index 000000000..c413cb0a9 --- /dev/null +++ b/services/collectionobject/pom.xml @@ -0,0 +1,62 @@ + + + + org.collectionspace.services.main + org.collectionspace.services + 1.0 + + 4.0.0 + org.collectionspace.services + 1.0 + org.collectionspace.services.collectionobject + pom + services.collectionobject + + + 5.2-SNAPSHOT + 1.5-SNAPSHOT + + + + jaxb + service + 3rdparty + client + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + org.apache.maven.plugins + maven-site-plugin + + /usr/bin/unzip -o > err.txt + + + + org.apache.maven.plugins + maven-war-plugin + 2.0.1 + + WEB-INF/lib/*.jar + + + true + + + + + + + + + diff --git a/services/collectionobject/service/src/main/java/org/collectionspace/services/CollectionObjectServiceNuxeoImpl.java b/services/collectionobject/service/src/main/java/org/collectionspace/services/CollectionObjectServiceNuxeoImpl.java index 17fa1cf30..cf3a9679e 100644 --- a/services/collectionobject/service/src/main/java/org/collectionspace/services/CollectionObjectServiceNuxeoImpl.java +++ b/services/collectionobject/service/src/main/java/org/collectionspace/services/CollectionObjectServiceNuxeoImpl.java @@ -33,7 +33,8 @@ public class CollectionObjectServiceNuxeoImpl extends final static String CO_NUXEO_DC_TITLE = "CollectionSpace-CollectionObject"; // replace WORKSPACE_UID for resource workspace - static String CS_COLLECTIONOBJECT_WORKSPACE_UID = "e4a8e3d4-0954-4c10-963a-cc4ed09d5112"; + static String CS_COLLECTIONOBJECT_WORKSPACE_UID = "ed110dfd-34e2-4870-bfdb-3be13d19c3f9"; + //"e4a8e3d4-0954-4c10-963a-cc4ed09d5112"; public Document deleteCollectionObject(String csid) throws DocumentException, IOException { diff --git a/services/common/pom.xml b/services/common/pom.xml index 2af9ac8c9..ec0232a6c 100644 --- a/services/common/pom.xml +++ b/services/common/pom.xml @@ -12,6 +12,14 @@ 1.0 services.common + + 4.2.3.GA + 3.0 + UTF-8 + 5.2-SNAPSHOT + 1.5-SNAPSHOT + + org.jboss.resteasy @@ -34,13 +42,22 @@ resteasy-multipart-provider 1.0.2.GA + + junit junit 4.1 test - + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + @@ -59,21 +76,67 @@ - + + jboss + jboss-remoting + 2.2.2.SP8 + provided + + + org.nuxeo.common + nuxeo-common + ${nuxeo.version.1.5} + provided + + + org.nuxeo.runtime + nuxeo-runtime + ${nuxeo.version.1.5} + org.nuxeo.ecm.core nuxeo-core-api ${nuxeo.version.1.5} - - - jboss-remoting - jboss - - + + + org.nuxeo.ecm.core + nuxeo-core-client + ${nuxeo.version.1.5} + + + org.nuxeo.ecm.core + nuxeo-core-query + ${nuxeo.version.1.5} + + + org.nuxeo.ecm.core + nuxeo-core-schema + ${nuxeo.version.1.5} + + + org.nuxeo.ecm.core + nuxeo-core-io + ${nuxeo.version.1.5} + + + org.nuxeo.ecm.core + nuxeo-core-facade + ${nuxeo.version.1.5} + + + org.nuxeo.runtime + nuxeo-runtime-osgi + ${nuxeo.version.1.5} + + + org.osgi + osgi-core + 4.1 + org.restlet org.restlet @@ -90,17 +153,88 @@ 1.0.7 - + jaxen jaxen 1.1.1 + + + com.sun.xml.bind + jaxb-impl + 2.0.2 + + + org.jvnet.jaxb2-commons + property-listener-injector + 1.0 + + + org.jvnet.jaxb2_commons + runtime + 0.4.1.4 + collectionspace-services-common + + org.jvnet.jaxb2.maven2 + maven-jaxb2-plugin + + + + generate + + + + + + + javax.activation + activation + 1.1 + + + com.sun.xml.bind + jaxb-impl + 2.1.2 + + + + + -XtoString + -Xinject-listener-code + + + + + + + org.jvnet.jaxb2_commons + + basic + 0.4.1 + + + + org.jvnet.jaxb2-commons + + + property-listener-injector + + 1.0 + + + + + + org.apache.maven.plugins maven-compiler-plugin diff --git a/services/common/src/main/config/service-config.xml b/services/common/src/main/config/service-config.xml new file mode 100644 index 000000000..552ff18d6 --- /dev/null +++ b/services/common/src/main/config/service-config.xml @@ -0,0 +1,33 @@ + + + + + + + + 127.0.0.1 + 62474 + Administrator + Administrator + + + + persons + People + + + collectionobjects + CollectionObjects + + + + diff --git a/services/common/src/main/java/org/collectionspace/services/common/ServiceContextListener.java b/services/common/src/main/java/org/collectionspace/services/common/ServiceContextListener.java new file mode 100644 index 000000000..5015d7394 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/ServiceContextListener.java @@ -0,0 +1,31 @@ +/** + * Copyright 2009 University of California at Berkeley + */ +package org.collectionspace.services.common; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +/** + * ServiceContextListener is a ServletContextListener that helps initialize + * the services layer at deployment and undeployment times + */ +public class ServiceContextListener implements ServletContextListener { + + public void contextInitialized(ServletContextEvent event) { + try{ + ServletContext sc = event.getServletContext(); + ServiceMain svcMain = ServiceMain.getInstance(); //first access initializes as well + svcMain.getWorkspaceIds(); + }catch(Exception e) { + e.printStackTrace(); + //fail here + throw new RuntimeException(e); + } + } + + public void contextDestroyed(ServletContextEvent event) { + ServiceMain.getInstance().release(); + } +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java new file mode 100644 index 000000000..c96971c5e --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java @@ -0,0 +1,137 @@ +/** + * Copyright 2009 University of California at Berkeley + */ +package org.collectionspace.services.common; + +import java.io.File; +import java.util.Hashtable; +import java.util.List; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Unmarshaller; +import org.collectionspace.services.common.ServiceConfig.NuxeoWorkspace; +import org.collectionspace.services.common.ServiceConfig.NuxeoWorkspace.Workspace; +import org.collectionspace.services.nuxeo.NuxeoConnector; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Main class for Services layer. It reads configuration and performs service + * level initialization. It is a singleton. + * @author + */ +public class ServiceMain { + + private static volatile ServiceMain instance = null; + final private static String CONFIG_FILE_NAME = "./cs/service-config.xml"; + final Logger logger = LoggerFactory.getLogger(ServiceMain.class); + private ServiceConfig serviceConfig; + private Hashtable serviceWorkspaces = new Hashtable(); + private NuxeoConnector nuxeoConnector; + + private ServiceMain() { + } + + /** + * getInstance returns the ServiceMain singleton instance after proper + * initialization in a thread-safe manner + * @return + */ + public static ServiceMain getInstance() { + if(instance == null){ + synchronized(ServiceMain.class){ + if(instance == null){ + ServiceMain temp = new ServiceMain(); + try{ + temp.initialize(); + }catch(Exception e){ + instance = null; + if(e instanceof RuntimeException){ + throw (RuntimeException) e; + }else{ + throw new RuntimeException(e); + } + } + instance = temp; + } + } + } + return instance; + } + + private void initialize() throws Exception { + serviceConfig = readConfig(); + nuxeoConnector = NuxeoConnector.getInstance(); + nuxeoConnector.initialize(serviceConfig.getNuxeoClientConfig()); + } + + /** + * release releases all resources occupied by service layer infrastructure + * but not necessarily those occupied by individual services + */ + public void release() { + try{ + if(nuxeoConnector != null){ + nuxeoConnector.release(); + } + serviceWorkspaces.clear(); + instance = null; + }catch(Exception e){ + e.printStackTrace(); + //gobble it + } + } + + private ServiceConfig readConfig() throws Exception { + JAXBContext jc = JAXBContext.newInstance(ServiceConfig.class); + Unmarshaller um = jc.createUnmarshaller(); + File configFile = new File(CONFIG_FILE_NAME); + if(!configFile.exists()){ + String msg = "Could not find configuration file " + CONFIG_FILE_NAME; + logger.error(msg); + throw new RuntimeException(msg); + } + ServiceConfig sconfig = (ServiceConfig) um.unmarshal(configFile); + if(logger.isDebugEnabled()){ + logger.debug("readConfig() read config file " + configFile.getAbsolutePath()); + } + return sconfig; + } + + synchronized public void getWorkspaceIds() throws Exception { + Hashtable workspaceIds = nuxeoConnector.retrieveWorkspaceIds(); + NuxeoWorkspace nuxeoWorkspace = serviceConfig.getNuxeoWorkspace(); + List workspaces = nuxeoWorkspace.getWorkspace(); + for(Workspace workspace : workspaces){ + String workspaceId = workspaceIds.get(workspace.getWorkspaceName().toLowerCase()); + if(workspaceId == null){ + logger.error("failed to retrieve workspace id for " + workspace.getWorkspaceName()); + //FIXME: should we throw an exception here? + continue; + } + serviceWorkspaces.put(workspace.getServiceName(), workspaceId); + if(logger.isDebugEnabled()){ + logger.debug("retrieved workspace id=" + workspaceId + + " service=" + workspace.getServiceName() + + " workspace=" + workspace.getWorkspaceName()); + } + } + } + + /** + * @return the serviceConfig + */ + public ServiceConfig getServiceConfig() { + return serviceConfig; + } + + synchronized public String getWorkspaceId(String serviceName) { + return serviceWorkspaces.get(serviceName); + } + + /** + * @return the nuxeoConnector + */ + public NuxeoConnector getNuxeoConnector() { + return nuxeoConnector; + } +} diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/CollectionSpaceServiceNuxeoImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/CollectionSpaceServiceNuxeoImpl.java index 5f131c81e..9b2318b9c 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/CollectionSpaceServiceNuxeoImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/CollectionSpaceServiceNuxeoImpl.java @@ -1,10 +1,12 @@ /** - * + * Copyright 2009 University of California at Berkeley */ package org.collectionspace.services.nuxeo; -import org.collectionspace.services.nuxeo.NuxeoRESTClient; - +import org.nuxeo.ecm.core.api.repository.RepositoryInstance; +import org.nuxeo.ecm.core.client.NuxeoClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @author remillet @@ -14,15 +16,34 @@ public abstract class CollectionSpaceServiceNuxeoImpl { //replace host if not running on localhost //static String CS_NUXEO_HOST = "173.45.234.217"; - static String CS_NUXEO_HOST = "localhost"; + static String CS_NUXEO_HOST = "localhost"; static String CS_NUXEO_URI = "http://" + CS_NUXEO_HOST + ":8080/nuxeo"; - + protected Logger logger = LoggerFactory.getLogger(CollectionSpaceServiceNuxeoImpl.class); + public NuxeoRESTClient getClient() { - NuxeoRESTClient nxClient = new NuxeoRESTClient(CS_NUXEO_URI); - - nxClient.setAuthType(NuxeoRESTClient.AUTH_TYPE_BASIC); - nxClient.setBasicAuthentication("Administrator", "Administrator"); - - return nxClient; - } + NuxeoRESTClient nxClient = new NuxeoRESTClient(CS_NUXEO_URI); + + nxClient.setAuthType(NuxeoRESTClient.AUTH_TYPE_BASIC); + nxClient.setBasicAuthentication("Administrator", "Administrator"); + + return nxClient; + } + + protected RepositoryInstance getRepositorySession() throws Exception { + //FIXME: is it possible to reuse repository session? + //Authentication failures happen while trying to reuse the session + NuxeoConnector nuxeoConnector = NuxeoConnector.getInstance(); + return nuxeoConnector.getRepositorySession(); + } + + protected void releaseRepositorySession(RepositoryInstance repoSession) { + try{ + //release session + NuxeoConnector nuxeoConnector = NuxeoConnector.getInstance(); + nuxeoConnector.releaseRepositorySession(repoSession); + }catch(Exception e){ + logger.error("Could not close the repository session", e); + //no need to throw this service specific exception + } + } } diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/NuxeoConnector.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/NuxeoConnector.java new file mode 100644 index 000000000..1767f228c --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/NuxeoConnector.java @@ -0,0 +1,205 @@ +/** + * Copyright 2009 University of California at Berkeley + */ +package org.collectionspace.services.nuxeo; + +import java.io.File; +import java.util.Collection; +import java.util.Hashtable; +import java.util.Iterator; +import org.collectionspace.services.common.ServiceConfig.NuxeoClientConfig; +import org.nuxeo.ecm.core.api.DocumentModel; +import org.nuxeo.ecm.core.api.DocumentModelList; +import org.nuxeo.ecm.core.api.repository.RepositoryInstance; +import org.nuxeo.ecm.core.client.NuxeoApp; +import org.nuxeo.ecm.core.client.NuxeoClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * NuxeoConnector creates Nuxeo remoting runtime and provides access to Nuxeo client. + * @author + */ +public class NuxeoConnector { + + private Logger logger = LoggerFactory.getLogger(NuxeoConnector.class); + private static final NuxeoConnector self = new NuxeoConnector(); + private NuxeoApp app; + private NuxeoClient client; + private volatile boolean initialized = false; //use volatile for lazy initialization in singleton + private NuxeoClientConfig nuxeoClientConfig; + + private NuxeoConnector() { + } + + public final static NuxeoConnector getInstance() { + return self; + } + + /** + * initialize initialize the Nuxeo connector. It makes sure that the connector + * is initialized in a thread-safe manner and not initialized more than once. + * Initialization involves starting Nuxeo runtime, loading Nuxeo APIs jars + * in OSGI container as well as establishing initial connection. + * @param nuxeoClientConfig + * @throws java.lang.Exception + */ + public void initialize(NuxeoClientConfig nuxeoClientConfig) throws Exception { + + if(initialized == false){ + synchronized(this){ + if(initialized == false){ + try{ + this.nuxeoClientConfig = nuxeoClientConfig; + setProperties(nuxeoClientConfig); + app = new NuxeoApp(); + app.start(); + if(logger.isDebugEnabled()){ + logger.debug("initialize() NuxeoApp started"); + } + loadBundles(); + client = NuxeoClient.getInstance(); + initialized = true; + }catch(Exception e){ + if(logger.isDebugEnabled()){ + e.printStackTrace(); + } + } + } + } + } + } + + /** + * release releases resources occupied by Nuxeo remoting client runtime + * @throws java.lang.Exception + */ + public void release() throws Exception { + if(initialized == true){ + client.disconnect(); + app.shutdown(); + } + } + + private void loadBundles() throws Exception { + String bundles = "nuxeo-client/lib/nuxeo-runtime-*:nuxeo-client/lib/nuxeo-*"; + Collection files = null; + if(bundles != null){ + files = NuxeoApp.getBundleFiles(new File("."), bundles, ":"); + } + if(logger.isDebugEnabled()){ + logger.debug("loadBundles(): deploying bundles: " + files); + } + if(files != null){ + app.deployBundles(files); + } + } + + private void setProperties(NuxeoClientConfig nuxeoClientConfig) { + System.setProperty("org.nuxeo.runtime.server.enabled", Boolean.FALSE.toString()); + System.setProperty("org.nuxeo.runtime.server.port", "" + nuxeoClientConfig.getPort()); + System.setProperty("org.nuxeo.runtime.server.host", nuxeoClientConfig.getHost()); + //System.setProperty("org.nuxeo.runtime.1.3.3.streaming.port", "3233"); + System.setProperty("org.nuxeo.runtime.streaming.serverLocator", "socket://" + nuxeoClientConfig.getHost() + ":3233"); + System.setProperty("org.nuxeo.runtime.streaming.isServer", Boolean.FALSE.toString()); + //org.nuxeo.client.remote is part of the fix to Nuxeo Runtime to use Java Remote APIs + //from JBoss + System.setProperty("org.nuxeo.client.remote", Boolean.TRUE.toString()); + } + + /** + * getClient get Nuxeo client for accessing Nuxeo services remotely using + * Nuxeo Java (EJB) Remote APIS + * @return NuxeoClient + * @throws java.lang.Exception + */ + public NuxeoClient getClient() throws Exception { + if(initialized == true){ +// if(client.isConnected()){ +// return client; +// }else{ + //authentication failure error comes when reusing the client + //fore connect for now + client.forceConnect(nuxeoClientConfig.getHost(), nuxeoClientConfig.getPort()); + if(logger.isDebugEnabled()){ + logger.debug("getClient(): connection successful port=" + + nuxeoClientConfig.getPort()); + } + return client; +// } + } + String msg = "NuxeoConnector is not initialized!"; + logger.error(msg); + throw new IllegalStateException(msg); + } + + /** + * getRepositorySession get session to default repository + * @return RepositoryInstance + * @throws java.lang.Exception + */ + public RepositoryInstance getRepositorySession() throws Exception { + RepositoryInstance repoSession = getClient().openRepository(); + if(logger.isDebugEnabled()){ + logger.debug("getRepositorySession() opened repository session"); + } + return repoSession; + } + + /** + * releaseRepositorySession releases given repository session + * @param repoSession + * @throws java.lang.Exception + */ + public void releaseRepositorySession(RepositoryInstance repoSession) throws Exception { + if(repoSession != null){ + getClient().releaseRepository(repoSession); + if(logger.isDebugEnabled()){ + if(logger.isDebugEnabled()){ + logger.debug("releaseRepositorySession() released repository session"); + } + } + } + } + + /** + * retrieveWorkspaceIds retrieves all workspace ids from default repository + * @return + * @throws java.lang.Exception + */ + public Hashtable retrieveWorkspaceIds() throws Exception { + RepositoryInstance repoSession = null; + Hashtable workspaceIds = new Hashtable(); + try{ + repoSession = getRepositorySession(); + DocumentModel rootDoc = repoSession.getRootDocument(); + DocumentModelList rootChildrenList = repoSession.getChildren(rootDoc.getRef()); + Iterator riter = rootChildrenList.iterator(); + if(riter.hasNext()){ + DocumentModel domain = riter.next(); + DocumentModelList domainChildrenList = repoSession.getChildren(domain.getRef()); + Iterator diter = domainChildrenList.iterator(); + if(diter.hasNext()){ + DocumentModel childNode = diter.next(); + if("Workspaces".equalsIgnoreCase(childNode.getName())){ + DocumentModelList workspaceList = repoSession.getChildren(childNode.getRef()); + Iterator wsiter = workspaceList.iterator(); + while(wsiter.hasNext()){ + DocumentModel workspace = wsiter.next(); + if(logger.isDebugEnabled()){ + logger.debug("workspace name=" + workspace.getName() + + " id=" + workspace.getId()); + } + workspaceIds.put(workspace.getName().toLowerCase(), workspace.getId()); + } + } + } + } + }finally{ + if(repoSession != null){ + releaseRepositorySession(repoSession); + } + } + return workspaceIds; + } +} diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/NuxeoUtils.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/NuxeoUtils.java new file mode 100644 index 000000000..059f2656c --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/NuxeoUtils.java @@ -0,0 +1,72 @@ +/** + * Copyright 2009 University of California at Berkeley + */ +package org.collectionspace.services.nuxeo; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import javax.ws.rs.WebApplicationException; +import org.dom4j.Document; +import org.dom4j.io.SAXReader; +import org.nuxeo.ecm.core.api.DocumentModel; +import org.nuxeo.ecm.core.api.repository.RepositoryInstance; +import org.nuxeo.ecm.core.io.DocumentPipe; +import org.nuxeo.ecm.core.io.DocumentReader; +import org.nuxeo.ecm.core.io.DocumentWriter; +import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl; +import org.nuxeo.ecm.core.io.impl.plugins.SingleDocumentReader; +import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentWriter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Various utilities related to Nuxeo + * @author + */ +public class NuxeoUtils { + + private static Logger logger = LoggerFactory.getLogger(NuxeoUtils.class); + + public static Document getDocument(RepositoryInstance repoSession, DocumentModel helloDoc) + throws Exception { + Document doc = null; + DocumentWriter writer = null; + DocumentReader reader = null; + ByteArrayOutputStream baos = null; + ByteArrayInputStream bais = null; + try{ + baos = new ByteArrayOutputStream(); + //nuxeo io.impl begin + reader = new SingleDocumentReader(repoSession, helloDoc); + writer = new XMLDocumentWriter(baos); + DocumentPipe pipe = new DocumentPipeImpl(); + //nuxeo io.impl end + pipe.setReader(reader); + pipe.setWriter(writer); + pipe.run(); + bais = new ByteArrayInputStream(baos.toByteArray()); + SAXReader saxReader = new SAXReader(); + doc = saxReader.read(bais); + }finally{ + if(reader != null){ + reader.close(); + } + if(writer != null){ + writer.close(); + } + try{ + if(bais != null){ + bais.close(); + } + if(baos != null){ + baos.close(); + } + }catch(IOException ioe){ + logger.error("Failed to close io streams with {}", ioe); + throw new WebApplicationException(); + } + } + return doc; + } +} diff --git a/services/common/src/main/resources/common.xsd b/services/common/src/main/resources/common.xsd new file mode 100644 index 000000000..9b6b135d6 --- /dev/null +++ b/services/common/src/main/resources/common.xsd @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- 2.47.3