[Registry-dev] svn commit r9391 - in trunk/registry/modules/core/src: main/java/org/wso2/registry/jdbc main/java/org/wso2/registry/secure main/java/org/wso2/registry/servlet test/java/org/wso2/registry/jdbc test/java/org/wso2/registry/secure

svn at wso2.org svn at wso2.org
Thu Nov 1 17:42:12 PDT 2007


Author: deepal
Date: Thu Nov  1 17:42:08 2007
New Revision: 9391

Added:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/RegistryRealm.java
      - copied, changed from r9389, trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/RegistryRealmFactory.java
Removed:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/RegistryRealmFactory.java
Modified:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/ConnectionFactory.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/HSQLDBInitializer.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
   trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java
   trunk/registry/modules/core/src/test/java/org/wso2/registry/secure/SecureRegistryTest.java
Log:
small refactoring and provide a way to give a datasource to user manager 

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/ConnectionFactory.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/ConnectionFactory.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/ConnectionFactory.java	Thu Nov  1 17:42:08 2007
@@ -40,25 +40,33 @@
     private DataSource dataSource = null;
 
     public ConnectionFactory() throws RegistryException {
+        this.dataSource =  getDataSource();
+    }
 
+    public  DataSource getDataSource() throws RegistryException {
+        if (dataSource != null) {
+            return dataSource;
+        }
         try {
             log.info("Trying to find the data source " + DatabaseConstants.DATASOURCE_NAME + "...");
 
             Context context = new InitialContext();
-            dataSource = (DataSource) context.lookup(DatabaseConstants.DATASOURCE_NAME);
+            DataSource  dataSource = (DataSource) context.lookup(DatabaseConstants.DATASOURCE_NAME);
 
             log.info("Found the data source " + DatabaseConstants.DATASOURCE_NAME + ".");
+            return dataSource;
 
         } catch (NamingException e) {
             log.info(Messages.
                     getMessage("datasource.not.found", DatabaseConstants.DATASOURCE_NAME));
-            
+
             log.info("Starting the in-memory database...");
 
             startInProcessDatabase();
 
             log.info("Successfully started the in-memory database.");
         }
+        return null;
     }
 
     public Connection getConnection() throws RegistryException {

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java	Thu Nov  1 17:42:08 2007
@@ -70,7 +70,6 @@
         this.ratingsDAO = new RatingsDAO();
 
         // check if root is added. if not add it.
-
         Connection conn = connectionFactory.getConnection();
 
         try {

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/HSQLDBInitializer.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/HSQLDBInitializer.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/HSQLDBInitializer.java	Thu Nov  1 17:42:08 2007
@@ -27,7 +27,7 @@
 
 public class HSQLDBInitializer {
 
-    private static final Log log = LogFactory.getLog(RegistryRealmFactory.class);
+    private static final Log log = LogFactory.getLog(RegistryRealm.class);
 
     public void createHSQLTables() throws RegistryException {
 

Copied: trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/RegistryRealm.java (from r9389, trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/RegistryRealmFactory.java)
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/RegistryRealmFactory.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/RegistryRealm.java	Thu Nov  1 17:42:08 2007
@@ -19,91 +19,106 @@
 
 package org.wso2.registry.secure;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.wso2.registry.ActionConstants;
 import org.wso2.registry.RegistryConstants;
 import org.wso2.registry.RegistryException;
 import org.wso2.registry.jdbc.DatabaseConstants;
+import org.wso2.registry.jdbc.ConnectionFactory;
 import org.wso2.usermanager.Realm;
+import org.wso2.usermanager.UserManagerException;
 import org.wso2.usermanager.readwrite.DefaultRealm;
 import org.wso2.usermanager.readwrite.DefaultRealmConfig;
 
-public class RegistryRealmFactory {
+import javax.sql.DataSource;
 
-    private static final Log log = LogFactory.getLog(RegistryRealmFactory.class);
-
-    public Realm newInMemoryRegistryRealm() throws RegistryException {
+public class RegistryRealm {
 
+    public static Realm createInMemoryRegistryRealm() throws RegistryException {
         new HSQLDBInitializer().createHSQLTables();
-        return newRegistryRealm(UserManagerConstants.HSQL_DB_URL, DatabaseConstants.HSQL_DRIVER_NAME);
-    }
-
-    public Realm newRegistryRealm(String dbURL, String driver)
-            throws RegistryException {
-
-        try {            
+        try {
             DefaultRealm realm = new DefaultRealm();
             DefaultRealmConfig config = (DefaultRealmConfig)realm.getRealmConfiguration();
-            config.setConnectionURL(dbURL);
-            config.setDriverName(driver);
+            config.setConnectionURL(UserManagerConstants.HSQL_DB_URL);
+            config.setDriverName(DatabaseConstants.HSQL_DRIVER_NAME);
             realm.init(config);
             // define built in roles
-            String[] roles = realm.getAllRoleNames();
+            populateRoles(realm);
+            return realm;
 
-            if (!containsString(RegistryConstants.ADMIN_ROLE, roles)) {
+        } catch (Exception e) {
+            throw new RegistryException(e.getMessage());
+        }
+    }
 
-                realm.addRole(RegistryConstants.ADMIN_ROLE);
+    public static Realm createDataSourceAwareRegistryRealm() throws RegistryException {
+        new HSQLDBInitializer().createHSQLTables();
+         try {
+             DefaultRealm realm = new DefaultRealm();
+             DefaultRealmConfig config = (DefaultRealmConfig)realm.getRealmConfiguration();
+             DataSource dataSource =  new ConnectionFactory().getDataSource();
+             config.setDataSource(dataSource);
+             realm.init(config);
+             // define built in roles
+            populateRoles(realm);
+            return realm;
 
-                realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.GET);
-                realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.PUT);
-                realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.DELETE);
-                realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.AUTHORIZE);
-
-                // authorizations for registry level actions are set in the root path
-                realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.ADD_USER);
-                realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.REMOVE_USER);
-                realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.ADD_ROLE);
-                realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.REMOVE_ROLE);
-            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RegistryException(e.getMessage());
+        }
+    }
 
-            if (!containsString(RegistryConstants.GUESTS_ROLE, roles)) {
-                realm.addRole(RegistryConstants.GUESTS_ROLE);
-            }
 
-            // define built in users
-            
-            String[] users = realm.getAllUserNames();
-
-            if (!containsString(RegistryConstants.ADMIN_USER, users)) {
-
-                realm.addUser(RegistryConstants.ADMIN_USER, "admin");
-
-                realm.authorizeUser(RegistryConstants.ADMIN_USER, RegistryConstants.ROOT_PATH, ActionConstants.GET);
-                realm.authorizeUser(RegistryConstants.ADMIN_USER, RegistryConstants.ROOT_PATH, ActionConstants.PUT);
-                realm.authorizeUser(RegistryConstants.ADMIN_USER, RegistryConstants.ROOT_PATH, ActionConstants.DELETE);
-                realm.authorizeUser(RegistryConstants.ADMIN_USER, RegistryConstants.ROOT_PATH, ActionConstants.AUTHORIZE);
-
-                // authorizations for registry level actions are set in the root path
-                realm.authorizeUser(RegistryConstants.ADMIN_USER, RegistryConstants.ROOT_PATH, ActionConstants.ADD_USER);
-                realm.authorizeUser(RegistryConstants.ADMIN_USER, RegistryConstants.ROOT_PATH, ActionConstants.REMOVE_USER);
-                realm.authorizeUser(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.ADD_ROLE);
-                realm.authorizeUser(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.REMOVE_ROLE);
-            }
 
-            if (!containsString(RegistryConstants.ANONYMOUS_USER, users)) {
-                realm.addUser(RegistryConstants.ANONYMOUS_USER, "guest");
-            }
+    private static void populateRoles(DefaultRealm realm) throws UserManagerException {
+        String[] roles = realm.getAllRoleNames();
 
-            return realm;
+        if (!containsString(RegistryConstants.ADMIN_ROLE, roles)) {
 
-        } catch (Exception e) {
-            e.printStackTrace();
-            throw new RegistryException(e.getMessage());
+            realm.addRole(RegistryConstants.ADMIN_ROLE);
+
+            realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.GET);
+            realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.PUT);
+            realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.DELETE);
+            realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.AUTHORIZE);
+
+            // authorizations for registry level actions are set in the root path
+            realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.ADD_USER);
+            realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.REMOVE_USER);
+            realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.ADD_ROLE);
+            realm.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.REMOVE_ROLE);
+        }
+
+        if (!containsString(RegistryConstants.GUESTS_ROLE, roles)) {
+            realm.addRole(RegistryConstants.GUESTS_ROLE);
+        }
+
+        // define built in users
+
+        String[] users = realm.getAllUserNames();
+
+        if (!containsString(RegistryConstants.ADMIN_USER, users)) {
+
+            realm.addUser(RegistryConstants.ADMIN_USER, "admin");
+
+            realm.authorizeUser(RegistryConstants.ADMIN_USER, RegistryConstants.ROOT_PATH, ActionConstants.GET);
+            realm.authorizeUser(RegistryConstants.ADMIN_USER, RegistryConstants.ROOT_PATH, ActionConstants.PUT);
+            realm.authorizeUser(RegistryConstants.ADMIN_USER, RegistryConstants.ROOT_PATH, ActionConstants.DELETE);
+            realm.authorizeUser(RegistryConstants.ADMIN_USER, RegistryConstants.ROOT_PATH, ActionConstants.AUTHORIZE);
+
+            // authorizations for registry level actions are set in the root path
+            realm.authorizeUser(RegistryConstants.ADMIN_USER, RegistryConstants.ROOT_PATH, ActionConstants.ADD_USER);
+            realm.authorizeUser(RegistryConstants.ADMIN_USER, RegistryConstants.ROOT_PATH, ActionConstants.REMOVE_USER);
+            realm.authorizeUser(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.ADD_ROLE);
+            realm.authorizeUser(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, ActionConstants.REMOVE_ROLE);
+        }
+
+        if (!containsString(RegistryConstants.ANONYMOUS_USER, users)) {
+            realm.addUser(RegistryConstants.ANONYMOUS_USER, "guest");
         }
     }
 
