From 268628a7705936df7171ab9fa6ab6142c3bd135a Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Tue, 11 Mar 2014 15:39:29 -0700 Subject: [PATCH] CSPACE-6168: Added more support for tenant qualified database names. --- .../services/client/IQueryManager.java | 1 + .../services/common/ServiceMain.java | 21 ++++- .../AuthorizationCommon.java | 19 ++-- .../config/TenantBindingConfigReaderImpl.java | 1 - .../common/context/ServiceBindingUtils.java | 2 - .../services/common/init/AddIndices.java | 15 +-- .../services/common/init/IInitHandler.java | 1 + .../services/common/init/InitHandler.java | 3 +- .../common/init/ModifyFieldDatatypes.java | 11 ++- .../services/common/init/RunSqlScripts.java | 9 +- .../services/common/query/QueryManager.java | 3 +- .../query/nuxeo/QueryManagerNuxeoImpl.java | 7 +- .../services/common/storage/JDBCTools.java | 92 +++++++++++++------ .../client/java/RepositoryJavaClientImpl.java | 3 +- .../services/common/config/ConfigUtils.java | 3 +- .../report/nuxeo/ReportPostInitHandler.java | 8 +- 16 files changed, 129 insertions(+), 70 deletions(-) diff --git a/services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java b/services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java index 88bb3736c..0719483cc 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java +++ b/services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java @@ -134,6 +134,7 @@ public interface IQueryManager { */ public String createWhereClauseForPartialMatch(String dataSourceName, String repositoryName, + String cspaceInstanceId, String field, boolean startingWildcard, String partialTerm); 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 bc965f27c..3b1f4953c 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 @@ -23,6 +23,7 @@ import org.collectionspace.services.common.config.ConfigReader; import org.collectionspace.services.common.config.ConfigUtils; import org.collectionspace.services.common.config.ServicesConfigReaderImpl; import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl; +import org.collectionspace.services.common.context.ServiceBindingUtils; import org.collectionspace.services.common.init.AddIndices; import org.collectionspace.services.config.service.InitHandler.Params.Field; import org.collectionspace.services.common.init.IInitHandler; @@ -165,12 +166,15 @@ public class ServiceMain { throw new RuntimeException("Unknown CollectionSpace services client type: " + getClientType()); } // - // Create all the default user accounts and permissions + // Create all the default user accounts and permissions. Since some of our "cspace" database config files + // for Spring need to be created at build time, the "cspace" database already will be suffixed with the + // correct 'cspaceInstanceId' so we don't need to pass it to the JDBCTools methods. // try { AuthorizationCommon.createDefaultWorkflowPermissions(tenantBindingConfigReader); String cspaceDatabaseName = getCspaceDatabaseName(); - DatabaseProductType databaseProductType = JDBCTools.getDatabaseProductType(JDBCTools.CSPACE_DATASOURCE_NAME, cspaceDatabaseName); + DatabaseProductType databaseProductType = JDBCTools.getDatabaseProductType(JDBCTools.CSPACE_DATASOURCE_NAME, + cspaceDatabaseName); AuthorizationCommon.createDefaultAccounts(tenantBindingConfigReader, databaseProductType, cspaceDatabaseName); } catch (Exception e) { @@ -241,6 +245,7 @@ public class ServiceMain { // //Loop through all tenants in tenant-bindings.xml // + String cspaceInstanceId = getCspaceInstanceId(); for (TenantBindingType tbt : tenantBindingTypeMap.values()) { List repositoryNameList = ConfigUtils.getRepositoryNameList(tbt); if (repositoryNameList != null && repositoryNameList.isEmpty() == false) { @@ -274,7 +279,8 @@ public class ServiceMain { // 1 fields.add(field); } - addindices.onRepositoryInitialized(JDBCTools.NUXEO_DATASOURCE_NAME, repositoryName, null, fields, null); + addindices.onRepositoryInitialized(JDBCTools.NUXEO_DATASOURCE_NAME, repositoryName, cspaceInstanceId, + null, fields, null); } } else { String errMsg = "repositoryNameList was empty or null."; @@ -289,13 +295,17 @@ public class ServiceMain { // //Loop through all tenants in tenant-bindings.xml // + String cspaceInstanceId = getCspaceInstanceId(); for (TenantBindingType tbt : tenantBindingTypeMap.values()) { // //Loop through all the services in this tenant // List sbtList = tbt.getServiceBindings(); for (ServiceBindingType sbt: sbtList) { - String repositoryName = ConfigUtils.getRepositoryName(tbt, sbt.getRepositoryDomain()); // Each service can have a different repo domain + String repositoryName = null; + if (sbt.getType().equalsIgnoreCase(ServiceBindingUtils.SERVICE_TYPE_SECURITY) == false) { + repositoryName = ConfigUtils.getRepositoryName(tbt, sbt.getRepositoryDomain()); // Each service can have a different repo domain + } //Get the list of InitHandler elements, extract the first one (only one supported right now) and fire it using reflection. List list = sbt.getInitHandler(); if (list != null && list.size() > 0) { @@ -313,7 +323,8 @@ public class ServiceMain { Object o = instantiate(initHandlerClassname, IInitHandler.class); if (o != null && o instanceof IInitHandler){ IInitHandler handler = (IInitHandler)o; - handler.onRepositoryInitialized(JDBCTools.NUXEO_DATASOURCE_NAME, repositoryName, sbt, fields, props); + handler.onRepositoryInitialized(JDBCTools.NUXEO_DATASOURCE_NAME, repositoryName, cspaceInstanceId, + sbt, fields, props); //The InitHandler may be the default one, // or specialized classes which still implement this interface and are registered in tenant-bindings.xml. } diff --git a/services/common/src/main/java/org/collectionspace/services/common/authorization_mgt/AuthorizationCommon.java b/services/common/src/main/java/org/collectionspace/services/common/authorization_mgt/AuthorizationCommon.java index 26518fb08..14c562c1f 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/authorization_mgt/AuthorizationCommon.java +++ b/services/common/src/main/java/org/collectionspace/services/common/authorization_mgt/AuthorizationCommon.java @@ -919,13 +919,10 @@ public class AuthorizationCommon { TenantBindingConfigReaderImpl tenantBindingConfigReader, DatabaseProductType databaseProductType, String cspaceDatabaseName) throws Exception { - if (logger.isDebugEnabled()) { - logger.debug("ServiceMain.createDefaultAccounts starting..."); - } + + logger.debug("ServiceMain.createDefaultAccounts starting..."); - String cspaceDbName = tenantBindingConfigReader.getRepositoryDomain(null).getStorageName(); Hashtable tenantInfo = getTenantNamesFromConfig(tenantBindingConfigReader); - Connection conn = null; // TODO - need to put in tests for existence first. // We could just look for the accounts per tenant up front, and assume that @@ -1041,14 +1038,16 @@ public class AuthorizationCommon { } if (result == null) { - logger.warn("Could not retrieve a lifecycle transition definition list from: " - + serviceBinding.getName() - + " with tenant ID = " - + tenantBinding.getId()); + if (serviceBinding.getType().equalsIgnoreCase(ServiceBindingUtils.SERVICE_TYPE_SECURITY) == false) { + logger.warn("Could not retrieve a lifecycle transition definition list from: " + + serviceBinding.getName() + + " with tenant ID = " + + tenantBinding.getId()); + } // return an empty list result = new TransitionDefList(); } else { - logger.debug("Successfully etrieved a lifecycle transition definition list from: " + logger.debug("Successfully retrieved a lifecycle transition definition list from: " + serviceBinding.getName() + " with tenant ID = " + tenantBinding.getId()); 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 b9ad278d1..9a49c241a 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 @@ -33,7 +33,6 @@ import java.util.Hashtable; import java.util.List; import org.apache.commons.io.FileUtils; -import org.collectionspace.services.common.ServiceMain; import org.collectionspace.services.common.api.JEEServerDeployment; import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.config.service.ServiceBindingType; diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java b/services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java index 0031dbdeb..6ab6e4975 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java @@ -17,8 +17,6 @@ import org.collectionspace.services.nuxeo.util.NuxeoUtils; import org.nuxeo.ecm.core.api.ClientException; import org.nuxeo.ecm.core.api.DocumentModel; import java.lang.IndexOutOfBoundsException; -import java.util.GregorianCalendar; -import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils; import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.common.document.DocumentUtils; import org.slf4j.Logger; diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java b/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java index 910934d47..376d59bfc 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java +++ b/services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java @@ -89,6 +89,7 @@ public class AddIndices extends InitHandler implements IInitHandler { @Override public void onRepositoryInitialized(String dataSourceName, String repositoryName, + String cspaceInstanceId, ServiceBindingType sbt, List fields, List properties) throws Exception { @@ -107,16 +108,17 @@ public class AddIndices extends InitHandler implements IInitHandler { if(Tools.notEmpty(param) && (param.indexOf(',')>-1)){ String[] fieldNames = param.split(","); for (String fn: fieldNames){ - rows = addOneIndex(dataSourceName, repositoryName, tableName, fn); + rows = addOneIndex(dataSourceName, repositoryName, cspaceInstanceId, tableName, fn); } } else { - rows = addOneIndex(dataSourceName, repositoryName, tableName, fieldName); + rows = addOneIndex(dataSourceName, repositoryName, cspaceInstanceId, tableName, fieldName); } } } private int addOneIndex(String dataSourceName, - String repositoryName, + String repositoryName, + String cspaceInstanceId, String tableName, String columnName) { int rows = 0; @@ -124,7 +126,7 @@ public class AddIndices extends InitHandler implements IInitHandler { String indexName = tableName + INDEX_SEP + columnName + INDEX_SUFFIX; try { DatabaseProductType databaseProductType = JDBCTools.getDatabaseProductType(dataSourceName, repositoryName); - if (indexExists(dataSourceName, repositoryName, databaseProductType, + if (indexExists(dataSourceName, repositoryName, cspaceInstanceId, databaseProductType, tableName, columnName, indexName)) { logger.trace("Index already exists for column " + columnName + " in table " + tableName); @@ -153,7 +155,7 @@ public class AddIndices extends InitHandler implements IInitHandler { // // If this assumption is no longer valid, we might instead // identify the relevant repository from the table name here. - rows = JDBCTools.executeUpdate(dataSourceName, repositoryName, sql); + rows = JDBCTools.executeUpdate(dataSourceName, repositoryName, cspaceInstanceId, sql); logger.trace("Index added to column ("+columnName+") on table ("+tableName+")"); } return rows; @@ -165,6 +167,7 @@ public class AddIndices extends InitHandler implements IInitHandler { private boolean indexExists(String dataSourceName, String repositoryName, + String cspaceInstanceId, DatabaseProductType databaseProductType, String tableName, String colName, @@ -210,7 +213,7 @@ public class AddIndices extends InitHandler implements IInitHandler { // // If this assumption is no longer valid, we might instead // identify the relevant repository from the table name here. - conn = JDBCTools.getConnection(dataSourceName, repositoryName); + conn = JDBCTools.getConnection(dataSourceName, repositoryName, cspaceInstanceId); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/IInitHandler.java b/services/common/src/main/java/org/collectionspace/services/common/init/IInitHandler.java index fc1154bec..27788d6ed 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/init/IInitHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/common/init/IInitHandler.java @@ -15,6 +15,7 @@ import org.collectionspace.services.config.service.ServiceBindingType; public interface IInitHandler { public void onRepositoryInitialized(String dataSourceName, String repositoryName, + String cspaceInstanceId, ServiceBindingType sbt, List fields, List property) throws Exception; diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java b/services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java index ca2411243..e33c129f6 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/common/init/InitHandler.java @@ -52,7 +52,8 @@ public class InitHandler implements IInitHandler { @Override public void onRepositoryInitialized(String dataSourceName, String repositoryName, - ServiceBindingType sbt, + String cspaceInstanceId, + ServiceBindingType sbt, List fields, List properties) throws Exception { diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/ModifyFieldDatatypes.java b/services/common/src/main/java/org/collectionspace/services/common/init/ModifyFieldDatatypes.java index ff8a3985e..6f8da421f 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/init/ModifyFieldDatatypes.java +++ b/services/common/src/main/java/org/collectionspace/services/common/init/ModifyFieldDatatypes.java @@ -56,6 +56,7 @@ public class ModifyFieldDatatypes extends InitHandler implements IInitHandler { @Override public void onRepositoryInitialized(String dataSourceName, String repositoryName, + String cspaceInstanceId, ServiceBindingType sbt, List fields, List properties) throws Exception { @@ -72,7 +73,8 @@ public class ModifyFieldDatatypes extends InitHandler implements IInitHandler { for (Field field : fields) { datatype = getDatatypeFromLogicalType(databaseProductType, field.getType()); // If the field is already of the desired datatype, skip it. - if (fieldHasDesiredDatatype(dataSourceName, repositoryName, databaseProductType, field, datatype)) { + if (fieldHasDesiredDatatype(dataSourceName, repositoryName, cspaceInstanceId, databaseProductType, + field, datatype)) { logger.trace("Field " + field.getTable() + "." + field.getCol() + " is already of desired datatype " + datatype); continue; @@ -102,7 +104,7 @@ public class ModifyFieldDatatypes extends InitHandler implements IInitHandler { // // If this assumption is no longer valid, we might instead // identify the relevant repository from the table name here. - rows = JDBCTools.executeUpdate(dataSourceName, repositoryName, sql); + rows = JDBCTools.executeUpdate(dataSourceName, repositoryName, cspaceInstanceId, sql); } } catch (Exception e) { throw e; @@ -146,6 +148,7 @@ public class ModifyFieldDatatypes extends InitHandler implements IInitHandler { private boolean fieldHasDesiredDatatype(String dataSourceName, String repositoryName, + String cspaceInstanceId, DatabaseProductType databaseProductType, Field field, String datatype) { @@ -162,9 +165,9 @@ public class ModifyFieldDatatypes extends InitHandler implements IInitHandler { ResultSet rs = null; try { - conn = JDBCTools.getConnection(dataSourceName, repositoryName); + conn = JDBCTools.getConnection(dataSourceName, repositoryName, cspaceInstanceId); stmt = conn.createStatement(); - String databaseName = JDBCTools.getDatabaseName(dataSourceName, repositoryName, conn); + String databaseName = JDBCTools.getDatabaseName(dataSourceName, repositoryName, cspaceInstanceId, conn); if (databaseProductType == DatabaseProductType.MYSQL) { sql = "SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS " diff --git a/services/common/src/main/java/org/collectionspace/services/common/init/RunSqlScripts.java b/services/common/src/main/java/org/collectionspace/services/common/init/RunSqlScripts.java index 0ac977b5e..51861d75e 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/init/RunSqlScripts.java +++ b/services/common/src/main/java/org/collectionspace/services/common/init/RunSqlScripts.java @@ -57,6 +57,7 @@ public class RunSqlScripts extends InitHandler implements IInitHandler { @Override public void onRepositoryInitialized(String dataSourceName, String repositoryName, + String cspaceInstanceId, ServiceBindingType sbt, List fields, List properties) throws Exception { @@ -90,7 +91,7 @@ public class RunSqlScripts extends InitHandler implements IInitHandler { logger.warn(CANNOT_PERFORM_TASKS_MESSAGE); continue; } - runScript(dataSourceName, repositoryName, scriptContents, "resource path " + scriptPath); + runScript(dataSourceName, repositoryName, cspaceInstanceId, scriptContents, "resource path " + scriptPath); } // Next, run a second sequence of SQL scripts, where those scripts may be @@ -114,7 +115,7 @@ public class RunSqlScripts extends InitHandler implements IInitHandler { logger.warn(CANNOT_PERFORM_TASKS_MESSAGE); continue; } - runScript(dataSourceName, repositoryName, scriptContents, "file " + scriptFile.getName()); + runScript(dataSourceName, repositoryName, cspaceInstanceId, scriptContents, "file " + scriptFile.getName()); } } @@ -236,10 +237,10 @@ public class RunSqlScripts extends InitHandler implements IInitHandler { return sb.toString(); } - private void runScript(String dataSourceName, String repositoryName, String scriptContents, String scriptPath) { + private void runScript(String dataSourceName, String repositoryName, String cspaceInstanceId, String scriptContents, String scriptPath) { int rows = 0; try { - rows = JDBCTools.executeUpdate(dataSourceName, repositoryName, scriptContents); + rows = JDBCTools.executeUpdate(dataSourceName, repositoryName, cspaceInstanceId, scriptContents); } catch (Throwable e) { logger.warn("Running SQL script from " + scriptPath + " resulted in error: ", e.getMessage()); rows = -1; diff --git a/services/common/src/main/java/org/collectionspace/services/common/query/QueryManager.java b/services/common/src/main/java/org/collectionspace/services/common/query/QueryManager.java index 6e330883b..d59b4a2d8 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/query/QueryManager.java +++ b/services/common/src/main/java/org/collectionspace/services/common/query/QueryManager.java @@ -63,6 +63,7 @@ public class QueryManager { static public String createWhereClauseForPartialMatch(ServiceContext ctx, String field, String partialTerm) throws Exception { + String cspaceInstanceId = ServiceMain.getInstance().getCspaceInstanceId(); String repositoryName = ctx.getRepositoryName(); // Otherwise, generate that list and cache it for re-use. TenantBindingConfigReaderImpl tReader = @@ -74,7 +75,7 @@ public class QueryManager { || Boolean.parseBoolean(ptStartingWildcardValue); return queryManager.createWhereClauseForPartialMatch(queryManager.getDatasourceName(), - repositoryName, field, ptStartingWildcard, partialTerm); + repositoryName, cspaceInstanceId, field, ptStartingWildcard, partialTerm); } /** diff --git a/services/common/src/main/java/org/collectionspace/services/common/query/nuxeo/QueryManagerNuxeoImpl.java b/services/common/src/main/java/org/collectionspace/services/common/query/nuxeo/QueryManagerNuxeoImpl.java index a8c4734ed..e9e2f4cb2 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/query/nuxeo/QueryManagerNuxeoImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/query/nuxeo/QueryManagerNuxeoImpl.java @@ -66,10 +66,10 @@ public class QueryManagerNuxeoImpl implements IQueryManager { private static Pattern advSearchSqlWildcard = Pattern.compile(".*?[I]*LIKE\\s*\\\"\\%\\\".*?"); - private static String getLikeForm(String dataSourceName, String repositoryName) { + private static String getLikeForm(String dataSourceName, String repositoryName, String cspaceInstanceId) { if (SEARCH_LIKE_FORM == null) { try { - DatabaseProductType type = JDBCTools.getDatabaseProductType(dataSourceName, repositoryName); + DatabaseProductType type = JDBCTools.getDatabaseProductType(dataSourceName, repositoryName, cspaceInstanceId); if (type == DatabaseProductType.MYSQL) { SEARCH_LIKE_FORM = IQueryManager.SEARCH_LIKE; } else if (type == DatabaseProductType.POSTGRESQL) { @@ -239,6 +239,7 @@ public class QueryManagerNuxeoImpl implements IQueryManager { @Override public String createWhereClauseForPartialMatch(String dataSourceName, String repositoryName, + String cspaceInstanceId, String field, boolean startingWildcard, String partialTerm) { @@ -259,7 +260,7 @@ public class QueryManagerNuxeoImpl implements IQueryManager { StringBuilder ptClause = new StringBuilder(trimmed.length()+field.length()+20); ptClause.append(field); - ptClause.append(getLikeForm(dataSourceName, repositoryName)); + ptClause.append(getLikeForm(dataSourceName, repositoryName, cspaceInstanceId)); ptClause.append(startingWildcard?"'%":"'"); ptClause.append(unescapedSingleQuote.matcher(trimmed).replaceAll("\\\\'")); ptClause.append("%'"); diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java b/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java index 5470435f7..f47c35b38 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java @@ -129,7 +129,18 @@ public class JDBCTools { return result; } - public static Connection getConnection(String dataSourceName, String repositoryName) throws NamingException, SQLException { + // + // Use this version of the getConnection() method when you don't want to qualify the database name + // with a CollectionSpace instance ID. + // + public static Connection getConnection(String dataSourceName, + String databaseName) throws NamingException, SQLException { + return getConnection(dataSourceName, databaseName, null); + } + + public static Connection getConnection(String dataSourceName, + String repositoryName, + String cspaceInstanceId) throws NamingException, SQLException { Connection result = null; if (Tools.isEmpty(dataSourceName) || Tools.isEmpty(repositoryName)) { @@ -154,7 +165,7 @@ public class JDBCTools { BasicDataSource dataSource = (BasicDataSource)getDataSource(dataSourceName); // Get the template URL value from the JNDI datasource and substitute the databaseName String urlTemplate = dataSource.getUrl(); - String databaseName = getDatabaseName(repositoryName); + String databaseName = getDatabaseName(repositoryName, cspaceInstanceId); String connectionUrl = urlTemplate.replace(URL_DATABASE_NAME, databaseName); // ATTENTION! @@ -181,11 +192,11 @@ public class JDBCTools { return result; } - public static CachedRowSet executeQuery(String dataSourceName, String repositoryName, String sql) throws Exception { + public static CachedRowSet executeQuery(String dataSourceName, String repositoryName, String cspaceInstanceId, String sql) throws Exception { Connection conn = null; Statement stmt = null; try { - conn = getConnection(dataSourceName, repositoryName); + conn = getConnection(dataSourceName, repositoryName, cspaceInstanceId); stmt = conn.createStatement(); RowSetFactory rowSetFactory = RowSetProvider.newFactory(); @@ -219,11 +230,11 @@ public class JDBCTools { } public static CachedRowSet executePreparedQuery(final PreparedStatementBuilder builder, - String dataSourceName, String repositoryName) throws Exception { + String dataSourceName, String repositoryName, String cspaceInstanceId) throws Exception { Connection conn = null; PreparedStatement ps = null; try { - conn = getConnection(dataSourceName, repositoryName); + conn = getConnection(dataSourceName, repositoryName, cspaceInstanceId); RowSetFactory rowSetFactory = RowSetProvider.newFactory(); CachedRowSet crs = rowSetFactory.createCachedRowSet(); ps = builder.build(conn); @@ -260,12 +271,12 @@ public class JDBCTools { // FIXME: This method's code significantly overlaps that of executePrepareQuery(), above, // and the two could be refactored into a single method, if desired. public static List executePreparedQueries(final List builders, - String dataSourceName, String repositoryName, Boolean executeWithinTransaction) throws Exception { + String dataSourceName, String repositoryName, String cspaceInstanceId, Boolean executeWithinTransaction) throws Exception { Connection conn = null; PreparedStatement ps = null; List results = new ArrayList<>(); try { - conn = getConnection(dataSourceName, repositoryName); + conn = getConnection(dataSourceName, repositoryName, cspaceInstanceId); if (executeWithinTransaction) { conn.setAutoCommit(false); } @@ -288,6 +299,7 @@ public class JDBCTools { } } catch (Exception e) { int rowcount = ps.executeUpdate(); + logger.debug(String.format("Row count for builder %s is %d", ps.toString(), rowcount)); // Throw uncaught exception here if update attempt also fails } } @@ -320,11 +332,14 @@ public class JDBCTools { } } - public static int executeUpdate(String dataSourceName, String repositoryName, String sql) throws Exception { + public static int executeUpdate(String dataSourceName, + String repositoryName, + String cspaceInstanceId, + String sql) throws Exception { Connection conn = null; Statement stmt = null; try { - conn = getConnection(dataSourceName, repositoryName); + conn = getConnection(dataSourceName, repositoryName, cspaceInstanceId); stmt = conn.createStatement(); int rows = stmt.executeUpdate(sql); stmt.close(); @@ -371,11 +386,12 @@ public class JDBCTools { * @return the database product name */ public static String getDatabaseProductName(String dataSourceName, - String repositoryName) { + String repositoryName, + String cspaceInstanceId) { if (DBProductName == null) { Connection conn = null; try { - conn = getConnection(dataSourceName, repositoryName); + conn = getConnection(dataSourceName, repositoryName, cspaceInstanceId); DBProductName = conn.getMetaData().getDatabaseProductName(); } catch (Exception e) { if (logger.isTraceEnabled() == true) { @@ -404,11 +420,31 @@ public class JDBCTools { * @return an enumerated value identifying the database product type * @throws Exception */ + public static DatabaseProductType getDatabaseProductType(String dataSourceName, + String repositoryName, + String cspaceInstanceId) throws Exception { + DatabaseProductType result = DatabaseProductType.UNRECOGNIZED; + + String productName = getDatabaseProductName(dataSourceName, repositoryName, cspaceInstanceId); + if (productName.matches("(?i).*mysql.*")) { + result = DatabaseProductType.MYSQL; + } else if (productName.matches("(?i).*postgresql.*")) { + result = DatabaseProductType.POSTGRESQL; + } else { + throw new Exception("Unrecognized database system " + productName); + } + + return result; + } + + // + // Same as method above except the cspace instance ID is not needed. + // public static DatabaseProductType getDatabaseProductType(String dataSourceName, String repositoryName) throws Exception { DatabaseProductType result = DatabaseProductType.UNRECOGNIZED; - String productName = getDatabaseProductName(dataSourceName, repositoryName); + String productName = getDatabaseProductName(dataSourceName, repositoryName, null); if (productName.matches("(?i).*mysql.*")) { result = DatabaseProductType.MYSQL; } else if (productName.matches("(?i).*postgresql.*")) { @@ -420,6 +456,10 @@ public class JDBCTools { return result; } + /* + * By convention, the repository name and database name are the same. However, this + * call encapulates that convention and allows overrides. + */ public static String getDatabaseName(String repoName, String cspaceInstanceId) { String result = repoName; @@ -431,20 +471,19 @@ public class JDBCTools { result = DEFAULT_NUXEO_DATABASE_NAME; } - result = result + cspaceInstanceId; + // + // If we have a non-null 'cspaceInstanceId' instance ID then we need to append it + // as a suffix to the database name. + // + if (cspaceInstanceId != null && !cspaceInstanceId.trim().isEmpty()) { + if (result.endsWith(cspaceInstanceId) == false) { // ensure we don't already have the suffix + result = result + cspaceInstanceId; + } + } return result; } - - /* - * By convention, the repository name and database name are the same. However, this - * call encapulates that convention and allows overrides. - */ - public static String getDatabaseName(String repoName) { - String cspaceInstanceId = ServiceMain.getInstance().getCspaceInstanceId(); - return getDatabaseName(repoName, cspaceInstanceId); - } - + /** * Returns the catalog/database name for an open JDBC connection. * @@ -454,6 +493,7 @@ public class JDBCTools { */ public static String getDatabaseName(String dataSourceName, String repositoryName, + String cspaceInstanceId, Connection conn) throws Exception { String databaseName = null; @@ -463,13 +503,13 @@ public class JDBCTools { // Format of the PostgreSQL JDBC URL: // http://jdbc.postgresql.org/documentation/80/connect.html - if (getDatabaseProductType(dataSourceName, repositoryName) == DatabaseProductType.POSTGRESQL) { + if (getDatabaseProductType(dataSourceName, repositoryName, cspaceInstanceId) == DatabaseProductType.POSTGRESQL) { String tokens[] = urlStr.split(JDBC_URL_DATABASE_SEPARATOR); databaseName = tokens[tokens.length - 1]; // Format of the MySQL JDBC URL: // http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html // FIXME: the last token could contain optional parameters, not accounted for here. - } else if (getDatabaseProductType(dataSourceName, repositoryName) == DatabaseProductType.MYSQL) { + } else if (getDatabaseProductType(dataSourceName, repositoryName, cspaceInstanceId) == DatabaseProductType.MYSQL) { String tokens[] = urlStr.split(JDBC_URL_DATABASE_SEPARATOR); databaseName = tokens[tokens.length - 1]; } diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java index 4af1eeca5..12d809bcd 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java @@ -1124,8 +1124,9 @@ public class RepositoryJavaClientImpl implements RepositoryClient docIds = new HashSet<>(); try { + String cspaceInstanceId = ServiceMain.getInstance().getCspaceInstanceId(); List resultsList = JDBCTools.executePreparedQueries(builders, - dataSourceName, repositoryName, EXECUTE_WITHIN_TRANSACTION); + dataSourceName, repositoryName, cspaceInstanceId, EXECUTE_WITHIN_TRANSACTION); // At least one set of results is expected, from the second prepared // statement to be executed. diff --git a/services/config/src/main/java/org/collectionspace/services/common/config/ConfigUtils.java b/services/config/src/main/java/org/collectionspace/services/common/config/ConfigUtils.java index 00f142a9a..92ebd9b93 100644 --- a/services/config/src/main/java/org/collectionspace/services/common/config/ConfigUtils.java +++ b/services/config/src/main/java/org/collectionspace/services/common/config/ConfigUtils.java @@ -36,7 +36,6 @@ public class ConfigUtils { public static String getRepositoryName(TenantBindingType tenantBindingType, String domainName) { String result = null; - if (domainName != null && domainName.trim().isEmpty() == false) { List repoDomainList = tenantBindingType.getRepositoryDomain(); if (repoDomainList != null && repoDomainList.isEmpty() == false) { @@ -48,7 +47,7 @@ public class ConfigUtils { } } } else { - logger.error(String.format("No domain name was specified on call to getRepositoryName() method.")); + logger.error(String.format("There was no domain name specified on a call to getRepositoryName() method.")); } if (result == null && logger.isTraceEnabled()) { diff --git a/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportPostInitHandler.java b/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportPostInitHandler.java index b97dfec15..f8c2c0201 100644 --- a/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportPostInitHandler.java +++ b/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportPostInitHandler.java @@ -18,13 +18,10 @@ package org.collectionspace.services.report.nuxeo; import java.sql.Connection; -import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.List; -import javax.sql.DataSource; - import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.common.init.IInitHandler; import org.collectionspace.services.common.init.InitHandler; @@ -59,6 +56,7 @@ public class ReportPostInitHandler extends InitHandler implements IInitHandler { @Override public void onRepositoryInitialized(String dataSourceName, String repositoryName, + String cspaceInstanceId, ServiceBindingType sbt, List fields, List propertyList) throws Exception { @@ -73,11 +71,13 @@ public class ReportPostInitHandler extends InitHandler implements IInitHandler { } } } + Connection conn = null; Statement stmt = null; String sql = ""; try { - DatabaseProductType databaseProductType = JDBCTools.getDatabaseProductType(dataSourceName, repositoryName); + DatabaseProductType databaseProductType = JDBCTools.getDatabaseProductType(dataSourceName, repositoryName, + cspaceInstanceId); if (databaseProductType == DatabaseProductType.MYSQL) { // Nothing to do: MYSQL already does wildcard grants in init_db.sql } else if(databaseProductType != DatabaseProductType.POSTGRESQL) { -- 2.47.3