You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2007/04/17 16:31:47 UTC
svn commit: r529623 [5/9] - in /incubator/felix/trunk: ./ ipojo.arch/
ipojo.arch/src/main/java/org/apache/felix/ipojo/arch/ ipojo.metadata/
ipojo.metadata/src/main/java/org/apache/felix/ipojo/metadata/ ipojo.plugin/
ipojo.plugin/src/main/java/org/apach...
Modified: incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/SvcInstance.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/SvcInstance.java?view=diff&rev=529623&r1=529622&r2=529623
==============================================================================
--- incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/SvcInstance.java (original)
+++ incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/SvcInstance.java Tue Apr 17 07:31:35 2007
@@ -22,6 +22,7 @@
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.Properties;
import java.util.Set;
import org.apache.felix.ipojo.ComponentInstance;
@@ -29,319 +30,391 @@
import org.apache.felix.ipojo.ServiceContext;
import org.apache.felix.ipojo.UnacceptableConfiguration;
import org.apache.felix.ipojo.architecture.PropertyDescription;
+import org.apache.felix.ipojo.util.Logger;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
/**
- * Manage a service instantiation.
- * This service create componenet instance providing the required service specification.
+ * Manage a service instantiation. This service create componenet instance
+ * providing the required service specification.
+ *
* @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
*/
public class SvcInstance implements ServiceListener {
-
- /**
- * Required specification.
- */
- private String m_specification;
-
- /**
- * Configuration to push to the instance.
- */
- private Dictionary m_configuration;
-
- /**
- * Map of factory references => instance or NO_INSTANCE.
- */
- private HashMap /*ServiceReference*/ m_usedRef = new HashMap();
-
- /**
- * Does we instantiate several provider ?
- */
- private boolean m_isAggregate = false;
-
- /**
- * Is the service optional ?
- */
- private boolean m_isOptional = false;
-
- /**
- * Handler creating the service instance.
- */
- private ServiceInstantiatorHandler m_handler;
-
- /**
- * Service Context (internal scope).
- */
- private ServiceContext m_context;
-
- /**
- * Parent context.
- */
- //private BundleContext m_parent;
-
- /**
- * True if the service instantiation is valid.
- */
- private boolean m_isValid = false;
-
- /**
- * String form of the factory filter.
- */
- private String m_filterStr;
-
- /**
- * Name of the last create instance.
- */
- private long m_index = 0;
-
- /**
- * Constructor.
- * @param h : the handler.
- * @param spec : required specification.
- * @param conf : instance configuration.
- * @param isAgg : is the svc instance an aggregate service ?
- * @param isOpt : is the svc instance optional ?
- */
- public SvcInstance(ServiceInstantiatorHandler h, String spec, Dictionary conf, boolean isAgg, boolean isOpt, String filt) {
- m_handler = h;
- m_context = h.getManager().getServiceContext();
- //m_parent = h.getManager().getContext();
- m_specification = spec;
- m_configuration = conf;
- m_isAggregate = isAgg;
- m_isOptional = isOpt;
- m_filterStr = filt;
- }
-
- /**
- * Start the service instance.
- * @param sc
- */
- public void start() {
- initFactoryList();
- // Register factory listener
- try {
- m_context.addServiceListener(this, m_filterStr);
- } catch (InvalidSyntaxException e) {
- e.printStackTrace(); // Should not happens
- }
-
- // Init the instances
- if (m_usedRef.size() > 0) {
- Set keys = m_usedRef.keySet();
- Iterator it = keys.iterator();
- if (m_isAggregate) {
- while (it.hasNext()) {
- ServiceReference ref = (ServiceReference) it.next();
- createInstance(ref);
- }
- } else {
- ServiceReference ref = (ServiceReference) it.next();
- createInstance(ref);
- }
- }
- m_isValid = isSatisfied();
- }
-
- /**
- * Stop the service instance.
- */
- public void stop() {
- m_context.removeServiceListener(this);
- Set keys = m_usedRef.keySet();
- Iterator it = keys.iterator();
- while (it.hasNext()) {
- ServiceReference ref = (ServiceReference) it.next();
- Object o = m_usedRef.get(ref);
- if (o != null) {
- ((ComponentInstance) o).stop();
- }
- }
- m_usedRef.clear();
- m_isValid = false;
- }
-
- /**
- * @return true if at least one instance is created.
- */
- private boolean isAnInstanceCreated() {
- Set keys = m_usedRef.keySet();
- Iterator it = keys.iterator();
- ServiceReference ref = (ServiceReference) it.next();
- Object o = m_usedRef.get(ref);
- return o != null;
- }
-
- /**
- * Create an instance for the given reference.
- */
- private void createInstance(ServiceReference ref) {
- try {
- Factory factory = (Factory) m_context.getService(ref);
- ComponentInstance instance = factory.createComponentInstance(m_configuration);
- m_usedRef.put(ref, instance);
- m_context.ungetService(ref);
- } catch (UnacceptableConfiguration e) {
- System.err.println("A matching factory (" + ref.getProperty("service.pid") + ") seems to refuse the given configuration : " + e.getMessage());
- }
- }
-
- /**
- * Create an instance for the next available factory.
- */
- private void createNextInstance() {
- Set keys = m_usedRef.keySet();
- Iterator it = keys.iterator();
- ServiceReference ref = (ServiceReference) it.next();
- try {
- Factory factory = (Factory) m_context.getService(ref);
- ComponentInstance instance = factory.createComponentInstance(m_configuration);
- m_usedRef.put(ref, instance);
- m_context.ungetService(ref);
- } catch (UnacceptableConfiguration e) {
- System.err.println("A matching factory seems to refuse the given configuration : " + e.getMessage());
- }
- }
-
- /**
- * Kill an instance (if exist).
- */
- private void stopInstance(ServiceReference ref) {
- Object o = m_usedRef.get(ref);
- if (o != null) {
- ((ComponentInstance) o).stop();
- }
- }
-
-
- /**
- * Init the list of available factory.
- */
- public void initFactoryList() {
- // Init factory list
- try {
- ServiceReference[] refs = m_context.getServiceReferences(Factory.class.getName(), m_filterStr);
- if (refs == null) { return; }
- for (int i = 0; i < refs.length; i++) {
- ServiceReference ref = refs[i];
- Factory fact = (Factory) m_context.getService(ref);
- // Check provided spec & conf
- if (match(fact)) {
- m_usedRef.put(ref, null);
- }
- fact = null;
- m_context.ungetService(ref);
- }
- } catch (InvalidSyntaxException e) {
- e.printStackTrace(); // Should not happen
- }
- }
-
- /**
- * @return true if the service instance if satisfied.
- */
- public boolean isSatisfied() {
- return m_isOptional || m_usedRef.size() > 0;
- }
-
- /**
- * Does the service instance match with the given factory.
- * @param fact : the factory to test.
- * @return true if the factory match, false otherwise.
- */
- private boolean match(Factory fact) {
- // Check if the factory can provide the spec
- for (int i = 0; i < fact.getComponentDescription().getprovidedServiceSpecification().length; i++) {
- if (fact.getComponentDescription().getprovidedServiceSpecification()[i].equals(m_specification)) {
-
- // Check that the factory needs every properties contained in the configuration
- Enumeration e = m_configuration.keys();
- while (e.hasMoreElements()) {
- String k = (String) e.nextElement();
- if (!containsProperty(k, fact)) {
- return false;
- }
- }
-
- // Add an unique name if not specified.
- if (m_configuration.get("name") == null) {
- m_configuration.put("name", this.toString() + "-" + m_index);
- m_index++;
- }
-
- // Check the acceptability.
- return (fact.isAcceptable(m_configuration));
- }
- }
- return false;
- }
-
- /**
- * Does the factory support the given property ?
- * @param name : name of the property
- * @param factory : factory to test
- * @return true if the factory support this property
- */
- private boolean containsProperty(String name, Factory factory) {
- PropertyDescription[] props = factory.getComponentDescription().getProperties();
- for (int i = 0; i < props.length; i++) {
- if (props[i].getName().equalsIgnoreCase(name)) { return true; }
- }
- if (name.equalsIgnoreCase("name")) { return true; } // Skip the name property
- return false;
- }
-
- /**
- * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
- */
- public void serviceChanged(ServiceEvent ev) {
- if (ev.getType() == ServiceEvent.REGISTERED) {
- // Check the matching
- Factory fact = (Factory) m_context.getService(ev.getServiceReference());
- if (match(fact)) {
- m_usedRef.put(ev.getServiceReference(), null);
- if (m_isAggregate) { // Create an instance for the new factory
- createInstance(ev.getServiceReference());
- if (!m_isValid) { m_isValid = true; m_handler.validate(); }
- } else {
- if (!isAnInstanceCreated()) { createInstance(ev.getServiceReference()); }
- if (!m_isValid) { m_isValid = true; m_handler.validate(); }
- }
- }
- fact = null;
- m_context.ungetService(ev.getServiceReference());
- return;
- }
- if (ev.getType() == ServiceEvent.UNREGISTERING) {
- // Remove the ref is contained
- Object o = m_usedRef.remove(ev.getServiceReference());
- if (o != null) {
- stopInstance(ev.getServiceReference());
- if (m_usedRef.size() > 0) {
- if (!m_isAggregate) {
- createNextInstance(); // Create an instance with another factory
- }
- } else { // No more candidate
- if (!m_isOptional) { m_isValid = false; m_handler.invalidate(); }
- }
- }
- }
- }
-
- /**
- * @return the required specification.
- */
- public String getSpecification() {
- return m_specification;
- }
-
- /**
- * @return the map of used references.
- */
- protected HashMap getUsedReferences() {
- return m_usedRef;
- }
+
+ /**
+ * Required specification.
+ */
+ private String m_specification;
+
+ /**
+ * Configuration to push to the instance.
+ */
+ private Dictionary m_configuration;
+
+ /**
+ * Map of factory references => instance or NO_INSTANCE.
+ */
+ private HashMap /* ServiceReference */m_usedRef = new HashMap();
+
+ /**
+ * Does we instantiate several provider ?
+ */
+ private boolean m_isAggregate = false;
+
+ /**
+ * Is the service optional ?
+ */
+ private boolean m_isOptional = false;
+
+ /**
+ * Handler creating the service instance.
+ */
+ private ServiceInstantiatorHandler m_handler;
+
+ /**
+ * Service Context (internal scope).
+ */
+ private ServiceContext m_context;
+
+ /**
+ * True if the service instantiation is valid.
+ */
+ private boolean m_isValid = false;
+
+ /**
+ * String form of the factory filter.
+ */
+ private String m_filterStr;
+
+ /**
+ * Name of the last create instance.
+ */
+ private static Long m_index = new Long(0);
+
+ /**
+ * Constructor.
+ *
+ * @param h : the handler.
+ * @param spec : required specification.
+ * @param conf : instance configuration.
+ * @param isAgg : is the svc instance an aggregate service ?
+ * @param isOpt : is the svc instance optional ?
+ * @param filt : LDAP filter
+ */
+ public SvcInstance(ServiceInstantiatorHandler h, String spec, Dictionary conf, boolean isAgg, boolean isOpt, String filt) {
+ m_handler = h;
+ m_context = h.getManager().getServiceContext();
+ m_specification = spec;
+ m_configuration = conf;
+ m_isAggregate = isAgg;
+ m_isOptional = isOpt;
+ m_filterStr = filt;
+ }
+
+ /**
+ * Start the service instance.
+ *
+ * @param sc
+ */
+ public void start() {
+ initFactoryList();
+ // Register factory listener
+ try {
+ m_context.addServiceListener(this, m_filterStr);
+ } catch (InvalidSyntaxException e) {
+ e.printStackTrace(); // Should not happens
+ }
+
+ // Init the instances
+ if (m_usedRef.size() > 0) {
+ Set keys = m_usedRef.keySet();
+ Iterator it = keys.iterator();
+ if (m_isAggregate) {
+ while (it.hasNext()) {
+ ServiceReference ref = (ServiceReference) it.next();
+ createInstance(ref);
+ }
+ } else {
+ ServiceReference ref = (ServiceReference) it.next();
+ createInstance(ref);
+ }
+ }
+ m_isValid = isSatisfied();
+ }
+
+ /**
+ * Stop the service instance.
+ */
+ public void stop() {
+ m_context.removeServiceListener(this);
+ Set keys = m_usedRef.keySet();
+ Iterator it = keys.iterator();
+ while (it.hasNext()) {
+ ServiceReference ref = (ServiceReference) it.next();
+ Object o = m_usedRef.get(ref);
+ if (o != null) {
+ ((ComponentInstance) o).dispose();
+ }
+ }
+ m_usedRef.clear();
+ m_isValid = false;
+ }
+
+ /**
+ * Check if an instance is already created.
+ * @return true if at least one instance is created.
+ */
+ private boolean isAnInstanceCreated() {
+ Set keys = m_usedRef.keySet();
+ Iterator it = keys.iterator();
+ ServiceReference ref = (ServiceReference) it.next();
+ Object o = m_usedRef.get(ref);
+ return o != null;
+ }
+
+ /**
+ * Create an instance for the given reference.
+ * @param ref : the service reference to used to create the instance.
+ */
+ private void createInstance(ServiceReference ref) {
+ try {
+ Factory factory = (Factory) m_context.getService(ref);
+
+ // Add an unique name if not specified.
+ Properties p = new Properties();
+ Enumeration kk = m_configuration.keys();
+ while (kk.hasMoreElements()) {
+ String k = (String) kk.nextElement();
+ p.put(k, m_configuration.get(k));
+ }
+
+ if (p.get("name") == null) {
+ synchronized (m_index) {
+ p.put("name", this.toString() + "-" + m_index.longValue());
+ m_index = new Long(m_index.longValue() + 1);
+ }
+ }
+
+ ComponentInstance instance = factory.createComponentInstance(p);
+ m_usedRef.put(ref, instance);
+ m_context.ungetService(ref);
+ } catch (UnacceptableConfiguration e) {
+ m_handler.getManager().getFactory().getLogger().log(Logger.ERROR,
+ "A matching factory (" + ref.getProperty("service.pid") + ") seems to refuse the given configuration : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Create an instance for the next available factory.
+ */
+ private void createNextInstance() {
+ Set keys = m_usedRef.keySet();
+ Iterator it = keys.iterator();
+ ServiceReference ref = (ServiceReference) it.next();
+ try {
+ Factory factory = (Factory) m_context.getService(ref);
+
+ // Add an unique name if not specified.
+ Properties p = new Properties();
+ Enumeration kk = m_configuration.keys();
+ while (kk.hasMoreElements()) {
+ String k = (String) kk.nextElement();
+ p.put(k, m_configuration.get(k));
+ }
+
+ if (p.get("name") == null) {
+ synchronized (m_index) {
+ p.put("name", this.toString() + "-" + m_index.longValue());
+ m_index = new Long(m_index.longValue() + 1);
+ }
+ }
+
+ ComponentInstance instance = factory.createComponentInstance(p);
+ m_usedRef.put(ref, instance);
+ m_context.ungetService(ref);
+ } catch (UnacceptableConfiguration e) {
+ m_handler.getManager().getFactory().getLogger().log(Logger.ERROR,
+ "A matching factory (" + ref.getProperty("service.pid") + ") seems to refuse the given configuration : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Kill an instance (if exist).
+ * If an instance if created by using the given reference, this reference is disposed.
+ * @param ref : the leaving reference.
+ */
+ private void stopInstance(ServiceReference ref) {
+ Object o = m_usedRef.get(ref);
+ if (o != null) {
+ ((ComponentInstance) o).dispose();
+ }
+ }
+
+ /**
+ * Init the list of available factory.
+ */
+ public void initFactoryList() {
+ // Init factory list
+ try {
+ ServiceReference[] refs = m_context.getServiceReferences(Factory.class.getName(), m_filterStr);
+ if (refs == null) {
+ return;
+ }
+ for (int i = 0; i < refs.length; i++) {
+ ServiceReference ref = refs[i];
+ Factory fact = (Factory) m_context.getService(ref);
+ // Check provided spec & conf
+ if (match(fact)) {
+ m_usedRef.put(ref, null);
+ }
+ fact = null;
+ m_context.ungetService(ref);
+ }
+ } catch (InvalidSyntaxException e) {
+ e.printStackTrace(); // Should not happen
+ }
+ }
+
+ /**
+ * Check if the service isntance is statisfed.
+ * @return true if the service instance if satisfied.
+ */
+ public boolean isSatisfied() {
+ return m_isOptional || m_usedRef.size() > 0;
+ }
+
+ /**
+ * Does the service instance match with the given factory.
+ *
+ * @param fact : the factory to test.
+ * @return true if the factory match, false otherwise.
+ */
+ private boolean match(Factory fact) {
+ // Check if the factory can provide the spec
+ for (int i = 0; i < fact.getComponentDescription().getprovidedServiceSpecification().length; i++) {
+ if (fact.getComponentDescription().getprovidedServiceSpecification()[i].equals(m_specification)) {
+
+ // Check that the factory needs every properties contained in
+ // the configuration
+ Enumeration e = m_configuration.keys();
+ while (e.hasMoreElements()) {
+ String k = (String) e.nextElement();
+ if (!containsProperty(k, fact)) {
+ return false;
+ }
+ }
+
+ // Add an unique name if not specified.
+ Properties p = new Properties();
+ Enumeration keys = m_configuration.keys();
+ while (keys.hasMoreElements()) {
+ String k = (String) keys.nextElement();
+ p.put(k, m_configuration.get(k));
+ }
+
+ if (p.get("name") == null) {
+ synchronized (m_index) {
+ p.put("name", this.toString() + "-" + m_index.longValue());
+ m_index = new Long(m_index.longValue() + 1);
+ }
+ }
+
+ // Check the acceptability.
+ return fact.isAcceptable(p);
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Does the factory support the given property ?
+ *
+ * @param name : name of the property
+ * @param factory : factory to test
+ * @return true if the factory support this property
+ */
+ private boolean containsProperty(String name, Factory factory) {
+ PropertyDescription[] props = factory.getComponentDescription().getProperties();
+ for (int i = 0; i < props.length; i++) {
+ if (props[i].getName().equalsIgnoreCase(name)) {
+ return true;
+ }
+ }
+ if (name.equalsIgnoreCase("name")) {
+ return true;
+ } // Skip the name property
+ return false;
+ }
+
+ /**
+ * Service Listener Implementation.
+ * @param ev : the service event
+ * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
+ */
+ public void serviceChanged(ServiceEvent ev) {
+ if (ev.getType() == ServiceEvent.REGISTERED) {
+ // Check the matching
+ Factory fact = (Factory) m_context.getService(ev.getServiceReference());
+ if (match(fact)) {
+ m_usedRef.put(ev.getServiceReference(), null);
+ if (m_isAggregate) { // Create an instance for the new
+ // factory
+ createInstance(ev.getServiceReference());
+ if (!m_isValid) {
+ m_isValid = true;
+ m_handler.validate();
+ }
+ } else {
+ if (!isAnInstanceCreated()) {
+ createInstance(ev.getServiceReference());
+ }
+ if (!m_isValid) {
+ m_isValid = true;
+ m_handler.validate();
+ }
+ }
+ }
+ fact = null;
+ m_context.ungetService(ev.getServiceReference());
+ return;
+ }
+ if (ev.getType() == ServiceEvent.UNREGISTERING) {
+ // Remove the ref is contained
+ Object o = m_usedRef.remove(ev.getServiceReference());
+ if (o != null) {
+ stopInstance(ev.getServiceReference());
+ if (m_usedRef.size() > 0) {
+ if (!m_isAggregate) {
+ createNextInstance(); // Create an instance with
+ // another factory
+ }
+ } else { // No more candidate
+ if (!m_isOptional) {
+ m_isValid = false;
+ m_handler.invalidate();
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Get the required specification.
+ * @return the required specification.
+ */
+ public String getSpecification() {
+ return m_specification;
+ }
+
+ /**
+ * Get the map of used references [ref, component instance].
+ * @return the map of used references.
+ */
+ protected HashMap getUsedReferences() {
+ return m_usedRef;
+ }
}
Modified: incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/handlers/architecture/ArchitectureHandler.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/handlers/architecture/ArchitectureHandler.java?view=diff&rev=529623&r1=529622&r2=529623
==============================================================================
--- incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/handlers/architecture/ArchitectureHandler.java (original)
+++ incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/handlers/architecture/ArchitectureHandler.java Tue Apr 17 07:31:35 2007
@@ -32,6 +32,7 @@
/**
* Achtiecture Handler : do reflection on your component.
+ *
* @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
*/
public class ArchitectureHandler extends Handler implements Architecture {
@@ -42,7 +43,8 @@
private InstanceManager m_manager;
/**
- * Service Registration of the Architecture service provided by this handler.
+ * Service Registration of the Architecture service provided by this
+ * handler.
*/
private ServiceRegistration m_sr;
@@ -52,12 +54,20 @@
private String m_name;
/**
- * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.InstanceManager, org.apache.felix.ipojo.metadata.Element)
+ * Configure the handler.
+ *
+ * @param im : the instance manager
+ * @param metadata : the metadata of the component
+ * @param configuration : the instance configuration
+ * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.InstanceManager,
+ * org.apache.felix.ipojo.metadata.Element)
*/
public void configure(InstanceManager im, Element metadata, Dictionary configuration) {
if (metadata.containsAttribute("architecture")) {
String isArchitectureEnabled = (metadata.getAttribute("architecture")).toLowerCase();
- if (isArchitectureEnabled.equalsIgnoreCase("true")) { im.register(this); }
+ if (isArchitectureEnabled.equalsIgnoreCase("true")) {
+ im.register(this);
+ }
}
m_name = (String) configuration.get("name");
@@ -66,20 +76,27 @@
}
/**
+ * Stop method.
+ * Unregister the service if published.
* @see org.apache.felix.ipojo.Handler#stop()
*/
public void stop() {
- try {
- if (m_sr != null) { m_sr.unregister(); m_sr = null; }
- } catch (Exception e) { return; }
+ if (m_sr != null) {
+ m_sr.unregister();
+ m_sr = null;
+ }
}
/**
+ * Start method.
+ * Register the service.
* @see org.apache.felix.ipojo.Handler#start()
*/
public void start() {
// Unregister the service if already registred
- if (m_sr != null) { m_sr.unregister(); }
+ if (m_sr != null) {
+ m_sr.unregister();
+ }
// Register the ManagedService
BundleContext bc = m_manager.getContext();
@@ -92,6 +109,8 @@
}
/**
+ * Get the instance description.
+ * @return the instance description
* @see org.apache.felix.ipojo.architecture.Architecture#getComponentDescription()
*/
public InstanceDescription getInstanceDescription() {
Modified: incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurableProperty.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurableProperty.java?view=diff&rev=529623&r1=529622&r2=529623
==============================================================================
--- incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurableProperty.java (original)
+++ incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurableProperty.java Tue Apr 17 07:31:35 2007
@@ -23,9 +23,11 @@
import java.lang.reflect.InvocationTargetException;
import org.apache.felix.ipojo.parser.ParseUtils;
+import org.apache.felix.ipojo.util.Logger;
/**
* Configurable Property.
+ *
* @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
*/
public class ConfigurableProperty {
@@ -52,38 +54,61 @@
/**
* Configurable Property Constructor.
+ *
* @param name : name of the property (optional)
* @param field : name of the field (mandatory)
* @param value : initial value of the property (optional)
+ * @param type : the type of the property
* @param ch : configuration handler managing this configurable property
*/
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); }
+ if (value != null) {
+ setValue(m_field, value, type);
+ }
}
/**
* Set the value of the property.
+ *
+ * @param field : name of the field attached to the property
* @param strValue : value of the property (String)
+ * @param type : type of the property
*/
private void setValue(String field, String strValue, String type) {
Object value = null;
- if (type.equals("string") || type.equals("String")) { value = new String(strValue); }
- if (type.equals("boolean")) { value = new Boolean(strValue); }
- if (type.equals("byte")) { value = new Byte(strValue); }
- if (type.equals("short")) { value = new Short(strValue); }
- if (type.equals("int")) { value = new Integer(strValue); }
- if (type.equals("long")) { value = new Long(strValue); }
- if (type.equals("float")) { value = new Float(strValue); }
- if (type.equals("double")) { value = new Double(strValue); }
+ if (type.equals("string") || type.equals("String")) {
+ value = new String(strValue);
+ }
+ if (type.equals("boolean")) {
+ value = new Boolean(strValue);
+ }
+ if (type.equals("byte")) {
+ value = new Byte(strValue);
+ }
+ if (type.equals("short")) {
+ value = new Short(strValue);
+ }
+ if (type.equals("int")) {
+ value = new Integer(strValue);
+ }
+ if (type.equals("long")) {
+ value = new Long(strValue);
+ }
+ if (type.equals("float")) {
+ value = new Float(strValue);
+ }
+ if (type.equals("double")) {
+ value = new Double(strValue);
+ }
// Array :
if (type.endsWith("[]")) {
String internalType = type.substring(0, type.length() - 2);
@@ -92,35 +117,34 @@
}
if (value == null) {
- // Else it is a neither a primitive type neither a String -> create the object by calling a constructor with a string in argument.
+ // Else it is a neither a primitive type neither a String -> create
+ // the object by calling a constructor with a string in argument.
try {
Class c = m_handler.getInstanceManager().getContext().getBundle().loadClass(type);
- Constructor cst = c.getConstructor(new Class[] {String.class});
- value = cst.newInstance(new Object[] {strValue});
+ Constructor cst = c.getConstructor(new Class[] { String.class });
+ value = cst.newInstance(new Object[] { strValue });
} catch (ClassNotFoundException e) {
- System.err.println("Class not found exception in setValue on " + type);
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR,
+ "Class not found exception in setValue on " + type + " : " + e.getMessage());
return;
} catch (SecurityException e) {
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "Security excption in setValue on " + type + " : " + e.getMessage());
return;
} catch (NoSuchMethodException e) {
- System.err.println("Constructor not found exeption in setValue on " + type);
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR,
+ "Constructor not found exeption in setValue on " + type + " : " + e.getMessage());
return;
} catch (IllegalArgumentException e) {
- System.err.println("Argument problem to call the constructor of the type " + type);
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "Argument problem to call the constructor of the type " + type);
return;
} catch (InstantiationException e) {
- System.err.println("Instantiation problem " + type);
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "Instantiation problem " + type);
return;
} catch (IllegalAccessException e) {
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "Illegal Access " + type);
+ return;
} catch (InvocationTargetException e) {
- System.err.println("Invocation problem " + type);
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "Invocation problem " + type);
return;
}
}
@@ -131,109 +155,123 @@
/**
* Set array value to the current property.
+ *
* @param internalType : type of the property
* @param values : new property value
*/
private void setArrayValue(String internalType, String[] values) {
- if (internalType.equals("string") || internalType.equals("String")) {
- String[] str = new String[values.length];
- for (int i = 0; i < values.length; i++) { str[i] = new String(values[i]); }
- m_value = str;
- return;
+ if (internalType.equals("string") || internalType.equals("String")) {
+ String[] str = new String[values.length];
+ for (int i = 0; i < values.length; i++) {
+ str[i] = new String(values[i]);
+ }
+ m_value = str;
+ return;
}
if (internalType.equals("boolean")) {
boolean[] bool = new boolean[values.length];
- for (int i = 0; i < values.length; i++) { bool[i] = new Boolean(values[i]).booleanValue(); }
+ for (int i = 0; i < values.length; i++) {
+ bool[i] = new Boolean(values[i]).booleanValue();
+ }
m_value = bool;
return;
}
if (internalType.equals("byte")) {
byte[] byt = new byte[values.length];
- for (int i = 0; i < values.length; i++) { byt[i] = new Byte(values[i]).byteValue(); }
+ for (int i = 0; i < values.length; i++) {
+ byt[i] = new Byte(values[i]).byteValue();
+ }
m_value = byt;
return;
}
if (internalType.equals("short")) {
short[] shor = new short[values.length];
- for (int i = 0; i < values.length; i++) { shor[i] = new Short(values[i]).shortValue(); }
+ for (int i = 0; i < values.length; i++) {
+ shor[i] = new Short(values[i]).shortValue();
+ }
m_value = shor;
return;
}
if (internalType.equals("int")) {
int[] in = new int[values.length];
- for (int i = 0; i < values.length; i++) { in[i] = new Integer(values[i]).intValue(); }
+ for (int i = 0; i < values.length; i++) {
+ in[i] = new Integer(values[i]).intValue();
+ }
m_value = in;
return;
}
if (internalType.equals("long")) {
long[] ll = new long[values.length];
- for (int i = 0; i < values.length; i++) { ll[i] = new Long(values[i]).longValue(); }
+ for (int i = 0; i < values.length; i++) {
+ ll[i] = new Long(values[i]).longValue();
+ }
m_value = ll;
return;
}
if (internalType.equals("float")) {
float[] fl = new float[values.length];
- for (int i = 0; i < values.length; i++) { fl[i] = new Float(values[i]).floatValue(); }
+ 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(); }
+ for (int i = 0; i < values.length; i++) {
+ dl[i] = new Double(values[i]).doubleValue();
+ }
m_value = dl;
return;
}
- // Else it is a neither a primitive type neither a String -> create the object by calling a constructor with a string in argument.
+ // Else it is a neither a primitive type neither a String -> create the
+ // object by calling a constructor with a string in argument.
try {
Class c = m_handler.getInstanceManager().getContext().getBundle().loadClass(internalType);
- Constructor cst = c.getConstructor(new Class[] {String.class});
+ Constructor cst = c.getConstructor(new Class[] { String.class });
Object[] ob = (Object[]) Array.newInstance(c, values.length);
for (int i = 0; i < values.length; i++) {
- ob[i] = cst.newInstance(new Object[] {values[i].trim()});
+ ob[i] = cst.newInstance(new Object[] { values[i].trim() });
}
m_value = ob;
return;
} catch (ClassNotFoundException e) {
- System.err.println("Class not found exception in setValue on " + internalType);
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "Class not found exception in setValue on " + internalType);
} catch (SecurityException e) {
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "Secutiry Exception in setValue on " + internalType);
} catch (NoSuchMethodException e) {
- System.err.println("Constructor not found exeption in setValue on " + internalType);
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "Constructor not found exception in setValue on " + internalType);
} catch (IllegalArgumentException e) {
- System.err.println("Argument problem to call the constructor of the type " + internalType);
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "Argument problem to call the constructor of the type " + internalType);
} catch (InstantiationException e) {
- System.err.println("Instantiation problem " + internalType);
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "Instantiation problem " + internalType);
} catch (IllegalAccessException e) {
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "Illegal Access Exception in " + internalType);
} catch (InvocationTargetException e) {
- System.err.println("Invocation problem " + internalType);
- e.printStackTrace();
+ m_handler.getInstanceManager().getFactory().getLogger().log(Logger.ERROR, "Invocation problem " + internalType);
}
}
- /**
- * @return the name of the property.
- */
- public String getName() { return m_name; }
+ public String getName() {
+ return m_name;
+ }
- /**
- * @return the field of the property.
- */
- public String getField() { return m_field; }
+ public String getField() {
+ return m_field;
+ }
- /**
- * @return the value of the property.
- */
- public Object getValue() { return m_value; }
+
+ public Object getValue() {
+ return m_value;
+ }
/**
* Fix the value of the property.
+ *
* @param value : the new value.
*/
- public void setValue(Object value) { m_value = value; }
+ public void setValue(Object value) {
+ m_value = value;
+ }
}
Modified: incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java?view=diff&rev=529623&r1=529622&r2=529623
==============================================================================
--- incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java (original)
+++ incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java Tue Apr 17 07:31:35 2007
@@ -33,6 +33,7 @@
/**
* Handler managing the Configuration Admin.
+ *
* @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
*/
public class ConfigurationHandler extends Handler {
@@ -48,8 +49,8 @@
private ConfigurableProperty[] m_configurableProperties = new ConfigurableProperty[0];
/**
- * ProvidedServiceHandler of the component.
- * It is useful to priopagate properties to service registrations.
+ * ProvidedServiceHandler of the component. It is useful to priopagate
+ * properties to service registrations.
*/
private ProvidedServiceHandler m_providedServiceHandler;
@@ -57,8 +58,7 @@
* Properties propagated at the last "updated".
*/
private Dictionary m_propagated = new Properties();
-
-
+
/**
* Properties to propage.
*/
@@ -75,12 +75,21 @@
private ServiceRegistration m_sr;
/**
+ * Get the instance manager.
* @return instance manager of this handler.
*/
- protected InstanceManager getInstanceManager() { return m_manager; }
+ protected InstanceManager getInstanceManager() {
+ return m_manager;
+ }
/**
- * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.InstanceManager, org.apache.felix.ipojo.metadata.Element)
+ * Configure the handler.
+ *
+ * @param im : the instance manager
+ * @param metadata : the metadata of the component
+ * @param configuration : the instance configuration
+ * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.InstanceManager,
+ * org.apache.felix.ipojo.metadata.Element)
*/
public void configure(InstanceManager im, Element metadata, Dictionary configuration) {
// Store the component manager
@@ -90,34 +99,41 @@
// Build the hashmap
Element[] confs = metadata.getElements("Properties", "");
- if (confs.length == 0) { return; }
+ if (confs.length == 0) {
+ return;
+ }
// Check if the component is dynamically configurable
m_isConfigurable = false;
- if (confs[0].containsAttribute("configurable") && confs[0].getAttribute("configurable").equalsIgnoreCase("true")) { m_isConfigurable = true; m_toPropagate = configuration; }
+ 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 (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
+ // Detect the type of the property
Element manipulation = metadata.getElements("Manipulation")[0];
String type = null;
for (int kk = 0; kk < manipulation.getElements("Field").length; kk++) {
@@ -125,14 +141,18 @@
type = manipulation.getElements("Field")[kk].getAttribute("type");
}
}
- if (type == null) { m_manager.getFactory().getLogger().log(Logger.ERROR, "[" + m_manager.getClassName() + "] The field " + fieldName + " does not exist in the implementation"); return; }
-
+ if (type == null) {
+ m_manager.getFactory().getLogger().log(Logger.ERROR,
+ "[" + m_manager.getClassName() + "] The field " + fieldName + " does not exist in the implementation");
+ return;
+ }
+
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);
@@ -143,15 +163,16 @@
for (int k = 0; k < m_configurableProperties.length; k++) {
fields[k] = m_configurableProperties[k].getField();
- // Check if the instance configuration contains value for the current property :
+ // 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);
@@ -159,11 +180,16 @@
}
/**
+ * Stop method.
+ * Do nothing.
* @see org.apache.felix.ipojo.Handler#stop()
*/
- public void stop() { }
+ public void stop() {
+ }
/**
+ * Start method.
+ * Propagate properties if the propagation is activated.
* @see org.apache.felix.ipojo.Handler#start()
*/
public void start() {
@@ -172,14 +198,19 @@
// Propagation
if (m_isConfigurable) {
- for (int i = 0; i < m_configurableProperties.length; i++) {
- m_toPropagate.put(m_configurableProperties[i].getName(), m_configurableProperties[i].getValue());
- }
- reconfigure(m_toPropagate);
+ for (int i = 0; i < m_configurableProperties.length; i++) {
+ m_toPropagate.put(m_configurableProperties[i].getName(), m_configurableProperties[i].getValue());
+ }
+ reconfigure(m_toPropagate);
}
}
+
/**
+ * Setter Callback Method.
+ * Check if the modified field is a configurable property to update the value.
+ * @param fieldName : field name
+ * @param value : new value
* @see org.apache.felix.ipojo.Handler#setterCallback(java.lang.String, java.lang.Object)
*/
public void setterCallback(String fieldName, Object value) {
@@ -193,11 +224,17 @@
}
}
}
- //Else do nothing
+ // Else do nothing
}
/**
- * @see org.apache.felix.ipojo.Handler#getterCallback(java.lang.String, java.lang.Object)
+ * Getter Callback Method.
+ * Check if the field is a configurable property to push the stored value.
+ * @param fieldName : field name
+ * @param value : value pushed by the previous handler
+ * @return the stored value or the previous value.
+ * @see org.apache.felix.ipojo.Handler#getterCallback(java.lang.String,
+ * java.lang.Object)
*/
public Object getterCallback(String fieldName, Object value) {
// Check if the field is a configurable property
@@ -210,26 +247,35 @@
}
/**
- * @see org.apache.felix.ipojo.Handler#stateChanged(int)
+ * Handler state changed.
+ * @param state : the new instance state.
+ * @see org.apache.felix.ipojo.CompositeHandler#stateChanged(int)
*/
public void stateChanged(int state) {
if (state == InstanceManager.VALID) {
- if (m_sr == null) { start(); }
+ if (m_sr == null) {
+ start();
+ }
return;
}
if (state == InstanceManager.INVALID) {
- if (m_sr != null) { stop(); }
+ if (m_sr != null) {
+ stop();
+ }
return;
}
}
/**
* Add the given property metadata to the property metadata list.
+ *
* @param p : property metdata to add
*/
protected void addProperty(ConfigurableProperty p) {
for (int i = 0; (m_configurableProperties != null) && (i < m_configurableProperties.length); i++) {
- if (m_configurableProperties[i].getName().equals(p.getName())) { return; }
+ if (m_configurableProperties[i].getName().equals(p.getName())) {
+ return;
+ }
}
if (m_configurableProperties.length > 0) {
@@ -238,27 +284,33 @@
newProp[m_configurableProperties.length] = p;
m_configurableProperties = newProp;
} else {
- m_configurableProperties = new ConfigurableProperty[] {p};
+ m_configurableProperties = new ConfigurableProperty[] { p };
}
}
/**
* Check if the liste contains the property.
+ *
* @param name : name of the property
* @return true if the property exist in the list
*/
protected boolean containsProperty(String name) {
for (int i = 0; (m_configurableProperties != null) && (i < m_configurableProperties.length); i++) {
- if (m_configurableProperties[i].getName().equals(name)) { return true; }
+ if (m_configurableProperties[i].getName().equals(name)) {
+ return true;
+ }
}
return false;
}
-
+
/**
+ * Reconfigure the component instance.
+ * Check if the new configuration modify the current configuration.
+ * @param np : the new configuration
* @see org.apache.felix.ipojo.Handler#reconfigure(java.util.Dictionary)
*/
public void reconfigure(Dictionary np) {
- Properties toPropagate = new Properties();
+ Properties toPropagate = new Properties();
Enumeration keysEnumeration = np.keys();
while (keysEnumeration.hasMoreElements()) {
String name = (String) keysEnumeration.nextElement();
@@ -269,14 +321,19 @@
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
+ 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
+ // The property is not a configurable property
toPropagate.put(name, value);
}
}
@@ -284,10 +341,10 @@
// 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;
}