-    private boolean containsString(String value, String[] array) {
+    private static boolean containsString(String value, String[] array) {
         for (int i = 0; i < array.length; i++) {
             if (value.equals(array[i])) {
                 return true;

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java	Thu Nov  1 17:42:08 2007
@@ -27,17 +27,15 @@
 import org.wso2.registry.RegistryException;
 import org.wso2.registry.i18n.Messages;
 import org.wso2.registry.jdbc.JDBCRegistry;
-import org.wso2.registry.secure.RegistryRealmFactory;
+import org.wso2.registry.secure.RegistryRealm;
 import org.wso2.registry.secure.UserManagerConstants;
 import org.wso2.usermanager.Realm;
 
 import javax.servlet.ServletConfig;
-import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.io.File;
 import java.io.IOException;
 
 /**
@@ -62,9 +60,7 @@
 
     public void init(ServletConfig config) throws ServletException {
         super.init(config);
-
         Realm registryRealm;
-
         try {
 
             // get the database config of the user manager from servlet init parameters. We can omit
@@ -88,16 +84,12 @@
                     throw new ServletException(msg);
                 }
             }*/
-
             if (dbURL == null || driver == null || dbURL.length() == 0 || driver.length() == 0) {
-                registryRealm = new RegistryRealmFactory().newInMemoryRegistryRealm();
+                registryRealm = RegistryRealm.createInMemoryRegistryRealm();
             } else {
-                registryRealm =
-                        new RegistryRealmFactory().newRegistryRealm(dbURL, driver);
+                registryRealm = RegistryRealm.createDataSourceAwareRegistryRealm();
             }
-
             this.coreRegistry = new JDBCRegistry(registryRealm);
-
             this.editProcessor = new EditProcessor();
         } catch (RegistryException e) {
             e.printStackTrace();

Modified: trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java
==============================================================================
--- trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java	(original)
+++ trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java	Thu Nov  1 17:42:08 2007
@@ -21,7 +21,7 @@
 
 import junit.framework.TestCase;
 import org.wso2.registry.*;
-import org.wso2.registry.secure.RegistryRealmFactory;
+import org.wso2.registry.secure.RegistryRealm;
 import org.wso2.usermanager.Realm;
 
 import java.util.ArrayList;
@@ -40,7 +40,7 @@
         try {
             if (registry == null) {
                 Realm registryRealm =
-                        new RegistryRealmFactory().newInMemoryRegistryRealm();
+                        new RegistryRealm().createInMemoryRegistryRealm();
                 registry = new JDBCRegistry(registryRealm);
             }
         } catch (RegistryException e) {

Modified: trunk/registry/modules/core/src/test/java/org/wso2/registry/secure/SecureRegistryTest.java
==============================================================================
--- trunk/registry/modules/core/src/test/java/org/wso2/registry/secure/SecureRegistryTest.java	(original)
+++ trunk/registry/modules/core/src/test/java/org/wso2/registry/secure/SecureRegistryTest.java	Thu Nov  1 17:42:08 2007
@@ -32,7 +32,7 @@
     public void setUp() {
         try {
             if (registry == null) {
-                realm = new RegistryRealmFactory().newInMemoryRegistryRealm();
+                realm = new RegistryRealm().createInMemoryRegistryRealm();
                 registry = new JDBCRegistry(realm);
             }
         } catch (RegistryException e) {



More information about the Registry-dev mailing list