]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
NOJIRA - refactoried tenantbindingconfigreader so it could be used just to read the...
authorSanjay Dalal <sanjay.dalal@berkeley.edu>
Fri, 14 May 2010 20:25:48 +0000 (20:25 +0000)
committerSanjay Dalal <sanjay.dalal@berkeley.edu>
Fri, 14 May 2010 20:25:48 +0000 (20:25 +0000)
test: service tests

M    services/authorization-mgt/import/pom.xml
M    services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContextImpl.java
M    services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java
M    services/common/src/main/java/org/collectionspace/services/common/config/TenantBindingConfigReaderImpl.java
A    services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/TenantRepository.java

services/authorization-mgt/import/pom.xml
services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java
services/common/src/main/java/org/collectionspace/services/common/config/TenantBindingConfigReaderImpl.java
services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContextImpl.java
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/TenantRepository.java [new file with mode: 0644]

index eeb18a8afed2428c2737dbc203799ac85dd41caa..a2ceb0b21e78176ebf31fb9bdf3b973c0600ea89 100644 (file)
             <artifactId>org.collectionspace.services.authorization.jaxb</artifactId>\r
             <version>${project.version}</version>\r
         </dependency>\r
+        <dependency>\r
+            <groupId>org.collectionspace.services</groupId>\r
+            <artifactId>org.collectionspace.services.common</artifactId>\r
+            <version>${project.version}</version>\r
+            <scope>provided</scope>\r
+        </dependency>\r
         <dependency>\r
             <groupId>org.collectionspace.services</groupId>\r
             <artifactId>org.collectionspace.services.client</artifactId>\r
index ebcbdc31c06c9a306e04cd6ad7d6ccce3ed8b86f..186812191fe1bfda4e0e05a3f368db166a6efc60 100644 (file)
@@ -12,6 +12,7 @@ import org.collectionspace.services.common.tenant.TenantBindingType;
 import org.collectionspace.services.common.types.PropertyItemType;
 import org.collectionspace.services.common.types.PropertyType;
 import org.collectionspace.services.nuxeo.client.java.NuxeoConnector;
