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

svn at wso2.org svn at wso2.org
Thu Mar 13 11:21:54 PDT 2008


Author: glen
Date: Thu Mar 13 11:21:49 2008
New Revision: 14785

Log:

Some more jiggling of the Lifecycle stuff to get it closer to right.

Introduce SimpleLifecycleTest, which demonstrates that registering, associating, and using a very simple two-state lifecycle is working.

Use a multi-valued property (as Deepal was doing) to store associated Lifecycle names in the Resource.  I'm a little concerned about this approach since users can change property values - I think I'd prefer to actually bake lifecycles in to the data model, but this is OK for right now.

We need to think about whether we need an initialized RegistryContext even outside the realm of the RegistryServlet - what about the embedded case?

Considering we didn't have RegistryContext available, I added simple methods to JDBCRegistry to register and retrieve Lifecycles by name.

Also added a way to set a whole List for a multi-valued property.

Added:
   trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/SimpleLifecycleTest.java
Modified:
   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/jdbc/JDBCRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/lifecycle/DefaultLifecycle.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/lifecycle/Lifecycle.java

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 Mar 13 11:21:49 2008
@@ -51,6 +51,8 @@
 
     void setProperty(String key, String value);
 
+    void setProperty(String key, List<String> value);
+
     void addProperty(String key, String value);
 
     void setProperties(Properties properties);
@@ -77,7 +79,9 @@
 
     void setContentStream(InputStream contentStream);
 
-    Lifecycle[] getLifecycles();
+    List<String> getLifecycles();
+
+    void addLifecycle(String name);
 
-    Lifecycle getLifecycle(String name);
+    void removeLifecycle(String name);
 }

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 Mar 13 11:21:49 2008
@@ -174,11 +174,6 @@
      */
     private VersionedResourceDAO resourceDAO;
 
-    /**
-     * Lifecycles (custom state machines) with which this Resource is associated
-     */
-    private Map <String, Lifecycle> lifecycles;
-
     public ResourceImpl() {
     }
 
@@ -286,14 +281,16 @@
     }
 
     public void setProperty(String key, String value) {
-
         List propValues = new ArrayList();
         propValues.add(value);
         properties.put(key, propValues);
     }
 
