You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2008/01/25 20:06:46 UTC
svn commit: r615307 - in /felix/sandbox/clement/ipojo: ant/
core/src/main/java/org/apache/felix/ipojo/
core/src/main/java/org/apache/felix/ipojo/handlers/configuration/
core/src/main/java/org/apache/felix/ipojo/handlers/dependency/
core/src/main/java/o...
Author: clement
Date: Fri Jan 25 11:06:44 2008
New Revision: 615307
URL: http://svn.apache.org/viewvc?rev=615307&view=rev
Log:
Remove the class checking after recompilation
Add comments
Modify the way to get the BundleContext to support OSGi R4.0 and KF.
Modified:
felix/sandbox/clement/ipojo/ant/pom.xml
felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/Extender.java
felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurableProperty.java
felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyCallback.java
felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/ServiceUsage.java
felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java
felix/sandbox/clement/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java
felix/sandbox/clement/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java
Modified: felix/sandbox/clement/ipojo/ant/pom.xml
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/ant/pom.xml?rev=615307&r1=615306&r2=615307&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/ant/pom.xml (original)
+++ felix/sandbox/clement/ipojo/ant/pom.xml Fri Jan 25 11:06:44 2008
@@ -15,12 +15,12 @@
<dependency>
<groupId>${pom.groupId}</groupId>
<artifactId>org.apache.felix.ipojo.metadata</artifactId>
- <version>0.7.6-SNAPSHOT</version>
+ <version>${pom.version}</version>
</dependency>
<dependency>
<groupId>${pom.groupId}</groupId>
<artifactId>org.apache.felix.ipojo.manipulator</artifactId>
- <version>0.7.6-SNAPSHOT</version>
+ <version>${pom.version}</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
@@ -53,6 +53,7 @@
org.apache.felix.ipojo.manipulator,
org.apache.felix.ipojo.xml.parser,
org.apache.felix.ipojo.manipulation*,
+ org.objectweb.asm.util;-split-package:=merge-first,
org.objectweb.asm;-split-package:=merge-first,
org.objectweb.asm.commons;-split-package:=merge-first,
org.apache.xerces.parsers,
Modified: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/Extender.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/Extender.java?rev=615307&r1=615306&r2=615307&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/Extender.java (original)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/Extender.java Fri Jan 25 11:06:44 2008
@@ -20,7 +20,9 @@
import java.io.IOException;
import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
@@ -50,46 +52,44 @@
* iPOJO Component Type and Instance declaration header.
*/
private static final String IPOJO_HEADER = "iPOJO-Components";
-
+
/**
* iPOJO Extension declaration header.
*/
private static final String IPOJO_EXTENSION = "IPOJO-Extension";
-
+
/**
* iPOJO Bundle Context.
*/
private BundleContext m_context;
-
+
/**
* Declared instance manager.
*/
private InstanceCreator m_creator;
-
+
/**
* iPOJO Bundle.
*/
private Bundle m_bundle;
-
+
/**
* List of factory types.
*/
private List m_abstractFactoryTypes = new ArrayList();
-
+
/**
* List of unbound types.
*/
private List m_unboundTypes = new ArrayList();
-
+
/**
* Bundle Listener Notification.
* @param event : the bundle event.
* @see org.osgi.framework.BundleListener#bundleChanged(org.osgi.framework.BundleEvent)
*/
public synchronized void bundleChanged(BundleEvent event) {
- if (event.getBundle() == m_bundle) {
- return;
- }
+ if (event.getBundle() == m_bundle) { return; }
switch (event.getType()) {
case BundleEvent.STARTED:
@@ -98,7 +98,7 @@
case BundleEvent.STOPPING:
closeManagementFor(event.getBundle());
break;
- default:
+ default:
break;
}
@@ -112,10 +112,10 @@
List toRemove = new ArrayList();
for (int k = 0; k < m_abstractFactoryTypes.size(); k++) {
ManagedAbstractFactoryType mft = (ManagedAbstractFactoryType) m_abstractFactoryTypes.get(k);
-
+
// Delete instances declared in the leaving bundle.
m_creator.removeInstancesFromBundle(bundle.getBundleId());
-
+
// Look for component type created from this bundle.
if (mft.m_createdFactories != null) {
List cfs = (List) mft.m_createdFactories.remove(bundle);
@@ -125,7 +125,7 @@
factory.stop();
}
}
-
+
// If the leaving bundle has declared mft : destroy all created factories.
if (mft.m_bundle == bundle) {
if (mft.m_createdFactories != null) {
@@ -143,7 +143,7 @@
toRemove.add(mft);
}
}
-
+
for (int i = 0; i < toRemove.size(); i++) {
ManagedAbstractFactoryType mft = (ManagedAbstractFactoryType) toRemove.get(i);
System.out.println("[iPOJO-Core] The factory type available: " + mft.m_type + " is no more available");
@@ -166,7 +166,7 @@
if (typeHeader != null) {
parseAbstractFactoryType(bundle, typeHeader);
}
-
+
// Check bundle
String header = (String) dict.get(IPOJO_HEADER);
if (header != null) {
@@ -179,7 +179,7 @@
}
}
}
-
+
/**
* Parse an IPOJO-Extension manifest header.
* @param bundle : bundle containing the header.
@@ -200,7 +200,7 @@
ManagedAbstractFactoryType mft = new ManagedAbstractFactoryType(clazz, type, bundle);
m_abstractFactoryTypes.add(mft);
System.out.println("[iPOJO-Core] New factory type available: " + type);
-
+
for (int j = m_unboundTypes.size() - 1; j >= 0; j--) {
UnboundComponentType unbound = (UnboundComponentType) m_unboundTypes.get(j);
if (unbound.m_type.equals(type)) {
@@ -210,7 +210,7 @@
}
}
}
-
+
/**
* Parse the internal metadata (from the manifest (in the iPOJO-Components property)).
* @param bundle : the owner bundle.
@@ -221,10 +221,12 @@
private void parse(Bundle bundle, String components) throws IOException, ParseException {
ManifestMetadataParser parser = new ManifestMetadataParser();
parser.parseHeader(components);
-
+
Element[] componentsMetadata = parser.getComponentsMetadata(); // Get the component type declaration
- for (int i = 0; i < componentsMetadata.length; i++) { createAbstractFactory(bundle, componentsMetadata[i]); }
-
+ for (int i = 0; i < componentsMetadata.length; i++) {
+ createAbstractFactory(bundle, componentsMetadata[i]);
+ }
+
Dictionary[] instances = parser.getInstances();
for (int i = 0; instances != null && i < instances.length; i++) {
m_creator.addInstance(instances[i], bundle.getBundleId());
@@ -265,7 +267,7 @@
*/
public void stop(BundleContext bc) throws Exception {
m_context.removeBundleListener(this);
-
+
for (int k = 0; k < m_abstractFactoryTypes.size(); k++) {
ManagedAbstractFactoryType mft = (ManagedAbstractFactoryType) m_abstractFactoryTypes.get(k);
@@ -282,11 +284,11 @@
}
}
}
-
+
m_abstractFactoryTypes = null;
m_creator = null;
}
-
+
/**
* Add a component factory to the factory list.
* @param cm : the new component metadata.
@@ -294,6 +296,7 @@
*/
private void createAbstractFactory(Bundle bundle, Element cm) {
ManagedAbstractFactoryType factoryType = null;
+ // First, look for factory-type (component, handler, composite ...)
for (int i = 0; factoryType == null && i < m_abstractFactoryTypes.size(); i++) {
ManagedAbstractFactoryType ft = (ManagedAbstractFactoryType) m_abstractFactoryTypes.get(i);
if (ft.m_type.equals(cm.getName())) {
@@ -301,17 +304,21 @@
}
}
+ // If not found, return. It will wait for a new component type factory.
if (factoryType == null) {
err("Type of component not yet recognized : " + cm.getName(), null);
m_unboundTypes.add(new UnboundComponentType(cm.getName(), cm, bundle));
return;
}
+ // Once found, we invoke the AbstractFactory constructor to create the component factory.
Class clazz = factoryType.m_clazz;
try {
- Constructor cst = clazz.getConstructor(new Class[] {BundleContext.class, Element.class});
- IPojoFactory factory = (IPojoFactory) cst.newInstance(new Object[] { bundle.getBundleContext(), cm});
+ // Look for the constructor, and invoke it.
+ Constructor cst = clazz.getConstructor(new Class[] { BundleContext.class, Element.class });
+ IPojoFactory factory = (IPojoFactory) cst.newInstance(new Object[] { getBundleContext(bundle), cm });
+ // Add the created factory in the m_createdFactories map.
if (factoryType.m_createdFactories == null) {
factoryType.m_createdFactories = new HashMap();
List list = new ArrayList();
@@ -327,9 +334,10 @@
list.add(factory);
}
}
-
- // Start the factory.
+
+ // Start the created factory.
factory.start();
+ // Then add the factory to the instance creator.
m_creator.addFactory(factory);
} catch (SecurityException e) {
@@ -344,9 +352,9 @@
err("Cannot instantiate an abstract factory from " + clazz.getName(), e);
} catch (InvocationTargetException e) {
err("Cannot instantiate an abstract factory from " + clazz.getName(), e.getTargetException());
- }
+ }
}
-
+
/**
* Log an error message in a log service (if available) and display the message in the console.
* @param message : the message to log
@@ -365,7 +373,7 @@
System.err.println("[iPOJO-Core] " + message);
}
}
-
+
/**
* String.split method.
* @param str : string to split.
@@ -398,7 +406,7 @@
String[] arr = (String[]) list.toArray(new String[list.size()]);
return arr;
}
-
+
/**
* Structure storing an iPOJO extension.
*/
@@ -407,19 +415,22 @@
* TYpe (i.e.) name of the extension.
*/
private String m_type;
+
/**
* Abstract Factory class.
*/
private Class m_clazz;
+
/**
* Bundle object containing the declaration of the extension.
*/
private Bundle m_bundle;
+
/**
* Factories created by this extension.
*/
private Map m_createdFactories;
-
+
/**
* Constructor.
* @param factory : abstract factory class.
@@ -432,7 +443,7 @@
m_type = type;
}
}
-
+
/**
* Structure storing unbound component type declaration.
* Unbound means that there is no extension able to manage it.
@@ -442,16 +453,17 @@
* Component type description.
*/
private Element m_description;
+
/**
* Bundle declaring this type.
*/
private Bundle m_bundle;
-
+
/**
* Required extension name.
*/
private String m_type;
-
+
/**
* Constructor.
* @param description : description of the component type.
@@ -461,9 +473,73 @@
private UnboundComponentType(String type, Element description, Bundle bundle) {
m_type = type;
m_description = description;
- m_bundle = bundle;
+ m_bundle = bundle;
}
}
+ /**
+ * Compute the bundle context from the bundle class by introspection.
+ * @param bundle : bundle.
+ * @return the bundle context object or null if not found.
+ */
+ public BundleContext getBundleContext(final Bundle bundle) {
+ if (bundle == null) { return null; }
+
+ // getBundleContext (OSGi 4.1)
+ Method meth = null;
+ try {
+ meth = bundle.getClass().getMethod("getBundleContext", new Class[0]);
+ } catch (SecurityException e) {
+ // Nothing do to, will try the Equinox method
+ } catch (NoSuchMethodException e) {
+ // Nothing do to, will try the Equinox method
+ }
+
+ // try Equinox getContext if not found.
+ if (meth == null) {
+ try {
+ meth = bundle.getClass().getMethod("getContext", new Class[0]);
+ } catch (SecurityException e) {
+ // Nothing do to, will try field inspection
+ } catch (NoSuchMethodException e) {
+ // Nothing do to, will try field inspection
+ }
+ }
+
+ if (meth != null) {
+ meth.setAccessible(true);
+ try {
+ return (BundleContext) meth.invoke(bundle, new Object[0]);
+ } catch (IllegalArgumentException e) {
+ err("Cannot get the BundleContext by invoking " + meth.getName(), e);
+ return null;
+ } catch (IllegalAccessException e) {
+ err("Cannot get the BundleContext by invoking " + meth.getName(), e);
+ return null;
+ } catch (InvocationTargetException e) {
+ err("Cannot get the BundleContext by invoking " + meth.getName(), e);
+ return null;
+ }
+ }
+
+ // Else : Field inspection (KF and Prosyst)
+ Field[] fields = bundle.getClass().getDeclaredFields();
+ for (int i = 0; i < fields.length; i++) {
+ if (BundleContext.class.isAssignableFrom(fields[i].getType())) {
+ fields[i].setAccessible(true);
+ try {
+ return (BundleContext) fields[i].get(bundle);
+ } catch (IllegalArgumentException e) {
+ err("Cannot get the BundleContext by invoking " + meth.getName(), e);
+ return null;
+ } catch (IllegalAccessException e) {
+ err("Cannot get the BundleContext by invoking " + meth.getName(), e);
+ return null;
+ }
+ }
+ }
+ err("Cannot find the BundleContext for " + bundle.getSymbolicName(), null);
+ return null;
+ }
}
Modified: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurableProperty.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurableProperty.java?rev=615307&r1=615306&r2=615307&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurableProperty.java (original)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurableProperty.java Fri Jan 25 11:06:44 2008
@@ -197,7 +197,6 @@
Class c = m_handler.getInstanceManager().getContext().getBundle().loadClass(internalType);
Object[] ob = (Object[]) Array.newInstance(c, 0);
m_type = ob.getClass();
- return;
} catch (ClassNotFoundException e) {
throw new ConfigurationException("Class not found exception in setValue on " + internalType);
} catch (SecurityException e) {
Modified: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyCallback.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyCallback.java?rev=615307&r1=615306&r2=615307&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyCallback.java (original)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyCallback.java Fri Jan 25 11:06:44 2008
@@ -116,6 +116,8 @@
}
if (ok) {
m_methodObj = methods[i]; // It is the looked method.
+ m_methodObj.setAccessible(true);
+ return;
}
}
@@ -123,51 +125,61 @@
}
}
- if (m_methodObj == null) { //look at parent classes
- Method[] methods = m_dependency.getHandler().getInstanceManager().getClazz().getMethods();
- for (int i = 0; m_methodObj == null && i < methods.length; i++) {
- // First check the method name
- if (methods[i].getName().equals(m_method)) {
- // Check arguments
- Class[] clazzes = methods[i].getParameterTypes();
- switch(clazzes.length) {
- case 0 :
- // Callback with no arguments.
- m_methodObj = methods[i];
- m_argument = new String[0];
- break;
- case 1 :
- if (clazzes[0].getName().equals(ServiceReference.class.getName())) {
- // Callback with a service reference.
- m_methodObj = methods[i];
- m_argument = new String[] {ServiceReference.class.getName()};
- break;
- }
- if (clazzes[0].getName().equals(m_dependency.getSpecification())) {
- // Callback with the service object.
- m_methodObj = methods[i];
- m_argument = new String[] {m_dependency.getSpecification()};
- break;
- }
- case 2 :
- if (clazzes[0].getName().equals(m_dependency.getSpecification()) && clazzes[1].getName().equals(ServiceReference.class.getName())) {
- // Callback with two arguments.
- m_methodObj = methods[i];
- m_argument = new String[] {m_dependency.getSpecification(), ServiceReference.class.getName()};
- }
- break;
- default :
- break;
- }
- }
- }
- }
+ // Not found => Try parent method.
+ searchParentMethod();
if (m_methodObj == null) {
+ // If not found, stop the instance (fatal error)
m_dependency.getHandler().error("The method " + m_method + " cannot be called : method not found");
m_dependency.getHandler().getInstanceManager().stop();
} else {
m_methodObj.setAccessible(true);
+ }
+ }
+
+ /**
+ * Introspaect parent class to find the method.
+ */
+ private void searchParentMethod() {
+ // look at parent classes
+ Method[] methods = m_dependency.getHandler().getInstanceManager().getClazz().getMethods();
+ for (int i = 0; m_methodObj == null && i < methods.length; i++) {
+ // First check the method name
+ if (methods[i].getName().equals(m_method)) {
+ // Check arguments
+ Class[] clazzes = methods[i].getParameterTypes();
+ switch (clazzes.length) {
+ case 0:
+ // Callback with no arguments.
+ m_methodObj = methods[i];
+ m_argument = new String[0];
+ return;
+ case 1:
+ if (clazzes[0].getName().equals(ServiceReference.class.getName())) {
+ // Callback with a service reference.
+ m_methodObj = methods[i];
+ m_argument = new String[] { ServiceReference.class.getName() };
+ return;
+ }
+ if (clazzes[0].getName().equals(m_dependency.getSpecification())) {
+ // Callback with the service object.
+ m_methodObj = methods[i];
+ m_argument = new String[] { m_dependency.getSpecification() };
+ return;
+ }
+ break;
+ case 2:
+ if (clazzes[0].getName().equals(m_dependency.getSpecification()) && clazzes[1].getName().equals(ServiceReference.class.getName())) {
+ // Callback with two arguments.
+ m_methodObj = methods[i];
+ m_argument = new String[] { m_dependency.getSpecification(), ServiceReference.class.getName() };
+ return;
+ }
+ break;
+ default:
+ break;
+ }
+ }
}
}
Modified: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/ServiceUsage.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/ServiceUsage.java?rev=615307&r1=615306&r2=615307&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/ServiceUsage.java (original)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/ServiceUsage.java Fri Jan 25 11:06:44 2008
@@ -75,6 +75,11 @@
return use.m_objects;
}
+ /**
+ * Add a service usage.
+ * @param o : object used
+ * @param ref : service reference
+ */
public void addUsage(Object o, ServiceReference ref) {
Usage use = (Usage) get();
use.m_objects.add(o);
@@ -82,13 +87,9 @@
set(use);
}
- public void removeUsage(Object o, ServiceReference ref) {
- Usage use = (Usage) get();
- use.m_objects.remove(o);
- use.m_refs.remove(ref);
- set(use);
- }
-
+ /**
+ * Delete all service usage.
+ */
public void clearUsage() {
Usage use = (Usage) get();
use.m_objects.clear();
Modified: felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java?rev=615307&r1=615306&r2=615307&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java (original)
+++ felix/sandbox/clement/ipojo/core/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java Fri Jan 25 11:06:44 2008
@@ -218,7 +218,7 @@
}
/**
- * Paser the given string.
+ * Parse the given string.
*
* @param s : the string to parse
*/
Modified: felix/sandbox/clement/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java?rev=615307&r1=615306&r2=615307&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java (original)
+++ felix/sandbox/clement/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java Fri Jan 25 11:06:44 2008
@@ -160,8 +160,8 @@
InputStream is2 = new ByteArrayInputStream(origin);
ClassReader cr0 = new ClassReader(is2);
ClassWriter cw0 = new ClassWriter(ClassWriter.COMPUTE_MAXS);
- CheckClassAdapter ch = new CheckClassAdapter(cw0);
- MethodCreator preprocess = new MethodCreator(ch, m_fields);
+ //CheckClassAdapter ch = new CheckClassAdapter(cw0);
+ MethodCreator preprocess = new MethodCreator(cw0, m_fields);
cr0.accept(preprocess, ClassReader.SKIP_FRAMES);
is2.close();
finalWriter = cw0;
Modified: felix/sandbox/clement/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java?rev=615307&r1=615306&r2=615307&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java (original)
+++ felix/sandbox/clement/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCreator.java Fri Jan 25 11:06:44 2008
@@ -68,7 +68,7 @@
Type[] args = Type.getArgumentTypes(desc);
if (args.length == 0) {
generateEmptyConstructor();
- } else {
+ } else if (args.length == 1 && args[0].getClassName().equals("org.osgi.framework.BundleContext")) {
generateBCConstructor();
}