Author: chathura
Date: Tue Feb 26 01:43:51 2008
New Revision: 14213

Log:


Added depedent chain analyzing.
Improved dependency management test cases to test above.
Now basic dependency management is done.



Added:
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/DependencyChain.java
      - copied, changed from r14205, 
trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependency.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/DependentChain.java
      - copied, changed from r14205, 
trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependent.java
Removed:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependency.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependent.java
Modified:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryRegistry.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/dao/VersionedResourceDAO.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
   
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/DependencyTest.java

Copied: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/DependencyChain.java
 (from r14205, 
trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependency.java)
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependency.java 
(original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/DependencyChain.java
    Tue Feb 26 01:43:51 2008
@@ -16,11 +16,11 @@
 
 package org.wso2.registry;
 
-public class Dependency {
+public class DependencyChain {
 
     private String dependencyPath;
 
-    private Dependency[] dependencies;
+    private DependencyChain[] dependencies;
 
     public String getDependencyPath() {
         return dependencyPath;
@@ -30,11 +30,11 @@
         this.dependencyPath = dependencyPath;
     }
 
-    public Dependency[] getDependencies() {
+    public DependencyChain[] getDependencies() {
         return dependencies;
     }
 
-    public void setDependencies(Dependency[] dependencies) {
+    public void setDependencies(DependencyChain[] dependencies) {
         this.dependencies = dependencies;
     }
 }

Copied: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/DependentChain.java 
(from r14205, 
trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependent.java)
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependent.java  
(original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/DependentChain.java 
    Tue Feb 26 01:43:51 2008
@@ -16,11 +16,11 @@
 
 package org.wso2.registry;
 
-public class Dependent {
+public class DependentChain {
 
     private String dependentPath;
 
-    private Dependent[] dependents;
+    private DependentChain[] dependents;
 
     public String getDependentPath() {
         return dependentPath;
@@ -30,11 +30,11 @@
         this.dependentPath = dependentPath;
     }
 
-    public Dependent[] getDependents() {
+    public DependentChain[] getDependents() {
         return dependents;
     }
 
-    public void setDependents(Dependent[] dependents) {
+    public void setDependents(DependentChain[] dependents) {
         this.dependents = dependents;
     }
 }

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java   
(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java   
Tue Feb 26 01:43:51 2008
@@ -96,7 +96,19 @@
      *
      * @return Chain of dependencies
      */
-    Dependency[] getAllDependencies(String resourcePath) throws 
RegistryException;
+    DependencyChain[] getAllDependencies(String resourcePath) throws 
RegistryException;
+
+    /**
+     * Get all dependents of the given resource. This is a chain of dependents 
starting from
+     * the given resource. This is useful to analyse how changes to the given 
resources would affect
+     * other resources.
+     *
+     * @param resourcePath Path of the resource to analyse dependents. This 
can be a path of a
+     * current resource or an old verion of a resource.
+     *
+     * @return Chain of dependents
+     */
+    DependentChain[] getAllDependents(String resourcePath) throws 
RegistryException;
 
     ////////////////////////////////////////////////////////
     // Tagging

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
 Tue Feb 26 01:43:51 2008
@@ -405,8 +405,12 @@
         //To change body of implemented methods use File | Settings | File 
Templates.
     }
 
-    public Dependency[] getAllDependencies(String resourcePath) throws 
RegistryException {
-        return new Dependency[0];  //To change body of implemented methods use 
File | Settings | File Templates.
+    public DependencyChain[] getAllDependencies(String resourcePath) throws 
RegistryException {
+        return new DependencyChain[0];  //To change body of implemented 
methods use File | Settings | File Templates.
+    }
+
+    public DependentChain[] getAllDependents(String resourcePath) throws 
RegistryException {
+        return new DependentChain[0];  //To change body of implemented methods 
use File | Settings | File Templates.
     }
 
     public void applyTag(String resourcePath, String tag) throws 
RegistryException {

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryRegistry.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryRegistry.java
  (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryRegistry.java
  Tue Feb 26 01:43:51 2008
@@ -216,7 +216,12 @@
                                                                     
"Dependencies"));
     }
 
-    public Dependency[] getAllDependencies(String resourcePath) throws 
RegistryException {
+    public DependencyChain[] getAllDependencies(String resourcePath) throws 
RegistryException {
+        throw new 
UnsupportedOperationException(Messages.getMessage("unsupported.exception",
+                                                                    
"Dependencies"));
+    }
+
+    public DependentChain[] getAllDependents(String resourcePath) throws 
RegistryException {
         throw new 
UnsupportedOperationException(Messages.getMessage("unsupported.exception",
                                                                     
"Dependencies"));
     }

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
  Tue Feb 26 01:43:51 2008
@@ -33,7 +33,6 @@
 import org.wso2.registry.utils.AuthorizationUtil;
 import org.wso2.registry.utils.VersionedPath;
 import org.wso2.registry.utils.RegistryUtils;
-import org.wso2.usermanager.Realm;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -582,9 +581,9 @@
         }
     }
 
-    public Dependency[] getAllDependencies(String resourcePath) throws 
RegistryException {
+    public DependencyChain[] getAllDependencies(String resourcePath) throws 
RegistryException {
 
-        Dependency[] dependencies = null;
+        DependencyChain[] dependencies = null;
 
         Connection conn = null;
         try {
@@ -617,6 +616,41 @@
         return dependencies;
     }
 
+    public DependentChain[] getAllDependents(String resourcePath) throws 
RegistryException {
+
+        DependentChain[] dependents = null;
+
+        Connection conn = null;
+        try {
+            conn = getConnection();
+
+            dependents = resourceDAO.getDependents(resourcePath, conn);
+
+        } catch (SQLException e) {
+            String msg = "Failed to get dependents of the resource: " + 
resourcePath;
+            log.error(msg, e);
+
+            try {
+                conn.rollback();
+            } catch (SQLException e1) {
+                log.error(e1);
+            }
+
+            throw new RegistryException(msg, e);
+
+        } finally {
+            if (conn != null) {
+                try {
+                    conn.close();
+                } catch (SQLException ignore) {
+                    log.info(Messages.getMessage("exception.closing.db"));
+                }
+            }
+        }
+
+        return dependents;
+    }
+
     ////////////////////////////////////////////////////////
     // Tagging
     ////////////////////////////////////////////////////////

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
      (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
      Tue Feb 26 01:43:51 2008
@@ -24,7 +24,6 @@
 import org.wso2.registry.utils.AuthorizationUtil;
 import org.wso2.registry.utils.VersionedPath;
 import org.wso2.registry.utils.RegistryUtils;
-import org.wso2.registry.users.UserRealm;
 
 import javax.activation.DataHandler;
 import java.io.IOException;
@@ -220,6 +219,55 @@
         return depList.toArray(new String[depList.size()]);
     }
 
+    private DependencyChain[] getOneLevelDependencies(long resourceID, long 
version, Connection conn)
+            throws SQLException {
+
+        String depSQL = "SELECT A.PATH, D.DAID, D.DVN FROM ARTIFACTS A, 
DEPENDENCY_MANAGEMENT D " +
+                "WHERE D.AID=? AND D.VN=? AND D.DTYPE=? AND A.AID=D.DAID";
+
+        PreparedStatement depSt = conn.prepareStatement(depSQL);
+        depSt.setLong(1, resourceID);
+        depSt.setLong(2, version);
+        depSt.setString(3, RegistryConstants.DEFAULT_DEPENDENCY);
+
+        List<DependencyChain> depList = new ArrayList();
+        ResultSet depResults = depSt.executeQuery();
+        while (depResults.next()) {
+            String dependencyPath = 
depResults.getString(DatabaseConstants.PATH_FIELD);
+            long dependencyVersion = depResults.getLong("DVN");
+            String fullDependencyPath = dependencyPath + "?v=" + 
dependencyVersion;
+
+            DependencyChain dependency = new DependencyChain();
+            dependency.setDependencyPath(fullDependencyPath);
+            depList.add(dependency);
+        }
+
+        depResults.close();
+
+        return depList.toArray(new DependencyChain[depList.size()]);
+    }
+
+    public DependencyChain[] getDependencies(String resourcePath, Connection 
conn)
+            throws SQLException, RegistryException {
+
+        VersionedPath versionedPath = 
RegistryUtils.getVersionedPath(resourcePath);
+
+        if (versionedPath.getVersion() == -1) {
+            
versionedPath.setVersion(getCurrentVersionNumber(versionedPath.getPath(), 
conn));
+        }
+        long resourceID = getResourceID(versionedPath.getPath(), conn);
+
+        DependencyChain[] dependencies =
+                getOneLevelDependencies(resourceID, 
versionedPath.getVersion(), conn);
+
+        for (DependencyChain dependency : dependencies) {
+            DependencyChain[] sDependencies = 
getDependencies(dependency.getDependencyPath(), conn);
+            dependency.setDependencies(sDependencies);
+        }
+
+        return dependencies;
+    }
+
     private String[] getDependents(long resourceID, long version, Connection 
conn)
             throws SQLException {
 
@@ -246,35 +294,35 @@
         return depList.toArray(new String[depList.size()]);
     }
 
-    private Dependency[] getOneLevelDependencies(long resourceID, long 
version, Connection conn)
+    private DependentChain[] getOneLevelDependents(long resourceID, long 
version, Connection conn)
             throws SQLException {
 
-        String depSQL = "SELECT A.PATH, D.DAID, D.DVN FROM ARTIFACTS A, 
DEPENDENCY_MANAGEMENT D " +
-                "WHERE D.AID=? AND D.VN=? AND D.DTYPE=? AND A.AID=D.DAID";
+        String depSQL = "SELECT A.PATH, D.AID, D.VN FROM ARTIFACTS A, 
DEPENDENCY_MANAGEMENT D " +
+                "WHERE D.DAID=? AND D.DVN=? AND D.DTYPE=? AND A.AID=D.AID";
 
         PreparedStatement depSt = conn.prepareStatement(depSQL);
         depSt.setLong(1, resourceID);
         depSt.setLong(2, version);
         depSt.setString(3, RegistryConstants.DEFAULT_DEPENDENCY);
 
-        List<Dependency> depList = new ArrayList();
+        List<DependentChain> depList = new ArrayList();
         ResultSet depResults = depSt.executeQuery();
         while (depResults.next()) {
-            String dependencyPath = 
depResults.getString(DatabaseConstants.PATH_FIELD);
-            long dependencyVersion = depResults.getLong("DVN");
-            String fullDependencyPath = dependencyPath + "?v=" + 
dependencyVersion;
+            String dependentPath = 
depResults.getString(DatabaseConstants.PATH_FIELD);
+            long dependentVersion = depResults.getLong("VN");
+            String fullDependentPath = dependentPath + "?v=" + 
dependentVersion;
 
-            Dependency dependency = new Dependency();
-            dependency.setDependencyPath(fullDependencyPath);
-            depList.add(dependency);
+            DependentChain dependents = new DependentChain();
+            dependents.setDependentPath(fullDependentPath);
+            depList.add(dependents);
         }
 
         depResults.close();
 
-        return depList.toArray(new Dependency[depList.size()]);
+        return depList.toArray(new DependentChain[depList.size()]);
     }
 
-    public Dependency[] getDependencies(String resourcePath, Connection conn)
+    public DependentChain[] getDependents(String resourcePath, Connection conn)
             throws SQLException, RegistryException {
 
         VersionedPath versionedPath = 
RegistryUtils.getVersionedPath(resourcePath);
@@ -284,15 +332,15 @@
         }
         long resourceID = getResourceID(versionedPath.getPath(), conn);
 
-        Dependency[] dependencies =
-                getOneLevelDependencies(resourceID, 
versionedPath.getVersion(), conn);
+        DependentChain[] dependents =
+                getOneLevelDependents(resourceID, versionedPath.getVersion(), 
conn);
 
-        for (Dependency dependency : dependencies) {
-            Dependency[] sDependencies = 
getDependencies(dependency.getDependencyPath(), conn);
-            dependency.setDependencies(sDependencies);
+        for (DependentChain dependent : dependents) {
+            DependentChain[] sDependents = 
getDependents(dependent.getDependentPath(), conn);
+            dependent.setDependents(sDependents);
         }
 
-        return dependencies;
+        return dependents;
     }
 
     /**
@@ -739,7 +787,7 @@
      * Adds dependencies to the given version of a resource. Note that only 
the first level
      * dependencies are added as other dependencies are not associated with 
this resource.
      */
-    public void addDependencies(long resourceID, long version, Dependency[] 
dependencies, Connection conn)
+    public void addDependencies(long resourceID, long version, 
DependencyChain[] dependencies, Connection conn)
             throws SQLException, RegistryException {
 
         String depAddSQL = "INSERT INTO DEPENDENCY_MANAGEMENT (AID, VN, DAID, 
DVN, DTYPE) VALUES (?,?,?,?,?)";
@@ -748,7 +796,7 @@
         String checkSQL = "SELECT AID FROM DEPENDENCY_MANAGEMENT WHERE AID=? 
AND VN=? AND DAID=? AND DVN=? AND DTYPE=?";
         PreparedStatement checkSt = conn.prepareStatement(checkSQL);
 
-        for (Dependency dependency : dependencies) {
+        for (DependencyChain dependency : dependencies) {
 
             VersionedPath versionedPath = 
RegistryUtils.getVersionedPath(dependency.getDependencyPath());
             if (versionedPath.getVersion() == -1) {

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
      (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
      Tue Feb 26 01:43:51 2008
@@ -444,13 +444,20 @@
         registry.addDependencies(dependentPath, dependencyPaths);
     }
 
-    public Dependency[] getAllDependencies(String resourcePath) throws 
RegistryException {
+    public DependencyChain[] getAllDependencies(String resourcePath) throws 
RegistryException {
 
         // todo: stop the dependency chain whenever a resource without the 
read permission is encountered.
         
         return registry.getAllDependencies(resourcePath);
     }
 
+    public DependentChain[] getAllDependents(String resourcePath) throws 
RegistryException {
+
+        // todo: stop the dependent chain whenever a resource without the read 
permission is encountered.
+
+        return registry.getAllDependents(resourcePath);
+    }
+
     public void applyTag(String resourcePath, String tag) throws 
RegistryException {
 
         String authorizationPath = getAuthorizationPath(resourcePath);

Modified: 
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/DependencyTest.java
==============================================================================
--- 
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/DependencyTest.java
        (original)
+++ 
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/DependencyTest.java
        Tue Feb 26 01:43:51 2008
@@ -17,10 +17,7 @@
 package org.wso2.registry.jdbc;
 
 import junit.framework.TestCase;
-import org.wso2.registry.Registry;
-import org.wso2.registry.RegistryException;
-import org.wso2.registry.Resource;
-import org.wso2.registry.Dependency;
+import org.wso2.registry.*;
 import org.wso2.registry.jdbc.realm.RegistryRealm;
 import org.wso2.registry.jdbc.realm.InMemoryRegistryRealm;
 
@@ -104,7 +101,7 @@
         registry.addDependencies("/depTest/test3/r3", new String[] 
{"/depTest/test3/r4"});
         registry.addDependencies("/depTest/test3/r3", new String[] 
{"/depTest/test3/r5"});
 
-        Dependency[] dependencies1 = 
registry.getAllDependencies("/depTest/test3/r1");
+        DependencyChain[] dependencies1 = 
registry.getAllDependencies("/depTest/test3/r1");
         
         assertEquals("There should be one dependency for the resource: 
/depTest/test3/r1",
                 1, dependencies1.length);
@@ -112,7 +109,7 @@
         assertTrue("/depTest/test3/r2 should be a dependency of 
/depTest/test3/r1",
                 
dependencies1[0].getDependencyPath().startsWith("/depTest/test3/r2"));
 
-        Dependency[] dependencies2 = dependencies1[0].getDependencies();
+        DependencyChain[] dependencies2 = dependencies1[0].getDependencies();
 
         assertEquals("There should be one dependency for the resource: 
/depTest/test3/r2",
                 1, dependencies2.length);
@@ -120,14 +117,14 @@
         assertTrue("/depTest/test3/r3 should be a dependency of 
/depTest/test3/r2",
                 
dependencies2[0].getDependencyPath().startsWith("/depTest/test3/r3"));
 
-        Dependency[] dependencies3 = dependencies2[0].getDependencies();
+        DependencyChain[] dependencies3 = dependencies2[0].getDependencies();
 
         assertEquals("There should be two dependencies for the resource: 
/depTest/test3/r3",
                 2, dependencies3.length);
 
         boolean r4Found = false;
         boolean r5Found = false;
-        for (Dependency dependency : dependencies3) {
+        for (DependencyChain dependency : dependencies3) {
             if 
(dependency.getDependencyPath().startsWith("/depTest/test3/r4")) {
                 r4Found = true;
             } else if 
(dependency.getDependencyPath().startsWith("/depTest/test3/r5")) {
@@ -137,6 +134,30 @@
 
         assertTrue("/depTest/test3/r4 should be a dependency of 
/depTest/test3/r3", r4Found);
         assertTrue("/depTest/test3/r5 should be a dependency of 
/depTest/test3/r3", r5Found);
+
+        DependentChain[] dependents1 = 
registry.getAllDependents("/depTest/test3/r5");
+
+        assertEquals("There should be one dependent for the resource: 
/depTest/test3/r5",
+                1, dependents1.length);
+
+        assertTrue("/depTest/test3/r3 should be a dependent of 
/depTest/test3/r5",
+                
dependents1[0].getDependentPath().startsWith("/depTest/test3/r3"));
+
+        DependentChain[] dependents2 = dependents1[0].getDependents();
+
+        assertEquals("There should be one dependent for the resource: 
/depTest/test3/r3",
+                1, dependents2.length);
+
+        assertTrue("/depTest/test3/r2 should be a dependent of 
/depTest/test3/r3",
+                
dependents2[0].getDependentPath().startsWith("/depTest/test3/r2"));
+
+        DependentChain[] dependents3 = dependents2[0].getDependents();
+
+        assertEquals("There should be one dependent for the resource: 
/depTest/test3/r2",
+                1, dependents3.length);
+
+        assertTrue("/depTest/test3/r1 should be a dependent of 
/depTest/test3/r2",
+                
dependents3[0].getDependentPath().startsWith("/depTest/test3/r1"));
     }
 
     private boolean containsString(String[] array, String value) {

_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev

Reply via email to