-    public void addProperty(String key, String value) {
+    public void setProperty(String key, List<String> value) {
+        properties.put(key, value);
+    }
 
+    public void addProperty(String key, String value) {
         List propValues = (List) properties.get(key);
         if (propValues != null) {
             if (!propValues.contains(value)) {
@@ -481,16 +478,21 @@
         this.contentModified = contentModified;
     }
 
-    public Lifecycle[] getLifecycles() {
-        if (lifecycles == null) return new Lifecycle[0];
-
-        // all the lifecycle assigned to the resource
-        return (Lifecycle[])lifecycles.values().toArray();
+    public List<String> getLifecycles() {
+        return getPropertyValues(Lifecycle.AVAILABLE_LIFE_CYCLES);
     }
 
-    public Lifecycle getLifecycle(String name) {
-        if (lifecycles == null) return null;
-
-        return lifecycles.get(name);
+    public void addLifecycle(String name) {
+        List lifecycles = getPropertyValues(Lifecycle.AVAILABLE_LIFE_CYCLES);
+        if (lifecycles == null)
+            lifecycles = new ArrayList<String>();
+        lifecycles.add(name);
+        setProperty(Lifecycle.AVAILABLE_LIFE_CYCLES, lifecycles);
+    }
+
+    public void removeLifecycle(String name) {
+        List lifecycles = getPropertyValues(Lifecycle.AVAILABLE_LIFE_CYCLES);
+        if (lifecycles != null)
+            lifecycles.remove(name);
     }
 }

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 Mar 13 11:21:49 2008
@@ -23,7 +23,6 @@
 import org.apache.commons.logging.LogFactory;
 import org.wso2.registry.Collection;
 import org.wso2.registry.*;
-import org.wso2.registry.config.RegistryContext;
 import org.wso2.registry.exceptions.ResourceNotFoundException;
 import org.wso2.registry.i18n.Messages;
 import org.wso2.registry.jdbc.dao.*;
@@ -1358,30 +1357,38 @@
     }
 
 
-    public void associateLifeCycle(String resourcePath, String lifecycle) throws RegistryException {
+    public void associateLifeCycle(String resourcePath, String name) throws RegistryException {
         //TODO need to do the security validation here
         Resource resource = get(resourcePath);
-        RegistryContext registryContext = (RegistryContext)System.
-                getProperties().get(RegistryConstants.REGISTRY_CONTEXT);
-        Lifecycle lifeCycle = registryContext.getLifecycle(lifecycle);
+        Lifecycle lifeCycle = getLifecycle(name);
         lifeCycle.associate(resource, this);
-        String currentLifecycle = resource.getProperty(Lifecycle.CURRENT_LIFE_CYCLE);
-        if (currentLifecycle == null) {
-            resource.setProperty(Lifecycle.CURRENT_LIFE_CYCLE, lifecycle);
-        }
-        resource.setProperty(Lifecycle.AVAILABLE_LIFE_CYCLES, lifecycle);
+        resource.addLifecycle(name);
         put(resource.getPath(), resource);
     }
 
+    private Map <String, Lifecycle> lifecycles = new HashMap<String,Lifecycle>();
+    private Lifecycle getLifecycle(String name) {
+        return lifecycles.get(name);
+    }
+    public void addLifecycle(String name, Lifecycle lifecycle) {
+        lifecycles.put(name, lifecycle);
+    }
+
     public void lifeCycleTransition(String resourcePath, String lifecycleName, String action)
             throws RegistryException {
         Resource resource = get(resourcePath);
 
+        Lifecycle lifecycle = getLifecycle(lifecycleName);
+        if (lifecycle == null) {
+            throw new RegistryException("Lifecycle '" + lifecycleName + "' is not registered!");
+        }
+
         RequestContext context = new RequestContext(this);
         context.setResource(resource);
 
-        Lifecycle lifecycle = resource.getLifecycle(lifecycleName);
-        if (lifecycle == null) {
+        // Confirm this lifecycle is associated with this Resource
+        List<String> resourceLifecycles = resource.getLifecycles();
+        if (resourceLifecycles == null || !resourceLifecycles.contains(lifecycleName)) {
             throw new RegistryException("Resource at '" + resourcePath +
                                         "' not associated with lifecycle '" + lifecycleName + "'");
         }
@@ -1392,5 +1399,6 @@
 //        }
 
         lifecycle.transition(context, action);
+        put(resourcePath, resource);
     }
 }

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/lifecycle/DefaultLifecycle.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/lifecycle/DefaultLifecycle.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/lifecycle/DefaultLifecycle.java	Thu Mar 13 11:21:49 2008
@@ -96,8 +96,7 @@
 
         VersionedResourceDAO versionedResourceDAO =
                 new VersionedResourceDAO(registryContext.getDataSource());
-        UserRealm realm = (UserRealm)System.getProperties().get(
-                RegistryConstants.REGISTRY_REALM);
+        UserRealm realm = (UserRealm)System.getProperties().get(RegistryConstants.REGISTRY_REALM);
         try {
             versionedResourceDAO.renameWithoutDelete(resource.getPath(),
                                                      newResourcePath,
@@ -108,5 +107,6 @@
             throw new RegistryException("Error while coping the resource");
         }
 
+        // TODO: So which resource gets the new state property??
     }
 }

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/lifecycle/Lifecycle.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/lifecycle/Lifecycle.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/lifecycle/Lifecycle.java	Thu Mar 13 11:21:49 2008
@@ -6,13 +6,9 @@
 import org.wso2.registry.jdbc.handlers.RequestContext;
 
 public abstract class Lifecycle {
-
-    public static final String CURRENT_LIFE_CYCLE = "currentPhase";
-    public static final String AVAILABLE_LIFE_CYCLES = "AvailablePhases";
-    public static final String COMPLETED = "completed";
+    public static final String AVAILABLE_LIFE_CYCLES = "Lifecycles";
 
     private String name;
-    private int location;
 
     public String getName() {
         return name;

Added: trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/SimpleLifecycleTest.java
==============================================================================
--- (empty file)
+++ trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/SimpleLifecycleTest.java	Thu Mar 13 11:21:49 2008
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2007, 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 org.wso2.registry.lifecycle.Lifecycle;
+import org.wso2.registry.Resource;
+import org.wso2.registry.Registry;
+import org.wso2.registry.RegistryException;
+import org.wso2.registry.RegistryFactory;
+import org.wso2.registry.jdbc.handlers.RequestContext;
+import junit.framework.TestCase;
+
+public class SimpleLifecycleTest extends TestCase {
+    public static class SimpleLifecycle extends Lifecycle {
+        public static final String STATE_PROP = "SimpleLifecycle.state";
+        public static final String INIT = "init";
+        public static final String FINAL = "final";
+
+        public static final String ACTION = "transition";
+
+        public void associate(Resource resource, Registry registry) throws RegistryException {
+            resource.setProperty(STATE_PROP, INIT);
+        }
+
+        public void transition(RequestContext context, String action) throws RegistryException {
+            if (!ACTION.equals(action)) {
+                throw new RegistryException("Wrong action");
+            }
+
+            Resource r = context.getResource();
+            String state = r.getProperty(STATE_PROP);
+            if (state == null) {
+                throw new RegistryException("No state property");
+            }
+
+            if (!INIT.equals(state)) {
+                throw new RegistryException("Invalid state '" + state + "'");
+            }
+
+            r.setProperty(STATE_PROP, FINAL);
+        }
+    }
+
+    public void testSimpleLifecycle() throws Exception {
+        final String RESOURCE = "/r1";
+        final String LIFECYCLE = "simpleLifecycle";
+
+        JDBCRegistry registry = (JDBCRegistry)RegistryFactory.newInstance().getRegistry();
+
+        registry.addLifecycle(LIFECYCLE, new SimpleLifecycle());
+
+        Resource resource = registry.newResource();
+        resource.setDescription("My thing");
+        registry.put(RESOURCE, resource);
+
+        registry.associateLifeCycle(RESOURCE, LIFECYCLE);
+
+        resource = registry.get(RESOURCE);
+        assertNotNull(resource);
+        String propValue = resource.getProperty(SimpleLifecycle.STATE_PROP);
+        assertNotNull(propValue);
+        assertEquals("Wrong initial state!", SimpleLifecycle.INIT, propValue);
+
+        registry.lifeCycleTransition(RESOURCE, LIFECYCLE, SimpleLifecycle.ACTION);
+
+        resource = registry.get(RESOURCE);
+
+        // OK, now we should be in the next state
+        propValue = resource.getProperty(SimpleLifecycle.STATE_PROP);
+        assertNotNull(propValue);
+        assertEquals("Wrong state!", SimpleLifecycle.FINAL, propValue);
+    }
+}



More information about the Registry-dev mailing list