David,

This patch assumes that the entire gbean data can be loaded either via
classSource bundle or configuration bundle. But that's not necessarily
true. For example, web module builder setups a gbean for the web app
context. That gbean has classSource set to tomcat or jetty
configuration. One of the attributes of that gbean is a Map object
that represents the jndi context for the application. During
deployment different naming builders put objects into that map. These
naming builders have totally separate classloaders (from web module
builder) so the objects within the Map will be of different
classloaders. And that means that during deserialization we can't read
in the entire Map object using just the jetty or tomcat configuration.

My proposed patch is attached. My solution does serialize bundle
symbolic names and use PackageAdmin to resolve them but it would be
very easy to switch it to another method of resolving the symbolic
names.

Also, maybe this entire classSource problem goes away if we can use a
ClassLoader that delegates to a bunch of Bundles.

Jarek

On Wed, Jan 20, 2010 at 5:44 PM, David Jencks <[email protected]> wrote:
>
> On Jan 20, 2010, at 11:38 AM, Jarek Gawor wrote:
>
>> Hey,
>>
>> For last few days I've been looking into getting the rfc66 extender
>> going in Geronimo but I ran into a problem. As previously mentioned on
>> this list, the idea for the extender was to call the Tomcat/Jetty
>> ModuleBuilders with a bundle as an input and once the deployment
>> process was done start the generated configuration. All without
>> creating any additional temporary or permanent bundles.
>>
>> Because we don't have a single classloader that can load all the
>> gbeans within the configuration, David Jencks added a special
>> "classSource" attribute to GBeanData which can be used to figure out
>> the right Bundle to load the gbean class. Now, since we use Java
>> serialization to save and load gbeans, we actually need to set the
>> right classloader when we deserialize the gbean. That is, during
>> deserialization as soon as we read the "classSource" we need to lookup
>> and set the right classloader and then read the rest of the gbean
>> data. This should (and seems to) work when all the attributes of the
>> gbean are accessible from the same classloader. But what about if the
>> gbean has some attribute with some values from different classloaders?
>> For example, the Map object we build for jndi context can contain
>> objects from different classloaders. I'm not exactly sure what to do
>> about it. Although maybe having a custom ObjectOutputStream with
>> annotateClass() method that saves "classSource" type of info for each
>> unique class might work. Ideas?
>>
>> Also, I'm dealing with lots of classloader issues since there is no
>> single classloader that load all the gbean classes and module classes.
>> A lot of Geronimo and other code assumes a single classloader and
>> resolving these problems is time consuming and not very fun (although
>> probably good in long term). So I'm wondering if we can still somehow
>> assemble a single classloader in the extender. For example, the
>> http://www.osgi.org/blog/2008/08/classy-solutions-to-tricky-proxies.html
>> post shows a classloader that delegates a number of different bundles.
>> Maybe we could use that in Geronimo especially since we can figure out
>> the bundles needed from the configuration environment information.
>>
>> Thoughts?
>>
>> Jarek
>
> I think that we are either going to load the gbean from the classSource
> bundle or from the configuration's bundle.  Maybe sometimes we need to mix
> them... not sure.  Anyway I think we can modify the ObjectInputStreamExt to
> look in the classSource's bundle or in the bundle as in the attached patch.
>  I chatted with Jarek a bit on irc and I think he is thinking of writing the
> bundle symbolic name into the output stream and using package admin to look
> it up again.  I'm a little worried with this approach that we may be mixing
> too much osgi into geronimo.  If we had a blueprint namespace handler I
> think we'd be able to load the classes in the namespace handler rather than
> needing to rely on something that is really like requireBundle.  So, my
> approach is somewhat similar but uses geronimo techniques rather than osgi
> techniques.  I think this might be appropriate since we are in fact loading
> gbeans.
>
> my patch compiles but I haven't tried it yet.
>
> thoughts?
>
> david jencks
>
> Index:
> src/test/java/org/apache/geronimo/kernel/mock/MockConfigurationManager.java
> ===================================================================
> ---
> src/test/java/org/apache/geronimo/kernel/mock/MockConfigurationManager.java
> (revision 901109)
> +++
> src/test/java/org/apache/geronimo/kernel/mock/MockConfigurationManager.java
> (working copy)
> @@ -122,7 +122,7 @@
>     public LifecycleResults loadConfiguration(ConfigurationData
> configurationData, LifecycleMonitor monitor) throws NoSuchConfigException,
> LifecycleException {
>         try {
>             Artifact configId =
> configurationData.getEnvironment().getConfigId();
> -            Configuration configuration = new
> Configuration(configurationData, new DependencyNode(configId, null, null),
> null, null, null);
> +            Configuration configuration = new
> Configuration(configurationData, new DependencyNode(configId, null, null),
> null, null, null, null);
>             configurations.put(configId, configuration);
>         } catch (InvalidConfigException e) {
>
> Index:
> src/test/java/org/apache/geronimo/kernel/config/ConfigurationUtilTest.java
> ===================================================================
> ---
> src/test/java/org/apache/geronimo/kernel/config/ConfigurationUtilTest.java
>  (revision 901109)
> +++
> src/test/java/org/apache/geronimo/kernel/config/ConfigurationUtilTest.java
>  (working copy)
> @@ -74,10 +74,10 @@
>
>     private void assertEquals(ConfigurationData data, ConfigurationData
> configurationData) throws InvalidConfigException {
>         List gbeans;
> -        gbeans = data.getGBeans(bundleContext.getBundle());
> +        gbeans = data.getGBeans(bundleContext.getBundle(), null);
>         assertEquals(configurationData.getId(), data.getId());
>         ConfigurationData data3 = (ConfigurationData)
> data.getChildConfigurations().get("testmodule");
> -        gbeans = data3.getGBeans(bundleContext.getBundle());
> +        gbeans = data3.getGBeans(bundleContext.getBundle(), null);
>         assertEquals(new QName("namespaceURI", "localPart"),
> ((GBeanData)gbeans.get(0)).getAttribute("someObject"));
>     }
>
> Index: src/main/java/org/apache/geronimo/kernel/ObjectInputStreamExt.java
> ===================================================================
> --- src/main/java/org/apache/geronimo/kernel/ObjectInputStreamExt.java
>  (revision 901109)
> +++ src/main/java/org/apache/geronimo/kernel/ObjectInputStreamExt.java
>  (working copy)
> @@ -22,6 +22,9 @@
>  import java.io.ObjectStreamClass;
>  import java.lang.reflect.Proxy;
>
> +import org.apache.geronimo.kernel.config.Configuration;
> +import org.apache.geronimo.kernel.config.InvalidConfigException;
> +import org.apache.geronimo.kernel.repository.Artifact;
>  import org.osgi.framework.Bundle;
>
>  /**
> @@ -29,19 +32,41 @@
>  */
>  public class ObjectInputStreamExt extends ObjectInputStream {
>
> -    private Bundle bundle;
> +    private final Bundle bundle;
> +    private final Kernel kernel;
> +    private Bundle sourceBundle;
>
> -    public ObjectInputStreamExt(InputStream in, Bundle bundle) throws
> IOException {
> +    public ObjectInputStreamExt(InputStream in, Bundle bundle, Kernel
> kernel) throws IOException {
>         super(in);
>         this.bundle = bundle;
> +        this.kernel = kernel;
>     }
>
> +    public void setClassSource(Artifact classSource) throws
> ClassNotFoundException {
> +        if (classSource != null) {
> +            try {
> +                Configuration configuration = (Configuration)
> kernel.getGBean(Configuration.getConfigurationAbstractName(classSource));
> +                sourceBundle = configuration.getBundle();
> +            } catch (GBeanNotFoundException e) {
> +                throw new ClassNotFoundException("Could not locate
> Configuration to deserialize", e);
> +            } catch (InvalidConfigException e) {
> +                throw new ClassNotFoundException("Could not identify
> Configuration to deserialize", e);
> +            }
> +        } else {
> +            sourceBundle = null;
> +        }
> +    }
> +
>     protected Class resolveClass(ObjectStreamClass classDesc) throws
> IOException, ClassNotFoundException {
> +        if (sourceBundle != null) {
> +            return ClassLoading.loadClass(classDesc.getName(), bundle);
> +        }
>         return ClassLoading.loadClass(classDesc.getName(), bundle);
>     }
>
>  //see
> http://www.osgi.org/blog/2008/08/classy-solutions-to-tricky-proxies.html
>     //currently broken
> +
>     protected Class resolveProxyClass(String[] interfaces) throws
> IOException, ClassNotFoundException {
>         Class[] cinterfaces = new Class[interfaces.length];
>         for (int i = 0; i < interfaces.length; i++)
> Index:
> src/main/java/org/apache/geronimo/kernel/config/ConfigurationData.java
> ===================================================================
> --- src/main/java/org/apache/geronimo/kernel/config/ConfigurationData.java
>    (revision 901109)
> +++ src/main/java/org/apache/geronimo/kernel/config/ConfigurationData.java
>    (working copy)
> @@ -28,6 +28,7 @@
>
>  import org.apache.geronimo.gbean.GBeanData;
>  import org.apache.geronimo.gbean.GBeanInfo;
> +import org.apache.geronimo.kernel.Kernel;
>  import org.apache.geronimo.kernel.Naming;
>  import org.apache.geronimo.kernel.repository.Artifact;
>  import org.apache.geronimo.kernel.repository.Environment;
> @@ -179,9 +180,9 @@
>         return environment.getManifest();
>     }
>
> -    public List<GBeanData> getGBeans(Bundle bundle) throws
> InvalidConfigException {
> +    public List<GBeanData> getGBeans(Bundle bundle, Kernel kernel) throws
> InvalidConfigException {
>         if (bundle == null) throw new NullPointerException("bundle is
> null");
> -        List<GBeanData> gbeans = gbeanState.getGBeans(bundle);
> +        List<GBeanData> gbeans = gbeanState.getGBeans(bundle, kernel);
>         if (null == configurationDataTransformer) {
>             return gbeans;
>         }
> Index:
> src/main/java/org/apache/geronimo/kernel/config/xstream/XStreamGBeanState.java
> ===================================================================
> ---
> src/main/java/org/apache/geronimo/kernel/config/xstream/XStreamGBeanState.java
>      (revision 901109)
> +++
> src/main/java/org/apache/geronimo/kernel/config/xstream/XStreamGBeanState.java
>      (working copy)
> @@ -32,6 +32,7 @@
>  import org.apache.geronimo.gbean.AbstractName;
>  import org.apache.geronimo.gbean.GBeanData;
>  import org.apache.geronimo.gbean.GBeanInfo;
> +import org.apache.geronimo.kernel.Kernel;
>  import org.apache.geronimo.kernel.Naming;
>  import org.apache.geronimo.kernel.util.XmlUtil;
>  import org.apache.geronimo.kernel.config.InvalidConfigException;
> @@ -73,7 +74,7 @@
>         return gbeanState;
>     }
>
> -    public List<GBeanData> getGBeans(Bundle bundle) throws
> InvalidConfigException {
> +    public List<GBeanData> getGBeans(Bundle bundle, Kernel kernel) throws
> InvalidConfigException {
>         if (gbeanState == null) {
>             return Collections.unmodifiableList(gbeans);
>         }
> Index: src/main/java/org/apache/geronimo/kernel/config/Configuration.java
> ===================================================================
> --- src/main/java/org/apache/geronimo/kernel/config/Configuration.java
>  (revision 901109)
> +++ src/main/java/org/apache/geronimo/kernel/config/Configuration.java
>  (working copy)
> @@ -38,8 +38,11 @@
>  import org.apache.geronimo.gbean.ReferencePatterns;
>  import org.apache.geronimo.gbean.annotation.GBean;
>  import org.apache.geronimo.gbean.annotation.ParamAttribute;
> +import org.apache.geronimo.gbean.annotation.ParamSpecial;
> +import org.apache.geronimo.gbean.annotation.SpecialAttributeType;
>  import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
>  import org.apache.geronimo.kernel.GBeanNotFoundException;
> +import org.apache.geronimo.kernel.Kernel;
>  import org.apache.geronimo.kernel.Naming;
>  import org.apache.geronimo.kernel.repository.Artifact;
>  import org.apache.geronimo.kernel.repository.Environment;
> @@ -86,6 +89,7 @@
>      * Converts an Artifact to an AbstractName for a configuration.  Does
> not
>      * validate that this is a reasonable or resolved Artifact, or that it
>      * corresponds to an actual Configuration.
> +     *
>      * @param configId id for configuration
>      * @return abstract name constructed from supplied id
>      * @throws InvalidConfigException if the ObjectName could not be
> constructed
> @@ -167,23 +171,24 @@
>
>     /**
>      * Creates a configuration.
> +     * <p/>
> +     * //     * @param classLoaderHolder Classloaders for this
> configuration
>      *
> -//     * @param classLoaderHolder Classloaders for this configuration
> -     * @param configurationData the module type, environment and classpath
> of the configuration
> -     * @param dependencyNode Class and Service parent ids
> -     * @param allServiceParents ordered list of transitive closure of
> service parents for gbean searches
> -     * @param attributeStore Customization info for gbeans
> +     * @param configurationData     the module type, environment and
> classpath of the configuration
> +     * @param dependencyNode        Class and Service parent ids
> +     * @param allServiceParents     ordered list of transitive closure of
> service parents for gbean searches
> +     * @param attributeStore        Customization info for gbeans
>      * @param configurationResolver (there should be a better way) Where
> this configuration is actually located in file system
> +     * @param kernel
>      * @throws InvalidConfigException if this configuration turns out to
> have a problem.
>      */
>     public Configuration(
> -//           �...@paramattribute(name = "classLoaderHolder") 
> ClassLoaderHolder
> classLoaderHolder,
>             @ParamAttribute(name = "configurationData") ConfigurationData
> configurationData,
>             @ParamAttribute(name = "dependencyNode") DependencyNode
> dependencyNode,
>             @ParamAttribute(name = "allServiceParents") List<Configuration>
> allServiceParents,
>             @ParamAttribute(name = "attributeStore")
> ManageableAttributeStore attributeStore,
> -           �...@paramattribute(name = "configurationResolver")
> ConfigurationResolver configurationResolver) throws InvalidConfigException {
> -//        if (classLoaderHolder == null) throw new
> NullPointerException("classLoaders are null");
> +           �...@paramattribute(name = "configurationResolver")
> ConfigurationResolver configurationResolver,
> +           �...@paramspecial(type = SpecialAttributeType.kernel) Kernel
> kernel) throws InvalidConfigException {
>         if (configurationData == null) throw new
> NullPointerException("configurationData is null");
>
>  //        this.classLoaderHolder = classLoaderHolder;
> @@ -199,7 +204,7 @@
>
>             // Deserialize the GBeans in the configurationData
>             Bundle bundle =
> configurationData.getBundleContext().getBundle();
> -            Collection<GBeanData> gbeans =
> configurationData.getGBeans(bundle);
> +            Collection<GBeanData> gbeans =
> configurationData.getGBeans(bundle, kernel);
>             if (attributeStore != null) {
>                 gbeans =
> attributeStore.applyOverrides(dependencyNode.getId(), gbeans, bundle);
>             }
> @@ -221,6 +226,7 @@
>
>     /**
>      * Add a contained configuration, such as for a war inside an ear
> +     *
>      * @param child contained configuration
>      */
>     void addChild(Configuration child) {
> @@ -311,6 +317,7 @@
>
>     /**
>      * Provide a way to locate where this configuration is for web apps and
> persistence units
> +     *
>      * @return the ConfigurationResolver for this configuration
>      */
>     public ConfigurationResolver getConfigurationResolver() {
> Index: src/main/java/org/apache/geronimo/kernel/config/GBeanState.java
> ===================================================================
> --- src/main/java/org/apache/geronimo/kernel/config/GBeanState.java
> (revision 901109)
> +++ src/main/java/org/apache/geronimo/kernel/config/GBeanState.java
> (working copy)
> @@ -20,6 +20,7 @@
>
>  import org.apache.geronimo.gbean.GBeanData;
>  import org.apache.geronimo.gbean.GBeanInfo;
> +import org.apache.geronimo.kernel.Kernel;
>  import org.apache.geronimo.kernel.Naming;
>  import org.apache.geronimo.kernel.repository.Environment;
>  import org.osgi.framework.Bundle;
> @@ -28,7 +29,7 @@
>  * @version $Rev$ $Date$
>  */
>  public interface GBeanState {
> -    List<GBeanData> getGBeans(Bundle bundle) throws InvalidConfigException;
> +    List<GBeanData> getGBeans(Bundle bundle, Kernel kernel) throws
> InvalidConfigException;
>
>     void addGBean(GBeanData gbeanData);
>
> Index:
> src/main/java/org/apache/geronimo/kernel/config/SerializedGBeanState.java
> ===================================================================
> ---
> src/main/java/org/apache/geronimo/kernel/config/SerializedGBeanState.java
> (revision 901109)
> +++
> src/main/java/org/apache/geronimo/kernel/config/SerializedGBeanState.java
> (working copy)
> @@ -31,6 +31,7 @@
>  import org.apache.geronimo.gbean.AbstractName;
>  import org.apache.geronimo.gbean.GBeanData;
>  import org.apache.geronimo.gbean.GBeanInfo;
> +import org.apache.geronimo.kernel.Kernel;
>  import org.apache.geronimo.kernel.Naming;
>  import org.apache.geronimo.kernel.ObjectInputStreamExt;
>  import org.apache.geronimo.kernel.repository.Environment;
> @@ -58,11 +59,11 @@
>         }
>     }
>
> -    public List<GBeanData> getGBeans(Bundle bundle) throws
> InvalidConfigException {
> +    public List<GBeanData> getGBeans(Bundle bundle, Kernel kernel) throws
> InvalidConfigException {
>         if (gbeanState == null) {
>             return Collections.unmodifiableList(gbeans);
>         }
> -        gbeans.addAll(loadGBeans(gbeanState, bundle));
> +        gbeans.addAll(loadGBeans(gbeanState, bundle, kernel));
>         return Collections.unmodifiableList(gbeans);
>     }
>
> @@ -110,7 +111,7 @@
>         stream.defaultWriteObject();
>     }
>
> -    private static List<GBeanData> loadGBeans(byte[] gbeanState, Bundle
> bundle) throws InvalidConfigException {
> +    private static List<GBeanData> loadGBeans(byte[] gbeanState, Bundle
> bundle, Kernel kernel) throws InvalidConfigException {
>         List<GBeanData> gbeans = new ArrayList<GBeanData>();
>         if (gbeanState != null && gbeanState.length > 0) {
>             // Set the thread context classloader so deserializing classes
> can grab the cl from the thread
> @@ -118,7 +119,7 @@
>             try {
>  //
>  Thread.currentThread().setContextClassLoader(classLoader);
>
> -                ObjectInputStream ois = new ObjectInputStreamExt(new
> ByteArrayInputStream(gbeanState), bundle);
> +                ObjectInputStream ois = new ObjectInputStreamExt(new
> ByteArrayInputStream(gbeanState), bundle, kernel);
>                 try {
>                     while (true) {
>                         GBeanData gbeanData = new GBeanData();
> Index:
> src/main/java/org/apache/geronimo/kernel/config/SimpleConfigurationManager.java
> ===================================================================
> ---
> src/main/java/org/apache/geronimo/kernel/config/SimpleConfigurationManager.java
>     (revision 901109)
> +++
> src/main/java/org/apache/geronimo/kernel/config/SimpleConfigurationManager.java
>     (working copy)
> @@ -424,7 +424,7 @@
>
>         List<Configuration> allServiceParents = buildAllServiceParents(null,
> dependencyNode);
>
> -        Configuration configuration = new Configuration(configurationData,
> dependencyNode, allServiceParents, null, configurationResolver);
> +        Configuration configuration = new Configuration(configurationData,
> dependencyNode, allServiceParents, null, configurationResolver, null);
>         configuration.doStart();
>         //TODO why???
>         resolvedParentIds.add(configuration.getId());
> @@ -1478,7 +1478,8 @@
>      */
>     private void applyOverrides(Configuration configuration) throws
> InvalidConfigException {
>         Bundle bundle = configuration.getBundle();
> -        Collection<GBeanData> gbeans =
> configuration.getConfigurationData().getGBeans(bundle);
> +        //TODO OSGI we might need a kernel here?
> +        Collection<GBeanData> gbeans =
> configuration.getConfigurationData().getGBeans(bundle, null);
>         if (configuration.getManageableAttributeStore() != null) {
>
> configuration.getManageableAttributeStore().applyOverrides(configuration.getId(),
> gbeans,
>                     bundle);
> Index: src/main/java/org/apache/geronimo/gbean/GBeanData.java
> ===================================================================
> --- src/main/java/org/apache/geronimo/gbean/GBeanData.java      (revision
> 901109)
> +++ src/main/java/org/apache/geronimo/gbean/GBeanData.java      (working
> copy)
> @@ -28,6 +28,7 @@
>  import java.util.Set;
>
>  import org.apache.geronimo.gbean.annotation.EncryptionSetting;
> +import org.apache.geronimo.kernel.ObjectInputStreamExt;
>  import org.apache.geronimo.kernel.repository.Artifact;
>
>  /**
> @@ -419,6 +420,9 @@
>
>         protected void readClassSource(ObjectInput in) throws
> ClassNotFoundException, IOException {
>             classSource = (Artifact) in.readObject();
> +            if (in instanceof ObjectInputStreamExt) {
> +                ((ObjectInputStreamExt)in).setClassSource(classSource);
> +            }
>         }
>     }
>
>
>
Index: src/main/java/org/apache/geronimo/kernel/ObjectInputStreamExt.java
===================================================================
--- src/main/java/org/apache/geronimo/kernel/ObjectInputStreamExt.java	(revision 900661)
+++ src/main/java/org/apache/geronimo/kernel/ObjectInputStreamExt.java	(working copy)
@@ -23,6 +23,8 @@
 import java.lang.reflect.Proxy;
 
 import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.packageadmin.PackageAdmin;
 
 /**
  * @version $Rev$ $Date$
@@ -30,18 +32,36 @@
 public class ObjectInputStreamExt extends ObjectInputStream {
 
     private Bundle bundle;
+    private ServiceReference reference;
+    private PackageAdmin packageAdmin;
 
     public ObjectInputStreamExt(InputStream in, Bundle bundle) throws IOException {
         super(in);
         this.bundle = bundle;
+        
+        this.reference = bundle.getBundleContext().getServiceReference(PackageAdmin.class.getName());
+        this.packageAdmin = (PackageAdmin) bundle.getBundleContext().getService(reference);
     }
 
+    @Override
     protected Class resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
-        return ClassLoading.loadClass(classDesc.getName(), bundle);
+        String bundleId = (String) readObject();
+        Bundle classBundle = bundle;        
+        if (bundleId != null) {
+            String[] parts = bundleId.split(":");
+            Bundle[] bundles = packageAdmin.getBundles(parts[0], parts[1]);
+            if (bundles == null || bundles.length == 0) {
+                classBundle = bundle;
+            } else {
+                classBundle = bundles[0];
+            }
+        }
+        return ClassLoading.loadClass(classDesc.getName(), classBundle);
     }
 
 //see http://www.osgi.org/blog/2008/08/classy-solutions-to-tricky-proxies.html
     //currently broken
+    @Override
     protected Class resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
         Class[] cinterfaces = new Class[interfaces.length];
         for (int i = 0; i < interfaces.length; i++)
@@ -54,4 +74,9 @@
         }
     }
 
+    @Override
+    public void close() throws IOException {
+        bundle.getBundleContext().ungetService(reference);
+        super.close();
+    }
 }
Index: src/main/java/org/apache/geronimo/kernel/ObjectOutputStreamExt.java
===================================================================
--- src/main/java/org/apache/geronimo/kernel/ObjectOutputStreamExt.java	(revision 0)
+++ src/main/java/org/apache/geronimo/kernel/ObjectOutputStreamExt.java	(revision 0)
@@ -0,0 +1,46 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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;
+
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.FrameworkUtil;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ObjectOutputStreamExt extends ObjectOutputStream {
+
+    public ObjectOutputStreamExt(OutputStream out) throws IOException {
+        super(out);
+    }
+
+    @Override
+    protected void annotateClass(Class<?> cl) throws IOException {
+        Bundle bundle = FrameworkUtil.getBundle(cl);
+        if (bundle == null) {
+            writeObject(null);
+        } else {
+            String bundleId = bundle.getSymbolicName() + ":" + bundle.getVersion().toString();
+            writeObject(bundleId);
+        }
+    }
+      
+}

Property changes on: src/main/java/org/apache/geronimo/kernel/ObjectOutputStreamExt.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + Date Revision
Added: svn:eol-style
   + native

Index: src/main/java/org/apache/geronimo/kernel/config/SerializedGBeanState.java
===================================================================
--- src/main/java/org/apache/geronimo/kernel/config/SerializedGBeanState.java	(revision 900661)
+++ src/main/java/org/apache/geronimo/kernel/config/SerializedGBeanState.java	(working copy)
@@ -33,6 +33,7 @@
 import org.apache.geronimo.gbean.GBeanInfo;
 import org.apache.geronimo.kernel.Naming;
 import org.apache.geronimo.kernel.ObjectInputStreamExt;
+import org.apache.geronimo.kernel.ObjectOutputStreamExt;
 import org.apache.geronimo.kernel.repository.Environment;
 import org.osgi.framework.Bundle;
 
@@ -147,7 +148,7 @@
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ObjectOutputStream oos;
         try {
-            oos = new ObjectOutputStream(baos);
+            oos = new ObjectOutputStreamExt(baos);
         } catch (IOException e) {
             throw (AssertionError) new AssertionError("Unable to initialize ObjectOutputStream").initCause(e);
         }
Index: src/main/java/org/apache/geronimo/gbean/runtime/GBeanAttribute.java
===================================================================
--- src/main/java/org/apache/geronimo/gbean/runtime/GBeanAttribute.java	(revision 900661)
+++ src/main/java/org/apache/geronimo/gbean/runtime/GBeanAttribute.java	(working copy)
@@ -204,7 +204,7 @@
         this.attributeInfo = attributeInfo;
         this.name = attributeInfo.getName();
         try {
-            this.type = ClassLoading.loadClass(attributeInfo.getType(), gbeanInstance.getBundle());
+            this.type = ClassLoading.loadClass(attributeInfo.getType(), gbeanInstance.getClassSourceBundle());
         } catch (ClassNotFoundException e) {
             throw new InvalidConfigurationException("Could not load attribute class: " + attributeInfo.getType(), e);
         }
Index: src/main/java/org/apache/geronimo/gbean/runtime/GBeanInstance.java
===================================================================
--- src/main/java/org/apache/geronimo/gbean/runtime/GBeanInstance.java	(revision 900661)
+++ src/main/java/org/apache/geronimo/gbean/runtime/GBeanInstance.java	(working copy)
@@ -31,8 +31,6 @@
 
 import javax.management.ObjectName;
 
-import org.apache.geronimo.kernel.config.Configuration;
-import org.apache.geronimo.kernel.config.InvalidConfigException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.apache.geronimo.gbean.AbstractName;
@@ -62,6 +60,8 @@
 import org.apache.xbean.recipe.Option;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.packageadmin.PackageAdmin;
 
 /**
  * A GBeanInstance is a J2EE Management Managed Object, and is standard base for Geronimo services.
@@ -213,6 +213,8 @@
     private InstanceRegistry instanceRegistry;
 
     private String stateReason;
+    
+    private final Bundle classSourceBundle;
 
     /**
      * Construct a GBeanMBean using the supplied GBeanData and bundle
@@ -229,24 +231,22 @@
         this.lifecycleBroadcaster = lifecycleBroadcaster;
         this.gbeanInstanceState = new GBeanInstanceState(abstractName, kernel, dependencyManager, this, lifecycleBroadcaster);
         this.bundleContext = bundleContext;
-        this.classLoader = new BundleClassLoader(bundleContext.getBundle());
-
-        GBeanInfo gbeanInfo = gbeanData.getGBeanInfo();
-        Bundle bundle;
+        this.classLoader = new BundleClassLoader(this.bundleContext.getBundle());
+               
         if (gbeanData.getClassSource() == null) {
-            bundle = bundleContext.getBundle();
+            classSourceBundle = bundleContext.getBundle();
         } else {
-            try {
-                Configuration configuration = (Configuration) kernel.getGBean(Configuration.getConfigurationAbstractName(gbeanData.getClassSource()));
-                bundle = configuration.getBundle();
-            } catch (GBeanNotFoundException e) {
-                throw new InvalidConfigurationException("Class loading configuration not found: " + gbeanData.getClassSource(), e);
-            } catch (InvalidConfigException e) {
-                throw new InvalidConfigurationException("Class loading configuration not identified: " + gbeanData.getClassSource(), e);
+            Bundle[] bundles = getLookupClassSource(gbeanData);
+            if (bundles == null || bundles.length == 0) {
+                classSourceBundle = bundleContext.getBundle();
+            } else {
+                classSourceBundle = bundles[0];
             }
         }
+        
+        GBeanInfo gbeanInfo = gbeanData.getGBeanInfo();
         try {
-            type = bundle.loadClass(gbeanInfo.getClassName());
+            type = classSourceBundle.loadClass(gbeanInfo.getClassName());
         } catch (ClassNotFoundException e) {
             throw new InvalidConfigurationException("Could not load GBeanInfo class from classloader: " + bundleContext +
                     " className=" + gbeanInfo.getClassName(), e);
@@ -319,6 +319,17 @@
         }
     }
 
+    private Bundle[] getLookupClassSource(GBeanData data) {
+        ServiceReference reference = bundleContext.getServiceReference(PackageAdmin.class.getName());
+        PackageAdmin packageAdmin = (PackageAdmin) bundleContext.getService(reference);
+        
+        Bundle[] bundles = packageAdmin.getBundles(data.getClassSource().getArtifactId(), null);
+        
+        bundleContext.ungetService(reference);
+        
+        return bundles;
+    }
+    
     protected ObjectRecipe newObjectRecipe(GBeanData gbeanData) {
         GBeanInfo beanInfo = gbeanData.getGBeanInfo();
         List<String> cstrNames = beanInfo.getConstructor().getAttributeNames();
@@ -438,6 +449,10 @@
     public Bundle getBundle() {
         return bundleContext.getBundle();
     }
+    
+    public Bundle getClassSourceBundle() {
+        return classSourceBundle;
+    }
 
     /**
      * Has this gbean instance been destroyed. An destroyed gbean can no longer be used.

Reply via email to