From: Patrick Schmitz Date: Fri, 14 Dec 2012 20:52:14 +0000 (-0800) Subject: Merge branch 'master' of github.com:collectionspace/services X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=58edd0c17f507cf181a350a302edfee2df2bcc69;p=tmp%2Fjakarta-migration.git Merge branch 'master' of github.com:collectionspace/services Conflicts: services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java --- 58edd0c17f507cf181a350a302edfee2df2bcc69 diff --cc services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java index 504a93715,6b20685f0..cdea33f53 --- a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java +++ b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java @@@ -395,185 -372,6 +395,185 @@@ public class ServiceMain // Set our AuthN's datasource to be the cspaceDataSource // AuthN.setDataSource(cspaceDataSource); + + // Get the NuxeoDS info and create the necessary databases. + // Consider the tenant bindings to find and get the data sources for each tenant. + // There may be only one, one per tenant, or something in between. + DatabaseProductType dbType = JDBCTools.getDatabaseProductType(); // only returns PG or MYSQL + String dbExistsQuery = (dbType==DatabaseProductType.POSTGRESQL)? + DB_EXISTS_QUERY_PSQL : DB_EXISTS_QUERY_MYSQL; + + Hashtable tenantBindings = + tenantBindingConfigReader.getTenantBindings(); + HashSet nuxeoDBsChecked = new HashSet(); + PreparedStatement pstmt = null; + Statement stmt = null; + Connection conn = null; + + try { + conn = nuxeoMgrDataSource.getConnection(); + // First check and create the roles as needed. (nuxeo and reader) + + + pstmt = conn.prepareStatement(dbExistsQuery); // create a statement + stmt = conn.createStatement(); + + for (TenantBindingType tenantBinding : tenantBindings.values()) { + String tId = tenantBinding.getId(); + String tName = tenantBinding.getName(); + List repoDomainList = tenantBinding.getRepositoryDomain(); + for (RepositoryDomainType repoDomain : repoDomainList) { - String repoName = repoDomain.getName(); - String dbName = /* repoDomain.getRepositoryName()?? */ "nuxeo"; ++ String repoDomainName = repoDomain.getName(); ++ String dbName = JDBCTools.getDatabaseName(repoDomain.getRepositoryName()); + if(nuxeoDBsChecked.contains(dbName)) { + if (logger.isDebugEnabled()) { - logger.debug("Another user of db: "+dbName+": Repo: "+repoName+" and tenant: " ++ logger.debug("Another user of db: "+dbName+": Repo: "+repoDomainName+" and tenant: " + +tName+" (id:"+tId+")"); + } + } else { + if (logger.isDebugEnabled()) { - logger.debug("Need to prepare db: "+dbName+" for Repo: "+repoName+" and tenant: " ++ logger.debug("Need to prepare db: "+dbName+" for Repo: "+repoDomainName+" and tenant: " + +tName+" (id:"+tId+")"); + } + + pstmt.setString(1, dbName); // set dbName param + ResultSet rs = pstmt.executeQuery(); + // extract data from the ResultSet + boolean dbExists = rs.next(); + rs.close(); + if(dbExists) { + if (logger.isDebugEnabled()) { + logger.debug("Database: "+dbName+" already exists."); + } + } else { + // Create the user as needed + createUserIfNotExists(conn, dbType, nuxeoUser, nuxeoPW); + createUserIfNotExists(conn, dbType, readerUser, readerPW); + // Create the database + createDatabaseWithRights(conn, dbType, dbName, nuxeoUser, nuxeoPW, readerUser, readerPW); + } + nuxeoDBsChecked.add(dbName); + } + } // Loop on repos for tenant + } // Loop on tenants + } catch(SQLException se) { + //Handle errors for JDBC + se.printStackTrace(); + } catch(Exception e) { + //Handle errors for Class.forName + e.printStackTrace(); + } finally { //close resources + try { + if(stmt!=null) { + stmt.close(); + } + } catch(SQLException se2) { + // nothing we can do + } + try{ + if(conn!=null) { + conn.close(); + } + }catch(SQLException se){ + se.printStackTrace(); + } + } + } + + private void createUserIfNotExists(Connection conn, DatabaseProductType dbType, + String username, String userPW) throws Exception { + PreparedStatement pstmt = null; + Statement stmt = null; + final String USER_EXISTS_QUERY_PSQL = + "SELECT 1 AS result FROM pg_roles WHERE rolname=?"; + String userExistsQuery; + if(dbType==DatabaseProductType.POSTGRESQL) { + userExistsQuery = USER_EXISTS_QUERY_PSQL; + } else { + throw new UnsupportedOperationException("CreateUserIfNotExists only supports PSQL - MySQL NYI!"); + } + try { + pstmt = conn.prepareStatement(userExistsQuery); // create a statement + pstmt.setString(1, username); // set dbName param + ResultSet rs = pstmt.executeQuery(); + // extract data from the ResultSet + boolean userExists = rs.next(); + rs.close(); + if(userExists) { + if (logger.isDebugEnabled()) { + logger.debug("User: "+username+" already exists."); + } + } else { + stmt = conn.createStatement(); + String sql = "CREATE ROLE "+username+" WITH PASSWORD '"+userPW+"' LOGIN"; + stmt.executeUpdate(sql); + // Really should do the grants as well. + if (logger.isDebugEnabled()) { + logger.debug("Created Users: '"+username+"' and 'reader'"); + } + } + } catch(Exception e) { + logger.error("createUserIfNotExists failed on exception: " + e.getLocalizedMessage()); + throw e; // propagate + } finally { //close resources + try { + if(pstmt!=null) { + pstmt.close(); + } + if(stmt!=null) { + stmt.close(); + } + } catch(SQLException se) { + // nothing we can do + } + } + } + + private void createDatabaseWithRights(Connection conn, DatabaseProductType dbType, String dbName, + String ownerName, String ownerPW, String readerName, String readerPW) throws Exception { + Statement stmt = null; + try { + stmt = conn.createStatement(); + if(dbType==DatabaseProductType.POSTGRESQL) { + // Postgres does not need passwords. + String sql = "CREATE DATABASE "+dbName+" ENCODING 'UTF8' OWNER "+ownerName; + stmt.executeUpdate(sql); + sql = "GRANT CONNECT ON DATABASE nuxeo TO "+readerName; + stmt.executeUpdate(sql); + if (logger.isDebugEnabled()) { + logger.debug("Created db: '"+dbName+"' with owner: '"+ownerName+"'"); + logger.debug(" Granted connect rights on: '"+dbName+"' to reader: '"+readerName+"'"); + } + // Note that select rights for reader must be granted after Nuxeo startup. + } else if(dbType==DatabaseProductType.MYSQL) { + String sql = "CREATE database "+dbName+" DEFAULT CHARACTER SET utf8"; + stmt.executeUpdate(sql); + sql = "GRANT ALL PRIVILEGES ON "+dbName+".* TO '"+ownerName+"'@'localhost' IDENTIFIED BY '" + +ownerPW+"' WITH GRANT OPTION"; + stmt.executeUpdate(sql); + sql = "GRANT SELECT ON "+dbName+".* TO '"+readerName+"'@'localhost' IDENTIFIED BY '" + +readerPW+"' WITH GRANT OPTION"; + stmt.executeUpdate(sql); + if (logger.isDebugEnabled()) { + logger.debug("Created db: '"+dbName+"' with owner: '"+ownerName+"'"); + logger.debug(" Granted SELECT rights on: '"+dbName+"' to reader: '"+readerName+"'"); + } + } else { + throw new UnsupportedOperationException("createDatabaseWithRights only supports PSQL - MySQL NYI!"); + } + } catch(Exception e) { + logger.error("createDatabaseWithRights failed on exception: " + e.getLocalizedMessage()); + throw e; // propagate + } finally { //close resources + try { + if(stmt!=null) { + stmt.close(); + } + } catch(SQLException se) { + // nothing we can do + } + } + } private void setServerRootDir() { diff --cc services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java index f2a95aa9d,4074db550..765c48cac --- 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 @@@ -41,12 -43,9 +43,12 @@@ public class JDBCTools public static String CSPACE_DATASOURCE_NAME = "CspaceDS"; public static String NUXEO_DATASOURCE_NAME = "NuxeoDS"; // Default database names - public static String DEFAULT_CSPACE_DATABASE_NAME = "cspace"; - public static String DEFAULT_NUXEO_REPOSITORY_NAME = "default"; - public static String DEFAULT_NUXEO_DATABASE_NAME = "nuxeo"; + public static String DEFAULT_CSPACE_DATABASE_NAME = ConfigUtils.DEFAULT_CSPACE_DATABASE_NAME; + public static String DEFAULT_NUXEO_REPOSITORY_NAME = ConfigUtils.DEFAULT_NUXEO_REPOSITORY_NAME; + public static String DEFAULT_NUXEO_DATABASE_NAME = ConfigUtils.DEFAULT_NUXEO_DATABASE_NAME; + public static String NUXEO_MANAGER_DATASOURCE_NAME = "NuxeoMgrDS"; + public static String NUXEO_READER_DATASOURCE_NAME = "NuxeoReaderDS"; + public static String NUXEO_USER_NAME = "nuxeo"; // // Private constants //