You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2006/07/20 12:26:08 UTC

svn commit: r423869 - in /incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo: Activator.java ComponentManager.java ComponentManagerFactory.java

Author: rickhall
Date: Thu Jul 20 03:26:08 2006
New Revision: 423869

URL: http://svn.apache.org/viewvc?rev=423869&view=rev
Log:
Applied patch (FELIX-95).

Modified:
    incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/Activator.java
    incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/ComponentManager.java
    incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/ComponentManagerFactory.java

Modified: incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/Activator.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/Activator.java?rev=423869&r1=423868&r2=423869&view=diff
==============================================================================
--- incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/Activator.java (original)
+++ incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/Activator.java Thu Jul 20 03:26:08 2006
@@ -80,7 +80,7 @@
      */
     public void addComponentFactory(Element cm) {
     	// Create the factory :
-    	ComponentManagerFactory factory = new ComponentManagerFactory(this, cm);
+    	ComponentManagerFactory factory = new ComponentManagerFactory(m_bundleContext, cm);
 
     	// If the factory array is not empty add the new factory at the end
         if (m_factories.length != 0) {

Modified: incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/ComponentManager.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/ComponentManager.java?rev=423869&r1=423868&r2=423869&view=diff
==============================================================================
--- incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/ComponentManager.java (original)
+++ incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/ComponentManager.java Thu Jul 20 03:26:08 2006
@@ -132,7 +132,6 @@
 				// It is not an internal handler, try to load it
 				try {
 					Class c = m_context.getBundle().loadClass(cm.getNamespaces()[i]);
-					//Class c = Class.forName(cm.getNamespaces()[i]);
 					Handler h = (Handler) c.newInstance();
 					h.configure(this, cm);
 				} catch (ClassNotFoundException e) {
@@ -241,8 +240,8 @@
 	 */
 	private void load() {
         try {
-        	m_clazz = m_factory.getBundleContext().getBundle().loadClass(m_metadata.getClassName());
-        } catch (Exception  e) {
+        	m_clazz = m_factory.loadClass(m_metadata.getClassName());
+        } catch (ClassNotFoundException  e) {
             Activator.getLogger().log(Level.SEVERE, "[" + m_metadata.getClassName() + "] Class not found during the loading phase : " + e.getMessage());
             return;
         }

Modified: incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/ComponentManagerFactory.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/ComponentManagerFactory.java?rev=423869&r1=423868&r2=423869&view=diff
==============================================================================
--- incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/ComponentManagerFactory.java (original)
+++ incubator/felix/trunk/org.apache.felix.ipojo/src/main/java/org/apache/felix/ipojo/ComponentManagerFactory.java Thu Jul 20 03:26:08 2006
@@ -16,6 +16,10 @@
  */
 package org.apache.felix.ipojo;
 
+import java.io.IOException;
+import java.net.URL;
+import java.security.ProtectionDomain;
+import java.util.Enumeration;
 import java.util.logging.Level;
 
 import org.apache.felix.ipojo.metadata.Element;
@@ -38,8 +42,77 @@
 	 */
 	private BundleContext m_bundleContext = null;
 
+	/**
+	 * Component class.
+	 */
+	private byte[] m_clazz = null;
+
+	/**
+	 * Component Class Name.
+	 */
+	private String m_componentClassName = null;
+
+	/**
+	 * Classloader to delegate loading.
+	 */
+	private FactoryClassloader m_classLoader = null;
+
 	//End field
 
+	/**
+	 * FactoryClassloader.
+	 */
+	private class FactoryClassloader extends ClassLoader {
+
+	    /**
+	     * load the class.
+	     * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
+	     * @param name : the name of the class
+	     * @param resolve : should be the class resolve now ?
+	     * @return : the loaded class
+	     * @throws ClassNotFoundException : the class to load is not found
+	     */
+	    protected synchronized Class loadClass(final String name,
+	            final boolean resolve) throws ClassNotFoundException {
+	       return m_bundleContext.getBundle().loadClass(name);
+	    }
+
+
+	    /**
+	     * Return the URL of the asked ressource.
+	     * @param arg : the name of the resource to find.
+	     * @return the URL of the resource.
+	     * @see java.lang.ClassLoader#getResource(java.lang.String)
+	     */
+	    public URL getResource(String arg) {
+	        return m_bundleContext.getBundle().getResource(arg);
+	    }
+
+	    /**
+	     * .
+	     * @param arg : resource to find
+	     * @return : the enumeration found
+	     * @throws IOException : if the lookup failed.
+	     * @see java.lang.ClassLoader#getResources(java.lang.String)
+	     */
+	    public Enumeration getRessources(String arg) throws IOException {
+	        return m_bundleContext.getBundle().getResources(arg);
+	    }
+
+	    /**
+	     * The defineClass method.
+	     * @param name : name of the class
+	     * @param b : the byte array of the class
+	     * @param domain : the protection domain
+	     * @return : the defined class.
+	     * @throws Exception : if a problem is detected during the loading
+	     */
+	    public Class defineClass(String name, byte[] b,
+	            ProtectionDomain domain) throws Exception {
+	    	return super.defineClass(name, b, 0, b.length, domain);
+	    }
+	}
+
 	// Field accessors
 
 	 /**
@@ -93,22 +166,26 @@
 	// End field accessors
 
 	/**
-	 * Constructor of a ComponentManagerFactory from a component metadata.
-	 * This contructor is use when the iPOJO Activator is used.
-	 * @param cm : Component Metadata for the component factory
+	 * Create a component manager factory and create a component manager with the given medatada.
+	 * @param bc : bundle context
+	 * @param cm : metadata of the component to create
 	 */
-	protected ComponentManagerFactory(Activator activator, Element cm) {
-		m_bundleContext = activator.getBundleContext();
+	public ComponentManagerFactory(BundleContext bc, Element cm) {
+		m_bundleContext = bc;
 		createComponentManager(cm);
+		m_componentClassName = cm.getAttribute("className");
 	}
 
 	/**
 	 * Create a component manager factory and create a component manager with the given medatada.
 	 * @param bc : bundle context
-	 * @param cm : metadata of the component to create
+	 * @param clazz : the component class
+	 * @param cm : metadata of the component
 	 */
-	public ComponentManagerFactory(BundleContext bc, Element cm) {
+	public ComponentManagerFactory(BundleContext bc, byte[] clazz, Element cm) {
 		m_bundleContext = bc;
+		m_clazz = clazz;
+		m_componentClassName = cm.getAttribute("className");
 		createComponentManager(cm);
 	}
 
@@ -154,6 +231,40 @@
 			ComponentManager cm = m_componentManagers[i];
 			cm.start();
 		}
+	}
+
+	/**
+	 * Load a class.
+	 * @param className : name of the class to load
+	 * @return the resulting Class object
+	 * @throws ClassNotFoundException : happen when the class is not found
+	 */
+	public Class loadClass(String className) throws ClassNotFoundException {
+		Activator.getLogger().log(Level.INFO, "[Bundle " + m_bundleContext.getBundle().getBundleId() + "] In load for : " + className);
+		if (m_clazz != null && className.equals(m_componentClassName)) {
+			if (m_classLoader == null) {
+				Activator.getLogger().log(Level.INFO, "[Bundle " + m_bundleContext.getBundle().getBundleId() + "] Create the FactoryClassLoader for : " + className);
+				m_classLoader = new FactoryClassloader();
+				}
+			try {
+				Class c = m_classLoader.defineClass(m_componentClassName, m_clazz, null);
+				Activator.getLogger().log(Level.INFO, "[Bundle " + m_bundleContext.getBundle().getBundleId() + "] Return " + c + " for " + className);
+				return c;
+			} catch (Exception e) {
+				Activator.getLogger().log(Level.SEVERE, "[Bundle " + m_bundleContext.getBundle().getBundleId() + "] Cannot define the class : " + className);
+				return null;
+			}
+		}
+		return m_bundleContext.getBundle().loadClass(className);
+	}
+
+	/**
+	 * Return the URL of a resource.
+	 * @param resName : resource name
+	 * @return the URL of the resource
+	 */
+	public URL getResource(String resName) {
+		return m_bundleContext.getBundle().getResource(resName);
 	}
 
 }