[Registry-dev] svn commit r16446 - in trunk/registry/modules/core/src: main/java/org/wso2/registry main/java/org/wso2/registry/app main/java/org/wso2/registry/config main/java/org/wso2/registry/jdbc main/java/org/wso2/registry/jdbc/dao main/java/org/wso2/registry/session test/java/org/wso2/registry

svn at wso2.org svn at wso2.org
Thu May 1 14:05:39 PDT 2008


Author: glen
Date: Thu May  1 14:05:27 2008
New Revision: 16446

Log:

* Rough in pagination API, Q&D implementation, and a test.  Not implemented in APP yet.

* Remove obsolete method Resource.isCollection(), replace with "instanceof Collection"

* Little more work on RegistryFactory

* Order children in results by path name


Modified:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/CollectionImpl.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/CoreRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryFactory.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/Resource.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/ResourceImpl.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.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/jdbc/NonTransactionalJDBCRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/Repository.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/TransactionalJDBCRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceDAO.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/session/UserRegistry.java
   trunk/registry/modules/core/src/test/java/org/wso2/registry/FactoryTest.java

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/CollectionImpl.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/CollectionImpl.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/CollectionImpl.java	Thu May  1 14:05:27 2008
@@ -42,10 +42,6 @@
         return new String[0];
     }
 
-    public boolean isCollection() {
-        return true;
-    }
-
     public List<Resource> getChildResources() {
         return null;
     }

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/CoreRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/CoreRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/CoreRegistry.java	Thu May  1 14:05:27 2008
@@ -41,6 +41,21 @@
     Resource get(String path) throws RegistryException;
 
     /**
+     * Returns the Collection at the given path, with the content paginated according to
+     * the arguments.
+     *
+     * @param path the path of the collection.  MUST point to a collection!
+     * @param start the initial index of the child to return.  If there are fewer children than
+     *              the specified value, a RegistryException will be thrown.
+     * @param pageSize the maximum number of results to return
+     * @return a Collection containing the specified results in the content
+     * @throws RegistryException if the resource is not found, or if the path does not
+     *                           reference a Collection, or if the start index is greater than
+     *                           the number of children.
+     */
+    Collection get(String path, int start, int pageSize) throws RegistryException;
+
+    /**
      * Check whether a resource exists at the given path
      *
      * @param path Path of the resource to be checked

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryFactory.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryFactory.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryFactory.java	Thu May  1 14:05:27 2008
@@ -26,7 +26,8 @@
 
 public abstract class RegistryFactory {
     public static final String PROPFILE_PROPERTY = "registry.propFile";
-    public static final String REGCLASS_PROPERTY = "registry.factoryClass";
+    public static final String FACTORY_CLASS_PROPERTY = "registry.factoryClass";
+    public static final String REG_CLASS_PROPERTY = "registry.class";
     public static final String PROPERTY_FILE_NAME = "registry.properties";
 
     /**
@@ -73,18 +74,21 @@
      * @throws RegistryException if there is a problem
      */
     public static RegistryFactory newInstance(Properties properties) throws RegistryException {
+        Class regClass = InMemoryJDBCRegistry.class;
+
         if (properties != null) {
-            String factoryClassname = properties.getProperty(REGCLASS_PROPERTY);
+            String factoryClassname = properties.getProperty(FACTORY_CLASS_PROPERTY);
             if (factoryClassname != null) {
-                Class regClass;
+                Class factoryClass;
+
                 try {
-                    regClass = Class.forName(factoryClassname);
+                    factoryClass = Class.forName(factoryClassname);
                 } catch (ClassNotFoundException e) {
                     throw new RegistryException("Couldn't load factory class " +
                                                 factoryClassname, e);
                 }
                 try {
-                    Constructor c = regClass.getConstructor(Properties.class);
+                    Constructor c = factoryClass.getConstructor(Properties.class);
                     try {
                         return (RegistryFactory)c.newInstance(properties);
                     } catch (Exception e) {
@@ -92,14 +96,30 @@
                                                     factoryClassname, e);
                     }
                 } catch (NoSuchMethodException e) {
-                    throw new RegistryException("Couldn't create factory of type " +
-                                                factoryClassname, e);
+                    // No property-reading constructor.  OK, if there's a default constructor
+                    // let's just use this class anyway.
+                    try {
+                        return (RegistryFactory)factoryClass.newInstance();
+                    } catch (Exception e1) {
+                        // Nope - don't know how to deal!
+                        throw new RegistryException("Couldn't create factory of type " +
+                                                    factoryClassname, e);
+                    }
+                }
+            }
+            String registryClassname = properties.getProperty(REG_CLASS_PROPERTY);
+            if (registryClassname != null) {
+                try {
+                    regClass = Class.forName(registryClassname);
+                } catch (ClassNotFoundException e) {
+                    throw new RegistryException("Couldn't load Registry class " +
+                                                registryClassname, e);
                 }
             }
         }
 
         // Return the default.
