[Registry-dev] svn commit r16480 - in trunk/registry/modules/core/src/main/java/org/wso2/registry: app config jdbc servlet

svn at wso2.org svn at wso2.org
Fri May 2 13:59:34 PDT 2008


Author: glen
Date: Fri May  2 13:59:31 2008
New Revision: 16480

Log:

Get APP using the same registry as the webapp.

* Revamp getting UserRegistry inside APP code - now we create it during the resolve() in the RegistryResolver and store it in a context attribute.  This also saves a lot of repeated work we were doing in RegistryAdapter.getSecureRegistry()

* Store embeddedRegistry (we need to change that name) in RegistryContext 

* General cleanup, avoid extra registry instances, pass around embeddedRegistry instead

TODO : Stop using singleton RegistryContext, need to make that thread-aware.


Modified:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RegistryAdapter.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RegistryProvider.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RegistryResolver.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/config/RegistryContext.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RegistryAdapter.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RegistryAdapter.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RegistryAdapter.java	Fri May  2 13:59:31 2008
@@ -16,14 +16,12 @@
 import org.apache.abdera.util.Constants;
 import org.apache.abdera.util.EntityTag;
 import org.apache.abdera.util.MimeTypeHelper;
-import org.apache.axiom.om.util.Base64;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.wso2.registry.Collection;
 import org.wso2.registry.Comment;
 import org.wso2.registry.*;
 import org.wso2.registry.exceptions.ResourceNotFoundException;
-import org.wso2.registry.jdbc.realm.RegistryRealm;
 import org.wso2.registry.jdbc.EmbeddedRegistry;
 
 import javax.activation.MimeType;
@@ -43,12 +41,10 @@
     RegistryResolver resolver;
     EmbeddedRegistry embeddedRegistry;
     Registry myRegistry;
-    RegistryRealm realm;
 
