Applied patch (FELIX-222) to allow properties to be reconfigured and
propagated to service registrations via ManagedServiceFactory.
git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@516458 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/Activator.java b/ipojo/src/main/java/org/apache/felix/ipojo/Activator.java
index ea63748..0c41a8f 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/Activator.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/Activator.java
@@ -75,9 +75,7 @@
System.arraycopy(m_factories, 0, newFactory, 0, m_factories.length);
newFactory[m_factories.length] = factory;
m_factories = newFactory;
- }
- // Else create an array of size one with the new Factory
- else { m_factories = new ComponentFactory[] {factory}; }
+ } else { m_factories = new ComponentFactory[] {factory}; } // Else create an array of size one with the new Factory
}
/**
@@ -111,7 +109,7 @@
ComponentFactory factory = m_factories[i];
factory.stop();
}
- if(m_creator != null) { m_creator.stop(); }
+ if (m_creator != null) { m_creator.stop(); }
m_factories = new ComponentFactory[0]; // Release all factories
}
@@ -140,32 +138,32 @@
*/
private void start() {
// Start the factories
- for(int j = 0; j < m_factories.length; j++) { m_factories[j].start(); }
+ for (int j = 0; j < m_factories.length; j++) { m_factories[j].start(); }
Dictionary[] outsiders = new Dictionary[0];
- for(int i = 0; i < m_configurations.length; i++) {
+ for (int i = 0; i < m_configurations.length; i++) {
Dictionary conf = m_configurations[i];
boolean created = false;
- for(int j = 0; j < m_factories.length; j++) {
+ for (int j = 0; j < m_factories.length; j++) {
String componentClass = m_factories[j].getComponentClassName();
String factoryName = m_factories[j].getFactoryName();
- if(conf.get("component") != null && (conf.get("component").equals(componentClass) || conf.get("component").equals(factoryName))) {
+ if (conf.get("component") != null && (conf.get("component").equals(componentClass) || conf.get("component").equals(factoryName))) {
try {
m_factories[j].createComponentInstance(conf);
created = true;
} catch (UnacceptableConfiguration e) {
- System.err.println("Cannot create the instance " + conf.get("name") +" : " + e.getMessage());
+ System.err.println("Cannot create the instance " + conf.get("name") + " : " + e.getMessage());
}
}
}
- if(!created && conf.get("component") != null) {
+ if (!created && conf.get("component") != null) {
if (outsiders.length != 0) {
Dictionary[] newList = new Dictionary[outsiders.length + 1];
System.arraycopy(outsiders, 0, newList, 0, outsiders.length);
newList[outsiders.length] = conf;
outsiders = newList;
- }
- else { outsiders = new Dictionary[] {conf}; }
+ } else { outsiders = new Dictionary[] {conf}; }
+
}
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/ComponentFactory.java b/ipojo/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
index 711fabf..b873c2f 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
@@ -94,7 +94,7 @@
private ComponentDescription m_componentDesc;
/**
- * Logger for the factory (and all component instance)
+ * Logger for the factory (and all component instance).
*/
private Logger m_logger;
@@ -103,7 +103,10 @@
*/
private class FactoryClassloader extends ClassLoader {
- HashMap m_definedClasses = new HashMap();
+ /**
+ * Map of defined classes [Name, Class Object].
+ */
+ private HashMap m_definedClasses = new HashMap();
/**
* load the class.
@@ -150,7 +153,7 @@
*/
public Class defineClass(String name, byte[] b,
ProtectionDomain domain) throws Exception {
- if(m_definedClasses.containsKey(name)) { return (Class) m_definedClasses.get(name); }
+ if (m_definedClasses.containsKey(name)) { return (Class) m_definedClasses.get(name); }
Class c = super.defineClass(name, b, 0, b.length, domain);
m_definedClasses.put(name, c);
return c;
@@ -184,8 +187,9 @@
m_componentMetadata = cm;
// Get factory PID :
- if (m_componentMetadata.containsAttribute("factory") && !m_componentMetadata.getAttribute("factory").equalsIgnoreCase("no")) { m_factoryName = m_componentMetadata.getAttribute("factory"); }
- else { m_factoryName = m_componentMetadata.getAttribute("className"); }
+ if (m_componentMetadata.containsAttribute("factory") && !m_componentMetadata.getAttribute("factory").equalsIgnoreCase("no")) {
+ m_factoryName = m_componentMetadata.getAttribute("factory");
+ } else { m_factoryName = m_componentMetadata.getAttribute("className"); }
m_logger = new Logger(m_context, m_factoryName, Logger.WARNING);
}
@@ -203,8 +207,9 @@
m_componentMetadata = cm;
// Get factory PID :
- if (m_componentMetadata.containsAttribute("factory") && !m_componentMetadata.getAttribute("factory").equalsIgnoreCase("no")) { m_factoryName = m_componentMetadata.getAttribute("factory"); }
- else { m_factoryName = m_componentMetadata.getAttribute("className"); }
+ if (m_componentMetadata.containsAttribute("factory") && !m_componentMetadata.getAttribute("factory").equalsIgnoreCase("no")) {
+ m_factoryName = m_componentMetadata.getAttribute("factory");
+ } else { m_factoryName = m_componentMetadata.getAttribute("className"); }
m_logger = new Logger(m_context, m_factoryName, Logger.WARNING);
}
@@ -217,7 +222,7 @@
Iterator it = col.iterator();
while (it.hasNext()) {
ComponentInstance ci = (ComponentInstance) it.next();
- if(ci.isStarted()) { ci.stop(); }
+ if (ci.isStarted()) { ci.stop(); }
}
m_componentInstances.clear();
if (m_sr != null) { m_sr.unregister(); }
@@ -279,8 +284,16 @@
return m_context.getBundle().loadClass(className);
}
+ /**
+ * 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(); }
+ if (m_classLoader == null) { m_classLoader = new FactoryClassloader(); }
return m_classLoader.defineClass(name, b, domain);
}
@@ -299,9 +312,9 @@
public ComponentInstance createComponentInstance(Dictionary configuration) throws UnacceptableConfiguration {
try {
_isAcceptable(configuration);
- } catch(UnacceptableConfiguration e) {
+ } catch (UnacceptableConfiguration e) {
m_logger.log(Logger.ERROR, "The configuration is not acceptable : " + e.getMessage());
- throw new UnacceptableConfiguration("The configuration " + configuration + " is not acceptable for " + m_factoryName + ": " + e.getMessage() );
+ throw new UnacceptableConfiguration("The configuration " + configuration + " is not acceptable for " + m_factoryName + ": " + e.getMessage());
}
IPojoContext context = new IPojoContext(m_context);
@@ -310,8 +323,9 @@
instance.configure(m_componentMetadata, configuration);
String pid = null;
- if (configuration.get("name") != null) { pid = (String) configuration.get("name"); }
- else { pid = m_componentMetadata.getAttribute("className"); }
+ if (configuration.get("name") != null) {
+ pid = (String) configuration.get("name");
+ } else { pid = m_componentMetadata.getAttribute("className"); }
m_componentInstances.put(pid, instance);
instance.start();
@@ -324,9 +338,9 @@
public ComponentInstance createComponentInstance(Dictionary configuration, ServiceContext serviceContext) throws UnacceptableConfiguration {
try {
_isAcceptable(configuration);
- } catch(UnacceptableConfiguration e) {
+ } catch (UnacceptableConfiguration e) {
m_logger.log(Logger.ERROR, "The configuration is not acceptable : " + e.getMessage());
- throw new UnacceptableConfiguration("The configuration " + configuration + " is not acceptable for " + m_factoryName + ": " + e.getMessage() );
+ throw new UnacceptableConfiguration("The configuration " + configuration + " is not acceptable for " + m_factoryName + ": " + e.getMessage());
}
IPojoContext context = new IPojoContext(m_context, serviceContext);
@@ -335,8 +349,11 @@
instance.configure(m_componentMetadata, configuration);
String pid = null;
- if (configuration.get("name") != null) { pid = (String) configuration.get("name"); }
- else { pid = m_componentMetadata.getAttribute("className"); }
+ if (configuration.get("name") != null) {
+ pid = (String) configuration.get("name");
+ } else {
+ pid = m_componentMetadata.getAttribute("className");
+ }
m_componentInstances.put(pid, instance);
instance.start();
@@ -350,8 +367,9 @@
*/
public void deleted(String pid) {
InstanceManager cm = (InstanceManager) m_componentInstances.remove(pid);
- if (cm == null) { return; } // do nothing, the component does not exist !
- else { cm.stop(); }
+ if (cm == null) {
+ return; // do nothing, the component does not exist !
+ } else { cm.stop(); }
}
/**
@@ -372,10 +390,7 @@
m_logger.log(Logger.ERROR, "The configuration is not acceptable : " + e.getMessage());
throw new ConfigurationException(properties.toString(), e.getMessage());
}
- }
- else {
- cm.stop(); // Stop the component
-
+ } else {
try {
properties.put("name", pid); // Add the name in the configuration
_isAcceptable(properties); // Test if the configuration is acceptable
@@ -383,9 +398,7 @@
m_logger.log(Logger.ERROR, "The configuration is not acceptable : " + e.getMessage());
throw new ConfigurationException(properties.toString(), e.getMessage());
}
-
- cm.configure(m_componentMetadata, properties); // re-configure the component
- cm.start(); // restart it
+ cm.reconfigure(properties); // re-configure the component
}
}
@@ -402,28 +415,47 @@
*/
public boolean isAcceptable(Dictionary conf) {
// First check that the configuration contains a name :
- if(conf.get("name") == null) { return false; }
+ if (conf.get("name") == null) { return false; }
PropertyDescription[] props = m_componentDesc.getProperties();
- for(int i = 0; i < props.length; i++) {
+ for (int i = 0; i < props.length; i++) {
PropertyDescription pd = props[i];
// Failed if the props has no default value and the configuration does not push a value
- if(pd.getValue() == null && conf.get(pd.getName()) == null) {
+ if (pd.getValue() == null && conf.get(pd.getName()) == null) {
return false;
}
}
return true;
}
+ /**
+ * Test is a configuration is acceptable for the factory.
+ * @param conf : the configuration to test.
+ * @throws UnacceptableConfiguration : the configuration is not acceptable.
+ */
private void _isAcceptable(Dictionary conf) throws UnacceptableConfiguration {
- if(conf == null || conf.get("name") == null) { throw new UnacceptableConfiguration("The configuration does not contains the \"name\" property"); }
+ if (conf == null || conf.get("name") == null) { throw new UnacceptableConfiguration("The configuration does not contains the \"name\" property"); }
PropertyDescription[] props = m_componentDesc.getProperties();
- for(int i = 0; i < props.length; i++) {
+ for (int i = 0; i < props.length; i++) {
PropertyDescription pd = props[i];
// Failed if the props has no default value and the configuration does not push a value
- if(pd.getValue() == null && conf.get(pd.getName()) == null) {
+ if (pd.getValue() == null && conf.get(pd.getName()) == null) {
throw new UnacceptableConfiguration("The configuration does not contains the \"" + pd.getName() + "\" property");
}
}
}
+ /**
+ * @see org.apache.felix.ipojo.Factory#reconfigure(java.util.Dictionary)
+ */
+ public void reconfigure(Dictionary properties) throws UnacceptableConfiguration {
+ if (properties == null || properties.get("name") == null) { throw new UnacceptableConfiguration("The configuration does not contains the \"name\" property"); }
+ String name = (String) properties.get("name");
+ InstanceManager cm = (InstanceManager) m_componentInstances.get(name);
+ if (cm == null) {
+ return; // The instance does not exist.
+ } else {
+ _isAcceptable(properties); // Test if the configuration is acceptable
+ }
+ cm.reconfigure(properties); // re-configure the component
+ }
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/ComponentInstance.java b/ipojo/src/main/java/org/apache/felix/ipojo/ComponentInstance.java
index 1861f5d..299c058 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/ComponentInstance.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/ComponentInstance.java
@@ -18,6 +18,8 @@
*/
package org.apache.felix.ipojo;
+import java.util.Dictionary;
+
import org.apache.felix.ipojo.architecture.ComponentDescription;
import org.osgi.framework.BundleContext;
@@ -85,6 +87,14 @@
* @return true if getState returns INVALID or VALID.
*/
boolean isStarted();
+
+ /**
+ * Re-configurare an instance.
+ * Do nothing if the instance does not support dynamic reconfiguration.
+ * The reconfiguration does not stop the instance.
+ * @param configuration : the new configuration.
+ */
+ void reconfigure(Dictionary configuration);
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/DefaultServiceContext.java b/ipojo/src/main/java/org/apache/felix/ipojo/DefaultServiceContext.java
index 4245db5..114c500 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/DefaultServiceContext.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/DefaultServiceContext.java
@@ -26,64 +26,117 @@
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
+/**
+ * Default iPOJO Service Context.
+ * this service context delegate all calls on the bundle context.
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+/**
+ * @author Clement
+ *
+ */
public class DefaultServiceContext implements ServiceContext {
+ /**
+ * The bundle context on which delegate.
+ */
private BundleContext m_context;
+ /**
+ * Instance attached to this service context.
+ */
private ComponentInstance m_instance;
+ /**
+ * Constructor.
+ * @param bc : the bundle context on which delegate.
+ */
public DefaultServiceContext(BundleContext bc) { m_context = bc; }
- public void addServiceListener(ServiceListener listener, String filter)
- throws InvalidSyntaxException {
+ /**
+ * @see org.apache.felix.ipojo.ServiceContext#addServiceListener(org.osgi.framework.ServiceListener, java.lang.String)
+ */
+ public void addServiceListener(ServiceListener listener, String filter) throws InvalidSyntaxException {
m_context.addServiceListener(listener, filter);
}
+ /**
+ * @see org.apache.felix.ipojo.ServiceContext#addServiceListener(org.osgi.framework.ServiceListener)
+ */
public void addServiceListener(ServiceListener listener) {
m_context.addServiceListener(listener);
}
+ /**
+ * @see org.apache.felix.ipojo.ServiceContext#getAllServiceReferences(java.lang.String, java.lang.String)
+ */
public ServiceReference[] getAllServiceReferences(String clazz,
String filter) throws InvalidSyntaxException {
return m_context.getAllServiceReferences(clazz, filter);
}
+ /**
+ * @see org.apache.felix.ipojo.ServiceContext#getService(org.osgi.framework.ServiceReference)
+ */
public Object getService(ServiceReference reference) {
return m_context.getService(reference);
}
+ /**
+ * @see org.apache.felix.ipojo.ServiceContext#getServiceReference(java.lang.String)
+ */
public ServiceReference getServiceReference(String clazz) {
return m_context.getServiceReference(clazz);
}
- public ServiceReference[] getServiceReferences(String clazz, String filter)
- throws InvalidSyntaxException {
+ /**
+ * @see org.apache.felix.ipojo.ServiceContext#getServiceReferences(java.lang.String, java.lang.String)
+ */
+ public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException {
return m_context.getServiceReferences(clazz, filter);
}
+ /**
+ * @see org.apache.felix.ipojo.ServiceContext#registerService(java.lang.String[], java.lang.Object, java.util.Dictionary)
+ */
public ServiceRegistration registerService(String[] clazzes,
Object service, Dictionary properties) {
return m_context.registerService(clazzes, service, properties);
}
+ /**
+ * @see org.apache.felix.ipojo.ServiceContext#registerService(java.lang.String, java.lang.Object, java.util.Dictionary)
+ */
public ServiceRegistration registerService(String clazz, Object service,
Dictionary properties) {
return m_context.registerService(clazz, service, properties);
}
+ /**
+ * @see org.apache.felix.ipojo.ServiceContext#removeServiceListener(org.osgi.framework.ServiceListener)
+ */
public void removeServiceListener(ServiceListener listener) {
m_context.removeServiceListener(listener);
}
+ /**
+ * @see org.apache.felix.ipojo.ServiceContext#ungetService(org.osgi.framework.ServiceReference)
+ */
public boolean ungetService(ServiceReference reference) {
return m_context.ungetService(reference);
}
+ /**
+ * @see org.apache.felix.ipojo.ServiceContext#getComponentInstance()
+ */
public ComponentInstance getComponentInstance() {
return m_instance;
}
+ /**
+ * @see org.apache.felix.ipojo.ServiceContext#setComponentInstance(org.apache.felix.ipojo.ComponentInstance)
+ */
public void setComponentInstance(ComponentInstance ci) { m_instance = ci; }
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/Factory.java b/ipojo/src/main/java/org/apache/felix/ipojo/Factory.java
index 86d006c..141a608 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/Factory.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/Factory.java
@@ -62,5 +62,14 @@
* @return the name of the factory.
*/
String getName();
+
+
+ /**
+ * Reconfigure an instance already created.
+ * This configuration need to have the name property to identify the instance.
+ * @param conf : the configuration to reconfigure the instance.
+ * @throws UnacceptableConfiguration : if the given configuration is not consistent for the tragetted instance.
+ */
+ void reconfigure(Dictionary conf) throws UnacceptableConfiguration;
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/Handler.java b/ipojo/src/main/java/org/apache/felix/ipojo/Handler.java
index 14542c1..54b46b2 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/Handler.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/Handler.java
@@ -34,6 +34,7 @@
* Configure the handler.
* @param im : the instance manager
* @param metadata : the metadata of the component
+ * @param configuration : the instance configuration
*/
public abstract void configure(InstanceManager im, Element metadata, Dictionary configuration);
@@ -84,4 +85,10 @@
* @return the description of the handler..
*/
public HandlerDescription getDescription() { return new HandlerDescription(this.getClass().getName(), isValid()); }
+
+ /**
+ * The instance is reconfiguring.
+ * @param configuration : New instance configuration.
+ */
+ public void reconfigure(Dictionary configuration) { }
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/IPojoContext.java b/ipojo/src/main/java/org/apache/felix/ipojo/IPojoContext.java
index e5dffd3..a654eeb 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/IPojoContext.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/IPojoContext.java
@@ -34,18 +34,18 @@
import org.osgi.framework.ServiceRegistration;
/**
- * The iPOJO Context is a BundleContext implementation allowing the separation between Bundle context and Service (Bundle) Context
+ * The iPOJO Context is a BundleContext implementation allowing the separation between Bundle context and Service (Bundle) Context.
* @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
*/
public class IPojoContext implements BundleContext {
/**
- * BundleContext used to access bundle method
+ * BundleContext used to access bundle method.
*/
private BundleContext m_bundleContext;
/**
- * Service Context used to access service interaction
+ * Service Context used to access service interaction.
*/
private ServiceContext m_serviceContext;
@@ -83,8 +83,9 @@
/**
* @see org.osgi.framework.BundleContext#addServiceListener(org.osgi.framework.ServiceListener, java.lang.String)
*/
- public void addServiceListener(ServiceListener listener, String filter)
- throws InvalidSyntaxException { m_serviceContext.addServiceListener(listener, filter); }
+ public void addServiceListener(ServiceListener listener, String filter) throws InvalidSyntaxException {
+ m_serviceContext.addServiceListener(listener, filter);
+ }
/**
* @see org.osgi.framework.BundleContext#addServiceListener(org.osgi.framework.ServiceListener)
@@ -94,13 +95,16 @@
/**
* @see org.osgi.framework.BundleContext#createFilter(java.lang.String)
*/
- public Filter createFilter(String filter) throws InvalidSyntaxException { return m_bundleContext.createFilter(filter); }
+ public Filter createFilter(String filter) throws InvalidSyntaxException {
+ return m_bundleContext.createFilter(filter);
+ }
/**
* @see org.osgi.framework.BundleContext#getAllServiceReferences(java.lang.String, java.lang.String)
*/
- public ServiceReference[] getAllServiceReferences(String clazz,
- String filter) throws InvalidSyntaxException { return m_serviceContext.getAllServiceReferences(clazz, filter); }
+ public ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException {
+ return m_serviceContext.getAllServiceReferences(clazz, filter);
+ }
/**
* @see org.osgi.framework.BundleContext#getBundle()
@@ -140,8 +144,9 @@
/**
* @see org.osgi.framework.BundleContext#getServiceReferences(java.lang.String, java.lang.String)
*/
- public ServiceReference[] getServiceReferences(String clazz, String filter)
- throws InvalidSyntaxException { return m_serviceContext.getServiceReferences(clazz, filter); }
+ public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException {
+ return m_serviceContext.getServiceReferences(clazz, filter);
+ }
/**
* @see org.osgi.framework.BundleContext#installBundle(java.lang.String)
@@ -151,8 +156,9 @@
/**
* @see org.osgi.framework.BundleContext#installBundle(java.lang.String, java.io.InputStream)
*/
- public Bundle installBundle(String location, InputStream input)
- throws BundleException { return m_bundleContext.installBundle(location, input); }
+ public Bundle installBundle(String location, InputStream input) throws BundleException {
+ return m_bundleContext.installBundle(location, input);
+ }
/**
* @see org.osgi.framework.BundleContext#registerService(java.lang.String[], java.lang.Object, java.util.Dictionary)
@@ -187,10 +193,10 @@
public boolean ungetService(ServiceReference reference) { return m_serviceContext.ungetService(reference); }
/**
- * Set the instance manager to the service context.
- * @param cm : the component manager
+ * Set the instance to the service context.
+ * @param ci : the component instance
*/
- public void setComponentInstance(ComponentInstance ci) {m_serviceContext.setComponentInstance(ci); }
+ public void setComponentInstance(ComponentInstance ci) { m_serviceContext.setComponentInstance(ci); }
/**
* Get the instance manager from the service context.
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/InstanceCreator.java b/ipojo/src/main/java/org/apache/felix/ipojo/InstanceCreator.java
index d6c0c7d..6fa3be8 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/InstanceCreator.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/InstanceCreator.java
@@ -34,8 +34,14 @@
*/
public class InstanceCreator implements ServiceListener {
+ /**
+ * Bundle Context.
+ */
private BundleContext m_context;
+ /**
+ * Logger to log messages if error occurs.
+ */
private Logger m_logger;
/**
@@ -43,11 +49,53 @@
* It stores all necessary information to create an instance and to track the factory.
*/
private class ManagedConfiguration {
- Dictionary configuration;
- String factoryName;
- ComponentInstance instance;
+ /**
+ * Configuration of the instance to create.
+ */
+ private Dictionary m_configuration;
- public ManagedConfiguration(Dictionary conf) { configuration = conf; }
+ /**
+ * Factory name.
+ */
+ private String m_factoryName;
+
+ /**
+ * Created instance.
+ */
+ private ComponentInstance m_instance;
+
+ /**
+ * Constructor.
+ * @param conf : the configuration to create.
+ */
+ ManagedConfiguration(Dictionary conf) { m_configuration = conf; }
+
+ /**
+ * @return the configuration.
+ */
+ Dictionary getConfiguration() { return m_configuration; }
+
+ /**
+ * @return the factory
+ */
+ String getFactory() { return m_factoryName; }
+
+ /**
+ * @return the instance (or null if no instance are created).
+ */
+ ComponentInstance getInstance() { return m_instance; }
+
+ /**
+ * Set the factory name.
+ * @param name : the factory name.
+ */
+ void setFactory(String name) { m_factoryName = name; }
+
+ /**
+ * Set the instance object.
+ * @param instance : the instance
+ */
+ void setInstance(ComponentInstance instance) { m_instance = instance; }
}
@@ -56,26 +104,30 @@
*/
private ManagedConfiguration[] m_configurations;
+ /**
+ * Constructor.
+ * @param context : the bundle context.
+ * @param configurations : configuration set to create and maintain.
+ */
public InstanceCreator(BundleContext context, Dictionary[] configurations) {
m_context = context;
- m_logger = new Logger(context, "InstanceCreator"+context.getBundle().getBundleId(), Logger.WARNING);
+ m_logger = new Logger(context, "InstanceCreator" + context.getBundle().getBundleId(), Logger.WARNING);
m_configurations = new ManagedConfiguration[configurations.length];
- for(int i = 0; i < configurations.length; i++) {
+ for (int i = 0; i < configurations.length; i++) {
ManagedConfiguration conf = new ManagedConfiguration(configurations[i]);
m_configurations[i] = conf;
// Get the component type name :
- String componentType = (String) conf.configuration.get("component");
+ String componentType = (String) conf.getConfiguration().get("component");
Factory fact = null;
try {
String fil = "(|(" + org.osgi.framework.Constants.SERVICE_PID + "=" + componentType + ")(component.class=" + componentType + "))";
ServiceReference[] refs = context.getServiceReferences(org.apache.felix.ipojo.Factory.class.getName(), fil);
- if(refs != null) {
+ if (refs != null) {
fact = (Factory) m_context.getService(refs[0]);
createInstance(fact, conf);
- }
- else {
+ } else {
m_logger.log(Logger.WARNING, "No factory available for the type : " + componentType);
}
} catch (InvalidSyntaxException e) { m_logger.log(Logger.ERROR, "Invalid syntax filter for the type : " + componentType, e); }
@@ -83,28 +135,36 @@
// Register a service listenner on Factory Service
try {
- m_context.addServiceListener(this, "(objectClass="+Factory.class.getName() + ")");
+ m_context.addServiceListener(this, "(objectClass=" + Factory.class.getName() + ")");
} catch (InvalidSyntaxException e) { m_logger.log(Logger.ERROR, "Invalid syntax filter when registering a listener on Factory Service", e); }
}
+ /**
+ * Create an instance using the given factory and the given configuration.
+ * @param fact : the facotry name to used.
+ * @param config : the configuration.
+ */
private void createInstance(Factory fact, ManagedConfiguration config) {
- Dictionary conf = config.configuration;
+ Dictionary conf = config.getConfiguration();
try {
- config.instance = fact.createComponentInstance(conf);
- config.factoryName = fact.getName();
+ config.setInstance(fact.createComponentInstance(conf));
+ config.setFactory(fact.getName());
} catch (UnacceptableConfiguration e) {
m_logger.log(Logger.ERROR, "A factory is available for the configuration but the configuration is not acceptable", e);
}
}
+ /**
+ * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
+ */
public void serviceChanged(ServiceEvent ev) {
ServiceReference ref = ev.getServiceReference();
String factoryName = (String) ref.getProperty(org.osgi.framework.Constants.SERVICE_PID);
String componentClass = (String) ref.getProperty("component.class");
- if(ev.getType() == ServiceEvent.REGISTERED) { //A new factory appears
- for(int i = 0; i < m_configurations.length; i++) {
- if(m_configurations[i].instance == null && (m_configurations[i].configuration.get("component").equals(factoryName) || m_configurations[i].configuration.get("component").equals(componentClass))) {
+ if (ev.getType() == ServiceEvent.REGISTERED) { //A new factory appears
+ for (int i = 0; i < m_configurations.length; i++) {
+ if (m_configurations[i].getInstance() == null && (m_configurations[i].getConfiguration().get("component").equals(factoryName) || m_configurations[i].getConfiguration().get("component").equals(componentClass))) {
Factory fact = (Factory) m_context.getService(ref);
createInstance(fact, m_configurations[i]);
}
@@ -112,27 +172,26 @@
return;
}
- if(ev.getType() == ServiceEvent.UNREGISTERING) {
- for(int i = 0; i < m_configurations.length; i++) {
- if(m_configurations[i].instance != null && m_configurations[i].factoryName.equals(factoryName)) {
- m_configurations[i].instance = null;
- m_configurations[i].factoryName = null;
+ if (ev.getType() == ServiceEvent.UNREGISTERING) {
+ for (int i = 0; i < m_configurations.length; i++) {
+ if (m_configurations[i].getInstance() != null && m_configurations[i].getFactory().equals(factoryName)) {
+ m_configurations[i].setInstance(null);
+ m_configurations[i].setFactory(null);
m_context.ungetService(ref);
}
}
return;
}
-
- //TODO manage modification ? normally a factory should not change its property.
}
/**
- * Stop all created instances
+ * Stop all created instances.
*/
public void stop() {
- for(int i = 0; i < m_configurations.length; i++) {
- if(m_configurations[i].instance != null) { m_configurations[i].instance.stop(); }
- m_configurations[i].instance = null;
+ for (int i = 0; i < m_configurations.length; i++) {
+ if (m_configurations[i].getInstance() != null) { m_configurations[i].getInstance().stop(); }
+ m_configurations[i].setInstance(null);
+ m_configurations[i].setFactory(null);
}
m_configurations = null;
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/InstanceManager.java b/ipojo/src/main/java/org/apache/felix/ipojo/InstanceManager.java
index 45e6038..4fdaf13 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/InstanceManager.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/InstanceManager.java
@@ -90,6 +90,7 @@
/**
* Construct a new Component Manager.
* @param factory : the factory managing the instance manager
+ * @param bc : the bundle context to give to the instance
*/
public InstanceManager(ComponentFactory factory, BundleContext bc) {
m_factory = factory;
@@ -100,7 +101,8 @@
/**
* Configure the instance manager.
* Stop the existings handler, clear the handler list, change the metadata, recreate the handlers
- * @param cm
+ * @param cm : the component type metadata
+ * @param configuration : the configuration of the instance
*/
public void configure(Element cm, Dictionary configuration) {
// Stop all previous registred handler
@@ -150,10 +152,8 @@
} catch (IllegalAccessException e) {
m_factory.getLogger().log(Logger.ERROR, "[" + m_name + "] Cannot instantiate the handler " + cm.getNamespaces()[i] + " : " + e.getMessage());
}
-
}
}
-
}
/**
@@ -161,6 +161,9 @@
*/
public ComponentDescription getComponentDescription() { return m_componentDesc; }
+ /**
+ * @return the instance description.
+ */
public InstanceDescription getInstanceDescription() {
int componentState = getState();
InstanceDescription instanceDescription = new InstanceDescription(m_name, m_className, componentState, getContext().getBundle().getBundleId());
@@ -200,6 +203,9 @@
*/
public String getComponentName() { return m_name; }
+ /**
+ * @return the implementation class name of the instance.
+ */
public String getClassName() { return m_className; }
// ===================== Lifecycle management =====================
@@ -208,7 +214,7 @@
* Start the instance manager.
*/
public void start() {
- if(m_state != STOPPED) { return; } // Instance already started
+ if (m_state != STOPPED) { return; } // Instance already started
// Start all the handlers
m_factory.getLogger().log(Logger.INFO, "[" + m_name + "] Start the instance manager with " + m_handlers.length + " handlers");
@@ -228,7 +234,7 @@
* Stop the instance manager.
*/
public void stop() {
- if(m_state == STOPPED) { return; } // Instance already stopped
+ if (m_state == STOPPED) { return; } // Instance already stopped
setState(INVALID);
// Stop all the handlers
@@ -311,8 +317,7 @@
System.arraycopy(m_pojoObjects, 0, newInstances, 0, m_pojoObjects.length);
newInstances[m_pojoObjects.length] = o;
m_pojoObjects = newInstances;
- }
- else {
+ } else {
m_pojoObjects = new Object[] {o};
}
}
@@ -328,12 +333,14 @@
}
if (idx >= 0) {
- if ((m_pojoObjects.length - 1) == 0) { m_pojoObjects = new Element[0]; }
- else {
+ if ((m_pojoObjects.length - 1) == 0) {
+ m_pojoObjects = new Element[0];
+ } else {
Object[] newInstances = new Object[m_pojoObjects.length - 1];
System.arraycopy(m_pojoObjects, 0, newInstances, 0, idx);
if (idx < newInstances.length) {
- System.arraycopy(m_pojoObjects, idx + 1, newInstances, idx, newInstances.length - idx); }
+ System.arraycopy(m_pojoObjects, idx + 1, newInstances, idx, newInstances.length - idx);
+ }
m_pojoObjects = newInstances;
}
}
@@ -366,8 +373,7 @@
Constructor constructor = m_clazz.getConstructor(new Class[] {InstanceManager.class, BundleContext.class});
constructor.setAccessible(true);
instance = constructor.newInstance(new Object[] {this, m_context});
- }
- catch (NoSuchMethodException e) { }
+ } catch (NoSuchMethodException e) { instance = null; }
// Create an instance if no instance are already created with <init>()BundleContext
if (instance == null) {
@@ -452,10 +458,13 @@
for (int i = 0; i < fields.length; i++) {
if (m_fieldRegistration.get(fields[i]) == null) {
m_fieldRegistration.put(fields[i], new Handler[] {h});
- }
- else {
+ } else {
Handler[] list = (Handler[]) m_fieldRegistration.get(fields[i]);
- for (int j = 0; j < list.length; j++) { if (list[j] == h) { return; } }
+ for (int j = 0; j < list.length; j++) {
+ if (list[j] == h) {
+ return;
+ }
+ }
Handler[] newList = new Handler[list.length + 1];
System.arraycopy(list, 0, newList, 0, list.length);
newList[list.length] = h;
@@ -472,8 +481,9 @@
*/
public void unregister(Handler h, String[] fields) {
for (int i = 0; i < fields.length; i++) {
- if (m_fieldRegistration.get(fields[i]) == null) { break; }
- else {
+ if (m_fieldRegistration.get(fields[i]) == null) {
+ break;
+ } else {
Handler[] list = (Handler[]) m_fieldRegistration.get(fields[i]);
int idx = -1;
for (int j = 0; j < list.length; j++) {
@@ -486,8 +496,7 @@
if (idx >= 0) {
if ((list.length - 1) == 0) {
list = new Handler[0];
- }
- else {
+ } else {
Handler[] newList = new Handler[list.length - 1];
System.arraycopy(list, 0, newList, 0, idx);
if (idx < newList.length) {
@@ -518,8 +527,7 @@
if (idx >= 0) {
if ((m_handlers.length - 1) == 0) {
m_handlers = new Handler[0];
- }
- else {
+ } else {
Handler[] newList = new Handler[m_handlers.length - 1];
System.arraycopy(m_handlers, 0, newList, 0, idx);
if (idx < newList.length) {
@@ -595,8 +603,20 @@
if (isValid && m_state == INVALID) { setState(VALID); }
}
+ /**
+ * @see org.apache.felix.ipojo.ComponentInstance#getInstanceName()
+ */
public String getInstanceName() { return m_name; }
+ /**
+ * @see org.apache.felix.ipojo.ComponentInstance#reconfigure(java.util.Dictionary)
+ */
+ public void reconfigure(Dictionary configuration) {
+ for (int i = 0; i < m_handlers.length; i++) {
+ m_handlers[i].reconfigure(configuration);
+ }
+ }
+
// ======================= end Handlers Management =====================
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/ServiceContext.java b/ipojo/src/main/java/org/apache/felix/ipojo/ServiceContext.java
index e384882..6afbe1b 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/ServiceContext.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/ServiceContext.java
@@ -25,30 +25,89 @@
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
+/**
+ * A service context give the access the a service registry.
+ * All service interaction should use this service context.
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
public interface ServiceContext {
+ /**
+ * @param listener
+ * @param filter
+ * @throws InvalidSyntaxException
+ */
void addServiceListener(ServiceListener listener, String filter) throws InvalidSyntaxException;
+ /**
+ * @param listener
+ */
void addServiceListener(ServiceListener listener);
+ /**
+ * @param clazz
+ * @param filter
+ * @return
+ * @throws InvalidSyntaxException
+ */
ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException;
+ /**
+ * @param reference
+ * @return
+ */
Object getService(ServiceReference reference);
+ /**
+ * @param clazz
+ * @return
+ */
ServiceReference getServiceReference(String clazz);
+ /**
+ * @param clazz
+ * @param filter
+ * @return
+ * @throws InvalidSyntaxException
+ */
ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException;
+ /**
+ * @param clazzes
+ * @param service
+ * @param properties
+ * @return
+ */
ServiceRegistration registerService(String[] clazzes, Object service, Dictionary properties);
+ /**
+ * @param clazz
+ * @param service
+ * @param properties
+ * @return
+ */
ServiceRegistration registerService(String clazz, Object service, Dictionary properties);
+ /**
+ * @param listener
+ */
void removeServiceListener(ServiceListener listener);
+ /**
+ * @param reference
+ * @return
+ */
boolean ungetService(ServiceReference reference);
+ /**
+ * @return the component instance who use this service context.
+ */
ComponentInstance getComponentInstance();
+ /**
+ * Set the component instance using the service context.
+ * @param ci
+ */
void setComponentInstance(ComponentInstance ci);
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/UnacceptableConfiguration.java b/ipojo/src/main/java/org/apache/felix/ipojo/UnacceptableConfiguration.java
index bcda55b..1084b8f 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/UnacceptableConfiguration.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/UnacceptableConfiguration.java
@@ -1,9 +1,19 @@
package org.apache.felix.ipojo;
+/**
+ * UnacceptableConfiguration is throwed when a factory refuses to create an instance.
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
public class UnacceptableConfiguration extends Exception {
+ /**
+ *
+ */
private static final long serialVersionUID = 2998931848886223965L;
+ /**
+ * @param message : message indicating the error.
+ */
public UnacceptableConfiguration(String message) { super(message); }
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/architecture/ComponentDescription.java b/ipojo/src/main/java/org/apache/felix/ipojo/architecture/ComponentDescription.java
index 884c3f1..ab702b2 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/architecture/ComponentDescription.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/architecture/ComponentDescription.java
@@ -75,8 +75,8 @@
* @param pd : the property to add
*/
public void addProperty(PropertyDescription pd) {
- for(int i = 0; i < m_properties.length; i++) {
- if(m_properties[i].getName().equals(pd.getName())) { return; }
+ for (int i = 0; i < m_properties.length; i++) {
+ if (m_properties[i].getName().equals(pd.getName())) { return; }
}
PropertyDescription[] newProps = new PropertyDescription[m_properties.length + 1];
@@ -95,10 +95,10 @@
* @param serviceSpecification : the provided service to add (interface name)
*/
public void addProvidedServiceSpecification(String serviceSpecification) {
- String[] newSs = new String[m_providedServiceSpecification.length + 1];
- System.arraycopy(m_providedServiceSpecification, 0, newSs, 0, m_providedServiceSpecification.length);
- newSs[m_providedServiceSpecification.length] = serviceSpecification;
- m_providedServiceSpecification = newSs;
+ String[] newSs = new String[m_providedServiceSpecification.length + 1];
+ System.arraycopy(m_providedServiceSpecification, 0, newSs, 0, m_providedServiceSpecification.length);
+ newSs[m_providedServiceSpecification.length] = serviceSpecification;
+ m_providedServiceSpecification = newSs;
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/architecture/InstanceDescription.java b/ipojo/src/main/java/org/apache/felix/ipojo/architecture/InstanceDescription.java
index 1604f17..ba0f895 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/architecture/InstanceDescription.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/architecture/InstanceDescription.java
@@ -57,8 +57,10 @@
/**
* Constructor.
- * @param name : the name of the component (the class name).
- * @param state : the state of the component.
+ * @param name : the name of the component instance.
+ * @param state : the state of the instance.
+ * @param className : implementation class name.
+ * @param bundleId : bundle id owning this instance.
*/
public InstanceDescription(String name, String className, int state, long bundleId) {
m_name = name;
@@ -106,10 +108,10 @@
}
}
// The component Description is not in the array, add it
- HandlerDescription[] newHd = new HandlerDescription[m_handlers.length + 1];
- System.arraycopy(m_handlers, 0, newHd, 0, m_handlers.length);
- newHd[m_handlers.length] = hd;
- m_handlers = newHd;
+ HandlerDescription[] newHd = new HandlerDescription[m_handlers.length + 1];
+ System.arraycopy(m_handlers, 0, newHd, 0, m_handlers.length);
+ newHd[m_handlers.length] = hd;
+ m_handlers = newHd;
}
/**
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/architecture/PropertyDescription.java b/ipojo/src/main/java/org/apache/felix/ipojo/architecture/PropertyDescription.java
index 1b98ea6..bb52d9a 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/architecture/PropertyDescription.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/architecture/PropertyDescription.java
@@ -70,8 +70,11 @@
* @see java.lang.Object#toString()
*/
public String toString() {
- if (m_value != null) { return getName() + " - " + getType() + " - " + getValue(); }
- else { return getName() + " - " + getType() + " - REQUIRED"; }
+ if (m_value != null) {
+ return getName() + " - " + getType() + " - " + getValue();
+ } else {
+ return getName() + " - " + getType() + " - REQUIRED";
+ }
}
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurableProperty.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurableProperty.java
index bdcc3c5..68fc08f 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurableProperty.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurableProperty.java
@@ -59,10 +59,12 @@
*/
public ConfigurableProperty(String name, String field, String value, String type, ConfigurationHandler ch) {
m_handler = ch;
- if (name != null) { m_name = name; }
- else { m_name = field; }
+ if (name != null) {
+ m_name = name;
+ } else {
+ m_name = field;
+ }
m_field = field;
-
if (value != null) { setValue(m_field, value, type); }
}
@@ -173,12 +175,14 @@
float[] fl = new float[values.length];
for (int i = 0; i < values.length; i++) { fl[i] = new Float(values[i]).floatValue(); }
m_value = fl;
- return; }
+ return;
+ }
if (internalType.equals("double")) {
double[] dl = new double[values.length];
for (int i = 0; i < values.length; i++) { dl[i] = new Double(values[i]).doubleValue(); }
m_value = dl;
- return; }
+ return;
+ }
// Else it is a neither a primitive type neither a String -> create the object by calling a constructor with a string in argument.
try {
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
index 557e164..a66472a 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
@@ -29,17 +29,13 @@
import org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandler;
import org.apache.felix.ipojo.metadata.Element;
import org.apache.felix.ipojo.util.Logger;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.cm.ConfigurationException;
-import org.osgi.service.cm.ManagedService;
/**
* Handler managing the Configuration Admin.
* @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
*/
-public class ConfigurationHandler extends Handler implements ManagedService {
+public class ConfigurationHandler extends Handler {
/**
* Reference on the instance manager.
@@ -61,14 +57,15 @@
* Properties propagated at the last "updated".
*/
private Dictionary m_propagated = new Properties();
-
+
+
/**
- * PID of the component.
+ * Properties to propage.
*/
- private String m_pid;
+ private Dictionary m_toPropagate = new Properties();
/**
- * should the component provided ManagedService ?
+ * should the component propagate configuration ?
*/
private boolean m_isConfigurable;
@@ -97,20 +94,28 @@
// Check if the component is dynamically configurable
m_isConfigurable = false;
- if (confs[0].containsAttribute("configurable") && confs[0].getAttribute("configurable").equalsIgnoreCase("true")) { m_isConfigurable = true; }
+ if (confs[0].containsAttribute("configurable") && confs[0].getAttribute("configurable").equalsIgnoreCase("true")) { m_isConfigurable = true; m_toPropagate = configuration; }
Element[] configurables = confs[0].getElements("Property");
for (int i = 0; i < configurables.length; i++) {
String fieldName = configurables[i].getAttribute("field");
String name = null;
- if (configurables[i].containsAttribute("name")) { name = configurables[i].getAttribute("name"); }
- else { name = fieldName; }
+ if (configurables[i].containsAttribute("name")) {
+ name = configurables[i].getAttribute("name");
+ } else {
+ name = fieldName;
+ }
String value = null;
if (configurables[i].containsAttribute("value")) { value = configurables[i].getAttribute("value"); }
- if (name != null && configuration.get(name) != null && configuration.get(name) instanceof String) { value = (String) configuration.get(name); }
- else { if (fieldName != null && configuration.get(fieldName) != null && configuration.get(fieldName) instanceof String) { value = (String) configuration.get(fieldName); } }
+ if (name != null && configuration.get(name) != null && configuration.get(name) instanceof String) {
+ value = (String) configuration.get(name);
+ } else {
+ if (fieldName != null && configuration.get(fieldName) != null && configuration.get(fieldName) instanceof String) {
+ value = (String) configuration.get(fieldName);
+ }
+ }
// Detect the type of the property
Element manipulation = metadata.getElements("Manipulation")[0];
@@ -124,19 +129,15 @@
ConfigurableProperty cp = new ConfigurableProperty(name, fieldName, value, type, this);
- if (cp.getValue() != null) { cd.addProperty(new PropertyDescription(name, type, cp.getValue().toString())); }
- else { cd.addProperty(new PropertyDescription(name, type, null)); }
+ if (cp.getValue() != null) {
+ cd.addProperty(new PropertyDescription(name, type, cp.getValue().toString()));
+ } else {
+ cd.addProperty(new PropertyDescription(name, type, null));
+ }
addProperty(cp);
}
- // Get the PID in the configuration name :
- if (configuration.get("name") != null) { m_pid = (String) configuration.get("name"); }
- else { m_pid = metadata.getAttribute("className"); }
-
- // Get the provided service handler :
- m_providedServiceHandler = (ProvidedServiceHandler) m_manager.getHandler(ProvidedServiceHandler.class.getName());
-
if (configurables.length > 0) {
String[] fields = new String[m_configurableProperties.length];
for (int k = 0; k < m_configurableProperties.length; k++) {
@@ -145,42 +146,36 @@
// Check if the instance configuration contains value for the current property :
String name = m_configurableProperties[k].getName();
String fieldName = m_configurableProperties[k].getField();
- if (name != null && configuration.get(name) != null && !(configuration.get(name) instanceof String)) { m_configurableProperties[k].setValue(configuration.get(name)); }
- else { if (fieldName != null && configuration.get(fieldName) != null && !(configuration.get(fieldName) instanceof String)) { m_configurableProperties[k].setValue(configuration.get(fieldName)); } }
+ if (name != null && configuration.get(name) != null && !(configuration.get(name) instanceof String)) {
+ m_configurableProperties[k].setValue(configuration.get(name));
+ } else {
+ if (fieldName != null && configuration.get(fieldName) != null && !(configuration.get(fieldName) instanceof String)) {
+ m_configurableProperties[k].setValue(configuration.get(fieldName));
+ }
+ }
}
m_manager.register(this, fields);
}
- else { return; }
}
/**
* @see org.apache.felix.ipojo.Handler#stop()
*/
- public void stop() {
- // Unregister the service
- if (m_isConfigurable && m_sr != null) {
- m_manager.getFactory().getLogger().log(Logger.INFO, "[" + m_manager.getClassName() + "] Unregister Managed Service");
- m_sr.unregister();
- m_sr = null;
- }
-
- }
+ public void stop() { }
/**
* @see org.apache.felix.ipojo.Handler#start()
*/
public void start() {
- // Unregister the service if already registred (it should not happen )
- if (m_isConfigurable && m_sr != null) { m_sr.unregister(); }
+ // Get the provided service handler :
+ m_providedServiceHandler = (ProvidedServiceHandler) m_manager.getHandler(ProvidedServiceHandler.class.getName());
- // Register the ManagedService
+ // Propagation
if (m_isConfigurable) {
- BundleContext bc = m_manager.getContext();
- Dictionary properties = new Properties();
- properties.put(Constants.SERVICE_PID, m_pid);
-
- m_manager.getFactory().getLogger().log(Logger.INFO, "[" + m_manager.getClassName() + "] Register Managed Service");
- m_sr = bc.registerService(ManagedService.class.getName(), this, properties);
+ for (int i = 0; i < m_configurableProperties.length; i++) {
+ m_toPropagate.put(m_configurableProperties[i].getName(), m_configurableProperties[i].getValue());
+ }
+ reconfigure(m_toPropagate);
}
}
@@ -229,47 +224,6 @@
}
/**
- * @see org.osgi.service.cm.ManagedService#updated(java.util.Dictionary)
- */
- public void updated(Dictionary np) throws ConfigurationException {
- Properties toPropagate = new Properties();
- if (np != null) {
- Enumeration keysEnumeration = np.keys();
- while (keysEnumeration.hasMoreElements()) {
- String name = (String) keysEnumeration.nextElement();
- Object value = np.get(name);
- boolean find = false;
- // Check if the field is a configurable property
- for (int i = 0; !find && i < m_configurableProperties.length; i++) {
- if (m_configurableProperties[i].getName().equals(name)) {
- // Check if the value has change
- if (m_configurableProperties[i].getValue() == null || !m_configurableProperties[i].getValue().equals(value)) {
- m_manager.setterCallback(m_configurableProperties[i].getField(), value); // says that the value has change
- }
- find = true;
- // Else do nothing
- }
- }
- if (!find) {
- //The property is not a configurable property
- m_manager.getFactory().getLogger().log(Logger.INFO, "[" + m_manager.getClassName() + "] The property " + name + " will be propagated to service registrations");
- toPropagate.put(name, value);
- }
- }
- }
- else { m_manager.getFactory().getLogger().log(Logger.WARNING, "[" + m_manager.getClassName() + "] The pushed configuration is null for " + m_pid); }
-
- // Propagation of the properties to service registrations :
- if (m_providedServiceHandler != null && !toPropagate.isEmpty()) {
- m_manager.getFactory().getLogger().log(Logger.INFO, "[" + m_manager.getClassName() + "] Properties will be propagated");
- m_providedServiceHandler.removeProperties(m_propagated);
- m_providedServiceHandler.addProperties(toPropagate);
- m_propagated = toPropagate;
- }
-
- }
-
- /**
* Add the given property metadata to the property metadata list.
* @param p : property metdata to add
*/
@@ -283,8 +237,7 @@
System.arraycopy(m_configurableProperties, 0, newProp, 0, m_configurableProperties.length);
newProp[m_configurableProperties.length] = p;
m_configurableProperties = newProp;
- }
- else {
+ } else {
m_configurableProperties = new ConfigurableProperty[] {p};
}
}
@@ -300,5 +253,44 @@
}
return false;
}
+
+ /**
+ * @see org.apache.felix.ipojo.Handler#reconfigure(java.util.Dictionary)
+ */
+ public void reconfigure(Dictionary np) {
+ Properties toPropagate = new Properties();
+ Enumeration keysEnumeration = np.keys();
+ while (keysEnumeration.hasMoreElements()) {
+ String name = (String) keysEnumeration.nextElement();
+ Object value = np.get(name);
+ boolean find = false;
+ // Check if the field is a configurable property
+ for (int i = 0; !find && i < m_configurableProperties.length; i++) {
+ if (m_configurableProperties[i].getName().equals(name)) {
+ // Check if the value has change
+ if (m_configurableProperties[i].getValue() == null || !m_configurableProperties[i].getValue().equals(value)) {
+ m_manager.setterCallback(m_configurableProperties[i].getField(), value); // says that the value has changed
+ }
+ find = true;
+ // Else do nothing
+ }
+ }
+ if (!find) {
+ //The property is not a configurable property
+ toPropagate.put(name, value);
+ }
+ }
+
+ // Propagation of the properties to service registrations :
+ if (m_providedServiceHandler != null && !toPropagate.isEmpty()) {
+ m_providedServiceHandler.removeProperties(m_propagated);
+
+ // Remove to name props
+ toPropagate.remove("name");
+
+ m_providedServiceHandler.addProperties(toPropagate);
+ m_propagated = toPropagate;
+ }
+ }
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java
index a0d3d28..ccd81c7 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java
@@ -123,7 +123,10 @@
/**
* Dependency contructor. After the creation the dependency is not started.
* @param dh : the dependency handler managing this dependency
- * @param dm : the depednency metadata
+ * @param field : field of the dependency
+ * @param spec : required specification
+ * @param filter : LDAP filter of the dependency
+ * @param isOptional : is the dependency an optional dependency ?
*/
public Dependency(DependencyHandler dh, String field, String spec, String filter, boolean isOptional) {
m_handler = dh;
@@ -174,12 +177,14 @@
System.arraycopy(m_callbacks, 0, newCallbacks, 0, m_callbacks.length);
newCallbacks[m_callbacks.length] = cb;
m_callbacks = newCallbacks;
- }
- else {
+ } else {
m_callbacks = new DependencyCallback[] {cb};
}
}
+ /**
+ * @return the filter attached to the dependency.
+ */
public String getFilter() { return m_strFilter; }
/**
@@ -224,9 +229,9 @@
if (!m_change) {
if (!m_isMultiple) {
if (m_services.length > 0) {
- return m_services[0]; }
- }
- else {
+ return m_services[0];
+ }
+ } else {
return m_services;
}
}
@@ -266,18 +271,18 @@
// m_handler.getInstanceManager().getFactory().getLogger().log(Logger.INFO, "[" + m_handler.getInstanceManager().getClassName() + "] Nullable object created for " + getMetadata().getServiceSpecification() + " -> " + instance);
return instance;
}
- }
- else { // Multiple dependency
+ } else { // Multiple dependency
return m_services;
}
} catch (Exception e) {
// There is a problem in the dependency resolving (like in stopping method)
if (!m_isMultiple) {
m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "[" + m_handler.getInstanceManager().getClassName() + "] Return null, an exception was throwed in the get method", e);
- return null; }
- else {
+ return null;
+ } else {
m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "[" + m_handler.getInstanceManager().getClassName() + "] Return an empty array, an exception was throwed in the get method", e);
- return Array.newInstance(m_clazz, 0); }
+ return Array.newInstance(m_clazz, 0);
+ }
}
}
@@ -298,19 +303,23 @@
// If a service arrives
if (event.getType() == ServiceEvent.REGISTERED) {
- if (m_filter.match(event.getServiceReference())) { arrivalManagement(event.getServiceReference()); return; }
- else { return; }
+ if (m_filter.match(event.getServiceReference())) {
+ arrivalManagement(event.getServiceReference());
+ }
+ return;
}
-
// If a service is modified
if (event.getType() == ServiceEvent.MODIFIED) {
if (m_filter.match(event.getServiceReference())) {
m_handler.getInstanceManager().getFactory().getLogger().log(Logger.INFO, "[" + m_handler.getInstanceManager().getClassName() + "] A service with a filter matching is arrived -> " + event.getServiceReference().getBundle());
- if (!containsSR(event.getServiceReference())) { arrivalManagement(event.getServiceReference()); }
- }
- else {
+ if (!containsSR(event.getServiceReference())) {
+ arrivalManagement(event.getServiceReference());
+ }
+ } else {
m_handler.getInstanceManager().getFactory().getLogger().log(Logger.INFO, "[" + m_handler.getInstanceManager().getClassName() + "] A service with a filter matching has gone -> " + event.getServiceReference().getBundle());
- if (containsSR(event.getServiceReference())) { departureManagement(event.getServiceReference()); }
+ if (containsSR(event.getServiceReference())) {
+ departureManagement(event.getServiceReference());
+ }
}
return;
}
@@ -569,8 +578,7 @@
String filter = "";
if (!m_strFilter.equals("")) {
filter = "(&" + classnamefilter + m_strFilter + ")";
- }
- else {
+ } else {
filter = classnamefilter;
}
@@ -596,8 +604,7 @@
m_filter = m_handler.getInstanceManager().getContext().createFilter(filter); // Store the filter
m_handler.getInstanceManager().getFactory().getLogger().log(Logger.INFO, "[" + m_handler.getInstanceManager().getClassName() + "] Create a filter from : " + filter);
m_change = true;
- }
- catch (InvalidSyntaxException e1) {
+ } catch (InvalidSyntaxException e1) {
m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "[" + m_handler.getInstanceManager().getClassName() + "] A filter is malformed : " + filter);
e1.printStackTrace();
}
@@ -626,7 +633,7 @@
* @return the state of the dependency (1 : valid, 2 : invalid)
*/
public int getState() {
- return ( m_isOptional ) ? 1 : m_state;
+ return (m_isOptional) ? 1 : m_state;
}
/**
@@ -653,8 +660,7 @@
System.arraycopy(m_ref, 0, newSR, 0, m_ref.length);
newSR[m_ref.length] = r;
m_ref = newSR;
- }
- else {
+ } else {
m_ref = new ServiceReference[] {r};
}
}
@@ -695,9 +701,7 @@
// If this is the module, then point to empty list.
if ((m_ref.length - 1) == 0) {
m_ref = new ServiceReference[0];
- }
- // Otherwise, we need to do some array copying.
- else {
+ } else { // Otherwise, we need to do some array copying.
ServiceReference[] newSR = new ServiceReference[m_ref.length - 1];
System.arraycopy(m_ref, 0, newSR, 0, idx);
if (idx < newSR.length) {
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyCallback.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyCallback.java
index 993bbcd..d197cb6 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyCallback.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyCallback.java
@@ -53,12 +53,12 @@
private String m_callback;
/**
- * Is the callback a static callback
+ * Is the callback a static callback.
*/
private boolean m_isStatic;
/**
- * The instance manager
+ * The instance manager.
*/
private InstanceManager m_manager;
@@ -92,8 +92,9 @@
Method method = m_manager.getClazz().getDeclaredMethod(m_callback, new Class[] {});
method.setAccessible(true);
- if (m_isStatic) { method.invoke(null, new Object[] {}); }
- else {
+ if (m_isStatic) {
+ method.invoke(null, new Object[] {});
+ } else {
// Two cases :
// - if instances already exists : call on each instances
// - if no instance exists : create an instance
@@ -120,8 +121,9 @@
Method method = m_manager.getClazz().getDeclaredMethod(m_callback, new Class[] {ServiceReference.class});
method.setAccessible(true);
- if (m_isStatic) { method.invoke(null, new Object[] {ref}); }
- else {
+ if (m_isStatic) {
+ method.invoke(null, new Object[] {ref});
+ } else {
// Two cases :
// - if instances already exists : call on each instances
// - if no instance exists : create an instance
@@ -148,8 +150,9 @@
Method method = m_manager.getClazz().getDeclaredMethod(m_callback, new Class[] {Object.class});
method.setAccessible(true);
- if (m_isStatic) { method.invoke(null, new Object[] {o}); }
- else {
+ if (m_isStatic) {
+ method.invoke(null, new Object[] {o});
+ } else {
// Two cases :
// - if instances already exists : call on each instances
// - if no instance exists : create an instance
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
index 55d758d..210f0e5 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
@@ -71,8 +71,7 @@
System.arraycopy(m_dependencies, 0, newDep, 0, m_dependencies.length);
newDep[m_dependencies.length] = dep;
m_dependencies = newDep;
- }
- else {
+ } else {
m_dependencies = new Dependency[] {dep};
}
}
@@ -92,8 +91,7 @@
System.arraycopy(m_nullableClasses, 0, newClass, 0, m_nullableClasses.length);
newClass[m_nullableClasses.length] = clazz;
m_nullableClasses = newClass;
- }
- else {
+ } else {
m_nullableClasses = new Class[] {clazz};
}
}
@@ -179,8 +177,7 @@
m_manager.getFactory().getLogger().log(Logger.WARNING, "[DependencyHandler on " + m_manager.getClassName() + "] The field type [" + type + "] and the needed service interface [" + dep.getSpecification() + "] are not the same");
dep.setSpecification(type);
}
- }
- else {
+ } else {
m_manager.getFactory().getLogger().log(Logger.WARNING, "[DependencyHandler on " + m_manager.getClassName() + "] The declared dependency " + dep.getField() + " does not exist in the code");
}
return true;
@@ -210,16 +207,22 @@
Dependency dep = new Dependency(this, field, serviceSpecification, filter, optional);
// Check the dependency :
Element manipulation = componentMetadata.getElements("Manipulation")[0];
- if (checkDependency(dep, manipulation)) { addDependency(dep); }
- else { m_manager.getFactory().getLogger().log(Logger.ERROR, "[DependencyHandler on " + m_manager.getClassName() + "] The dependency on " + dep.getField() + " is not valid"); }
+ if (checkDependency(dep, manipulation)) {
+ addDependency(dep);
+ } else {
+ m_manager.getFactory().getLogger().log(Logger.ERROR, "[DependencyHandler on " + m_manager.getClassName() + "] The dependency on " + dep.getField() + " is not valid");
+ }
// Look for dependency callback :
for (int j = 0; j < (deps[i].getElements("Callback", "")).length; j++) {
String method = deps[i].getElements("Callback", "")[j].getAttribute("method");
String type = deps[i].getElements("Callback", "")[j].getAttribute("type");
int methodType = 0;
- if (type.equals("bind")) { methodType = DependencyCallback.BIND; }
- else { methodType = DependencyCallback.UNBIND; }
+ if (type.equals("bind")) {
+ methodType = DependencyCallback.BIND;
+ } else {
+ methodType = DependencyCallback.UNBIND;
+ }
boolean isStatic = false;
if (deps[i].getElements("Callback", "")[j].containsAttribute("isStatic") && deps[i].getElements("Callback", "")[j].getAttribute("isStatic").equals("true")) { isStatic = true; }
DependencyCallback dc = new DependencyCallback(dep, method, methodType, isStatic);
@@ -245,7 +248,7 @@
//String[] segment = dep.getMetadata().getServiceSpecification().split("[.]");
//String className = "org/apache/felix/ipojo/" + segment[segment.length - 1] + "Nullable";
- String className = dep.getSpecification()+"Nullable";
+ String className = dep.getSpecification() + "Nullable";
String resource = dep.getSpecification().replace('.', '/') + ".class";
URL url = m_manager.getContext().getBundle().getResource(resource);
@@ -254,8 +257,9 @@
Class c = null;
try {
c = m_manager.getFactory().defineClass(className, b, null);
+ } catch (Exception e) {
+ System.err.println("Cannot define !!!");
}
- catch(Exception e) { System.err.println("Cannot define !!!");}
addNullableClass(c);
m_manager.getFactory().getLogger().log(Logger.INFO, "[DependencyHandler on " + m_manager.getClassName() + "] Nullable class created for " + dep.getSpecification());
} catch (Exception e2) {
@@ -357,6 +361,9 @@
return valide;
}
+ /**
+ * @see org.apache.felix.ipojo.Handler#getDescription()
+ */
public HandlerDescription getDescription() {
DependencyHandlerDescription dhd = new DependencyHandlerDescription(isValid());
for (int j = 0; j < getDependencies().length; j++) {
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/nullable/MethodSignatureVisitor.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/nullable/MethodSignatureVisitor.java
index ea17c7b..a5d9799 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/nullable/MethodSignatureVisitor.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/nullable/MethodSignatureVisitor.java
@@ -78,8 +78,7 @@
System.arraycopy(list, 0, newList, 0, list.length);
newList[list.length] = method;
return newList;
- }
- else {
+ } else {
list = new MethodSignature[] {method};
return list;
}
@@ -88,36 +87,61 @@
/**
* @see org.objectweb.asm.ClassVisitor#visit(int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[])
+ * @param arg0 : version
+ * @param arg1 : access
+ * @param arg2 : name
+ * @param arg3 : signature
+ * @param arg4 : superName
+ * @param arg5 : interfaces
*/
public void visit(int arg0, int arg1, String arg2, String arg3, String arg4, String[] arg5) { }
/**
* @see org.objectweb.asm.ClassVisitor#visitSource(java.lang.String, java.lang.String)
+ * @param arg0 : source
+ * @param arg1 : debug
*/
public void visitSource(String arg0, String arg1) { }
/**
* @see org.objectweb.asm.ClassVisitor#visitOuterClass(java.lang.String, java.lang.String, java.lang.String)
+ * @param arg0 : owner
+ * @param arg1 : name
+ * @param arg2 : desc
*/
public void visitOuterClass(String arg0, String arg1, String arg2) { }
/**
* @see org.objectweb.asm.ClassVisitor#visitAnnotation(java.lang.String, boolean)
+ * @param arg0 : desc
+ * @param arg1 : visible
+ * @return annotationvisitor
*/
public AnnotationVisitor visitAnnotation(String arg0, boolean arg1) { return null; }
/**
* @see org.objectweb.asm.ClassVisitor#visitAttribute(org.objectweb.asm.Attribute)
+ * @param arg0 : Attribute
*/
public void visitAttribute(Attribute arg0) { }
/**
* @see org.objectweb.asm.ClassVisitor#visitInnerClass(java.lang.String, java.lang.String, java.lang.String, int)
+ * @param arg0 : name
+ * @param arg1 : outerclass
+ * @param arg2 : innerclass
+ * @param arg3 : access
*/
public void visitInnerClass(String arg0, String arg1, String arg2, int arg3) { }
/**
* @see org.objectweb.asm.ClassVisitor#visitField(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object)
+ * @param arg0 : access
+ * @param arg1 : name
+ * @param arg2 : desc
+ * @param arg3 : signature
+ * @param arg4 : value
+ * @return the field visitor
*/
public FieldVisitor visitField(int arg0, String arg1, String arg2, String arg3, Object arg4) { return null; }
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/nullable/NullableObjectWriter.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/nullable/NullableObjectWriter.java
index b9d0284..0228e10 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/nullable/NullableObjectWriter.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/dependency/nullable/NullableObjectWriter.java
@@ -57,7 +57,7 @@
//String[] segment = contractName.split("[.]");
//String className = "org/apache/felix/ipojo/" + segment[segment.length - 1] + "Nullable";
- String className=contractName.replace('.', '/')+"Nullable";
+ String className = contractName.replace('.', '/') + "Nullable";
// Create the class
@@ -65,12 +65,12 @@
"java/lang/Object", new String[]{contractName.replace('.', '/'), "org/apache/felix/ipojo/Nullable"});
// Inject a constructor <INIT>()V
- MethodVisitor cst = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
- cst.visitVarInsn(ALOAD, 0);
- cst.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
- cst.visitInsn(RETURN);
- cst.visitMaxs(0, 0);
- cst.visitEnd();
+ MethodVisitor cst = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
+ cst.visitVarInsn(ALOAD, 0);
+ cst.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
+ cst.visitInsn(RETURN);
+ cst.visitMaxs(0, 0);
+ cst.visitEnd();
// Methods Generation :
MethodSignature[] methods = msv.getMethods();
@@ -87,7 +87,7 @@
Type returnType = Type.getReturnType(desc);
// TODO : manage the other type primitive for Nullable
// Primitive type :
- switch (returnType.getSort()) {
+ switch (returnType.getSort()) {
case Type.BOOLEAN:
case Type.INT:
case Type.BYTE:
@@ -117,7 +117,7 @@
default :
System.err.println("Type not yet managed : " + returnType);
break;
- }
+ }
mv.visitMaxs(0, 0);
mv.visitEnd();
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallback.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallback.java
index 6e03efb..95d9588 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallback.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallback.java
@@ -52,7 +52,10 @@
/**
* LifecycleCallback constructor.
* @param hh : the callback handler calling the callback
- * @param hm : the callback metadata
+ * @param initialState : initial state of the callback
+ * @param finalState : finali state of the callback
+ * @param method : method to invoke
+ * @param isStatic : is the method static ?
*/
public LifecycleCallback(LifecycleCallbackHandler hh, String initialState, String finalState, String method, boolean isStatic) {
if (initialState.equals("VALID")) { m_initialState = InstanceManager.VALID; }
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallbackHandler.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallbackHandler.java
index 3c85db7..3ee96ea 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallbackHandler.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallbackHandler.java
@@ -49,7 +49,7 @@
private InstanceManager m_manager;
/**
- * Does a POJO object be created at starting
+ * Does a POJO object be created at starting.
*/
private boolean m_immediate = false;
@@ -67,8 +67,7 @@
System.arraycopy(m_callbacks, 0, newHk, 0, m_callbacks.length);
newHk[m_callbacks.length] = hk;
m_callbacks = newHk;
- }
- else {
+ } else {
m_callbacks = new LifecycleCallback[] {hk};
}
@@ -121,7 +120,7 @@
*/
public void stateChanged(int state) {
// Manage immediate component
- if(m_state == ComponentInstance.INVALID && state == ComponentInstance.VALID && m_manager.getPojoObjects().length == 0) {
+ if (m_state == ComponentInstance.INVALID && state == ComponentInstance.VALID && m_manager.getPojoObjects().length == 0) {
m_manager.createPojoObject();
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/Property.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/Property.java
index f1638c6..6e0664f 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/Property.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/Property.java
@@ -70,7 +70,11 @@
/**
* Property constructor.
* @param ps : the provided service
- * @param pm : metadata of the property
+ * @param name : name of the property
+ * @param field : name of the field (if a field is attached to the property)
+ * @param type : type of the property
+ * @param value : initial value of the property
+ * @param manipulation : manipulation metadata
*/
public Property(ProvidedService ps, String name, String field, String type, String value, Element manipulation) {
m_providedService = ps;
@@ -85,15 +89,15 @@
}
// Check type if not already set
- if(m_type == null) {
- if(field == null) { ps.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "The property "+ m_name + " has neither type neither field."); return; }
- for (int j = 0; j < manipulation.getElements("Field").length; j++) {
- if (field.equals(manipulation.getElements("Field")[j].getAttribute("name"))) {
- m_type = manipulation.getElements("Field")[j].getAttribute("type");
- break;
- }
+ if (m_type == null) {
+ if (field == null) { ps.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "The property " + m_name + " has neither type neither field."); return; }
+ for (int j = 0; j < manipulation.getElements("Field").length; j++) {
+ if (field.equals(manipulation.getElements("Field")[j].getAttribute("name"))) {
+ m_type = manipulation.getElements("Field")[j].getAttribute("type");
+ break;
}
- if (m_type == null) { m_providedService.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "[" + ps.getInstanceManager().getClassName() + "] A declared property was not found in the class : " + m_field); }
+ }
+ if (m_type == null) { m_providedService.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "[" + ps.getInstanceManager().getClassName() + "] A declared property was not found in the class : " + m_field); }
}
if (m_initialValue != null) { setValue(m_initialValue); }
@@ -125,7 +129,6 @@
*/
protected void set(String s) {
setValue(s);
- m_providedService.update();
}
/**
@@ -135,7 +138,6 @@
*/
protected void set(Object o) {
m_value = o;
- m_providedService.update();
}
/**
@@ -238,12 +240,14 @@
float[] fl = new float[values.length];
for (int i = 0; i < values.length; i++) { fl[i] = new Float(values[i]).floatValue(); }
m_value = fl;
- return; }
+ return;
+ }
if (internalType.equals("double")) {
double[] dl = new double[values.length];
for (int i = 0; i < values.length; i++) { dl[i] = new Double(values[i]).doubleValue(); }
m_value = dl;
- return; }
+ return;
+ }
// Else it is a neither a primitive type neither a String -> create the object by calling a constructor with a string in argument.
try {
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedService.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedService.java
index 3662094..d446087 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedService.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedService.java
@@ -88,7 +88,8 @@
/**
* Construct a provided service object.
* @param handler : the provided service handler.
- * @param psm : the provided service metadata.
+ * @param specification : specifications provided by this provided service
+ * @param factoryPolicy : service providing policy
*/
public ProvidedService(ProvidedServiceHandler handler, String[] specification, int factoryPolicy) {
m_handler = handler;
@@ -107,7 +108,7 @@
* @param props : the properties to attached to the service registration
*/
protected void setProperties(Property[] props) {
- for(int i = 0; i < props.length; i++) {
+ for (int i = 0; i < props.length; i++) {
addProperty(props[i]);
}
}
@@ -126,8 +127,9 @@
System.arraycopy(m_properties, 0, newProp, 0, m_properties.length);
newProp[m_properties.length] = p;
m_properties = newProp;
+ } else {
+ m_properties = new Property[] {p};
}
- else { m_properties = new Property[] {p}; }
}
/**
@@ -141,12 +143,14 @@
}
if (idx >= 0) {
- if ((m_properties.length - 1) == 0) { m_properties = new Property[0]; }
- else {
+ if ((m_properties.length - 1) == 0) {
+ m_properties = new Property[0];
+ } else {
Property[] newPropertiesList = new Property[m_properties.length - 1];
System.arraycopy(m_properties, 0, newPropertiesList, 0, idx);
if (idx < newPropertiesList.length) {
- System.arraycopy(m_properties, idx + 1, newPropertiesList, idx, newPropertiesList.length - idx); }
+ System.arraycopy(m_properties, idx + 1, newPropertiesList, idx, newPropertiesList.length - idx);
+ }
m_properties = newPropertiesList;
}
}
@@ -156,8 +160,9 @@
* @return the service reference of the provided service (null if the service is not published).
*/
public ServiceReference getServiceReference() {
- if (m_serviceRegistration != null) { return m_serviceRegistration.getReference(); }
- else { return null; }
+ if (m_serviceRegistration != null) {
+ return m_serviceRegistration.getReference();
+ } else { return null; }
}
/**
@@ -179,7 +184,7 @@
default :
m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "[" + m_handler.getInstanceManager().getClassName() + "] Unknown factory policy for " + m_serviceSpecification + " : " + m_factoryPolicy);
- return null;
+ return null;
}
}
@@ -231,8 +236,11 @@
* @return The state of the provided service.
*/
public int getState() {
- if(m_serviceRegistration == null) { return UNREGISTERED; }
- else { return REGISTERED; }
+ if (m_serviceRegistration == null) {
+ return UNREGISTERED;
+ } else {
+ return REGISTERED;
+ }
}
/**
@@ -273,14 +281,14 @@
// Contruct the service properties list
Properties serviceProperties = getServiceProperties();
- if(serviceProperties == null) {
+ if (serviceProperties == null) {
m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "Cannot get the properties of the provided service");
}
// Update the service registration
if (m_serviceRegistration != null) {
- m_serviceRegistration.setProperties(serviceProperties);
- }
+ m_serviceRegistration.setProperties(serviceProperties);
+ }
}
/**
@@ -295,7 +303,6 @@
Property prop = new Property(this, key, value);
addProperty(prop);
}
- update();
}
/**
@@ -308,7 +315,6 @@
String key = (String) keys.nextElement();
removeProperty(key);
}
- update();
}
/**
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceDescription.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceDescription.java
index 0c15af2..0041298 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceDescription.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceDescription.java
@@ -30,12 +30,12 @@
public class ProvidedServiceDescription {
/**
- * State : the service is unregistered
+ * State : the service is unregistered.
*/
public static final int UNREGISTERED = 0;
/**
- * State : the service is registered
+ * State : the service is registered.
*/
public static final int REGISTERED = 1;
@@ -75,9 +75,8 @@
* @param serviceSpecification : the provided contract
* @param state : state (UNREGITRED | REGISTRED)
* @param sr : Service Registration (to obtain the reference), or null if state is UNREGISTRED
- * @param parent : the component description declaring this proided service
*/
- public ProvidedServiceDescription(String[] serviceSpecification, int state, ServiceReference sr/*, InstanceDescription parent*/) {
+ public ProvidedServiceDescription(String[] serviceSpecification, int state, ServiceReference sr) {
m_serviceSpecification = serviceSpecification;
m_state = state;
m_serviceReference = sr;
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceHandler.java b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceHandler.java
index 25f3884..7253e15 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceHandler.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceHandler.java
@@ -62,8 +62,9 @@
System.arraycopy(m_providedServices, 0, newPS, 0, m_providedServices.length);
newPS[m_providedServices.length] = ps;
m_providedServices = newPS;
+ } else {
+ m_providedServices = new ProvidedService[] {ps};
}
- else { m_providedServices = new ProvidedService[] {ps}; }
}
/**
@@ -125,8 +126,13 @@
String field = null;
if (props[j].containsAttribute("field")) { field = props[j].getAttribute("field"); }
- if (name != null && configuration.get(name) != null && configuration.get(name) instanceof String) { value = (String) configuration.get(name); }
- else { if (field != null && configuration.get(field) != null && configuration.get(field) instanceof String) { value = (String) configuration.get(field); } }
+ if (name != null && configuration.get(name) != null && configuration.get(name) instanceof String) {
+ value = (String) configuration.get(name);
+ } else {
+ if (field != null && configuration.get(field) != null && configuration.get(field) instanceof String) {
+ value = (String) configuration.get(field);
+ }
+ }
Property prop = new Property(ps, name, field, type, value, manipulation);
properties[j] = prop;
@@ -140,12 +146,11 @@
// Change ComponentInfo
for (int k = 0; k < ps.getServiceSpecification().length; k++) { cd.addProvidedServiceSpecification(ps.getServiceSpecification()[k]); }
for (int k = 0; k < ps.getProperties().length; k++) {
- if(!ps.getProperties()[k].getName().equals(Constants.SERVICE_PID) && !ps.getProperties()[k].getName().equals("factory.pid")) {
+ if (!ps.getProperties()[k].getName().equals(Constants.SERVICE_PID) && !ps.getProperties()[k].getName().equals("factory.pid")) {
cd.addProperty(new PropertyDescription(ps.getProperties()[k].getName(), ps.getProperties()[k].getType(), ps.getProperties()[k].getInitialValue()));
}
}
- }
- else {
+ } else {
String itfs = "";
for (int j = 0; j < serviceSpecification.length; j++) {
itfs = itfs + " " + serviceSpecification[j];
@@ -164,9 +169,13 @@
Property prop = ps.getProperties()[j];
// Check if the instance configuration has a value for this property
- if (prop.getName() != null && configuration.get(prop.getName()) != null && !(configuration.get(prop.getName()) instanceof String)) { prop.set(configuration.get(prop.getName())); }
- else { if (prop.getField() != null && configuration.get(prop.getField()) != null && !(configuration.get(prop.getField()) instanceof String)) { prop.set(configuration.get(prop.getField())); } }
-
+ if (prop.getName() != null && configuration.get(prop.getName()) != null && !(configuration.get(prop.getName()) instanceof String)) {
+ prop.set(configuration.get(prop.getName()));
+ } else {
+ if (prop.getField() != null && configuration.get(prop.getField()) != null && !(configuration.get(prop.getField()) instanceof String)) {
+ prop.set(configuration.get(prop.getField()));
+ }
+ }
if (prop.getField() != null) {
String[] newFields = new String[fields.length + 1];
System.arraycopy(fields, 0, newFields, 0, fields.length);
@@ -176,7 +185,8 @@
}
}
- m_manager.register(this, fields); }
+ m_manager.register(this, fields);
+ }
}
/**
@@ -193,7 +203,7 @@
b = true;
}
}
- if(!b) {
+ if (!b) {
m_manager.getFactory().getLogger().log(Logger.ERROR, "[" + m_manager.getClassName() + "] The service specification " + ps.getServiceSpecification()[i] + " is not implemented by the component class");
return false;
}
@@ -226,26 +236,16 @@
}
/**
- * Stop the provided service handler : unregister all provided services.
+ * Stop the provided service handler.
* @see org.apache.felix.ipojo.Handler#stop()
*/
- public void stop() {
- for (int i = 0; i < m_providedServices.length; i++) {
- m_providedServices[i].unregisterService();
- }
- }
+ public void stop() { }
/**
- * Start the provided service handler : register the service if the component is resolved.
- * Else do nothing and whait for a component state change event
+ * Start the provided service handler.
* @see org.apache.felix.ipojo.Handler#start()
*/
- public void start() {
- m_manager.getFactory().getLogger().log(Logger.INFO, "[" + m_manager.getClassName() + "] Start the provided service handler");
- for (int i = 0; (m_manager.getState() == InstanceManager.VALID) && i < m_providedServices.length; i++) {
- m_providedServices[i].registerService();
- }
- }
+ public void start() { }
/**
* @see org.apache.felix.ipojo.Handler#setterCallback(java.lang.String, java.lang.Object)
@@ -254,13 +254,16 @@
// Verify that the field name coreespond to a dependency
for (int i = 0; i < m_providedServices.length; i++) {
ProvidedService ps = m_providedServices[i];
+ boolean update = false;
for (int j = 0; j < ps.getProperties().length; j++) {
Property prop = ps.getProperties()[j];
if (fieldName.equals(prop.getField())) {
// it is the associated property
prop.set(value);
+ update = true;
}
}
+ if (update) { ps.update(); }
}
//Else do nothing
}
@@ -291,13 +294,17 @@
public void stateChanged(int state) {
// If the new state is UNRESOLVED => unregister all the services
if (state == InstanceManager.INVALID) {
- stop();
+ for (int i = 0; i < m_providedServices.length; i++) {
+ m_providedServices[i].unregisterService();
+ }
return;
}
// If the new state is VALID => regiter all the services
if (state == InstanceManager.VALID) {
- start();
+ for (int i = 0; i < m_providedServices.length; i++) {
+ m_providedServices[i].registerService();
+ }
return;
}
@@ -310,6 +317,7 @@
public void addProperties(Dictionary dict) {
for (int i = 0; i < m_providedServices.length; i++) {
m_providedServices[i].addProperties(dict);
+ m_providedServices[i].update();
}
}
@@ -320,6 +328,7 @@
public void removeProperties(Dictionary dict) {
for (int i = 0; i < m_providedServices.length; i++) {
m_providedServices[i].deleteProperties(dict);
+ m_providedServices[i].update();
}
}
@@ -341,8 +350,27 @@
psd.setProperty(props);
pshd.addProvidedService(psd);
}
-
return pshd;
}
+
+ /**
+ * @see org.apache.felix.ipojo.Handler#reconfigure(java.util.Dictionary)
+ */
+ public void reconfigure(Dictionary dict) {
+ for (int j = 0; j < getProvidedService().length; j++) {
+ ProvidedService ps = getProvidedService()[j];
+ Property[] props = ps.getProperties();
+ boolean update = false;
+ for (int k = 0; k < props.length; k++) {
+ if (dict.get(props[k].getName()) != null) {
+ update = true;
+ if (dict.get(props[k].getName()) instanceof String) {
+ props[k].set((String) dict.get(props[k].getName()));
+ } else { props[k].set(dict.get(props[k].getName())); }
+ }
+ }
+ if (update) { ps.update(); }
+ }
+ }
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java b/ipojo/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java
index c6bfcc3..23adeb1 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java
@@ -62,32 +62,44 @@
return dicts;
}
+ /**
+ * Parse an Element to get a dictionary.
+ * @param instance : the Element describing an instance.
+ * @return : the resulting dictionary
+ * @throws ParseException
+ */
private Dictionary parseInstance(Element instance) throws ParseException {
Dictionary dict = new Properties();
- if(!instance.containsAttribute("name")) { throw new ParseException("An instance does not have the 'name' attribute"); }
- if(!instance.containsAttribute("component")) { throw new ParseException("An instance does not have the 'component' attribute"); }
+ if (!instance.containsAttribute("name")) { throw new ParseException("An instance does not have the 'name' attribute"); }
+ if (!instance.containsAttribute("component")) { throw new ParseException("An instance does not have the 'component' attribute"); }
dict.put("name", instance.getAttribute("name"));
dict.put("component", instance.getAttribute("component"));
- for(int i=0; i < instance.getElements("property").length; i++) {
+ for (int i = 0; i < instance.getElements("property").length; i++) {
parseProperty(instance.getElements("property")[i], dict);
}
return dict;
}
+ /**
+ * @param prop
+ * @param dict
+ * @throws ParseException
+ */
private void parseProperty(Element prop, Dictionary dict) throws ParseException {
// Check that the property has a name
- if(!prop.containsAttribute("name")) { throw new ParseException("A property does not have the 'name' attribute"); }
+ if (!prop.containsAttribute("name")) { throw new ParseException("A property does not have the 'name' attribute"); }
// Final case : the property element has a 'value' attribute
- if(prop.containsAttribute("value")) { dict.put(prop.getAttribute("name"), prop.getAttribute("value")); }
- else {
+ if (prop.containsAttribute("value")) {
+ dict.put(prop.getAttribute("name"), prop.getAttribute("value"));
+ } else {
// Recursive case
// Check if there is 'property' element
Element[] subProps = prop.getElements("property");
- if(subProps.length == 0) { throw new ParseException("A complex property must have at least one 'property' sub-element"); }
+ if (subProps.length == 0) { throw new ParseException("A complex property must have at least one 'property' sub-element"); }
Dictionary dict2 = new Properties();
- for(int i = 0; i < subProps.length; i++) {
+ for (int i = 0; i < subProps.length; i++) {
parseProperty(subProps[i], dict2);
dict.put(prop.getAttribute("name"), dict2);
}
@@ -104,8 +116,7 @@
System.arraycopy(m_elements, 0, newElementsList, 0, m_elements.length);
newElementsList[m_elements.length] = elem;
m_elements = newElementsList;
- }
- else { m_elements = new Element[] {elem}; }
+ } else { m_elements = new Element[] {elem}; }
}
/**
@@ -120,8 +131,7 @@
if ((m_elements.length - 1) == 0) {
// It is the last element of the list;
m_elements = new Element[0];
- }
- else {
+ } else {
// Remove the last element of the list :
Element[] newElementsList = new Element[m_elements.length - 1];
System.arraycopy(m_elements, 0, newElementsList, 0, idx);
@@ -169,8 +179,8 @@
char c = string[i];
switch(c) {
-
- case '$' :
+ // Beginning of an attribute.
+ case '$' :
String attName = "";
String attValue = "";
String attNs = "";
@@ -199,41 +209,45 @@
m_elements[m_elements.length - 1].addAttribute(att);
break;
- case '}' :
+ // End of an element
+ case '}' :
Element lastElement = removeLastElement();
if (m_elements.length != 0) {
Element newQueue = m_elements[m_elements.length - 1];
newQueue.addElement(lastElement);
- }
- else {
+ } else {
addElement(lastElement);
}
break;
- case ' ' : break; // do nothing;
+
+ // Space
+ case ' ' :
+ break; // do nothing;
+
+ // Default case
default :
String name = "";
- String ns = "";
- c = string[i];
- while (c != ' ') {
- if (c == ':') {
- ns = name;
- name = "";
- i++;
- c = string[i];
- }
- else {
- name = name + c;
- i++;
- c = string[i];
- }
- }
- // Skip spaces
- while (string[i] == ' ') { i = i + 1; }
- i = i + 1; // skip {
- Element elem = new Element(name, ns);
- addElement(elem);
- break;
- }
+ String ns = "";
+ c = string[i];
+ while (c != ' ') {
+ if (c == ':') {
+ ns = name;
+ name = "";
+ i++;
+ c = string[i];
+ } else {
+ name = name + c;
+ i++;
+ c = string[i];
+ }
+ }
+ // Skip spaces
+ while (string[i] == ' ') { i = i + 1; }
+ i = i + 1; // skip {
+ Element elem = new Element(name, ns);
+ addElement(elem);
+ break;
+ }
}
}
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/parser/ParseUtils.java b/ipojo/src/main/java/org/apache/felix/ipojo/parser/ParseUtils.java
index 823337b..84ebf3b 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/parser/ParseUtils.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/parser/ParseUtils.java
@@ -1,18 +1,40 @@
+/*
+ * 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.felix.ipojo.parser;
+/**
+ * Parse Utils Methods.
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
public class ParseUtils {
/**
- * Parse the string form of an array as {a, b, c}
+ * Parse the string form of an array as {a, b, c}.
* @param str : the string form
* @return the resulting string array
*/
public static String[] parseArrays(String str) {
// Remove { and }
- if(str.startsWith("{") && str.endsWith("}")) {
+ if (str.startsWith("{") && str.endsWith("}")) {
String m = str.substring(1, str.length() - 1);
String[] values = m.split(",");
- for(int i = 0; i < values.length; i++) {
+ for (int i = 0; i < values.length; i++) {
values[i] = values[i].trim();
}
return values;
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/util/Callback.java b/ipojo/src/main/java/org/apache/felix/ipojo/util/Callback.java
index 8e3f2b6..3e8f3f3 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/util/Callback.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/util/Callback.java
@@ -69,8 +69,9 @@
Method method = m_manager.getClazz().getDeclaredMethod(m_method, new Class[] {});
method.setAccessible(true);
- if (m_isStatic) { method.invoke(null, new Object[]{}); }
- else {
+ if (m_isStatic) {
+ method.invoke(null, new Object[]{});
+ } else {
// Two cases :
// - if instances already exists : call on each instances
// - if no instance exists : create an instance
@@ -119,8 +120,9 @@
Method method = m_manager.getClazz().getDeclaredMethod(m_method, classes);
method.setAccessible(true);
- if (m_isStatic) { method.invoke(null, arg); }
- else {
+ if (m_isStatic) {
+ method.invoke(null, arg);
+ } else {
// Two cases :
// - if instances already exists : call on each instances
// - if no instance exists : create an instance
diff --git a/ipojo/src/main/java/org/apache/felix/ipojo/util/Logger.java b/ipojo/src/main/java/org/apache/felix/ipojo/util/Logger.java
index 0bf1daa..b548efb 100644
--- a/ipojo/src/main/java/org/apache/felix/ipojo/util/Logger.java
+++ b/ipojo/src/main/java/org/apache/felix/ipojo/util/Logger.java
@@ -25,70 +25,131 @@
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
-public class Logger implements ServiceListener{
+/**
+ * iPOJO Logger.
+ * This logger send log message to a log service if presents.
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+public class Logger implements ServiceListener {
+ // TODO how to stop the logging (remove listener ...)
+
+ /**
+ * Log Level ERROR.
+ */
public static final int ERROR = 1;
+
+ /**
+ * Log Level WARNING.
+ */
public static final int WARNING = 2;
+
+ /**
+ * Log Level INFO.
+ */
public static final int INFO = 3;
+
+ /**
+ * Log Level DEBUG.
+ */
public static final int DEBUG = 4;
+ /**
+ * Bundle Context.
+ */
private BundleContext m_context;
+
+ /**
+ * Service Reference of the log service is available.
+ */
private ServiceReference m_ref;
+
+ /**
+ * Log service object.
+ */
private LogService m_log;
+ /**
+ * Name of the logger.
+ */
private String m_name;
+
+ /**
+ * trace level of this logger.
+ */
private int m_level;
+ /**
+ * Constructor.
+ * @param bc : bundle context
+ * @param name : name of the logger
+ * @param level : trace level
+ */
public Logger(BundleContext bc, String name, int level) {
m_name = name;
m_level = level;
m_context = bc;
m_ref = m_context.getServiceReference(LogService.class.getName());
- if(m_ref != null) { m_log = (LogService) m_context.getService(m_ref); }
+ if (m_ref != null) { m_log = (LogService) m_context.getService(m_ref); }
try {
- m_context.addServiceListener(this, "(objectClass="+LogService.class.getName() + ")");
+ m_context.addServiceListener(this, "(objectClass=" + LogService.class.getName() + ")");
} catch (InvalidSyntaxException e) { e.printStackTrace(); }
}
+ /**
+ * Log a message.
+ * @param level : level of the message
+ * @param msg : the message to log
+ */
public void log(int level, String msg) {
- if(m_level >= level) {
- synchronized(this) { _log(level, msg, null); }
+ if (m_level >= level) {
+ synchronized (this) { _log(level, msg, null); }
}
}
+ /**
+ * Log a message with an exception.
+ * @param level : level of the message
+ * @param msg : message to log
+ * @param ex : exception attached to the message
+ */
public void log(int level, String msg, Throwable ex) {
- if(m_level >= level) {
- synchronized(this) {_log(level, msg, ex); }
+ if (m_level >= level) {
+ synchronized (this) { _log(level, msg, ex); }
}
}
- private void _log(int level, String msg, Throwable ex)
- {
+ /**
+ * Internal log method.
+ * @param level : level of the message.
+ * @param msg : message to log
+ * @param ex : exception attached to the message
+ */
+ private void _log(int level, String msg, Throwable ex) {
String s = msg;
s = (ex == null) ? s : s + " (" + ex.getMessage() + ")";
String message;
- switch (level)
- {
+ switch (level) {
case DEBUG:
message = "[" + m_name + "] DEBUG: " + s;
- if(m_log != null) { m_log.log(LogService.LOG_DEBUG, message); }
+ if (m_log != null) { m_log.log(LogService.LOG_DEBUG, message); }
System.err.println(message);
break;
case ERROR:
message = "[" + m_name + "] ERROR: " + s;
- if(m_log != null) { m_log.log(LogService.LOG_ERROR, message); }
+ if (m_log != null) { m_log.log(LogService.LOG_ERROR, message); }
System.err.println(message);
break;
case INFO:
message = "[" + m_name + "] INFO: " + s;
- if(m_log != null) { m_log.log(LogService.LOG_INFO, message); }
+ if (m_log != null) { m_log.log(LogService.LOG_INFO, message); }
System.err.println(message);
break;
case WARNING:
message = "[" + m_name + "] WARNING: " + s;
- if(m_log != null) { m_log.log(LogService.LOG_WARNING, message); }
+ if (m_log != null) { m_log.log(LogService.LOG_WARNING, message); }
System.err.println(message);
break;
default:
@@ -97,16 +158,19 @@
}
}
+ /**
+ * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
+ */
public void serviceChanged(ServiceEvent ev) {
- if(ev.getType() == ServiceEvent.REGISTERED && m_ref == null) {
+ if (ev.getType() == ServiceEvent.REGISTERED && m_ref == null) {
m_ref = ev.getServiceReference();
m_log = (LogService) m_context.getService(m_ref);
}
- if(ev.getType() == ServiceEvent.UNREGISTERING && m_ref == ev.getServiceReference()) {
+ if (ev.getType() == ServiceEvent.UNREGISTERING && m_ref == ev.getServiceReference()) {
m_context.ungetService(m_ref);
m_log = null;
m_ref = m_context.getServiceReference(LogService.class.getName());
- if(m_ref != null) { m_log = (LogService) m_context.getService(m_ref); }
+ if (m_ref != null) { m_log = (LogService) m_context.getService(m_ref); }
}
}