From 35f25dd4629ce9d4f60da785d840a93a90129f21 Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Tue, 8 Feb 2011 18:42:59 +0000 Subject: [PATCH] CSPACE-3450: Tenant bindings are now split into separate files. We look for tenant bindings in the following deployment folder ..\cspace\config\services\tenants. There should be a folder in this folder for each tenant. Each tenant now has their own copy of the "tenant-bindings.xml" config file. --- services/authorization-mgt/import/pom.xml | 2 +- .../importer/AuthorizationGen.java | 8 +- .../spring/SpringPermissionEvaluator.java | 4 +- services/common/build.xml | 6 +- .../services/common/ServiceMain.java | 12 +-- .../config/AbstractConfigReaderImpl.java | 80 ++++++++++++++-- .../config/TenantBindingConfigReaderImpl.java | 96 +++++++++++++++---- services/common/src/main/resources/tenant.xsd | 2 +- 8 files changed, 165 insertions(+), 45 deletions(-) diff --git a/services/authorization-mgt/import/pom.xml b/services/authorization-mgt/import/pom.xml index ec5bfe0dd..c9e5fbdc2 100644 --- a/services/authorization-mgt/import/pom.xml +++ b/services/authorization-mgt/import/pom.xml @@ -146,7 +146,7 @@ -p does_not_matter -b - ${basedir}/../../common/src/main/config/services/tenant-bindings.xml + ${basedir}/../../common/src/main -edir ${basedir}/target diff --git a/services/authorization-mgt/import/src/main/java/org/collectionspace/services/authorization/importer/AuthorizationGen.java b/services/authorization-mgt/import/src/main/java/org/collectionspace/services/authorization/importer/AuthorizationGen.java index a7f69f0eb..450b0f3c0 100644 --- a/services/authorization-mgt/import/src/main/java/org/collectionspace/services/authorization/importer/AuthorizationGen.java +++ b/services/authorization-mgt/import/src/main/java/org/collectionspace/services/authorization/importer/AuthorizationGen.java @@ -80,15 +80,15 @@ public class AuthorizationGen { private Hashtable tenantBindings = new Hashtable(); - public void initialize(String tenantBindingFileName) throws Exception { + public void initialize(String tenantRootDirPath) throws Exception { TenantBindingConfigReaderImpl tenantBindingConfigReader = - new TenantBindingConfigReaderImpl(null); - tenantBindingConfigReader.read(tenantBindingFileName); + new TenantBindingConfigReaderImpl(tenantRootDirPath); + tenantBindingConfigReader.read(); tenantBindings = tenantBindingConfigReader.getTenantBindings(); cspaceAdminRole = buildCSpaceAdminRole(); if (logger.isDebugEnabled()) { - logger.debug("initialized with tenant bindings from " + tenantBindingFileName); + logger.debug("initialized with tenant bindings from " + tenantRootDirPath); } } diff --git a/services/authorization/service/src/main/java/org/collectionspace/services/authorization/spring/SpringPermissionEvaluator.java b/services/authorization/service/src/main/java/org/collectionspace/services/authorization/spring/SpringPermissionEvaluator.java index 5fdd1f3c3..e8fce22f9 100644 --- a/services/authorization/service/src/main/java/org/collectionspace/services/authorization/spring/SpringPermissionEvaluator.java +++ b/services/authorization/service/src/main/java/org/collectionspace/services/authorization/spring/SpringPermissionEvaluator.java @@ -43,7 +43,7 @@ import org.springframework.security.core.context.SecurityContextHolder; */ public class SpringPermissionEvaluator implements CSpacePermissionEvaluator { - final Log log = LogFactory.getLog(SpringPermissionEvaluator.class); + final Log log = LogFactory.getLog(SpringPermissionEvaluator.class); //FIXEME: REM - Use SLF4J interfaces instead of directly using Apache Commons Logging. private SpringAuthorizationProvider provider; SpringPermissionEvaluator(SpringAuthorizationProvider provider) { @@ -68,7 +68,7 @@ public class SpringPermissionEvaluator implements CSpacePermissionEvaluator { Serializable objectIdId, String objectIdType, Permission perm) { - if (log.isDebugEnabled() == true) { + if (log.isTraceEnabled() == true) { log.debug(this.getClass().getCanonicalName() + ":" + this); String resourceTarget = "[" + res.getId() + "]" + " | " + "[" + "objectIdId: " + objectIdType + "(" + objectIdId + ")]"; diff --git a/services/common/build.xml b/services/common/build.xml index 96fd57012..a4e7ab650 100644 --- a/services/common/build.xml +++ b/services/common/build.xml @@ -161,9 +161,11 @@ + - + + - + 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 2043cde1a..058bf3889 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 @@ -59,6 +59,7 @@ public class ServiceMain { public static String DEFAULT_REPOSITORY_NAME = "CspaceDS"; private ServiceMain() { + //empty } /** @@ -88,26 +89,25 @@ public class ServiceMain { return instance; } - private void initialize() throws Exception { + private void initialize() throws Exception { setServerRootDir(); readConfig(); propagateConfiguredProperties(); try { createDefaultAccounts(); } catch(Exception e) { - logger.error("Default Account setup failed on exception: "+e.getLocalizedMessage()); + logger.error("Default Account setup failed on exception: " + e.getLocalizedMessage()); } try { firePostInitHandlers(); } catch(Exception e) { - logger.error("ServiceMain.initialize firePostInitHandlers failed on exception: "+e.getLocalizedMessage()); + logger.error("ServiceMain.initialize firePostInitHandlers failed on exception: " + e.getLocalizedMessage()); } if (getClientType().equals(ClientType.JAVA)) { nuxeoConnector = NuxeoConnector.getInstance(); nuxeoConnector.initialize(getServicesConfigReader().getConfiguration().getRepositoryClient()); } - } /** @@ -129,9 +129,9 @@ public class ServiceMain { private void readConfig() throws Exception { //read service config servicesConfigReader = new ServicesConfigReaderImpl(getServerRootDir()); - getServicesConfigReader().read(); + servicesConfigReader.read(); - tenantBindingConfigReader = new TenantBindingConfigReaderImpl(getServerRootDir()); + tenantBindingConfigReader = new TenantBindingConfigReaderImpl(getServerRootDir()); tenantBindingConfigReader.read(); } diff --git a/services/common/src/main/java/org/collectionspace/services/common/config/AbstractConfigReaderImpl.java b/services/common/src/main/java/org/collectionspace/services/common/config/AbstractConfigReaderImpl.java index 034ba5770..c76f04875 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/config/AbstractConfigReaderImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/config/AbstractConfigReaderImpl.java @@ -24,7 +24,12 @@ package org.collectionspace.services.common.config; import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,6 +62,51 @@ public abstract class AbstractConfigReaderImpl @Override abstract public T getConfiguration(); + /** + * Gets a list of File items in the specified directory. If 'isDirectory' is true, then this + * method will return a list of items that are directories/folders; otherwise, it returns a list + * of file/document items. + * + * @param rootDir the root dir + * @param isDirectory the is directory + * @return the file children + * @throws IOException Signals that an I/O exception has occurred. + */ + List getFileChildren(File rootDir, boolean getDirectories) throws IOException { + ArrayList result = new ArrayList(); + File[] children = rootDir.listFiles(); + if (children != null) { + for (File child : children) { + if (child.isHidden() == false) { + if (getDirectories == child.isDirectory()) { + result.add(child); + } + } + } + } else { + String errMessage = "An IO exception and/or error occurred while reading the directory: " + + rootDir.getAbsolutePath(); + logger.debug(errMessage); + throw new IOException(errMessage); + } + return result; + } + + /** + * Gets a list of files/documents in the specified folder -does not return directories/folders. + * + * @param rootDir the root dir + * @return the files + * @throws IOException Signals that an I/O exception has occurred. + */ + List getFiles(File rootDir) throws IOException { + return getFileChildren(rootDir, false); + } + + List getDirectories(File rootDir) throws IOException { + return getFileChildren(rootDir, true); + } + /** * parse parses given configuration file from the disk based on given class * definition @@ -65,14 +115,22 @@ public abstract class AbstractConfigReaderImpl * @return A JAXB object * @throws Exception */ - protected Object parse(File configFile, Class clazz) throws Exception { - JAXBContext jc = JAXBContext.newInstance(clazz); - Unmarshaller um = jc.createUnmarshaller(); - Object readObject = um.unmarshal(configFile); - if (logger.isDebugEnabled()) { - logger.debug("read() read file " + configFile.getAbsolutePath()); - } - return readObject; + protected Object parse(File configFile, Class clazz) { + Object result = null; + try { + JAXBContext jc = JAXBContext.newInstance(clazz); + Unmarshaller um = jc.createUnmarshaller(); + result = um.unmarshal(configFile); + if (logger.isDebugEnabled()) { + logger.debug("read() read file " + configFile.getAbsolutePath()); + } + } catch (JAXBException e) { + if (logger.isErrorEnabled() == true) { + logger.error("Could not unmarshal CollectionSpace config file: " + + configFile.getAbsolutePath(), e); + } + } + return result; } protected String getAbsoluteFileName(String configFileName) { @@ -85,4 +143,10 @@ public abstract class AbstractConfigReaderImpl protected String getServerRootDir() { return serverRootDir; } + + protected String getConfigRootDir() { + return serverRootDir + + File.separator + CSPACE_DIR_NAME + + File.separator + CONFIG_DIR_NAME; + } } 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 4762d8735..f956f674f 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 @@ -24,6 +24,7 @@ package org.collectionspace.services.common.config; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; @@ -43,11 +44,13 @@ import org.slf4j.LoggerFactory; * $LastChangedDate: $ */ public class TenantBindingConfigReaderImpl - extends AbstractConfigReaderImpl { - - final private static String CONFIG_FILE_NAME = "tenant-bindings.xml"; + extends AbstractConfigReaderImpl> { + final private static String TENANT_BINDINGS_ERROR = "Tenant bindings error: "; + final private static String TENANT_BINDINGS_FILENAME = "tenant-bindings.xml"; + final private static String TENANT_BINDINGS_ROOTDIRNAME = "tenants"; + final Logger logger = LoggerFactory.getLogger(TenantBindingConfigReaderImpl.class); - private TenantBindingConfig tenantBindingConfig; + private List tenantBindingTypeList; //tenant id, tenant binding private Hashtable tenantBindings = new Hashtable(); @@ -64,28 +67,39 @@ public class TenantBindingConfigReaderImpl @Override public String getFileName() { - return CONFIG_FILE_NAME; + return TENANT_BINDINGS_FILENAME; } - + + protected File getTenantsRootDir() { + File result = null; + String tenantsRootPath = getConfigRootDir() + File.separator + TENANT_BINDINGS_ROOTDIRNAME; + File tenantsRootDir = new File(tenantsRootPath); + if (tenantsRootDir.exists() == true) { + result = tenantsRootDir; + logger.debug("Tenants home directory is: " + tenantsRootDir.getAbsolutePath()); //FIXME: REM - Add proper if (logger.isDebug() == true) check + } else { + logger.error("Tenants home directory is missing. Can't find: " + tenantsRootDir.getAbsolutePath()); //FIXME: REM - Add proper if (logger.isError() == true) check + } + return result; + } + @Override public void read() throws Exception { - String configFileName = getAbsoluteFileName(CONFIG_FILE_NAME); - read(configFileName); + String tenantsRootPath = getTenantsRootDir().getAbsolutePath(); + read(tenantsRootPath); } @Override - public void read(String configFileName) throws Exception { - if (logger.isDebugEnabled()) { - logger.debug("read() config file=" + configFileName); - } - File configFile = new File(configFileName); - if (!configFile.exists()) { - String msg = "Could not find configuration file " + configFileName; - logger.error(msg); - throw new RuntimeException(msg); + public void read(String tenantRootDirPath) throws Exception { + File tenantsRootDir = new File(tenantRootDirPath); + if (tenantsRootDir.exists() == false) { + throw new Exception("Cound not find tenant bindings root directory: " + + tenantRootDirPath); } - tenantBindingConfig = (TenantBindingConfig) parse(configFile, TenantBindingConfig.class); - for (TenantBindingType tenantBinding : tenantBindingConfig.getTenantBinding()) { + List tenantDirs = getDirectories(tenantsRootDir); + tenantBindingTypeList = readTenantConfigs(tenantDirs); + + for (TenantBindingType tenantBinding : tenantBindingTypeList) { tenantBindings.put(tenantBinding.getId(), tenantBinding); readDomains(tenantBinding); readServiceBindings(tenantBinding); @@ -96,6 +110,46 @@ public class TenantBindingConfigReaderImpl } } + List readTenantConfigs(List tenantDirList) throws IOException { + List result = new ArrayList(); + // + // Iterate through a list of directories. + // + for (File tenantDir : tenantDirList) { + boolean found = false; + String errMessage = null; + File configFile = new File(tenantDir.getAbsoluteFile() + File.separator + TENANT_BINDINGS_FILENAME); + if (configFile.exists() == true) { + TenantBindingConfig tenantBindingConfig = (TenantBindingConfig) parse( + configFile, TenantBindingConfig.class); + if (tenantBindingConfig != null) { + TenantBindingType binding = tenantBindingConfig.getTenantBinding(); + if (binding != null) { + result.add(binding); + found = true; + if (logger.isInfoEnabled() == true) { + logger.info("Parsed tenant configureation for: " + binding.getDisplayName()); + } + } else { + errMessage = "Cound not parse the tentant bindings in: "; + } + } else { + errMessage = "Could not parse the tenant bindings file: "; + } + } else { + errMessage = "Cound not find a tenant configuration file: "; + } + if (found == false) { + if (logger.isErrorEnabled() == true) { + errMessage = errMessage != null ? errMessage : TENANT_BINDINGS_ERROR; + logger.error(errMessage + configFile.getAbsolutePath()); + } + } + } // else-for + + return result; + } + private void readDomains(TenantBindingType tenantBinding) throws Exception { for (RepositoryDomainType domain : tenantBinding.getRepositoryDomain()) { domains.put(domain.getName(), domain); @@ -116,8 +170,8 @@ public class TenantBindingConfigReaderImpl } @Override - public TenantBindingConfig getConfiguration() { - return tenantBindingConfig; + public List getConfiguration() { + return tenantBindingTypeList; } /** diff --git a/services/common/src/main/resources/tenant.xsd b/services/common/src/main/resources/tenant.xsd index 042ba2e82..43d00ac49 100644 --- a/services/common/src/main/resources/tenant.xsd +++ b/services/common/src/main/resources/tenant.xsd @@ -31,7 +31,7 @@ - + -- 2.47.3