-    public RegistryAdapter(RegistryResolver resolver, EmbeddedRegistry embeddedRegistry,
-                           RegistryRealm realm) throws RegistryException {
+    public RegistryAdapter(RegistryResolver resolver, EmbeddedRegistry embeddedRegistry)
+            throws RegistryException {
         this.resolver = resolver;
-        this.realm = realm;
         this.embeddedRegistry = embeddedRegistry;
         myRegistry = embeddedRegistry.getSystemRegistry();
     }
@@ -1311,28 +1307,10 @@
      * @throws RegistryException : If something went wrong
      */
     private Registry getSecureRegistry(RequestContext request) throws RegistryException {
-        String authorizationString = request.getAuthorization();
-        if (authorizationString != null) {
-            // spliting the Authorization string "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
-            String values[] = authorizationString.split("\\ ");
-            if ("Basic".equals(values[0])) {
-                //decoding the username and passord
-                authorizationString = new String(Base64.decode(values[1]));
-                //spliting the decode sting to get the userName and the password;
-                values = authorizationString.split("\\:");
-                String userName = values[0];
-                String password = values[1];
-                // user name and password have found
-                return embeddedRegistry.getUserRegistry(userName, password);
-                //return new SecureRegistry(userName, password, myRegistry, realm);
-            } else {
-                throw new RegistryException("Invalid Authorization string");
-            }
-        } else {
-            // seems like need to create a secure jdbcregistry with annon user
-            return embeddedRegistry.getUserRegistry();
-            //return new SecureRegistry(RegistryConstants.ANONYMOUS_USER, "guest", myRegistry, realm);
-        }
+        Registry reg = (Registry)request.getAttribute(RequestContext.Scope.REQUEST, "userRegistry");
+        if (reg == null)
+            throw new RegistryException("Couldn't find UserRegistry in RequestContext!");
+        return reg;
     }
 
     int curResource = 1;

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RegistryProvider.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RegistryProvider.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RegistryProvider.java	Fri May  2 13:59:31 2008
@@ -20,11 +20,8 @@
 import org.apache.abdera.protocol.server.impl.AbstractWorkspaceProvider;
 import org.apache.abdera.protocol.server.impl.SimpleWorkspaceInfo;
 import org.apache.abdera.protocol.server.impl.TemplateTargetBuilder;
-import org.wso2.registry.Registry;
 import org.wso2.registry.config.RegistryContext;
 import org.wso2.registry.config.RegistryConfigurationProcessor;
-import org.wso2.registry.jdbc.InMemoryJDBCRegistry;
-import org.wso2.registry.jdbc.JDBCRegistry;
 import org.wso2.registry.jdbc.EmbeddedRegistry;
 import org.wso2.registry.jdbc.InMemoryEmbeddedRegistry;
 import org.wso2.registry.jdbc.realm.InMemoryRegistryRealm;
@@ -37,7 +34,6 @@
 public class RegistryProvider extends AbstractWorkspaceProvider {
     RegistryAdapter adapter;
     RegistryResolver resolver;
-    Registry registry;
     public static final String baseURI = "/wso2registry";
 
     public class SimpleFilter implements Filter {
@@ -70,22 +66,29 @@
 //    }
 
     public RegistryProvider() throws Exception {
-        RegistryRealm realm = new InMemoryRegistryRealm();
-        EmbeddedRegistry embeddedRegistry = new InMemoryEmbeddedRegistry(realm);
-        registry = embeddedRegistry.getSystemRegistry();
-
-        RegistryContext ctx = new RegistryContext();
-        InputStream is = null;
-        try {
-            is = new FileInputStream("registry.xml");
-        } catch (FileNotFoundException e) {
-            // no problemo
+        RegistryContext context = RegistryContext.getRegistryContext();
+        EmbeddedRegistry embeddedRegistry = null;
+        if (context != null) {
+            embeddedRegistry = context.getEmbeddedRegistry();
+        } else {
+            RegistryContext ctx = new RegistryContext();
+            InputStream is = null;
+            try {
+                is = new FileInputStream("registry.xml");
+            } catch (FileNotFoundException e) {
+                // no problemo
+            }
+            RegistryConfigurationProcessor.populateRegistryConfig(is, ctx);
+            RegistryContext.setRegistryContext(ctx);
         }
-        RegistryConfigurationProcessor.populateRegistryConfig(is, ctx);
-        registry.setRegistryContext(ctx);
 
-        resolver = new RegistryResolver(registry, baseURI);
-        adapter = new RegistryAdapter(resolver, embeddedRegistry, realm);
+        if (embeddedRegistry == null) {
+            RegistryRealm realm = new InMemoryRegistryRealm();
+            embeddedRegistry = new InMemoryEmbeddedRegistry(realm);
+        }
+
+        resolver = new RegistryResolver(embeddedRegistry, baseURI);
+        adapter = new RegistryAdapter(resolver, embeddedRegistry);
         
         this.setTargetResolver(resolver);
 
@@ -99,9 +102,4 @@
         addWorkspace(workspace);
         addFilter(new SimpleFilter());
     }
-
-    protected ResponseContext processCollection(RequestContext context,
-                                                CollectionAdapter adapter) {
-        return null;
-    }
 }

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RegistryResolver.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RegistryResolver.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RegistryResolver.java	Fri May  2 13:59:31 2008
@@ -23,7 +23,9 @@
 import org.apache.abdera.protocol.Request;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.axiom.om.util.Base64;
 import org.wso2.registry.*;
+import org.wso2.registry.jdbc.EmbeddedRegistry;
 import org.wso2.registry.exceptions.ResourceNotFoundException;
 
 import javax.activation.MimeType;
@@ -56,11 +58,11 @@
     public static final TargetType DELETE_TYPE = TargetType.get("delete", true);
     public static final TargetType COLLECTION_CUSTOM_TYPE = TargetType.get("col-custom", true);
 
-    private Registry registry;
+    private EmbeddedRegistry embeddedRegistry;
     private String basePath;
 
-    public RegistryResolver(Registry registry, String basePath) {
-        this.registry = registry;
+    public RegistryResolver(EmbeddedRegistry embeddedRegistry, String basePath) {
+        this.embeddedRegistry = embeddedRegistry;
         this.basePath = basePath;
     }
 
@@ -176,6 +178,40 @@
             return null;
         }
 
+        Registry registry;
+
+        // Set up secure registry instance
+        String authorizationString = request.getAuthorization();
+        if (authorizationString != null) {
+            // spliting the Authorization string "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
+            String values[] = authorizationString.split("\\ ");
+            if ("Basic".equals(values[0])) {
+                // Decode usename/password
+                authorizationString = new String(Base64.decode(values[1]));
+                values = authorizationString.split("\\:");
+                String userName = values[0];
+                String password = values[1];
+                try {
+                    registry = embeddedRegistry.getUserRegistry(userName, password);
+                } catch (RegistryException e) {
+                    return null;
+                }
+            } else {
+                // TODO - return an ExceptionTarget which contains the auth problem
+                // return new ExceptionTarget(400, "Only basic auth is supported!");
+                return null;
+            }
+        } else {
+            try {
+                registry = embeddedRegistry.getUserRegistry();
+            } catch (RegistryException e) {
+                return null;
+            }
+        }
+
+        // Squirrel this away so the adapter can get it later (after all that work we just did!)
+        context.setAttribute("userRegistry", registry);
+
         if (uri.length() == 0) uri ="/";
         
         Resource resource = null;

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/config/RegistryContext.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/config/RegistryContext.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/config/RegistryContext.java	Fri May  2 13:59:31 2008
@@ -22,6 +22,7 @@
 import org.wso2.registry.RegistryException;
 import org.wso2.registry.i18n.Messages;
 import org.wso2.registry.jdbc.Repository;
+import org.wso2.registry.jdbc.EmbeddedRegistry;
 import org.wso2.registry.jdbc.utils.RegistryDataSource;
 import org.wso2.registry.jdbc.utils.creators.DatabaseCreator;
 import org.wso2.registry.jdbc.utils.creators.DatabaseCreatorFactory;
@@ -47,6 +48,7 @@
     private Repository repository;
     private Map aspects = new HashMap();
     private DataSource dataSource;
+    private EmbeddedRegistry embeddedRegistry;
 
     private static RegistryContext registryContext = null;
 
@@ -73,6 +75,14 @@
         this.repository = repository;
     }
 
+    public EmbeddedRegistry getEmbeddedRegistry() {
+        return embeddedRegistry;
+    }
+
+    public void setEmbeddedRegistry(EmbeddedRegistry embeddedRegistry) {
+        this.embeddedRegistry = embeddedRegistry;
+    }
+
     public DataBaseConfiguration getDefaultDataBaseConfiguration() {
         return defaultDataBaseConfiguration;
     }

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	Fri May  2 13:59:31 2008
@@ -61,7 +61,6 @@
     private static final Log log = LogFactory.getLog(JDBCRegistry.class);
 
     private DataSource dataSource = null;
-    private QueryProcessorManager queryProcessorManager;
     private ResourceDAO resourceDAO = null;
 
     protected HandlerManager handlerManager;
@@ -147,7 +146,8 @@
         repository = new Repository(dataSource);
         versionRepository = new VersionRepository(dataSource);
         handlerManager = new HandlerManager(this, repository);
-        queryProcessorManager = new QueryProcessorManager(dataSource, realm, this);
+        QueryProcessorManager queryProcessorManager =
+                new QueryProcessorManager(dataSource, realm, this);
 
         RegistryContext registryContext = RegistryContext.getRegistryContext();
         //RegistryContext registryContext = (RegistryContext)System.
@@ -480,18 +480,6 @@
         }
     }
 
