You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by da...@apache.org on 2006/04/07 05:15:29 UTC

svn commit: r392168 - in /geronimo/branches/1.1/modules/kernel/src: java/org/apache/geronimo/kernel/config/ java/org/apache/geronimo/kernel/config/xstream/ java/org/apache/geronimo/kernel/repository/ test/org/apache/geronimo/kernel/config/

Author: dain
Date: Thu Apr  6 20:15:26 2006
New Revision: 392168

URL: http://svn.apache.org/viewcvs?rev=392168&view=rev
Log:
Added version number to xstream saved configurations so in the future we can detect old versions.
Added better default selection code to the configuration marshaler.

Added:
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/ConfigurationDataConverter.java
Modified:
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/ConfigurationData.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/ConfigurationUtil.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/SerializedConfigurationMarshaler.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/XStreamConfigurationMarshaler.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/XStreamUtil.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java
    geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/config/ConfigurationUtilTest.java

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/ConfigurationData.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/ConfigurationData.java?rev=392168&r1=392167&r2=392168&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/ConfigurationData.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/ConfigurationData.java Thu Apr  6 20:15:26 2006
@@ -78,6 +78,22 @@
      */
     private transient ConfigurationStore configurationStore;
 
+    public ConfigurationData(Artifact configId, Naming naming, GBeanState gbeanState) {
+        this(new Environment(configId), naming, gbeanState);
+    }
+
+    public ConfigurationData(Environment environment, Naming naming, GBeanState gbeanState) {
+        if (environment == null) throw new NullPointerException("environment is null");
+        if (environment.getConfigId() == null) throw new NullPointerException("environment.configId is null");
+        if (naming == null) throw new NullPointerException("naming is null");
+
+        this.environment = environment;
+        this.naming = naming;
+        this.gbeanState = gbeanState;
+
+        this.moduleType = ConfigurationModuleType.CAR;
+    }
+
     public ConfigurationData(Artifact configId, Naming naming) {
         this(new Environment(configId), naming);
     }