-        return new BasicFactory(InMemoryJDBCRegistry.class);
+        return new BasicFactory(regClass);
     }
 
     /**

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/Resource.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/Resource.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/Resource.java	Thu May  1 14:05:27 2008
@@ -24,8 +24,6 @@
 
     String getId();
 
-    boolean isCollection();
-
     String getAuthorUserName();
 
     Date getCreatedTime();

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/ResourceImpl.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/ResourceImpl.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/ResourceImpl.java	Thu May  1 14:05:27 2008
@@ -437,10 +437,6 @@
         this.lastUpdaterUserName = lastUpdaterUserName;
     }
 
-    public boolean isCollection() {
-        return false;
-    }
-
     public boolean isContentModified() {
         return contentModified;
     }

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java	Thu May  1 14:05:27 2008
@@ -142,6 +142,11 @@
         return resource;
     }
 
+    public Collection get(String path, int start, int pageSize) throws RegistryException {
+        // todo - implement me
+        return null;
+    }
+
     /**
      * This method will generate a resource object representing the Feed object and the logic will
      * be simply the reverse of the create feed of the Atom registry

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	Thu May  1 14:05:27 2008
@@ -18,18 +18,16 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.wso2.registry.Aspect;
 import org.wso2.registry.RegistryException;
 import org.wso2.registry.i18n.Messages;
+import org.wso2.registry.jdbc.Repository;
 import org.wso2.registry.jdbc.utils.RegistryDataSource;
 import org.wso2.registry.jdbc.utils.creators.DatabaseCreator;
 import org.wso2.registry.jdbc.utils.creators.DatabaseCreatorFactory;
-import org.wso2.registry.jdbc.Repository;
-import org.wso2.registry.Aspect;
 import org.wso2.registry.secure.HSQLDBInitializer;
 
 import javax.sql.DataSource;
-import java.sql.Connection;
-import java.sql.SQLException;
 import java.util.*;
 
 public class RegistryContext {

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 May  1 14:05:27 2008
@@ -339,6 +339,16 @@
         return resource;
     }
 
+    public Collection get(String path, int start, int pageSize) throws RegistryException {
+        Collection collection;
+        if (Transaction.isStarted()) {
+            collection = transactionalRegistry.get(path, start, pageSize);
+        } else {
+            collection = nonTransactionalRegistry.get(path, start, pageSize);            
+        }
+        return collection;
+    }
+
     public boolean resourceExists(String path) throws RegistryException {
 
         if (Transaction.isStarted()) {

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/NonTransactionalJDBCRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/NonTransactionalJDBCRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/NonTransactionalJDBCRegistry.java	Thu May  1 14:05:27 2008
@@ -196,6 +196,26 @@
         }
     }
 
+    public Collection get(String path, int start, int pageSize) throws RegistryException {
+        boolean success = false;
+        try {
+            beginTransaction();
+
+            Collection collection = transactionalJDBCRegistry.get(path, start, pageSize);
+
+            success = true;
+
+            return collection;
+
+        } finally {
+            if (success) {
+                commitTransaction();
+            } else {
+                rollbackTransaction();
+            }
+        }
+    }
+
     public boolean resourceExists(String path) throws RegistryException {
 
         boolean exists = false;

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/Repository.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/Repository.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/Repository.java	Thu May  1 14:05:27 2008
@@ -102,6 +102,27 @@
         return resource;
     }
 
+    public Collection get(String path, int start, int pageLen) throws RegistryException {
+        String purePath = RegistryUtils.getPureResourcePath(path);
+
+        String resourceID = resourceDAO.getResourceID(purePath);
+        if (resourceID == null) {
+            return null;
+        }
+
+        if (!AuthorizationUtils.authorize(resourceID, ActionConstants.GET)) {
+            String msg = "User " + CurrentSession.getUser() + " is not authorized to " +
+                    "read the resource " + purePath + ".";
+            log.error(msg);
+            throw new AuthorizationFailedException(msg);
+        }
+
+        CollectionImpl resource = resourceDAO.get(purePath, start, pageLen);
+        resource.setDataSource(dataSource);
+
+        return resource;
+    }
+
     /**
      * Adds or updates the resource in the given path with the given resource. Put is executed if
      * the current user has authorization to do so. Below is the method of evaluating
@@ -250,6 +271,7 @@
      *
      * @param oldPath Path of a existing resource
      * @param newPath New path of the resource
+     * @return the actual new path
      * @throws RegistryException
      */
     public String move(String oldPath, String newPath) throws RegistryException {

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/TransactionalJDBCRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/TransactionalJDBCRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/TransactionalJDBCRegistry.java	Thu May  1 14:05:27 2008
@@ -145,6 +145,10 @@
         return resource;
     }
 
