From bf25b2ac1b2a2190bdb485a7dabfeb74e8778453 Mon Sep 17 00:00:00 2001 From: Sanjay Dalal Date: Fri, 14 May 2010 20:25:48 +0000 Subject: [PATCH] NOJIRA - refactoried tenantbindingconfigreader so it could be used just to read the bindings, not for workspace retrieval. moved nuxeo workspace specific stuff to new class ...nuxeo.client.java.TenantRepository 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 | 6 + .../services/common/ServiceMain.java | 70 ++++--- .../config/TenantBindingConfigReaderImpl.java | 128 +------------ .../context/AbstractServiceContextImpl.java | 3 +- .../nuxeo/client/java/TenantRepository.java | 177 ++++++++++++++++++ 5 files changed, 234 insertions(+), 150 deletions(-) create mode 100644 services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/TenantRepository.java diff --git a/services/authorization-mgt/import/pom.xml b/services/authorization-mgt/import/pom.xml index eeb18a8af..a2ceb0b21 100644 --- a/services/authorization-mgt/import/pom.xml +++ b/services/authorization-mgt/import/pom.xml @@ -43,6 +43,12 @@ org.collectionspace.services.authorization.jaxb ${project.version} + + org.collectionspace.services + org.collectionspace.services.common + ${project.version} + provided + org.collectionspace.services org.collectionspace.services.client 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 index ebcbdc31c..186812191 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java +++ b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java @@ -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 repoPropListHolder = - servicesConfigReader.getConfiguration().getRepositoryClient().getProperties(); - if(repoPropListHolder != null && !repoPropListHolder.isEmpty()) { - List propList = repoPropListHolder.get(0).getItem(); - if(propList != null && !propList.isEmpty()) { - tenantBindingConfigReader.setDefaultPropertiesOnTenants(propList, true); - } - } + List repoPropListHolder = + servicesConfigReader.getConfiguration().getRepositoryClient().getProperties(); + if (repoPropListHolder != null && !repoPropListHolder.isEmpty()) { + List 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 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 diff --git a/services/common/src/main/java/org/collectionspace/services/common/config/TenantBindingConfigReaderImpl.java b/services/common/src/main/java/org/collectionspace/services/common/config/TenantBindingConfigReaderImpl.java index c30b1fbe8..df575594a 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/config/TenantBindingConfigReaderImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/config/TenantBindingConfigReaderImpl.java @@ -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 serviceBindings = new Hashtable(); - //tenant-qualified service, workspace - private Hashtable serviceWorkspaces = new Hashtable(); 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 workspaceIds = new Hashtable(); - 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 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. diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContextImpl.java b/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContextImpl.java index 54d821bf4..90b49250a 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContextImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContextImpl.java @@ -271,8 +271,7 @@ public abstract class AbstractServiceContextImpl */ @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 index 000000000..ab0df0d13 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/TenantRepository.java @@ -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 serviceWorkspaces = new Hashtable(); + + private TenantRepository() { + + } + + public static TenantRepository get() { + return self; + } + + /** + * retrieveWorkspaceIds is called at initialization time to retrieve + * workspace ids of all the tenants + * @param hashtable + * @throws Exception + */ + synchronized public void retrieveAllWorkspaceIds(Hashtable 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 workspaceIds = new Hashtable(); + 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); + } +} -- 2.47.3