@@ -138,6 +154,10 @@
 
     public Map getChildConfigurations() {
         return Collections.unmodifiableMap(childConfigurations);
+    }
+
+    public void addChildConfiguration(ConfigurationData configurationData) {
+        childConfigurations.put(configurationData.getId(), configurationData);
     }
 
     public Environment getEnvironment() {

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/ConfigurationUtil.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/ConfigurationUtil.java?rev=392168&r1=392167&r2=392168&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/ConfigurationUtil.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/ConfigurationUtil.java Thu Apr  6 20:15:26 2006
@@ -50,49 +50,63 @@
  * @version $Rev:386276 $ $Date$
  */
 public final class ConfigurationUtil {
+    private static final Log log = LogFactory.getLog(ConfigurationUtil.class);
     private static final ConfigurationMarshaler configurationMarshaler;
 
     static {
-        Log log = LogFactory.getLog(ConfigurationUtil.class);
-
         ConfigurationMarshaler marshaler = null;
         String marshalerClass = System.getProperty("org.apache.geronimo.kernel.config.Marshaler");
         if (marshalerClass != null) {
-            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-            Class clazz = null;
-            if (classLoader != null) {
-                try {
-                    clazz = ClassLoading.loadClass(marshalerClass, classLoader);
-                } catch (ClassNotFoundException ignored) {
-                    // doesn't matter
-                }
+            try {
+                marshaler = createConfigurationMarshaler(marshalerClass);
+            } catch (Exception e) {
+                log.error("Error creating configuration marshaler class " + marshalerClass , e);
             }
-            if (clazz == null) {
-                classLoader = ConfigurationUtil.class.getClassLoader();
-                try {
-                    clazz = ClassLoading.loadClass(marshalerClass, classLoader);
-                } catch (ClassNotFoundException ignored) {
-                    // doesn't matter
-                }
+        }
+
+        // todo this code effectively makes the default format xstream
+        //if (marshaler == null) {
+        //    try {
+        //        marshaler = createConfigurationMarshaler("org.apache.geronimo.kernel.config.xstream.XStreamConfigurationMarshaler");
+        //    } catch (Throwable ignored) {
+        //    }
+        //}
+
+        if (marshaler == null) {
+            marshaler = new SerializedConfigurationMarshaler();
+        }
+
+        configurationMarshaler = marshaler;
+    }
+
+    public static ConfigurationMarshaler createConfigurationMarshaler(String marshalerClass) throws Exception {
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        Class clazz = null;
+        if (classLoader != null) {
+            try {
+                clazz = ClassLoading.loadClass(marshalerClass, classLoader);
+            } catch (ClassNotFoundException ignored) {
+                // doesn't matter
             }
-            if (clazz != null) {
-                try {
-                    Object object = clazz.newInstance();
-                    if (object instanceof ConfigurationMarshaler) {
-                        marshaler = (ConfigurationMarshaler) object;
-                    } else {
-                        log.warn("Configuration marshaler class is not an istance of ConfigurationMarshaler " + marshalerClass + ": using default configuration ");
-                    }
-                } catch (Exception e) {
-                    log.error("Error creating configuration marshaler class " + marshalerClass , e);
-                }
+        }
+        if (clazz == null) {
+            classLoader = ConfigurationUtil.class.getClassLoader();
+            try {
+                clazz = ClassLoading.loadClass(marshalerClass, classLoader);
+            } catch (ClassNotFoundException ignored) {
+                // doesn't matter
             }
         }
-        if (marshaler != null) {
-            configurationMarshaler = marshaler;
-        } else {
-            configurationMarshaler = new SerializedConfigurationMarshaler();
+
+        if (clazz != null) {
+            Object object = clazz.newInstance();
+            if (object instanceof ConfigurationMarshaler) {
+                return (ConfigurationMarshaler) object;
+            } else {
+                log.warn("Configuration marshaler class is not an istance of ConfigurationMarshaler " + marshalerClass + ": using default configuration ");
+            }
         }
+        return null;
     }
 
     private ConfigurationUtil() {

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/SerializedConfigurationMarshaler.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/SerializedConfigurationMarshaler.java?rev=392168&r1=392167&r2=392168&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/SerializedConfigurationMarshaler.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/SerializedConfigurationMarshaler.java Thu Apr  6 20:15:26 2006
@@ -21,14 +21,38 @@
 import java.io.ObjectOutputStream;
 import java.io.InputStream;
 import java.io.ObjectInputStream;
+import java.io.ObjectStreamConstants;
+import java.io.PushbackInputStream;
 import java.util.Collection;
 
 /**
  * @version $Rev$ $Date$
  */
 public class SerializedConfigurationMarshaler implements ConfigurationMarshaler {
+    private static byte[] SERIALIZED_MAGIC = new byte[] {
+            (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 8) & 0xFF),
+            (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 0) & 0xFF)
+    };
+
     public ConfigurationData readConfigurationData(InputStream in) throws IOException, ClassNotFoundException {
-        ObjectInputStream oin = new ObjectInputStream(in);
+        PushbackInputStream pushbackInputStream = new PushbackInputStream(in, 2);
+        byte[] streamHeader = new byte[2];
+        if (pushbackInputStream.read(streamHeader) != 2) throw new AssertionError("Cound not read stream header");
+        pushbackInputStream.unread(streamHeader);
+
+        // if this is a serialized config, fallback to the serialization marshaler
+        if (SERIALIZED_MAGIC[0] != streamHeader[0] || SERIALIZED_MAGIC[1] != streamHeader[1]) {
+            ConfigurationMarshaler marshaler;
+            try {
+                marshaler = ConfigurationUtil.createConfigurationMarshaler("org.apache.geronimo.kernel.config.xstream.XStreamConfigurationMarshaler");
+            } catch (Throwable ignored) {
+                throw new IOException("Input does not contain a Java Object Serialization stream");
+            }
+            return marshaler.readConfigurationData(pushbackInputStream);
+
+        }
+
+        ObjectInputStream oin = new ObjectInputStream(pushbackInputStream);
         try {
             return (ConfigurationData) oin.readObject();
         } finally {

Added: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/ConfigurationDataConverter.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/ConfigurationDataConverter.java?rev=392168&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/ConfigurationDataConverter.java (added)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/ConfigurationDataConverter.java Thu Apr  6 20:15:26 2006
@@ -0,0 +1,43 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  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.apache.geronimo.kernel.config.xstream;
+
+import com.thoughtworks.xstream.converters.MarshallingContext;
+import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
+import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
+import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+import com.thoughtworks.xstream.mapper.Mapper;
+import org.apache.geronimo.kernel.config.ConfigurationData;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ConfigurationDataConverter extends ReflectionConverter {
+    public ConfigurationDataConverter(ReflectionProvider reflectionProvider, Mapper mapper) {
+        super(mapper, reflectionProvider);
+    }
+
+    public boolean canConvert(Class clazz) {
+        return ConfigurationData.class.isAssignableFrom(clazz);
+    }
+
+    public void marshal(Object object, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) {
+        ConfigurationData configurationData = (ConfigurationData) object;
+        writer.addAttribute("configuration-data-version", "1.1");
+        super.marshal(configurationData, writer, marshallingContext);
+    }
+}

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/XStreamConfigurationMarshaler.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/XStreamConfigurationMarshaler.java?rev=392168&r1=392167&r2=392168&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/XStreamConfigurationMarshaler.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/XStreamConfigurationMarshaler.java Thu Apr  6 20:15:26 2006
@@ -40,6 +40,11 @@
             (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 0) & 0xFF)
     };
 
+    public XStreamConfigurationMarshaler() {
+        // create an xstream just to assuer all the required libraries are present
+        XStreamUtil.createXStream();
+    }
+
     public ConfigurationData readConfigurationData(InputStream in) throws IOException, ClassNotFoundException {
         PushbackInputStream pushbackInputStream = new PushbackInputStream(in, 2);
         byte[] streamHeader = new byte[2];

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/XStreamUtil.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/XStreamUtil.java?rev=392168&r1=392167&r2=392168&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/XStreamUtil.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/config/xstream/XStreamUtil.java Thu Apr  6 20:15:26 2006
@@ -19,12 +19,15 @@
 import java.net.URI;
 
 import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
+import com.thoughtworks.xstream.core.JVM;
 import org.apache.geronimo.kernel.config.ConfigurationData;
 import org.apache.geronimo.kernel.config.ConfigurationModuleType;
 import org.apache.geronimo.kernel.repository.Artifact;
 import org.apache.geronimo.kernel.repository.Dependency;
 import org.apache.geronimo.kernel.repository.Version;
 import org.apache.geronimo.kernel.repository.ImportType;
+import org.apache.geronimo.kernel.management.StateManageable;
 import org.apache.geronimo.gbean.AbstractName;
 import org.apache.geronimo.gbean.GBeanData;
 import org.apache.geronimo.gbean.GBeanInfo;
@@ -38,8 +41,9 @@
     }
 
     public static XStream createXStream() {
-        XStream xstream = new XStream();
-        xstream.alias("configurationData", ConfigurationData.class);
+        JVM jvm = new JVM();
+        ReflectionProvider reflectionProvider = jvm.bestReflectionProvider();
+        XStream xstream = new XStream(reflectionProvider);
 
         // AbstractName
         xstream.alias("abstractName", AbstractName.class);
@@ -55,6 +59,10 @@
         xstream.alias("artifact", Artifact.class);
         xstream.addImmutableType(Artifact.class);
 
+        // ConfigurationData
+        xstream.alias("configurationData", ConfigurationData.class);
+        xstream.registerConverter(new ConfigurationDataConverter(reflectionProvider, xstream.getClassMapper()));
+
         // ConfigurationModuleTypeConverter
         xstream.alias("moduleType", ConfigurationModuleType.class);
         xstream.addImmutableType(ConfigurationModuleType.class);
@@ -93,5 +101,4 @@
 
         return xstream;
     }
-
 }

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java?rev=392168&r1=392167&r2=392168&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java Thu Apr  6 20:15:26 2006
@@ -32,7 +32,6 @@
     private final String artifactId;
     private final Version version;
     private final String type;
-    private final boolean resolved;
 
     public Artifact(String groupId, String artifactId, String version, String type) {
         this(groupId, artifactId, version == null? null: new Version(version), type);
@@ -44,7 +43,6 @@
         this.artifactId = artifactId;
         this.version = version;
         this.type = type;
-        this.resolved = groupId != null && artifactId != null && version != null && type != null;
     }
 
     public static Artifact create(String id) {
@@ -72,7 +70,7 @@
     }
 
     public boolean isResolved() {
-        return resolved;
+        return groupId != null && artifactId != null && version != null && type != null;
     }
 
     public int compareTo(Object object) {

Modified: geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/config/ConfigurationUtilTest.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/config/ConfigurationUtilTest.java?rev=392168&r1=392167&r2=392168&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/config/ConfigurationUtilTest.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/config/ConfigurationUtilTest.java Thu Apr  6 20:15:26 2006
@@ -19,6 +19,7 @@
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.util.Collections;
 import java.util.List;
 
 import junit.framework.TestCase;
@@ -28,10 +29,10 @@
 import org.apache.geronimo.kernel.Jsr77Naming;
 import org.apache.geronimo.kernel.MockGBean;
 import org.apache.geronimo.kernel.config.xstream.XStreamConfigurationMarshaler;
+import org.apache.geronimo.kernel.config.xstream.XStreamGBeanState;
 import org.apache.geronimo.kernel.repository.Artifact;
 import org.apache.geronimo.kernel.repository.Environment;
 import org.apache.geronimo.kernel.repository.ImportType;
-import org.apache.geronimo.kernel.repository.Version;
 
 /**
  * @version $Rev$ $Date$
@@ -41,7 +42,11 @@
     private SerializedConfigurationMarshaler serializedConfigurationMarshaler = new SerializedConfigurationMarshaler();
     private ConfigurationData configurationData1;
     private ConfigurationData configurationData2;
-//    private ConfigurationData configurationData3;
+
+//    public void testPrint() throws Exception {
+//        xstreamConfigurationMarshaler.writeConfigurationData(configurationData1, System.out);
+//
+//    }
 
     public void test() throws Exception {
         copyTest(configurationData1);
@@ -80,12 +85,7 @@
         Jsr77Naming naming = new Jsr77Naming();
 
         Artifact artifact1 = new Artifact("test", "1", "1.1", "bar");
-        Artifact artifact2 = new Artifact("test", "2", "2.2", "bar");
-
-        Environment e1 = new Environment();
-        e1.setConfigId(artifact1);
-        configurationData1 = new ConfigurationData(e1, naming);
-        configurationData1 = new ConfigurationData(new Artifact("test", "test", "", "car"), naming);
+        configurationData1 = new ConfigurationData(artifact1, naming, new XStreamGBeanState(Collections.EMPTY_SET));
 
         GBeanData mockBean1 = configurationData1.addGBean("MyMockGMBean1", MockGBean.getGBeanInfo());
         AbstractName gbeanName1 = mockBean1.getAbstractName();
@@ -94,7 +94,6 @@
         mockBean1.setAttribute("finalInt", new Integer(1));
 
         GBeanData mockBean2 = configurationData1.addGBean("MyMockGMBean2", MockGBean.getGBeanInfo());
-//        AbstractName gbeanName2 = mockBean2.getAbstractName();
         mockBean2.setAttribute("value", "5678");
         mockBean2.setAttribute("name", "Parent");
         mockBean2.setAttribute("finalInt", new Integer(3));
@@ -104,13 +103,15 @@
 
 
         Environment e2 = new Environment();
+        Artifact artifact2 = new Artifact("test", "2", "2.2", "bar");
         e2.setConfigId(artifact2);
-        e2.addDependency(new Artifact("test", "1", (Version) null, "bar"), ImportType.ALL);
-        configurationData2 = new ConfigurationData(e2, naming);
+        e2.addDependency(artifact1, ImportType.ALL);
+        configurationData2 = new ConfigurationData(e2, naming, new XStreamGBeanState(Collections.EMPTY_SET));
 
-//        Environment e3 = new Environment();
-//        e3.setConfigId(artifact3);
-//        e3.addDependency(new Artifact("test", "2", (Version) null, "bar"), ImportType.ALL);
-//        configurationData3 = new ConfigurationData(e3, kernel.getNaming());
+        Artifact artifact3 = new Artifact("test", "3", "3.3", "bar");
+        ConfigurationData configurationData3 = new ConfigurationData(artifact3, naming, new XStreamGBeanState(Collections.EMPTY_SET));
+        configurationData1.addChildConfiguration(configurationData3);
+        GBeanData childConfigurationGBean = configurationData3.addGBean("ChildConfigurationGBean", MockGBean.getGBeanInfo());
+        childConfigurationGBean.setAttribute("name", "foo");
     }
 }