Fix a bug if the property has no field.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@581223 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java b/ipojo/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
index 527d195..f4fa940 100644
--- a/ipojo/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
+++ b/ipojo/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
@@ -19,6 +19,7 @@
package org.apache.felix.ipojo;
import java.net.URL;
+import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Dictionary;
@@ -112,6 +113,11 @@
* Component Type Name.
*/
protected String m_typeName = null;
+
+ /**
+ * Class loader to delegate loading.
+ */
+ private FactoryClassloader m_classLoader = null;
/**
* Component Implementation class.
@@ -297,6 +303,21 @@
instance.start();
return instance;
}
+
+ /**
+ * Define a class.
+ * @param name : qualified name of the class
+ * @param b : byte array of the class
+ * @param domain : protection domain of the class
+ * @return the defined class object
+ * @throws Exception : an exception occur during the definition
+ */
+ public Class defineClass(String name, byte[] b, ProtectionDomain domain) throws Exception {
+ if (m_classLoader == null) {
+ m_classLoader = new FactoryClassloader();
+ }
+ return m_classLoader.defineClass(name, b, domain);
+ }
/**
* Delete an instance.
@@ -420,18 +441,17 @@
* @throws ClassNotFoundException : happen when the class is not found
*/
public Class loadClass(String className) throws ClassNotFoundException {
- if (m_clazz != null) {
- // Used the factory classloader to load the component implementation class
- ClassLoader cl = new ClassLoader() {
- public Class loadClass(String name) throws ClassNotFoundException {
- if (name.equals(m_componentClassName)) {
- return defineClass(name, m_clazz, 0, m_clazz.length, null);
- } else {
- return m_context.getBundle().loadClass(name);
- }
- }
- };
- return cl.loadClass(m_componentClassName);
+ if (m_clazz != null && className.equals(m_componentClassName)) {
+ // Used the factory classloader to load the component implementation
+ // class
+ if (m_classLoader == null) {
+ m_classLoader = new FactoryClassloader();
+ }
+ try {
+ return m_classLoader.defineClass(m_componentClassName, m_clazz, null);
+ } catch (Exception e) {
+ throw new ClassNotFoundException("[Bundle " + m_context.getBundle().getBundleId() + "] Cannot define the class : " + className, e);
+ }
}
return m_context.getBundle().loadClass(className);
}
@@ -566,6 +586,7 @@
m_tracker = null;
m_componentDesc = null;
+ m_classLoader = null;
m_clazz = null;
m_state = INVALID;
}
@@ -985,21 +1006,55 @@
}
}
-// /**
-// * FactoryClassloader.
-// */
-// private class FactoryClassloader extends ClassLoader {
-// /**
-// * 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);
-// }
-// }
+ /**
+ * FactoryClassloader.
+ */
+ private class FactoryClassloader extends ClassLoader {
+
+ /**
+ * Map of defined classes [Name, Class Object].
+ */
+ private Map m_definedClasses = new HashMap();
+
+ /**
+ * 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 {
+ if (m_definedClasses.containsKey(name)) {
+ return (Class) m_definedClasses.get(name);
+ }
+ final Class c = super.defineClass(name, b, 0, b.length, domain);
+ m_definedClasses.put(name, c);
+ return c;
+ }
+
+ /**
+ * Return the URL of the asked resource.
+ *
+ * @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_context.getBundle().getResource(arg);
+ }
+
+ /**
+ * 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 Class loadClass(final String name, final boolean resolve) throws ClassNotFoundException {
+ return m_context.getBundle().loadClass(name);
+ }
+ }
}
diff --git a/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java b/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
index 1e4fd21..629cc7c 100644
--- a/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
+++ b/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
@@ -337,7 +337,7 @@
public Object getterCallback(String fieldName, Object value) {
// Check if the field is a configurable property
for (int i = 0; i < m_configurableProperties.length; i++) {
- if (m_configurableProperties[i].getField().equals(fieldName)) { return m_configurableProperties[i].getValue(); }
+ if (m_configurableProperties[i].hasField() && m_configurableProperties[i].getField().equals(fieldName)) { return m_configurableProperties[i].getValue(); }
}
return value;
}
diff --git a/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java b/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
index 95e58d7..926b3ec 100644
--- a/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
+++ b/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
@@ -339,24 +339,15 @@
// dep.getMetadata().getServiceSpecification().split("[.]");
// String className = "org/apache/felix/ipojo/" + segment[segment.length
// - 1] + "Nullable";
- final String className = dep.getSpecification() + "Nullable";
+ String className = dep.getSpecification() + "Nullable";
String resource = dep.getSpecification().replace('.', '/') + ".class";
URL url = getInstanceManager().getContext().getBundle().getResource(resource);
try {
- final byte[] b = NullableObjectWriter.dump(url, dep.getSpecification());
+ byte[] b = NullableObjectWriter.dump(url, dep.getSpecification());
Class c = null;
try {
- ClassLoader cl = new ClassLoader() {
- public Class loadClass(String name) throws ClassNotFoundException {
- if (name.equals(className)) {
- return defineClass(name, b, 0, b.length, null);
- } else {
- return getInstanceManager().getContext().getBundle().loadClass(name);
- }
- }
- };
- c = cl.loadClass(className);
+ c = getInstanceManager().getFactory().defineClass(className, b, null);
} catch (Exception e) {
log(Logger.ERROR, "Cannot define the nullable class : " + e.getMessage());
e.printStackTrace();