-    public void addDependencies(String dependentPath, String[] dependencyPaths)
-            throws RegistryException {
-    }
-
-    //public DependencyChain[] getAllDependencies(String resourcePath) throws RegistryException {
-    //    return new DependencyChain[0];
-    //}
-    //
-    //public DependentChain[] getAllDependents(String resourcePath) throws RegistryException {
-    //    return new DependentChain[0];
-    //}
-
     public void applyTag(String resourcePath, String tag) throws RegistryException {
 
         if (Transaction.isStarted()) {

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	Fri May  2 13:59:31 2008
@@ -28,14 +28,10 @@
 import org.wso2.registry.config.RegistryContext;
 import org.wso2.registry.config.HandlerConfiguration;
 import org.wso2.registry.i18n.Messages;
-import org.wso2.registry.jdbc.InMemoryJDBCRegistry;
-import org.wso2.registry.jdbc.JDBCRegistry;
 import org.wso2.registry.jdbc.EmbeddedRegistry;
 import org.wso2.registry.jdbc.InMemoryEmbeddedRegistry;
-import org.wso2.registry.jdbc.realm.InMemoryRegistryRealm;
 import org.wso2.registry.jdbc.realm.RegistryRealm;
 import org.wso2.registry.jdbc.utils.RegistryDataSource;
-import org.wso2.registry.secure.SecureRegistry;
 import org.wso2.registry.servlet.utils.Utils;
 import org.wso2.registry.users.AccessControlAdmin;
 import org.wso2.registry.users.UserStoreException;
@@ -133,6 +129,7 @@
                     RegistryConstants.SYSTEM_USER, RegistryConstants.SYSTEM_PASSWORD);
 
             systemRegistry.setRegistryContext(registryContext);
+            registryContext.setEmbeddedRegistry(embeddedRegistry);
 
             // add configured handers to the jdbc registry
             Iterator<HandlerConfiguration> handlers =



More information about the Registry-dev mailing list