+import org.collectionspace.services.nuxeo.client.java.TenantRepository;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -41,17 +42,17 @@ public class ServiceMain {
      * @return
      */
     public static ServiceMain getInstance() {
-        if(instance == null){
-            synchronized(ServiceMain.class){
-                if(instance == null){
+        if (instance == null) {
+            synchronized (ServiceMain.class) {
+                if (instance == null) {
                     ServiceMain temp = new ServiceMain();
-                    try{
+                    try {
                         temp.initialize();
-                    }catch(Exception e){
+                    } catch (Exception e) {
                         instance = null;
-                        if(e instanceof RuntimeException){
+                        if (e instanceof RuntimeException) {
                             throw (RuntimeException) e;
-                        }else{
+                        } else {
                             throw new RuntimeException(e);
                         }
                     }
@@ -66,7 +67,7 @@ public class ServiceMain {
         setServerRootDir();
         readConfig();
         propagateConfiguredProperties();
-        if(getClientType().equals(ClientType.JAVA)){
+        if (getClientType().equals(ClientType.JAVA)) {
             nuxeoConnector = NuxeoConnector.getInstance();
             nuxeoConnector.initialize(
                     getServicesConfigReader().getConfiguration().getRepositoryClient());
@@ -78,12 +79,12 @@ public class ServiceMain {
      * but not necessarily those occupied by individual services
      */
     public void release() {
-        try{
-            if(nuxeoConnector != null){
+        try {
+            if (nuxeoConnector != null) {
                 nuxeoConnector.release();
             }
             instance = null;
-        }catch(Exception e){
+        } catch (Exception e) {
             e.printStackTrace();
             //gobble it
         }
@@ -97,21 +98,33 @@ public class ServiceMain {
         tenantBindingConfigReader = new TenantBindingConfigReaderImpl(getServerRootDir());
         getTenantBindingConfigReader().read();
     }
-    
+
     private void propagateConfiguredProperties() {
-       List<PropertyType> repoPropListHolder = 
-               servicesConfigReader.getConfiguration().getRepositoryClient().getProperties();
-       if(repoPropListHolder != null && !repoPropListHolder.isEmpty()) {
-               List<PropertyItemType> propList = repoPropListHolder.get(0).getItem();
-               if(propList != null && !propList.isEmpty()) {
-                               tenantBindingConfigReader.setDefaultPropertiesOnTenants(propList, true);
-               }
-       }
+        List<PropertyType> repoPropListHolder =
+                servicesConfigReader.getConfiguration().getRepositoryClient().getProperties();
+        if (repoPropListHolder != null && !repoPropListHolder.isEmpty()) {
+            List<PropertyItemType> propList = repoPropListHolder.get(0).getItem();
+            if (propList != null && !propList.isEmpty()) {
+                tenantBindingConfigReader.setDefaultPropertiesOnTenants(propList, true);
+            }
+        }
     }
 
     void retrieveAllWorkspaceIds() throws Exception {
         //all configs are read, connector is initialized, retrieve workspaceids
-        getTenantBindingConfigReader().retrieveAllWorkspaceIds();
+        Hashtable<String, TenantBindingType> tenantBindings =
+                getTenantBindingConfigReader().getTenantBindings();
+        TenantRepository.get().retrieveAllWorkspaceIds(tenantBindings);
+    }
+
+    /**
+     * getWorkspaceId returns workspace id for given tenant and service name
+     * @param tenantId
+     * @param serviceName
+     * @return
+     */
+    public String getWorkspaceId(String tenantId, String serviceName) {
+        return TenantRepository.get().getWorkspaceId(tenantId, serviceName);
     }
 
     /**
@@ -120,20 +133,21 @@ public class ServiceMain {
     public NuxeoConnector getNuxeoConnector() {
         return nuxeoConnector;
     }
+    
+    /**
+     * @return the serverRootDir
+     */
+    public String getServerRootDir() {
+        return serverRootDir;
+    }
 
     private void setServerRootDir() {
         serverRootDir = System.getProperty("jboss.server.home.dir");
-        if(serverRootDir == null){
+        if (serverRootDir == null) {
             serverRootDir = "."; //assume server is started from server root, e.g. server/cspace
         }
     }
 
-    /**
-     * @return the serverRootDir
-     */
-    public String getServerRootDir() {
-        return serverRootDir;
-    }
 
     /**
      * @return the serviceConfig
index c30b1fbe88088f0360ab1dad2b078b72c629e9df..df575594aae1b4a04eb165812352f4a2ce1cc14a 100644 (file)
@@ -28,11 +28,6 @@ import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.List;
 
-import org.collectionspace.services.common.ClientType;
-import org.collectionspace.services.common.RepositoryClientConfigType;
-import org.collectionspace.services.common.ServiceMain;
-import org.collectionspace.services.common.repository.RepositoryClient;
-import org.collectionspace.services.common.repository.RepositoryClientFactory;
 import org.collectionspace.services.common.service.ServiceBindingType;
 import org.collectionspace.services.common.tenant.TenantBindingType;
 import org.collectionspace.services.common.tenant.TenantBindingConfig;
@@ -58,8 +53,6 @@ public class TenantBindingConfigReaderImpl
     //tenant-qualified servicename, service binding
     private Hashtable<String, ServiceBindingType> serviceBindings =
             new Hashtable<String, ServiceBindingType>();
-    //tenant-qualified service, workspace
-    private Hashtable<String, String> serviceWorkspaces = new Hashtable<String, String>();
 
     public TenantBindingConfigReaderImpl(String serverRootDir) {
         super(serverRootDir);
@@ -103,110 +96,19 @@ public class TenantBindingConfigReaderImpl
         }
     }
 
-    /**
-     * retrieveWorkspaceIds is called at initialization time to retrieve
-     * workspace ids of all the tenants
-     * @throws Exception
-     */
-    public void retrieveAllWorkspaceIds() throws Exception {
-        for (TenantBindingType tenantBinding : tenantBindings.values()) {
-            retrieveWorkspaceIds(tenantBinding);
-        }
-    }
-
-    /**
-     * retrieveWorkspaceIds retrieves workspace ids for services used by
-     * the given tenant
-     * @param tenantBinding
-     * @throws Exception
-     */
-    public void retrieveWorkspaceIds(TenantBindingType tenantBinding) throws Exception {
-        Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
-        ServiceMain svcMain = ServiceMain.getInstance();
-        RepositoryClientConfigType rclientConfig = svcMain.getServicesConfigReader().getConfiguration().getRepositoryClient();
-        ClientType clientType = svcMain.getClientType();
-        if (clientType.equals(ClientType.JAVA)
-                && rclientConfig.getName().equalsIgnoreCase("nuxeo-java")) {
-            //FIXME only one repository client is recognized
-            workspaceIds = svcMain.getNuxeoConnector().retrieveWorkspaceIds(
-                    tenantBinding.getRepositoryDomain());
-        }
-        //verify if workspace exists for each service in the tenant binding
-        for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
-            String serviceName = serviceBinding.getName();
-            String repositoryClientName = serviceBinding.getRepositoryClient();
-            if (repositoryClientName == null) {
-                //no repository needed for this service...skip
-                if (logger.isInfoEnabled()) {
-                    logger.info("The service " + serviceName
-                            + " does not seem to require a document repository.");
-                }
-                continue;
-            }
-
-            if (repositoryClientName.isEmpty()) {
-                String msg = "Invalid repositoryClient " + serviceName;
-                logger.error(msg);
-                continue;
-            }
-            repositoryClientName = repositoryClientName.trim();
-            RepositoryClient repositoryClient = getRepositoryClient(
-                    repositoryClientName);
-            if (repositoryClient == null) {
-                String msg = "Could not find repositoryClient " + repositoryClientName
-                        + " for service=" + serviceName;
-                logger.error(msg);
-                continue;
-            }
-            String workspaceId = null;
-            //workspace name is service name by convention
-            String workspace = serviceName.toLowerCase();
-            if (clientType.equals(ClientType.JAVA)) {
-                workspaceId = workspaceIds.get(workspace);
-                if (workspaceId == null) {
-                    if (logger.isWarnEnabled()) {
-                        logger.warn("Failed to retrieve workspace ID for " + workspace
-                                + " from repository, trying to create a new workspace ...");
-                    }
-                    workspaceId = repositoryClient.createWorkspace(
-                            tenantBinding.getRepositoryDomain(),
-                            serviceBinding.getName());
-                    if (workspaceId == null) {
-                        if (logger.isWarnEnabled()) {
-                            logger.warn("Failed to create workspace in repository"
-                                    + " for service=" + workspace);
-                        }
-                        continue;
-                    }
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Successfully created workspace in repository" +
-                                " id=" + workspaceId + " for service=" + workspace);
-                    }
-                }
-            } else {
-                workspaceId = serviceBinding.getRepositoryWorkspaceId();
-                if (workspaceId == null || "".equals(workspaceId)) {
-                    logger.error("Could not find workspace in repository for" +
-                            " service=" + workspace);
-                    //FIXME: should we throw an exception here?
-                    continue;
-                }
-            }
-            String tenantService = getTenantQualifiedServiceName(tenantBinding.getId(), serviceName);
-            serviceWorkspaces.put(tenantService, workspaceId);
-            if (logger.isInfoEnabled()) {
-                logger.info("Created/retrieved repository workspace=" +
-                        workspace + " id=" + workspaceId
-                        + " for service=" + serviceName);
-            }
-        }
-    }
-
     @Override
     public TenantBindingConfig getConfiguration() {
         return tenantBindingConfig;
     }
 
+    /**
+     * getTenantBindings returns all the tenant bindings read from configuration
+     * @return
+     */
+    public Hashtable<String, TenantBindingType> getTenantBindings() {
+        return tenantBindings;
+    }
+    
     /**
      * getTenantBinding gets tenant binding for given tenant
      * @param tenantId
@@ -252,16 +154,6 @@ public class TenantBindingConfigReaderImpl
         return list;
     }
 
-    /**
-     * getWorkspaceId retrieves workspace id for given tenant for given service
-     * @param tenantId
-     * @param serviceName
-     * @return
-     */
-    public String getWorkspaceId(String tenantId, String serviceName) {
-        String tenantService = getTenantQualifiedServiceName(tenantId, serviceName);
-        return serviceWorkspaces.get(tenantService);
-    }
 
     /**
      * @param tenantId
@@ -273,10 +165,6 @@ public class TenantBindingConfigReaderImpl
         return tenantId + "." + serviceName.toLowerCase();
     }
 
-    private RepositoryClient getRepositoryClient(String clientName) {
-        return RepositoryClientFactory.getInstance().getClient(clientName);
-    }
-
     /**
      * Sets properties in the passed list on the local properties for this TenantBinding.
      * Note: will only set properties not already set on the TenantBinding.
index 54d821bf48b5ceab1ce6178037edaddb41ab7f35..90b49250aa6c0f8c4138a39144aba488b649f960 100644 (file)
@@ -271,8 +271,7 @@ public abstract class AbstractServiceContextImpl<IT, OT>
      */
     @Override
     public String getRepositoryWorkspaceId() {
-        TenantBindingConfigReaderImpl tbConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
-        return tbConfigReader.getWorkspaceId(getTenantId(), getServiceName());
+        return ServiceMain.getInstance().getWorkspaceId(getTenantId(), getServiceName());
     }
 
     /* (non-Javadoc)
diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/TenantRepository.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/TenantRepository.java
new file mode 100644 (file)
index 0000000..ab0df0d
--- /dev/null
@@ -0,0 +1,177 @@
+/**
+ *  This document is a part of the source code and related artifacts
+ *  for CollectionSpace, an open source collections management system
+ *  for museums and related institutions:
+
+ *  http://www.collectionspace.org
+ *  http://wiki.collectionspace.org
+
+ *  Copyright 2010 University of California at Berkeley
+
+ *  Licensed under the Educational Community License (ECL), Version 2.0.
+ *  You may not use this file except in compliance with this License.
+
+ *  You may obtain a copy of the ECL 2.0 License at
+
+ *  https://source.collectionspace.org/collection-space/LICENSE.txt
+
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.collectionspace.services.nuxeo.client.java;
+
+import java.util.Hashtable;
+import org.collectionspace.services.common.ServiceMain;
+import org.collectionspace.services.common.ClientType;
+import org.collectionspace.services.common.RepositoryClientConfigType;
+import org.collectionspace.services.common.repository.RepositoryClient;
+import org.collectionspace.services.common.repository.RepositoryClientFactory;
+import org.collectionspace.services.common.service.ServiceBindingType;
+import org.collectionspace.services.common.tenant.TenantBindingType;
+import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * TenatnRepository is used to retrieve workspaces for a tenant as well as to
+ * create workspaces for each service used by a teant
+ * @author
+ */
+public class TenantRepository {
+
+    final private static TenantRepository self = new TenantRepository();
+    
+    final Logger logger = LoggerFactory.getLogger(TenantRepository.class);
+    //tenant-qualified service, workspace
+    private Hashtable<String, String> serviceWorkspaces = new Hashtable<String, String>();
+
+    private TenantRepository() {
+        
+    }
+
+    public static TenantRepository get() {
+        return self;
+    }
+    
+    /**
+     * retrieveWorkspaceIds is called at initialization time to retrieve
+     * workspace ids of all the tenants
+     * @param hashtable <tenant name, tenantbinding>
+     * @throws Exception
+     */
+    synchronized public void retrieveAllWorkspaceIds(Hashtable<String, TenantBindingType> tenantBindings)
+            throws Exception {
+        for (TenantBindingType tenantBinding : tenantBindings.values()) {
+            retrieveWorkspaceIds(tenantBinding);
+        }
+    }
+
+    /**
+     * retrieveWorkspaceIds retrieves workspace ids for services used by
+     * the given tenant
+     * @param tenantBinding
+     * @throws Exception
+     */
+    synchronized public void retrieveWorkspaceIds(TenantBindingType tenantBinding) throws Exception {
+        Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
+        ServiceMain svcMain = ServiceMain.getInstance();
+        RepositoryClientConfigType rclientConfig = svcMain.getServicesConfigReader().getConfiguration().getRepositoryClient();
+        ClientType clientType = svcMain.getClientType();
+        if (clientType.equals(ClientType.JAVA)
+                && rclientConfig.getName().equalsIgnoreCase("nuxeo-java")) {
+            //FIXME only one repository client is recognized
+            workspaceIds = svcMain.getNuxeoConnector().retrieveWorkspaceIds(
+                    tenantBinding.getRepositoryDomain());
+        }
+        //verify if workspace exists for each service in the tenant binding
+        for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
+            String serviceName = serviceBinding.getName();
+            String repositoryClientName = serviceBinding.getRepositoryClient();
+            if (repositoryClientName == null) {
+                //no repository needed for this service...skip
+                if (logger.isInfoEnabled()) {
+                    logger.info("The service " + serviceName
+                            + " does not seem to require a document repository.");
+                }
+                continue;
+            }
+
+            if (repositoryClientName.isEmpty()) {
+                String msg = "Invalid repositoryClient " + serviceName;
+                logger.error(msg);
+                continue;
+            }
+            repositoryClientName = repositoryClientName.trim();
+            RepositoryClient repositoryClient = getRepositoryClient(
+                    repositoryClientName);
+            if (repositoryClient == null) {
+                String msg = "Could not find repositoryClient " + repositoryClientName
+                        + " for service=" + serviceName;
+                logger.error(msg);
+                continue;
+            }
+            String workspaceId = null;
+            //workspace name is service name by convention
+            String workspace = serviceName.toLowerCase();
+            if (clientType.equals(ClientType.JAVA)) {
+                workspaceId = workspaceIds.get(workspace);
+                if (workspaceId == null) {
+                    if (logger.isWarnEnabled()) {
+                        logger.warn("Failed to retrieve workspace ID for " + workspace
+                                + " from repository, trying to create a new workspace ...");
+                    }
+                    workspaceId = repositoryClient.createWorkspace(
+                            tenantBinding.getRepositoryDomain(),
+                            serviceBinding.getName());
+                    if (workspaceId == null) {
+                        if (logger.isWarnEnabled()) {
+                            logger.warn("Failed to create workspace in repository"
+                                    + " for service=" + workspace);
+                        }
+                        continue;
+                    }
+                    if (logger.isDebugEnabled()) {
+                        logger.debug("Successfully created workspace in repository"
+                                + " id=" + workspaceId + " for service=" + workspace);
+                    }
+                }
+            } else {
+                workspaceId = serviceBinding.getRepositoryWorkspaceId();
+                if (workspaceId == null || "".equals(workspaceId)) {
+                    logger.error("Could not find workspace in repository for"
+                            + " service=" + workspace);
+                    //FIXME: should we throw an exception here?
+                    continue;
+                }
+            }
+            String tenantService =
+                    TenantBindingConfigReaderImpl.getTenantQualifiedServiceName(tenantBinding.getId(), serviceName);
+            serviceWorkspaces.put(tenantService, workspaceId);
+            if (logger.isInfoEnabled()) {
+                logger.info("Created/retrieved repository workspace="
+                        + workspace + " id=" + workspaceId
+                        + " for service=" + serviceName);
+            }
+        }
+    }
+
+    /**
+     * getWorkspaceId for a tenant's service
+     * @param tenantId
+     * @param serviceName
+     * @return workspace id
+     */
+    public String getWorkspaceId(String tenantId, String serviceName) {
+        String tenantService =
+                TenantBindingConfigReaderImpl.getTenantQualifiedServiceName(tenantId, serviceName);
+        return serviceWorkspaces.get(tenantService);
+    }
+
+    
+    private RepositoryClient getRepositoryClient(String clientName) {
+        return RepositoryClientFactory.getInstance().getClient(clientName);
+    }
+}