+    public Collection get(String path, int start, int pageSize) throws RegistryException {
+        return repository.get(path, start, pageSize);
+    }
+
     public boolean resourceExists(String path) throws RegistryException {
 
         VersionedPath versionedPath = RegistryUtils.getVersionedPath(path);

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceDAO.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceDAO.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceDAO.java	Thu May  1 14:05:27 2008
@@ -17,6 +17,7 @@
 package org.wso2.registry.jdbc.dao;
 
 import org.wso2.registry.*;
+import org.wso2.registry.Collection;
 import org.wso2.registry.users.accesscontrol.AccessControlConstants;
 import org.wso2.registry.utils.AuthorizationUtils;
 import org.wso2.registry.session.CurrentSession;
@@ -260,8 +261,8 @@
 
         ResourceImpl resourceImpl = getResource(path);
 
-        if (resourceImpl.isCollection()) {
-            fillChildren(resourceImpl);
+        if (resourceImpl instanceof Collection) {
+            fillChildren((CollectionImpl)resourceImpl, -1, -1);
         }
 
         fillResourceProperties(resourceImpl);
@@ -269,6 +270,13 @@
         return resourceImpl;
     }
 
+    public CollectionImpl get(String path, int start, int pageLen) throws RegistryException {
+        CollectionImpl collection = (CollectionImpl)getResource(path);
+        fillChildren(collection, start, pageLen);
+        fillResourceProperties(collection);
+        return collection;
+    }
+
     private void fillResourceProperties(ResourceImpl resourceImpl) throws RegistryException {
 
         Connection conn = Transaction.getConnection();
@@ -307,7 +315,7 @@
     public void add(String path, String parentID, ResourceImpl resourceImpl)
             throws RegistryException {
 
-        if (!resourceImpl.isCollection()) {
+        if (!(resourceImpl instanceof Collection)) {
             addContent(resourceImpl);
         }
 
@@ -415,33 +423,43 @@
         }
     }
 
-    private void fillChildren(ResourceImpl resourceImpl) throws RegistryException {
+    private void fillChildren(CollectionImpl collection, int start, int pageLen)
+            throws RegistryException {
 
         Connection conn = Transaction.getConnection();
 
+        // TODO - Cache the results in a more manageable way when using pagination!
+
         try {
-            String sql = "SELECT R.RID, R.PATH FROM RESOURCE R, DEPENDENCY D WHERE D.PARENT_RID=? AND D.CHILD_RID=R.RID";
+            String sql = "SELECT R.RID, R.PATH FROM RESOURCE R, DEPENDENCY D WHERE D.PARENT_RID=?" +
+                         " AND D.CHILD_RID=R.RID ORDER BY R.PATH";
 
             PreparedStatement ps = conn.prepareStatement(sql);
-            ps.setString(1, resourceImpl.getId());
+            ps.setString(1, collection.getId());
 
             List <String> childPathList = new ArrayList();
             ResultSet results = ps.executeQuery();
+            int current = 0;
             while (results.next()) {
                 String childID = results.getString("RID");
                 if (AuthorizationUtils.authorize(childID, ActionConstants.GET)) {
-                    childPathList.add(results.getString("PATH"));
+                    if (current >= start && (pageLen == -1 || current < start + pageLen))
+                        childPathList.add(results.getString("PATH"));
+                    current++;
                 }
             }
+            if (current < start) {
+                throw new RegistryException("Didn't have enough results to start at #" + start);
+            }
             ps.close();
 
             String[] childPaths = childPathList.toArray(new String[childPathList.size()]);
-            resourceImpl.setContent(childPaths);
+            collection.setContent(childPaths);
 
         } catch (SQLException e) {
 
             String msg = "Failed to get child paths of resource " +
-                    resourceImpl.getPath() + ". " + e.getMessage();
+                    collection.getPath() + ". " + e.getMessage();
             log.error(msg, e);
             throw new RegistryException(msg, e);
         }
@@ -581,7 +599,7 @@
             ps.setString(1, resourceImpl.getId());
             ps.setString(2, path);
             ps.setString(3, resourceImpl.getMediaType());
-            ps.setInt(4, resourceImpl.isCollection()? 1: 0);
+            ps.setInt(4, resourceImpl instanceof Collection ? 1 : 0);
             ps.setString(5, CurrentSession.getUser());
             ps.setTimestamp(6, new Timestamp(now));
             ps.setString(7, CurrentSession.getUser());

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/session/UserRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/session/UserRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/session/UserRegistry.java	Thu May  1 14:05:27 2008
@@ -193,6 +193,14 @@
         return coreRegistry.get(path);
     }
 
+    public Collection get(String path, int start, int pageSize) throws RegistryException {
+
+        CurrentSession.setUser(userName);
+        CurrentSession.setRealm(userRealm);
+
+        return coreRegistry.get(path, start, pageSize);
+    }
+
     public boolean resourceExists(String path) throws RegistryException {
 
         CurrentSession.setUser(userName);

Modified: trunk/registry/modules/core/src/test/java/org/wso2/registry/FactoryTest.java
==============================================================================
--- trunk/registry/modules/core/src/test/java/org/wso2/registry/FactoryTest.java	(original)
+++ trunk/registry/modules/core/src/test/java/org/wso2/registry/FactoryTest.java	Thu May  1 14:05:27 2008
@@ -32,7 +32,7 @@
 
         // Now try one with properties
         Properties props = new Properties();
-        props.setProperty(RegistryFactory.REGCLASS_PROPERTY,
+        props.setProperty(RegistryFactory.FACTORY_CLASS_PROPERTY,
                           "org.wso2.registry.app.RemoteRegistryFactory");
         boolean caughtException = false;
         try {



More information about the Registry-dev mailing list