Author: chathura
Date: Mon Feb 25 01:09:02 2008
New Revision: 14155

Log:


Implementing the dependency management.



Added:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependency.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependent.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/utils/RegistryUtils.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/utils/VersionedPath.java
   
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/DependencyTest.java
Modified:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/Resource.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/jdbc/hsql/DBUtils.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/DefaultMediaTypeHandler.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java

Added: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependency.java
==============================================================================
--- (empty file)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependency.java 
Mon Feb 25 01:09:02 2008
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wso2.registry;
+
+public class Dependency {
+
+    private String dependencyPath;
+
+    private Dependency[] dependencies;
+
+    public String getDependencyPath() {
+        return dependencyPath;
+    }
+
+    public void setDependencyPath(String dependencyPath) {
+        this.dependencyPath = dependencyPath;
+    }
+
+    public Dependency[] getDependencies() {
+        return dependencies;
+    }
+
+    public void setDependencies(Dependency[] dependencies) {
+        this.dependencies = dependencies;
+    }
+}

Added: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependent.java
==============================================================================
--- (empty file)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/Dependent.java  
Mon Feb 25 01:09:02 2008
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wso2.registry;
+
+public class Dependent {
+
+    private String dependentPath;
+
+    private Dependent[] dependents;
+
+    public String getDependentPath() {
+        return dependentPath;
+    }
+
+    public void setDependentPath(String dependentPath) {
+        this.dependentPath = dependentPath;
+    }
+
+    public Dependent[] getDependents() {
+        return dependents;
+    }
+
+    public void setDependents(Dependent[] 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   
Mon Feb 25 01:09:02 2008
@@ -74,6 +74,30 @@
      */
     void restoreVersion(String versionPath) throws RegistryException;
 
+    /**
+     * Adds a dependency stating that the resource at "dependentPath" depends 
on the resource at
+     * "dependencyPath". Paths may be the resource paths of the current 
versions or paths of the
+     * old versions. If a path refers to the current version, it should 
contain the path in the
+     * form /c1/c2/r1. If it refers to an old version, it should be in the 
form /c1/c2/r1?v=2.
+     *
+     * @param dependentPath Path of the dependent resource
+     * @param dependencyPaths Paths of dependency resources
+     * @throws RegistryException Depends on the implementation
+     */
+    void addDependencies(String dependentPath, String[] dependencyPaths) 
throws RegistryException;
+
+    /**
+     * Get all dependencies of the given resource. This is a chain of 
dependencies starting from
+     * the given resource. This is useful to analyse how changes to other 
resources would affect
+     * the given resource.
+     *
+     * @param resourcePath Path of the resource to analyse dependencies. This 
can be a path of a
+     * current resource or an old verion of a resource.
+     *
+     * @return Chain of dependencies
+     */
+    Dependency[] getAllDependencies(String resourcePath) throws 
RegistryException;
+
     ////////////////////////////////////////////////////////
     // Tagging
     ////////////////////////////////////////////////////////

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
  (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
  Mon Feb 25 01:09:02 2008
@@ -74,6 +74,9 @@
     public static final int ACTIVE_STATE = 100;
     public static final int DELETED_STATE = 101;
 
+    // dependency types for setting dependencies between resources
+    public static final String DEFAULT_DEPENDENCY = "default";
+
     // internal media types
     public static final String DEFAULT_MEDIA_TYPE = "default";
     public static final String SQL_QUERY_MEDIA_TYPE = "sql-query";

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   
Mon Feb 25 01:09:02 2008
@@ -22,6 +22,8 @@
 import java.sql.Timestamp;
 import java.util.Date;
 import java.util.Properties;
+import java.util.List;
+import java.util.ArrayList;
 
 /**
  * Represents any file or collection stored in the registry. It encapsulates 
both the content
@@ -106,19 +108,19 @@
      * Properties associated with the resource. A resource can contain zero or 
more properties,
      * where each property is a name->value pair. Both name and the value 
should be strings.
      */
-    private Properties properties;
+    private Properties properties = new Properties();
 
     /**
      * Paths of the resources on which this resource depends on. This feature 
is not implemented
      * currently.
      */
-    private String[] dependsOn;
+    private List<String> dependencies = new ArrayList();
 
     /**
      * Paths of the resources that depends on this resource. This feature is 
not implemented
      * currently.
      */
-    private String[] dependedOnBy;
+    private List<String> dependents = new ArrayList();
 
     /**
      * Content of the resource. Object and the type stored in this field 
depends on the resource
@@ -139,7 +141,6 @@
     private boolean directory = false;
 
     public Resource() {
-        this.properties = new Properties();
     }
 
     public long getId() {
@@ -270,20 +271,28 @@
         this.directory = directory;
     }
 
-    public String[] getDependedOnBy() {
-        return dependedOnBy;
+    public String[] getDependents() {
+        return dependents.toArray(new String[dependents.size()]);
     }
 
-    public void setDependedOnBy(String[] dependedOnBy) {
-        this.dependedOnBy = dependedOnBy;
+    public void addDependent(String dependentPath) {
+        dependents.add(dependentPath);
     }
 
-    public String[] getDependsOn() {
-        return dependsOn;
+    public String[] getDependencies() {
+        return dependencies.toArray(new String[dependencies.size()]);
     }
 
-    public void setDependsOn(String[] dependsOn) {
-        this.dependsOn = dependsOn;
+    public void setDependencies(String[] dependencies) {
+        this.dependencies = new ArrayList();
+
+        for (String dependency : dependencies) {
+            this.dependencies.add(dependency);
+        }
+    }
+
+    public void addDependency(String dependencyPath) {
+        dependencies.add(dependencyPath);
     }
 
     public boolean isContentModified() {

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
 Mon Feb 25 01:09:02 2008
@@ -401,6 +401,14 @@
         }
     }
 
+    public void addDependencies(String dependentPath, String[] 
dependencyPaths) throws RegistryException {
+        //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 void applyTag(String resourcePath, String tag) throws 
RegistryException {
         Abdera abdera = new Abdera();
         AbderaClient abderaClient = new AbderaClient(abdera);

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
  Mon Feb 25 01:09:02 2008
@@ -211,6 +211,16 @@
                                                                     
"Versioning"));
     }
 
+    public void addDependencies(String dependentPath, String[] 
dependencyPaths) throws RegistryException {
+        throw new 
UnsupportedOperationException(Messages.getMessage("unsupported.exception",
+                                                                    
"Dependencies"));
+    }
+
+    public Dependency[] getAllDependencies(String resourcePath) throws 
RegistryException {
+        throw new 
UnsupportedOperationException(Messages.getMessage("unsupported.exception",
+                                                                    
"Dependencies"));
+    }
+
     public synchronized void applyTag(String resourcePath, String tag) throws 
RegistryException {
         tagManager.applyTag(resourcePath, tag);
     }

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
  Mon Feb 25 01:09:02 2008
@@ -31,7 +31,9 @@
 import org.wso2.registry.jdbc.urlhandlers.URLHandlerManager;
 import org.wso2.registry.users.UserRealm;
 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;
@@ -141,8 +143,8 @@
                 resourceDAO.add(root, conn);
 
                 AuthorizationUtil.setDefaultAuthorizations(defaultRealm,
-                                                           
RegistryConstants.ROOT_PATH,
-                                                           
RegistryConstants.SYSTEM_USER);
+                        RegistryConstants.ROOT_PATH,
+                        RegistryConstants.SYSTEM_USER);
 
                 root = 
resourceDAO.getLatestVersion(RegistryConstants.ROOT_PATH, conn);
                 root.setLastUpdaterUserName(RegistryConstants.SYSTEM_USER);
@@ -340,9 +342,9 @@
     public String rename(String currentPath, String newPath) throws 
RegistryException {
 
         if (currentPath == null || currentPath.length() == 0 ||
-            newPath == null || newPath.length() == 0) {
+                newPath == null || newPath.length() == 0) {
             String msg = "Invlaid resource paths. Current path: " +
-                         currentPath + ". New path: " + newPath;
+                    currentPath + ". New path: " + newPath;
             throw new RegistryException(msg);
         }
 
@@ -375,7 +377,7 @@
                 } catch (SQLException e) {
 
                     String msg = "Failed to rename the resource: " +
-                                 currentPath + " to the name: " + newPath;
+                            currentPath + " to the name: " + newPath;
                     log.error(msg, e);
                     throw new RegistryException(msg, e);
 
@@ -538,6 +540,78 @@
         }
     }
 
+    public void addDependencies(String dependentPath, String[] dependencyPaths)
+            throws RegistryException {
+
+        VersionedPath versionedPath = 
RegistryUtils.getVersionedPath(dependentPath);
+
+        Connection conn = null;
+
+        try {
+            conn = getConnection();
+            long dependentID = 
resourceDAO.getResourceID(versionedPath.getPath(), conn);
+
+            resourceDAO.addDependencies(
+                    dependentID, versionedPath.getVersion(), dependencyPaths, 
conn);
+
+        } catch (SQLException e) {
+            String msg = "Failed to add dependencies for the resource: " + 
dependentPath;
+            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"));
+                }
+            }
+        }
+    }
+
+    public Dependency[] getAllDependencies(String resourcePath) throws 
RegistryException {
+
+        Dependency[] dependencies = null;
+
+        Connection conn = null;
+        try {
+            conn = getConnection();
+
+            dependencies = resourceDAO.getDependencies(resourcePath, conn);
+
+        } catch (SQLException e) {
+            String msg = "Failed to get dependencies 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 dependencies;
+    }
+
     ////////////////////////////////////////////////////////
     // Tagging
     ////////////////////////////////////////////////////////
@@ -724,7 +798,7 @@
         } catch (SQLException e) {
 
             String msg = "Could not remove the tag: " + tag + " from the 
resource at path: " +
-                         path + ".";
+                    path + ".";
             log.error(msg, e);
 
             try {
@@ -880,13 +954,13 @@
                 if (ratingsDAO.ratingExist(resource.getId(), userID, conn)) {
                     ratingsDAO.updateRating(resource.getId(), userID, rating, 
conn);
                     log.info("Updated the rating on the resource " +
-                             resourcePath + " by user " + userID);
+                            resourcePath + " by user " + userID);
                 } else {
                     ratingsDAO.addRating(resource.getId(), userID, rating, 
conn);
                 }
 
                 logsDAO.addLog(resource.getId(),
-                               userID, LogEntry.RATING, 
Integer.toString(rating), conn);
+                        userID, LogEntry.RATING, Integer.toString(rating), 
conn);
 
             } else {
                 String msg = Messages.getMessage("rate.on.null.artfact", 
resourcePath);
@@ -968,7 +1042,7 @@
         } catch (SQLException e) {
 
             String msg = "Could not get the rating of the resource " + path +
-                         " given by the user " + userName + ". Caused by: " + 
e.getMessage();
+                    " given by the user " + userName + ". Caused by: " + 
e.getMessage();
             log.error(msg, e);
             throw new RegistryException(msg, e);
 
@@ -995,9 +1069,9 @@
 
         // make the path of the query (e.g. /users/chathura/queries/myquery1 )
         String queryPath = RegistryConstants.ROOT_PATH + 
RegistryConstants.USERS_COLLECTION_NAME +
-                           RegistryConstants.PATH_SEPARATOR + userID + 
RegistryConstants
+                RegistryConstants.PATH_SEPARATOR + userID + RegistryConstants
                 .PATH_SEPARATOR +
-                                RegistryConstants.QUERIES_COLLECTION_NAME + 
RegistryConstants
+                RegistryConstants.QUERIES_COLLECTION_NAME + RegistryConstants
                 .PATH_SEPARATOR + name;
 
 

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
      Mon Feb 25 01:09:02 2008
@@ -22,6 +22,9 @@
 import org.wso2.registry.jdbc.DatabaseConstants;
 import org.wso2.registry.users.UserRealm;
 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;
@@ -101,8 +104,9 @@
             resource.setContent(children);
         }
 
-        // get resource properties
+        resource.setDependencies(getDependencies(resource.getId(), 
versionNumber, conn));
 
+        // get resource properties
         return getResourceWithProperties(conn, resource);
     }
 
@@ -138,7 +142,9 @@
             return null;
         }
 
+        boolean latestVerions = false;
         if (versionNumber == -1) {
+            latestVerions = true;
             versionNumber = getLatestVersionNumber(artifactID, conn);
         }
         String sqlVersionedFields = "SELECT * FROM VERSIONS WHERE AID=? AND 
VN=?";
@@ -165,7 +171,8 @@
         }
 
         // get resource properties
-
+        resource.setDependencies(
+                getDependencies(resource.getId(), 
latestVerions?-1:versionNumber, conn));
         return getResourceWithProperties(conn, resource);
     }
 
@@ -187,6 +194,76 @@
         return resource;
     }
 
+    private String[] getDependencies(long resourceID, long version, Connection 
conn)
+            throws SQLException {
+
+        String depSQL = "SELECT A.PATH, D.DAID, D.DVN FROM ARTIFACTS A, 
DEPENDENCY 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<String> 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;
+
+            depList.add(fullDependencyPath);
+        }
+
+        depResults.close();
+
+        return depList.toArray(new String[depList.size()]);
+    }
+
+    private Dependency[] getOneLevelDependencies(long resourceID, long 
version, Connection conn)
+            throws SQLException {
+
+        String depSQL = "SELECT A.PATH, D.DAID, D.DVN FROM ARTIFACTS A, 
DEPENDENCY 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<Dependency> 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;
+
+            Dependency dependency = new Dependency();
+            dependency.setDependencyPath(fullDependencyPath);
+            depList.add(dependency);
+        }
+
+        depResults.close();
+
+        return depList.toArray(new Dependency[depList.size()]);
+    }
+
+    public Dependency[] getDependencies(String resourcePath, Connection conn) 
throws SQLException {
+
+        VersionedPath versionedPath = 
RegistryUtils.getVersionedPath(resourcePath);
+        long resourceID = getResourceID(versionedPath.getPath(), conn);
+
+        Dependency[] dependencies =
+                getOneLevelDependencies(resourceID, 
versionedPath.getVersion(), conn);
+
+        for (Dependency dependency : dependencies) {
+            Dependency[] sDependencies = 
getDependencies(dependency.getDependencyPath(), conn);
+            dependency.setDependencies(sDependencies);
+        }
+
+        return dependencies;
+    }
+
     /**
      * Returns the latest (current) version of an artifact. If artifact is a 
collection, all child
      * paths will be added as latest paths in the form /a/b
@@ -260,6 +337,8 @@
             resource.setContent(children);
         }
 
+        resource.setDependencies(getDependencies(resource.getId(), 
latestVersionNumber, conn));
+
         // get resource properties
         return getResourceWithProperties(conn, resource);
     }
@@ -276,7 +355,7 @@
      * @param conn : Connection to DB
      * @return : verion number of the resource user requested
      */
-    private long getCurrentVersionNumber(String path, Connection conn) throws 
RegistryException {
+    public long getCurrentVersionNumber(String path, Connection conn) throws 
RegistryException {
         if (path != null) {
             try {
                 String[] parts = path.split(RegistryConstants.PATH_SEPARATOR);
@@ -477,7 +556,7 @@
      * @param conn     : Connection to Db
      * @throws java.sql.SQLException : if something went wrong
      */
-    public void add(Resource resource, Connection conn) throws SQLException {
+    public void add(Resource resource, Connection conn) throws SQLException, 
RegistryException {
 
         // first add the resource data into the artifacts table
 
@@ -505,7 +584,8 @@
         addProperties(newArtifactID, resource.getProperties(), conn);
     }
 
-    public void update(String path, Resource resource, Connection conn) throws 
SQLException {
+    public void update(String path, Resource resource, Connection conn)
+            throws SQLException, RegistryException {
 
         // first update the artifacts table
 
@@ -575,6 +655,99 @@
         }
     }
 
+    /**
+     * Adds dependencyPaths to the given version of a resource. If dependency 
path does not contain
+     * a version number, latest version of the dependency will be used.
+     */
+    public void addDependencies(
+            long resourceID, long version, String[] dependencyPaths, 
Connection conn)
+            throws SQLException, RegistryException {
+
+        String depAddSQL = "INSERT INTO DEPENDENCY (AID, VN, DAID, DVN, DTYPE) 
" +
+                "VALUES (?,?,?,?,?)";
+        PreparedStatement addSt = conn.prepareStatement(depAddSQL);
+
+        String checkSQL = "SELECT AID FROM DEPENDENCY " +
+                "WHERE AID=? AND VN=? AND DAID=? AND DVN=? AND DTYPE=?";
+        PreparedStatement checkSt = conn.prepareStatement(checkSQL);
+
+        for (String dependencyPath : dependencyPaths) {
+
+            VersionedPath versionedPath = 
RegistryUtils.getVersionedPath(dependencyPath);
+            if (versionedPath.getVersion() == -1) {
+                
versionedPath.setVersion(getCurrentVersionNumber(versionedPath.getPath(), 
conn));
+            }
+
+            long dependencyID = getResourceID(versionedPath.getPath(), conn);
+
+            checkSt.setLong(1, resourceID);
+            checkSt.setLong(2, version);
+            checkSt.setLong(3, dependencyID);
+            checkSt.setLong(4, versionedPath.getVersion());
+            checkSt.setString(5, RegistryConstants.DEFAULT_DEPENDENCY);
+
+            ResultSet result = checkSt.executeQuery();
+            if (!result.next()) {
+
+                addSt.setLong(1, resourceID);
+                addSt.setLong(2, version);
+                addSt.setLong(3, dependencyID);
+                addSt.setLong(4, versionedPath.getVersion());
+                addSt.setString(5, RegistryConstants.DEFAULT_DEPENDENCY);
+
+                addSt.executeUpdate();
+                addSt.clearParameters();
+            }
+
+            checkSt.clearParameters();
+        }
+    }
+
+    /**
+     * 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)
+            throws SQLException, RegistryException {
+
+        String depAddSQL = "INSERT INTO DEPENDENCY (AID, VN, DAID, DVN, DTYPE) 
VALUES (?,?,?,?,?)";
+        PreparedStatement addSt = conn.prepareStatement(depAddSQL);
+
+        String checkSQL = "SELECT AID FROM DEPENDENCY WHERE AID=? AND VN=? AND 
DAID=? AND DVN=? AND DTYPE=?";
+        PreparedStatement checkSt = conn.prepareStatement(checkSQL);
+
+        for (Dependency dependency : dependencies) {
+
+            VersionedPath versionedPath = 
RegistryUtils.getVersionedPath(dependency.getDependencyPath());
+            if (versionedPath.getVersion() == -1) {
+                
versionedPath.setVersion(getCurrentVersionNumber(versionedPath.getPath(), 
conn));
+            }
+
+            long dependencyID = getResourceID(versionedPath.getPath(), conn);
+
+            checkSt.setLong(1, resourceID);
+            checkSt.setLong(2, version);
+            checkSt.setLong(3, dependencyID);
+            checkSt.setLong(4, versionedPath.getVersion());
+            checkSt.setString(5, RegistryConstants.DEFAULT_DEPENDENCY);
+
+            ResultSet result = checkSt.executeQuery();
+            if (!result.next()) {
+
+                addSt.setLong(1, resourceID);
+                addSt.setLong(2, version);
+                addSt.setLong(3, dependencyID);
+                addSt.setLong(4, versionedPath.getVersion());
+                addSt.setString(5, RegistryConstants.DEFAULT_DEPENDENCY);
+
+                addSt.executeUpdate();
+                addSt.clearParameters();
+            }
+
+            checkSt.clearParameters();
+        }
+    }
+
     public void removeProperties(long resourceID, Connection conn) throws 
SQLException {
 
         String removePropsSQL = "DELETE FROM PROPERTIES WHERE AID=?";
@@ -1238,5 +1411,4 @@
         }
         markActivated(collection.getId(), conn);
     }
-
 }

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/hsql/DBUtils.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/hsql/DBUtils.java
  (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/hsql/DBUtils.java
  Mon Feb 25 01:09:02 2008
@@ -104,6 +104,7 @@
                                                   "VN INTEGER NOT NULL," +
                                                   "DAID INTEGER NOT NULL," +
                                                   "DVN INTEGER NOT NULL," +
+                                                  "DTYPE VARCHAR (20)," +     
                                                   "UNIQUE (AID,VN,DAID,DVN)," +
                                                   "FOREIGN KEY (DAID) 
REFERENCES ARTIFACTS (AID)," +
                                                   "FOREIGN KEY (AID) 
REFERENCES ARTIFACTS (AID));";

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/DefaultMediaTypeHandler.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/DefaultMediaTypeHandler.java
    (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/DefaultMediaTypeHandler.java
    Mon Feb 25 01:09:02 2008
@@ -146,11 +146,17 @@
                                                     "collection with the same 
name exist in the system");
                     }
                 }
+                
                 if (resource.isContentModified() ||
                     a.getState() == RegistryConstants.DELETED_STATE) {
                     resourceDAO.addResourceVersion(resource, conn);
                 }
 
+                long resourceID = resourceDAO.getResourceID(path, conn);
+                long currentVersion = 
resourceDAO.getCurrentVersionNumber(path, conn);
+                resourceDAO.addDependencies(
+                        resourceID, currentVersion, 
resource.getDependencies(), conn);
+
             } else {
 
                 // given resource does not exist. add it as a new resource.
@@ -170,8 +176,11 @@
 
                 resource.setId(resourceDAO.getResourceID(path, conn));
                 resourceDAO.addResourceVersion(resource, conn);
-                // add new resource as a child of the current parent
-                //resourceDAO.addChild(currentParent.getId(), 
resource.getId(), conn);
+
+                long resourceID = resourceDAO.getResourceID(path, conn);
+                long currentVersion = 
resourceDAO.getCurrentVersionNumber(path, conn);
+                resourceDAO.addDependencies(
+                        resourceID, currentVersion, 
resource.getDependencies(), conn);
             }
 
             conn.commit();

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
      Mon Feb 25 01:09:02 2008
@@ -414,8 +414,44 @@
         registry.restoreVersion(versionPath);
     }
 
-    public void applyTag(String resourcePath, String tag)
-            throws RegistryException {
+    /**
+     * Checks if the current user has PUT permission for the dependentPath. 
User should have write
+     * permission to specify a resourceis dependent on another resource.
+     *
+     * @param dependentPath Path of the dependent resource
+     * @param dependencyPaths
+     * @throws RegistryException On authorizing error or an error caused by 
the underlying core
+     * registry impl.
+     */
+    public void addDependencies(String dependentPath, String[] 
dependencyPaths) throws RegistryException {
+
+        String authPath = getAuthorizationPath(dependentPath);
+
+        try {
+            if (!authorizer.isUserAuthorized(userID, authPath, 
ActionConstants.PUT)) {
+                String msg = "User: " + userID + " does not have write 
permission on the resource: " +
+                             authPath + ". Users should have write permission 
to specify that " +
+                             "resource is dependent on another resource.";
+                log.info(msg);
+                throw new AuthorizationFailedException(msg);
+            }
+        } catch (UserStoreException e) {
+            String msg = "Could not check authorization. \nCaused by " + 
e.getMessage();
+            log.error(msg, e);
+            throw new RegistryException(msg, e);
+        }
+
+        registry.addDependencies(dependentPath, dependencyPaths);
+    }
+
+    public Dependency[] getAllDependencies(String resourcePath) throws 
RegistryException {
+
+        // todo: stop the dependency chain whenever a resource without the 
read permission is encountered.
+        
+        return registry.getAllDependencies(resourcePath);
+    }
+
+    public void applyTag(String resourcePath, String tag) throws 
RegistryException {
 
         String authorizationPath = getAuthorizationPath(resourcePath);
 

Added: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/utils/RegistryUtils.java
==============================================================================
--- (empty file)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/utils/RegistryUtils.java
        Mon Feb 25 01:09:02 2008
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wso2.registry.utils;
+
+import org.wso2.registry.RegistryConstants;
+
+public class RegistryUtils {
+
+    public static VersionedPath getVersionedPath(String resourcePath) {
+
+        String plainPath = resourcePath;
+        long versionNumber = -1;
+        if (resourcePath.indexOf("?v=") != -1) {
+            String[] parts = resourcePath.split("\\?v=");
+            plainPath = parts[0];
+            versionNumber = Long.parseLong(parts[1]);
+        }
+
+        plainPath = getPreparedPath(plainPath);
+
+        VersionedPath versionedPath = new VersionedPath();
+        versionedPath.setPath(plainPath);
+        versionedPath.setVersion(versionNumber);
+
+        return versionedPath;
+    }
+
+    public static String getPreparedPath(String resourcePath) {
+
+        String preparedPath = resourcePath;
+        if (preparedPath.equals(RegistryConstants.ROOT_PATH)) {
+            return preparedPath;
+
+        } else {
+
+            if (!preparedPath.startsWith(RegistryConstants.ROOT_PATH)) {
+                preparedPath = RegistryConstants.ROOT_PATH + preparedPath;
+            }
+
+            if (preparedPath.endsWith(RegistryConstants.PATH_SEPARATOR)) {
+                preparedPath = preparedPath.substring(0, preparedPath.length() 
- 1);
+            }
+        }
+
+        return preparedPath;
+    }
+}

Added: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/utils/VersionedPath.java
==============================================================================
--- (empty file)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/utils/VersionedPath.java
        Mon Feb 25 01:09:02 2008
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wso2.registry.utils;
+
+public class VersionedPath {
+
+    private String path;
+
+    private long version;
+
+    public String getPath() {
+        return path;
+    }
+
+    public void setPath(String path) {
+        this.path = path;
+    }
+
+    public long getVersion() {
+        return version;
+    }
+
+    public void setVersion(long version) {
+        this.version = version;
+    }
+}

Added: 
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/DependencyTest.java
==============================================================================
--- (empty file)
+++ 
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/DependencyTest.java
        Mon Feb 25 01:09:02 2008
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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.jdbc.realm.RegistryRealm;
+import org.wso2.registry.jdbc.realm.InMemoryRegistryRealm;
+
+public class DependencyTest extends TestCase {
+
+    protected static Registry registry = null;
+    protected static RegistryRealm realm = null;
+
+    public void setUp() {
+        try {
+            if (registry == null) {
+                realm = new InMemoryRegistryRealm();
+                registry = new InMemoryJDBCRegistry(realm);
+            }
+        } catch (RegistryException e) {
+            fail("Failed to initialize the registry. Caused by: " + 
e.getMessage());
+        }
+    }
+
+    public void testSettingDependenciesInResources() throws RegistryException {
+
+        Resource r1 = new Resource();
+        r1.setContent("some content for first r1".getBytes());
+        registry.put("/depTest/test1/r1", r1);
+
+        Resource r2 = new Resource();
+        r2.setContent("this is dependent on r1".getBytes());
+        r2.addDependency("/depTest/test1/r1");
+        registry.put("/depTest/test1/r2", r2);
+
+        Resource r22 = registry.get("/depTest/test1/r2");
+
+        assertTrue(containsString(r22.getDependencies(), "/depTest/test1/r1"));
+    }
+
+    private boolean containsString(String[] array, String value) {
+
+        boolean found = false;
+        for (String anArray : array) {
+            if (anArray.startsWith(value)) {
+                found = true;
+                break;
+            }
+        }
+
+        return found;
+    }
+
+
+}

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

Reply via email to