]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5761 additional cleanup on reporting post-init
authorPatrick Schmitz <pschmitz@berkeley.edu>
Sat, 15 Dec 2012 00:55:11 +0000 (16:55 -0800)
committerPatrick Schmitz <pschmitz@berkeley.edu>
Sat, 15 Dec 2012 00:55:11 +0000 (16:55 -0800)
services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java
services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportPostInitHandler.java

index 466e969cfe74a3fa9b30ba64d362ffaeae02cdb8..111123a7212a7d27772ddddb44f03ec6531f75bc 100644 (file)
@@ -177,6 +177,15 @@ public class ServiceMain {
         } catch(Throwable e) {         \r
                logger.error("Default accounts and permissions setup failed with exception(s): " + e.getLocalizedMessage(), e);\r
         }        \r
+        \r
+        /*\r
+         * This might be useful for something, but the reader grants are better handled in the ReportPostInitHandler.\r
+        try {\r
+               handlePostNuxeoInitDBTasks();\r
+        } catch(Throwable e) {         \r
+               logger.error("handlePostNuxeoInitDBTasks failed with exception(s): " + e.getLocalizedMessage(), e);\r
+        }\r
+        */\r
     }\r
 \r
     /**\r
@@ -390,12 +399,16 @@ public class ServiceMain {
        // Get the template URL value from the JNDI datasource and substitute the databaseName\r
        String nuxeoUser = tomcatDataSource.getUsername();\r
        String nuxeoPW = tomcatDataSource.getPassword();\r
-       // HACK - this should come from another DataSource\r
-       tomcatDataSource =\r
-                       (org.apache.tomcat.dbcp.dbcp.BasicDataSource)nuxeoReaderDataSource;\r
-       // Get the template URL value from the JNDI datasource and substitute the databaseName\r
-       String readerUser = tomcatDataSource.getUsername();\r
-       String readerPW = tomcatDataSource.getPassword();\r
+       // Get reader data source, if any\r
+       String readerUser = null;\r
+       String readerPW = null;\r
+       if(nuxeoReaderDataSource!= null) {\r
+               tomcatDataSource =\r
+                               (org.apache.tomcat.dbcp.dbcp.BasicDataSource)nuxeoReaderDataSource;\r
+               // Get the template URL value from the JNDI datasource and substitute the databaseName\r
+               readerUser = tomcatDataSource.getUsername();\r
+               readerPW = tomcatDataSource.getPassword();\r
+       }\r
        \r
        //\r
        // Set our AuthN's datasource to be the cspaceDataSource\r
@@ -456,7 +469,9 @@ public class ServiceMain {
                                } else {\r
                                        // Create the user as needed\r
                                        createUserIfNotExists(conn, dbType, nuxeoUser, nuxeoPW);\r
-                                       createUserIfNotExists(conn, dbType, readerUser, readerPW);\r
+                                       if(readerUser!=null) {\r
+                                               createUserIfNotExists(conn, dbType, readerUser, readerPW);\r
+                                       }\r
                                        // Create the database\r
                                        createDatabaseWithRights(conn, dbType, dbName, nuxeoUser, nuxeoPW, readerUser, readerPW);\r
                                }\r
@@ -546,11 +561,15 @@ public class ServiceMain {
                        // Postgres does not need passwords.\r
                        String sql = "CREATE DATABASE "+dbName+" ENCODING 'UTF8' OWNER "+ownerName;\r
                        stmt.executeUpdate(sql);\r
-                       sql = "GRANT CONNECT ON DATABASE "+dbName+" TO "+readerName;\r
-                       stmt.executeUpdate(sql);\r
                        if (logger.isDebugEnabled()) {\r
                                logger.debug("Created db: '"+dbName+"' with owner: '"+ownerName+"'");\r
-                               logger.debug(" Granted connect rights on: '"+dbName+"' to reader: '"+readerName+"'");\r
+                       }\r
+                       if(readerName!= null) {\r
+                               sql = "GRANT CONNECT ON DATABASE "+dbName+" TO "+readerName;\r
+                               stmt.executeUpdate(sql);\r
+                               if (logger.isDebugEnabled()) {\r
+                                       logger.debug(" Granted connect rights on: '"+dbName+"' to reader: '"+readerName+"'");\r
+                               }\r
                        }\r
                        // Note that select rights for reader must be granted after Nuxeo startup.\r
                } else if(dbType==DatabaseProductType.MYSQL) {\r
@@ -559,12 +578,16 @@ public class ServiceMain {
                        sql = "GRANT ALL PRIVILEGES ON "+dbName+".* TO '"+ownerName+"'@'localhost' IDENTIFIED BY '"\r
                                        +ownerPW+"' WITH GRANT OPTION";\r
                        stmt.executeUpdate(sql);\r
-                       sql = "GRANT SELECT ON "+dbName+".* TO '"+readerName+"'@'localhost' IDENTIFIED BY '"\r
-                                       +readerPW+"' WITH GRANT OPTION";\r
-                       stmt.executeUpdate(sql);\r
                        if (logger.isDebugEnabled()) {\r
                                logger.debug("Created db: '"+dbName+"' with owner: '"+ownerName+"'");\r
-                               logger.debug(" Granted SELECT rights on: '"+dbName+"' to reader: '"+readerName+"'");\r
+                       }\r
+                       if(readerName!= null) {\r
+                               sql = "GRANT SELECT ON "+dbName+".* TO '"+readerName+"'@'localhost' IDENTIFIED BY '"\r
+                                               +readerPW+"' WITH GRANT OPTION";\r
+                               stmt.executeUpdate(sql);\r
+                               if (logger.isDebugEnabled()) {\r
+                                       logger.debug(" Granted SELECT rights on: '"+dbName+"' to reader: '"+readerName+"'");\r
+                               }\r
                        }\r
                } else {\r
                        throw new UnsupportedOperationException("createDatabaseWithRights only supports PSQL - MySQL NYI!");\r
@@ -584,6 +607,56 @@ public class ServiceMain {
 \r
     }\r
     \r
+    /*\r
+     * This might be useful for something, but the reader grants are better handled in the ReportPostInitHandler.\r
+    private void handlePostNuxeoInitDBTasks() throws Exception {\r
+       Statement stmt = null;\r
+               Connection conn = null;\r
+               \r
+       try {\r
+               DataSource nuxeoMgrDataSource = JDBCTools.getDataSource(JDBCTools.NUXEO_MANAGER_DATASOURCE_NAME);\r
+               DataSource nuxeoReaderDataSource = JDBCTools.getDataSource(JDBCTools.NUXEO_READER_DATASOURCE_NAME);\r
+               \r
+               if(nuxeoReaderDataSource!=null) {\r
+                       // We need to fetch the user name and password from the nuxeoDataSource, to do grants below\r
+                       org.apache.tomcat.dbcp.dbcp.BasicDataSource tomcatDataSource =\r
+                                       (org.apache.tomcat.dbcp.dbcp.BasicDataSource)nuxeoReaderDataSource;\r
+                       // Get the template URL value from the JNDI datasource and substitute the databaseName\r
+                       String readerUser = tomcatDataSource.getUsername();\r
+                       DatabaseProductType dbType = JDBCTools.getDatabaseProductType(\r
+                                       JDBCTools.CSPACE_DATASOURCE_NAME,\r
+                                       JDBCTools.DEFAULT_CSPACE_DATABASE_NAME); // only returns PG or MYSQL\r
+       \r
+                       conn = nuxeoMgrDataSource.getConnection();\r
+                       stmt = conn.createStatement();\r
+                       if(dbType==DatabaseProductType.POSTGRESQL) {\r
+                               // Note that select rights for reader must be granted after Nuxeo startup.\r
+                               String sql = "GRANT SELECT ON ALL TABLES IN SCHEMA public TO "+readerUser;\r
+                               stmt.executeUpdate(sql);\r
+                               if (logger.isDebugEnabled()) {\r
+                                       logger.debug(" Granted SELECT rights on all public tables to reader: '"+readerUser+"'");\r
+                               }\r
+                       } else if(dbType==DatabaseProductType.MYSQL) {\r
+                       } else {\r
+                               throw new UnsupportedOperationException("handlePostNuxeoInitDBTasks only supports Postgres/MySQL.");\r
+                       }\r
+               }\r
+       } catch(Exception e) {\r
+               logger.error("handlePostNuxeoInitDBTasks failed on exception: " + e.getLocalizedMessage());\r
+               throw e;        // propagate\r
+       } finally {   //close resources\r
+               try {\r
+                       if(stmt!=null) {\r
+                               stmt.close();\r
+                       }\r
+               } catch(SQLException se) {\r
+                       // nothing we can do\r
+               }\r
+       }\r
+\r
+    }\r
+     */\r
+    \r
     private void setServerRootDir() {\r
         serverRootDir = System.getProperty(SERVER_HOME_PROPERTY);\r
         if (serverRootDir == null) {\r
index c215e0590bd07a10b9a18e6d4ac834707eea4dd0..b97dfec1597ac76fc26c4bfdff0547f74baa8d25 100644 (file)
@@ -83,14 +83,12 @@ public class ReportPostInitHandler extends InitHandler implements IInitHandler {
             } else if(databaseProductType != DatabaseProductType.POSTGRESQL) {\r
                 throw new Exception("Unrecognized database system " + databaseProductType);\r
             } else {\r
-               //\r
-               // FIXME: REM 12/11/2012 - The local 'stmt' is alway null? An exception will always get thrown inside\r
-               // this clause\r
-               //\r
-                sql = "REVOKE SELECT ON ALL TABLES IN SCHEMA public FROM "+readerRoleName;\r
-                    stmt.execute(sql);\r
+                conn = JDBCTools.getConnection(dataSourceName, repositoryName);\r
+                stmt = conn.createStatement();                \r
+                //sql = "REVOKE SELECT ON ALL TABLES IN SCHEMA public FROM "+readerRoleName;\r
+                //stmt.execute(sql);\r
                 sql = "GRANT SELECT ON ALL TABLES IN SCHEMA public TO "+readerRoleName;\r
-                    stmt.execute(sql);\r
+                stmt.execute(sql);\r
             }\r
             \r
         } catch (SQLException sqle) {\r