You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2007/09/25 15:27:54 UTC
svn commit: r579239 [5/8] - in /felix/trunk/ipojo: annotations/ ant/ arch/
arch/src/main/java/org/apache/felix/ipojo/arch/ arch/src/main/resources/
core/ core/src/main/java/org/apache/felix/ipojo/
core/src/main/java/org/apache/felix/ipojo/architecture/...
Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/importer/ServiceImporter.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/importer/ServiceImporter.java?rev=579239&r1=579238&r2=579239&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/importer/ServiceImporter.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/importer/ServiceImporter.java Tue Sep 25 06:27:49 2007
@@ -25,12 +25,11 @@
import org.apache.felix.ipojo.PolicyServiceContext;
import org.apache.felix.ipojo.ServiceContext;
+import org.apache.felix.ipojo.util.Tracker;
+import org.apache.felix.ipojo.util.TrackerCustomizer;
import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceEvent;
-import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
@@ -39,7 +38,7 @@
*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
-public class ServiceImporter implements ServiceListener {
+public class ServiceImporter implements TrackerCustomizer {
/**
* Destination context.
@@ -85,11 +84,16 @@
* Resolving policy.
*/
private int m_policy;
+
+ /**
+ * TRacker tracking imported service.
+ */
+ private Tracker m_tracker;
/**
* Reference on the handler.
*/
- private ImportExportHandler m_handler;
+ private ImportHandler m_handler;
private class Record {
/**
@@ -104,6 +108,20 @@
* Exposed Object.
*/
private Object m_svcObject;
+
+ /**
+ * Test object equality.
+ * @param o : object to confront against the current object.
+ * @return true if the two objects are equals (same service reference).
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ public boolean equals(Object o) {
+ if (o instanceof Record) {
+ Record rec = (Record) o;
+ return rec.m_ref == m_ref;
+ }
+ return false;
+ }
}
/**
@@ -135,7 +153,7 @@
* @param in : handler
*/
public ServiceImporter(String specification, String filter, boolean multiple, boolean optional, BundleContext from, ServiceContext to, int policy, String id,
- ImportExportHandler in) {
+ ImportHandler in) {
this.m_destination = to;
try {
this.m_filter = from.createFilter(filter);
@@ -165,44 +183,9 @@
* Start method to begin the import.
*/
public void start() {
- try {
- m_origin = new PolicyServiceContext(m_handler.getManager().getGlobalContext(), m_handler.getManager().getParentServiceContext(), m_policy);
- ServiceReference[] refs = m_origin.getServiceReferences(m_specification, null);
- if (refs != null) {
- for (int i = 0; i < refs.length; i++) {
- if (m_filter.match(refs[i])) {
- Record rec = new Record();
- rec.m_ref = refs[i];
- m_records.add(rec);
- }
- }
- }
- } catch (InvalidSyntaxException e) {
- e.printStackTrace();
- }
-
- // Publish available services
- if (m_records.size() > 0) {
- if (m_aggregate) {
- for (int i = 0; i < m_records.size(); i++) {
- Record rec = (Record) m_records.get(i);
- rec.m_svcObject = m_origin.getService(rec.m_ref);
- rec.m_reg = m_destination.registerService(m_specification, rec.m_svcObject, getProps(rec.m_ref));
- }
- } else {
- Record rec = (Record) m_records.get(0);
- rec.m_svcObject = m_origin.getService(rec.m_ref);
- rec.m_reg = m_destination.registerService(m_specification, rec.m_svcObject, getProps(rec.m_ref));
- }
- }
-
- // Register service listener
- try {
- m_origin.addServiceListener(this, "(" + Constants.OBJECTCLASS + "=" + m_specification + ")");
- } catch (InvalidSyntaxException e) {
- e.printStackTrace();
- }
-
+ m_origin = new PolicyServiceContext(m_handler.getCompositeManager().getGlobalContext(), m_handler.getCompositeManager().getParentServiceContext(), m_policy);
+ m_tracker = new Tracker(m_origin, m_filter, this);
+ m_tracker.open();
m_isValid = isSatisfied();
}
@@ -226,18 +209,19 @@
*/
public void stop() {
- m_origin.removeServiceListener(this);
+ m_tracker.close();
for (int i = 0; i < m_records.size(); i++) {
Record rec = (Record) m_records.get(i);
rec.m_svcObject = null;
if (rec.m_reg != null) {
rec.m_reg.unregister();
- m_origin.ungetService(rec.m_ref);
+ m_tracker.ungetService(rec.m_ref);
rec.m_ref = null;
}
}
-
+
+ m_tracker = null;
m_records.clear();
}
@@ -267,102 +251,6 @@
return l;
}
- /**
- * 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) {
- arrivalManagement(ev.getServiceReference());
- }
- if (ev.getType() == ServiceEvent.UNREGISTERING) {
- departureManagement(ev.getServiceReference());
- }
-
- if (ev.getType() == ServiceEvent.MODIFIED) {
- if (m_filter.match(ev.getServiceReference())) {
- // Test if the reference is always matching with the filter
- List l = getRecordsByRef(ev.getServiceReference());
- if (l.size() > 0) { // The reference is already contained => update the properties
- for (int i = 0; i < l.size(); i++) { // Stop the implied record
- Record rec = (Record) l.get(i);
- if (rec.m_reg != null) {
- rec.m_reg.setProperties(getProps(rec.m_ref));
- }
- }
- } else { // it is a new matching service => add it
- arrivalManagement(ev.getServiceReference());
- }
- } else {
- List l = getRecordsByRef(ev.getServiceReference());
- if (l.size() > 0) { // The reference is already contained => the service does no more match
- departureManagement(ev.getServiceReference());
- }
- }
- }
- }
-
- /**
- * Manage the arrival of a consistent service.
- * @param ref : the arrival service reference
- */
- private void arrivalManagement(ServiceReference ref) {
- // Check if the new service match
- if (m_filter.match(ref)) {
- // Add it to the record list
- Record rec = new Record();
- rec.m_ref = ref;
- m_records.add(rec);
- // Publishing ?
- if (m_records.size() == 1 || m_aggregate) { // If the service is the first one, or if it is a multiple imports
- rec.m_svcObject = m_origin.getService(rec.m_ref);
- rec.m_reg = m_destination.registerService(m_specification, rec.m_svcObject, getProps(rec.m_ref));
- }
- // Compute the new state
- if (!m_isValid && isSatisfied()) {
- m_isValid = true;
- m_handler.validating(this);
- }
- }
- }
-
- /**
- * Manage the departure of a used reference.
- *
- * @param ref : the leaving reference
- */
- private void departureManagement(ServiceReference ref) {
- List l = getRecordsByRef(ref);
- for (int i = 0; i < l.size(); i++) { // Stop the implied record
- Record rec = (Record) l.get(i);
- if (rec.m_reg != null) {
- rec.m_svcObject = null;
- rec.m_reg.unregister();
- rec.m_reg = null;
- m_origin.ungetService(rec.m_ref);
- }
- }
- m_records.removeAll(l);
-
- // Check the validity & if we need to re-import the service
- if (m_records.size() > 0) {
- // There is other available services
- if (!m_aggregate) { // Import the next one
- Record rec = (Record) m_records.get(0);
- if (rec.m_svcObject == null) { // It is the first service which disappears - create the next one
- rec.m_svcObject = m_origin.getService(rec.m_ref);
- rec.m_reg = m_destination.registerService(m_specification, rec.m_svcObject, getProps(rec.m_ref));
- }
- }
- } else {
- if (!m_optional) {
- m_isValid = false;
- m_handler.invalidating(this);
- }
- }
- }
-
public String getSpecification() {
return m_specification;
}
@@ -374,7 +262,7 @@
protected List getProviders() {
List l = new ArrayList();
for (int i = 0; i < m_records.size(); i++) {
- l.add((((Record) m_records.get(i)).m_ref).getProperty(Constants.SERVICE_PID));
+ l.add((((Record) m_records.get(i)).m_ref).getProperty("instance.name"));
}
return l;
@@ -408,6 +296,87 @@
public boolean isOptional() {
return m_optional;
+ }
+
+ /**
+ * A new service is detected.
+ * @param reference : service reference
+ * @return true if not already imported.
+ * @see org.apache.felix.ipojo.util.TrackerCustomizer#addingService(org.osgi.framework.ServiceReference)
+ */
+ public boolean addingService(ServiceReference reference) {
+ // Else add it to the record list
+ Record rec = new Record();
+ rec.m_ref = reference;
+ if (m_records.contains(rec)) {
+ return false;
+ }
+
+ m_records.add(rec);
+ // Publishing ?
+ if (m_records.size() == 1 || m_aggregate) { // If the service is the first one, or if it is a multiple imports
+ rec.m_svcObject = m_tracker.getService(rec.m_ref);
+ rec.m_reg = m_destination.registerService(m_specification, rec.m_svcObject, getProps(rec.m_ref));
+ }
+ // Compute the new state
+ if (!m_isValid && isSatisfied()) {
+ m_isValid = true;
+ m_handler.validating(this);
+ }
+ return true;
+ }
+
+ /**
+ * An imported service was modified.
+ * @param reference : service reference
+ * @param service : service object (if already get)
+ * @see org.apache.felix.ipojo.util.TrackerCustomizer#modifiedService(org.osgi.framework.ServiceReference, java.lang.Object)
+ */
+ public void modifiedService(ServiceReference reference, Object service) {
+ List l = getRecordsByRef(reference);
+ for (int i = 0; i < l.size(); i++) { // Stop the implied record
+ Record rec = (Record) l.get(i);
+ if (rec.m_reg != null) {
+ rec.m_reg.setProperties(getProps(rec.m_ref));
+ }
+ }
+ }
+
+ /**
+ * An imported service disappears.
+ *@param reference : service reference
+ * @param service : service object (if already get)
+ * @see org.apache.felix.ipojo.util.TrackerCustomizer#removedService(org.osgi.framework.ServiceReference, java.lang.Object)
+ */
+ public void removedService(ServiceReference reference, Object service) {
+ List l = getRecordsByRef(reference);
+ for (int i = 0; i < l.size(); i++) { // Stop the implied record
+ Record rec = (Record) l.get(i);
+ if (rec.m_reg != null) {
+ rec.m_svcObject = null;
+ rec.m_reg.unregister();
+ rec.m_reg = null;
+ m_tracker.ungetService(rec.m_ref);
+ }
+ }
+ m_records.removeAll(l);
+
+ // Check the validity & if we need to re-import the service
+ if (m_records.size() > 0) {
+ // There is other available services
+ if (!m_aggregate) { // Import the next one
+ Record rec = (Record) m_records.get(0);
+ if (rec.m_svcObject == null) { // It is the first service which disappears - create the next one
+ rec.m_svcObject = m_tracker.getService(rec.m_ref);
+ rec.m_reg = m_destination.registerService(m_specification, rec.m_svcObject, getProps(rec.m_ref));
+ }
+ }
+ } else {
+ if (!m_optional) {
+ m_isValid = false;
+ m_handler.invalidating(this);
+ }
+ }
}
}
Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/ServiceInstantiatorDescription.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/ServiceInstantiatorDescription.java?rev=579239&r1=579238&r2=579239&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/ServiceInstantiatorDescription.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/ServiceInstantiatorDescription.java Tue Sep 25 06:27:49 2007
@@ -24,6 +24,7 @@
import java.util.Set;
import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.CompositeHandler;
import org.apache.felix.ipojo.architecture.HandlerDescription;
import org.apache.felix.ipojo.metadata.Attribute;
import org.apache.felix.ipojo.metadata.Element;
@@ -44,12 +45,11 @@
/**
* Constructor.
*
- * @param arg0 : name of the handler
- * @param arg1 : validity of the handler
+ * @param h : composite handler
* @param insts : list of service instance
*/
- public ServiceInstantiatorDescription(String arg0, boolean arg1, List insts) {
- super(arg0, arg1);
+ public ServiceInstantiatorDescription(CompositeHandler h, List insts) {
+ super(h);
m_instances = insts;
}
@@ -77,7 +77,7 @@
Object o = map.get(ref);
if (o != null) {
Element fact = new Element("Factory", "");
- fact.addAttribute(new Attribute("Name", ((ComponentInstance) o).getComponentDescription().getName()));
+ fact.addAttribute(new Attribute("Name", ((ComponentInstance) o).getFactory().getName()));
service.addElement(fact);
}
}
Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/ServiceInstantiatorHandler.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/ServiceInstantiatorHandler.java?rev=579239&r1=579238&r2=579239&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/ServiceInstantiatorHandler.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/ServiceInstantiatorHandler.java Tue Sep 25 06:27:49 2007
@@ -24,7 +24,7 @@
import java.util.Properties;
import org.apache.felix.ipojo.CompositeHandler;
-import org.apache.felix.ipojo.CompositeManager;
+import org.apache.felix.ipojo.ConfigurationException;
import org.apache.felix.ipojo.Factory;
import org.apache.felix.ipojo.architecture.HandlerDescription;
import org.apache.felix.ipojo.metadata.Element;
@@ -38,14 +38,10 @@
public class ServiceInstantiatorHandler extends CompositeHandler {
/**
- * Composite Manager.
- */
- private CompositeManager m_manager;
-
- /**
* Is the handler valid ?
+ * (Lifecycle controller)
*/
- private boolean m_isValid = false;
+ private boolean m_isValid;
/**
* List of instances to manage.
@@ -55,25 +51,27 @@
/**
* Configure the handler.
*
- * @param im : the instance manager
* @param metadata : the metadata of the component
* @param conf : the instance configuration
+ * @throws ConfigurationException : the specification attribute is missing
* @see org.apache.felix.ipojo.CompositeHandler#configure(org.apache.felix.ipojo.CompositeManager,
* org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
*/
- public void configure(CompositeManager im, Element metadata, Dictionary conf) {
- m_manager = im;
+ public void configure(Element metadata, Dictionary conf) throws ConfigurationException {
Element[] services = metadata.getElements("service");
for (int i = 0; i < services.length; i++) {
+ if (!services[i].containsAttribute("specification")) {
+ throw new ConfigurationException("Malformed service : the specification attribute is mandatory", getCompositeManager().getFactory().getName());
+ }
String spec = services[i].getAttribute("specification");
- String filter = "(&(objectClass=" + Factory.class.getName() + ")(!(service.pid=" + m_manager.getComponentDescription().getName() + ")))"; // Cannot reinstantiate yourself
+ String filter = "(&(objectClass=" + Factory.class.getName() + ")(!(factory.name=" + getCompositeManager().getFactory().getComponentDescription().getName() + "))(factory.state=1))"; // Cannot reinstantiate yourself
if (services[i].containsAttribute("filter")) {
- String classnamefilter = "(&(objectClass=" + Factory.class.getName() + ")(!(service.pid=" + m_manager.getComponentDescription().getName() + ")))"; // Cannot reinstantiate yourself
- filter = "";
- if (!services[i].getAttribute("filter").equals("")) {
- filter = "(&" + classnamefilter + services[i].getAttribute("filter") + ")";
- } else {
+ String classnamefilter = "(&(objectClass=" + Factory.class.getName() + ")(!(factory.name=" + getCompositeManager().getFactory().getComponentDescription().getName() + "))(factory.state=1))"; // Cannot reinstantiate yourself
+ filter = null;
+ if ("".equals(services[i].getAttribute("filter"))) {
filter = classnamefilter;
+ } else {
+ filter = "(&" + classnamefilter + services[i].getAttribute("filter") + ")";
}
}
Properties prop = new Properties();
@@ -93,9 +91,6 @@
SvcInstance inst = new SvcInstance(this, spec, prop, agg, opt, filter);
m_instances.add(inst);
}
- if (m_instances.size() > 0) {
- m_manager.register(this);
- }
}
/**
@@ -110,7 +105,7 @@
inst.start();
}
- m_isValid = isValid();
+ m_isValid = isHandlerValid();
}
/**
@@ -118,7 +113,7 @@
* @return true if all created service instances are valid
* @see org.apache.felix.ipojo.CompositeHandler#isValid()
*/
- public boolean isValid() {
+ private boolean isHandlerValid() {
for (int i = 0; i < m_instances.size(); i++) {
SvcInstance inst = (SvcInstance) m_instances.get(i);
if (!inst.isSatisfied()) {
@@ -146,8 +141,7 @@
*/
public void validate() {
if (!m_isValid) {
- if (isValid()) {
- m_manager.checkInstanceState();
+ if (isHandlerValid()) {
m_isValid = true;
}
}
@@ -158,8 +152,7 @@
*/
public void invalidate() {
if (m_isValid) {
- if (!isValid()) {
- m_manager.checkInstanceState();
+ if (!isHandlerValid()) {
m_isValid = false;
}
}
@@ -171,15 +164,7 @@
* @see org.apache.felix.ipojo.CompositeHandler#getDescription()
*/
public HandlerDescription getDescription() {
- return new ServiceInstantiatorDescription(this.getClass().getName(), isValid(), m_instances);
- }
-
- /**
- * Get the composite manager.
- * @return the composite manager.
- */
- protected CompositeManager getManager() {
- return m_manager;
+ return new ServiceInstantiatorDescription(this, m_instances);
}
public List getInstances() {
Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/SvcInstance.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/SvcInstance.java?rev=579239&r1=579238&r2=579239&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/SvcInstance.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/SvcInstance.java Tue Sep 25 06:27:49 2007
@@ -27,14 +27,16 @@
import java.util.Set;
import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.ConfigurationException;
import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.MissingHandlerException;
import org.apache.felix.ipojo.ServiceContext;
import org.apache.felix.ipojo.UnacceptableConfiguration;
import org.apache.felix.ipojo.metadata.Element;
import org.apache.felix.ipojo.util.Logger;
+import org.apache.felix.ipojo.util.Tracker;
+import org.apache.felix.ipojo.util.TrackerCustomizer;
import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceEvent;
-import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
/**
@@ -43,7 +45,7 @@
*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
-public class SvcInstance implements ServiceListener {
+public class SvcInstance implements TrackerCustomizer {
/**
* Required specification.
@@ -86,13 +88,12 @@
private boolean m_isValid = false;
/**
- * String form of the factory filter.
+ * Tracker used to track required factory.
*/
- private String m_filterStr;
+ private Tracker m_tracker;
/**
* Constructor.
- *
* @param h : the handler.
* @param spec : required specification.
* @param conf : instance configuration.
@@ -102,42 +103,24 @@
*/
public SvcInstance(ServiceInstantiatorHandler h, String spec, Dictionary conf, boolean isAgg, boolean isOpt, String filt) {
m_handler = h;
- m_context = h.getManager().getServiceContext();
+ m_context = h.getCompositeManager().getServiceContext();
m_specification = spec;
m_configuration = conf;
m_isAggregate = isAgg;
m_isOptional = isOpt;
- m_filterStr = filt;
+ try {
+ m_tracker = new Tracker(m_context, h.getCompositeManager().getContext().createFilter(filt), this);
+ } catch (InvalidSyntaxException e) {
+ e.printStackTrace();
+ }
}
/**
* 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
- }
-
- // Initialize 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_tracker.open();
m_isValid = isSatisfied();
}
@@ -145,7 +128,8 @@
* Stop the service instance.
*/
public void stop() {
- m_context.removeServiceListener(this);
+ m_tracker.close();
+
Set keys = m_usedRef.keySet();
Iterator it = keys.iterator();
while (it.hasNext()) {
@@ -156,6 +140,7 @@
}
}
m_usedRef.clear();
+ m_tracker = null;
m_isValid = false;
}
@@ -166,33 +151,39 @@
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;
+ while (it.hasNext()) {
+ if (m_usedRef.get(it.next()) != null) {
+ return true;
+ }
+ }
+ return false;
}
/**
* Create an instance for the given reference.
- * @param ref : the service reference to used to create the instance.
- */
- private void createInstance(ServiceReference ref) {
+ * The instance is not added inside the map.
+ * @param factory : the factory from which we need to create the instance.
+ * @return the created component instance.
+ */
+ private ComponentInstance createInstance(Factory factory) {
+ // 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));
+ }
+ ComponentInstance instance = null;
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));
- }
- ComponentInstance instance = factory.createComponentInstance(p);
- m_usedRef.put(ref, instance);
- m_context.ungetService(ref);
- } catch (Throwable e) {
- m_handler.getManager().getFactory().getLogger().log(Logger.ERROR,
- "A matching factory (" + ref.getProperty("service.pid") + ") seems to refuse the given configuration : " + e.getMessage());
+ instance = factory.createComponentInstance(p);
+ } catch (UnacceptableConfiguration e) {
+ e.printStackTrace();
+ } catch (MissingHandlerException e) {
+ e.printStackTrace();
+ } catch (ConfigurationException e) {
+ e.printStackTrace();
}
+ return instance;
}
/**
@@ -217,50 +208,18 @@
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());
+ m_handler.log(Logger.ERROR, "A matching factory (" + ref.getProperty("instance.name") + ") seems to refuse the given configuration : " + e.getMessage());
+ } catch (MissingHandlerException e) {
+ m_handler.log(Logger.ERROR, "A matching factory (" + ref.getProperty("instance.name") + ") seems to refuse the given configuration : " + e.getMessage());
+ } catch (ConfigurationException e) {
+ m_handler.log(Logger.ERROR, "A matching factory (" + ref.getProperty("instance.name") + ") 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();
- }
- }
- /**
- * Initialize the list of available factory.
- */
- public void initFactoryList() {
- // Initialize 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 specification & configuration
- 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 instance is satisfed.
+ * Check if the service instance is satisfied.
* @return true if the service instance if satisfied.
*/
public boolean isSatisfied() {
@@ -325,58 +284,6 @@
}
/**
- * 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 reference 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.
*/
@@ -398,6 +305,73 @@
*/
protected Map getUsedReferences() {
return m_usedRef;
+ }
+
+ /**
+ * A factory potentially matching with the managed instance appears.
+ * @param reference : service reference
+ * @return : true if the factory match
+ * @see org.apache.felix.ipojo.util.TrackerCustomizer#addingService(org.osgi.framework.ServiceReference)
+ */
+ public boolean addingService(ServiceReference reference) {
+ Factory fact = (Factory) m_tracker.getService(reference);
+ if (match(fact)) {
+ if (m_isAggregate) { // Create an instance for the new factory
+ m_usedRef.put(reference, createInstance(fact));
+ if (!m_isValid) {
+ m_isValid = true;
+ m_handler.validate();
+ }
+ } else {
+ if (!isAnInstanceCreated()) {
+ m_usedRef.put(reference, createInstance(fact));
+ } else {
+ m_usedRef.put(reference, null); // Store the reference
+ }
+ if (!m_isValid) {
+ m_isValid = true;
+ m_handler.validate();
+ }
+ }
+ m_tracker.ungetService(reference);
+ return true;
+ } else {
+ m_tracker.ungetService(reference);
+ return false;
+ }
+
+ }
+
+ /**
+ * A used factory was modified.
+ * @param reference : service reference
+ * @param service : object if already get
+ * @see org.apache.felix.ipojo.util.TrackerCustomizer#modifiedService(org.osgi.framework.ServiceReference, java.lang.Object)
+ */
+ public void modifiedService(ServiceReference reference, Object service) { }
+
+ /**
+ * A used factory disappears.
+ * @param reference : service reference
+ * @param service : object if already get
+ * @see org.apache.felix.ipojo.util.TrackerCustomizer#removedService(org.osgi.framework.ServiceReference, java.lang.Object)
+ */
+ public void removedService(ServiceReference reference, Object service) {
+ // Remove the reference is contained
+ Object o = m_usedRef.remove(reference);
+ if (o != null) {
+ ((ComponentInstance) o).dispose();
+ 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();
+ }
+ }
+ }
}
}
Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/CompositionMetadata.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/CompositionMetadata.java?rev=579239&r1=579238&r2=579239&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/CompositionMetadata.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/CompositionMetadata.java Tue Sep 25 06:27:49 2007
@@ -33,7 +33,6 @@
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.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
@@ -95,7 +94,7 @@
String methodName = mappings[i].getAttribute("method");
MethodMetadata method = m_specification.getMethodByName(methodName);
if (method == null) {
- m_handler.getManager().getFactory().getLogger().log(Logger.ERROR, "The method " + methodName + " does not exist in the specicifation " + spec);
+ m_handler.log(Logger.ERROR, "The method " + methodName + " does not exist in the specicifation " + spec);
return;
}
@@ -127,9 +126,9 @@
for (int i = 0; i < m_handler.getInstanceType().size(); i++) {
String type = (String) m_handler.getInstanceType().get(i);
try {
- ServiceReference[] refs = m_context.getServiceReferences(Factory.class.getName(), "(" + Constants.SERVICE_PID + "=" + type + ")");
+ ServiceReference[] refs = m_context.getServiceReferences(Factory.class.getName(), "(factory.name=" + type + ")");
if (refs == null) {
- m_handler.getManager().getFactory().getLogger().log(Logger.ERROR, "The factory " + type + " is not available, cannot check the composition");
+ m_handler.log(Logger.ERROR, "The factory " + type + " is not available, cannot check the composition");
throw new CompositionException("The factory " + type + " needs to be available to check the composition");
} else {
String className = (String) refs[0].getProperty("component.class");
@@ -142,9 +141,9 @@
index++;
}
} catch (InvalidSyntaxException e) {
- m_handler.getManager().getFactory().getLogger().log(Logger.ERROR, "A LDAP filter is not valid : " + e.getMessage());
+ m_handler.log(Logger.ERROR, "A LDAP filter is not valid : " + e.getMessage());
} catch (ClassNotFoundException e) {
- m_handler.getManager().getFactory().getLogger().log(Logger.ERROR, "The implementation class of a component cannot be loaded : " + e.getMessage());
+ m_handler.log(Logger.ERROR, "The implementation class of a component cannot be loaded : " + e.getMessage());
}
}
@@ -207,7 +206,7 @@
if (!found) { // If not found looks inside method contained in services.
keys = availableSvcMethods.keySet(); // Look first in methods contained in the glue code
it = keys.iterator();
- while (it.hasNext() & !found) {
+ while (!found && it.hasNext()) {
MethodMetadata met = (MethodMetadata) it.next();
if (met.equals(method)) {
found = true;
@@ -216,7 +215,7 @@
method.setDelegation(field);
// Test optional
if (field.isOptional() && !method.getExceptions().contains("java/lang/UnsupportedOperationException")) {
- m_handler.getManager().getFactory().getLogger().log(Logger.WARNING, "The method " + method.getMethodName() + " could not be provided correctly : the specification " + field.getSpecification().getName() + " is optional");
+ m_handler.log(Logger.WARNING, "The method " + method.getMethodName() + " could not be provided correctly : the specification " + field.getSpecification().getName() + " is optional");
}
}
}
@@ -243,7 +242,7 @@
} catch (IOException e) {
e.printStackTrace();
}
- return null;
+ return new byte[0];
}
/**
@@ -257,6 +256,9 @@
Attribute factory = new Attribute("factory", "false");
elem.addAttribute(className);
elem.addAttribute(factory);
+
+ // Add architcture for debug
+ elem.addAttribute(new Attribute("architecture", "true"));
// Provides
Element provides = new Element("provides", "");
@@ -274,7 +276,7 @@
if (field.getSpecification().isOptional()) {
dep.addAttribute(new Attribute("optional", "true"));
}
- dep.addAttribute(new Attribute("filter", "(!(service.pid=" + in + "))"));
+ dep.addAttribute(new Attribute("filter", "(!(instance.name=" + in + "))"));
elem.addElement(dep);
}
}
@@ -294,7 +296,7 @@
// Insert information to metadata
elem.addElement(m_manipulationMetadata);
-
+
return elem;
}
Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/POJOWriter.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/POJOWriter.java?rev=579239&r1=579238&r2=579239&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/POJOWriter.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/POJOWriter.java Tue Sep 25 06:27:49 2007
@@ -169,26 +169,7 @@
}
}
- if (!delegator.isAggregate()) {
- mv.visitVarInsn(ALOAD, 0);
- mv.visitFieldInsn(GETFIELD, className, delegator.getName(), "L" + delegator.getSpecification().getName().replace('.', '/') + ";");
-
- // Loads args
- Type[] args = Type.getArgumentTypes(desc);
- for (int i = 0; i < args.length; i++) {
- writeLoad(args[i], i + 1, mv);
- }
-
- // Invoke
- if (delegator.getSpecification().isInterface()) {
- mv.visitMethodInsn(INVOKEINTERFACE, delegator.getSpecification().getName().replace('.', '/'), name, desc);
- } else {
- mv.visitMethodInsn(INVOKEVIRTUAL, delegator.getSpecification().getName().replace('.', '/'), name, desc);
- }
-
- // Return
- writeReturn(Type.getReturnType(desc), mv);
- } else {
+ if (delegator.isAggregate()) {
if (method.getPolicy() == MethodMetadata.ONE_POLICY) {
// Aggregate and One Policy
mv.visitVarInsn(ALOAD, 0);
@@ -254,6 +235,25 @@
mv.visitLabel(l5b);
mv.visitInsn(RETURN);
}
+ } else {
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitFieldInsn(GETFIELD, className, delegator.getName(), "L" + delegator.getSpecification().getName().replace('.', '/') + ";");
+
+ // Loads args
+ Type[] args = Type.getArgumentTypes(desc);
+ for (int i = 0; i < args.length; i++) {
+ writeLoad(args[i], i + 1, mv);
+ }
+
+ // Invoke
+ if (delegator.getSpecification().isInterface()) {
+ mv.visitMethodInsn(INVOKEINTERFACE, delegator.getSpecification().getName().replace('.', '/'), name, desc);
+ } else {
+ mv.visitMethodInsn(INVOKEVIRTUAL, delegator.getSpecification().getName().replace('.', '/'), name, desc);
+ }
+
+ // Return
+ writeReturn(Type.getReturnType(desc), mv);
}
mv.visitMaxs(0, 0);
Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedService.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedService.java?rev=579239&r1=579238&r2=579239&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedService.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedService.java Tue Sep 25 06:27:49 2007
@@ -24,13 +24,14 @@
import org.apache.felix.ipojo.ComponentFactory;
import org.apache.felix.ipojo.ComponentInstance;
import org.apache.felix.ipojo.CompositeManager;
+import org.apache.felix.ipojo.ConfigurationException;
+import org.apache.felix.ipojo.MissingHandlerException;
import org.apache.felix.ipojo.ServiceContext;
import org.apache.felix.ipojo.UnacceptableConfiguration;
import org.apache.felix.ipojo.composite.instance.InstanceHandler;
import org.apache.felix.ipojo.metadata.Element;
import org.apache.felix.ipojo.util.Logger;
import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
/**
* Composite Provided Service.
@@ -96,7 +97,7 @@
* @param name : name of this provided service.
*/
public ProvidedService(ProvidedServiceHandler handler, Element element, String name) {
- m_manager = handler.getManager();
+ m_manager = handler.getCompositeManager();
m_scope = m_manager.getServiceContext();
m_context = m_manager.getContext();
m_composition = new CompositionMetadata(m_manager.getContext(), element, handler, name);
@@ -110,7 +111,7 @@
public void start() throws CompositionException {
m_composition.buildMapping();
- m_instanceName = m_composition.getSpecificationMetadata().getName() + "Provider";
+ m_instanceName = m_composition.getSpecificationMetadata().getName() + "Provider-Gen";
m_clazz = m_composition.buildPOJO();
m_metadata = m_composition.buildMetadata(m_instanceName);
@@ -119,7 +120,7 @@
m_factory.start();
// Create the exports
- m_exports = new ServiceExporter(m_composition.getSpecificationMetadata().getName(), "(" + Constants.SERVICE_PID + "=" + m_instanceName + ")", false, false,
+ m_exports = new ServiceExporter(m_composition.getSpecificationMetadata().getName(), "(instance.name=" + m_instanceName + ")", false, false,
m_scope, m_context, this);
m_exports.start();
}
@@ -189,10 +190,13 @@
}
}
try {
- m_instance = m_factory.createComponentInstance(p, m_scope);
+ m_instance = m_factory.createComponentInstance(p, m_manager.getServiceContext());
} catch (UnacceptableConfiguration e) {
e.printStackTrace();
- return;
+ } catch (MissingHandlerException e) {
+ e.printStackTrace();
+ } catch (ConfigurationException e) {
+ e.printStackTrace();
}
}
}
Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedServiceHandler.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedServiceHandler.java?rev=579239&r1=579238&r2=579239&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedServiceHandler.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedServiceHandler.java Tue Sep 25 06:27:49 2007
@@ -25,11 +25,15 @@
import org.apache.felix.ipojo.ComponentInstance;
import org.apache.felix.ipojo.CompositeHandler;
-import org.apache.felix.ipojo.CompositeManager;
+import org.apache.felix.ipojo.ConfigurationException;
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.HandlerManager;
+import org.apache.felix.ipojo.IPojoConfiguration;
import org.apache.felix.ipojo.PolicyServiceContext;
+import org.apache.felix.ipojo.architecture.ComponentDescription;
import org.apache.felix.ipojo.architecture.HandlerDescription;
import org.apache.felix.ipojo.composite.instance.InstanceHandler;
-import org.apache.felix.ipojo.composite.service.importer.ImportExportHandler;
+import org.apache.felix.ipojo.composite.service.importer.ImportHandler;
import org.apache.felix.ipojo.composite.service.importer.ServiceImporter;
import org.apache.felix.ipojo.composite.service.instantiator.ServiceInstantiatorHandler;
import org.apache.felix.ipojo.composite.service.instantiator.SvcInstance;
@@ -38,6 +42,7 @@
import org.apache.felix.ipojo.parser.ParseException;
import org.apache.felix.ipojo.util.Logger;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
/**
* Composite Provided Service Handler.
@@ -46,11 +51,6 @@
public class ProvidedServiceHandler extends CompositeHandler {
/**
- * Reference on the instance.
- */
- private CompositeManager m_manager;
-
- /**
* External context.
*/
private BundleContext m_context;
@@ -66,7 +66,8 @@
private List m_managedServices = new ArrayList();
/**
- * Handler validity. False if
+ * Handler validity.
+ * (Lifecycle controller)
*/
private boolean m_valid = false;
@@ -76,40 +77,42 @@
private List m_types;
/**
+ * Initialize the component type.
+ * @param cd : component type description to populate.
+ * @param metadata : component type metadata.
+ * @throws ConfigurationException : metadata are incorrect.
+ * @see org.apache.felix.ipojo.Handler#initializeComponentFactory(org.apache.felix.ipojo.architecture.ComponentDescription, org.apache.felix.ipojo.metadata.Element)
+ */
+ public void initializeComponentFactory(ComponentDescription cd, Element metadata) throws ConfigurationException {
+ Element[] provides = metadata.getElements("provides", "");
+ for (int i = 0; i < provides.length; i++) {
+ if (provides[i].containsAttribute("specification")) {
+ String spec = provides[i].getAttribute("specification");
+ cd.addProvidedServiceSpecification(spec);
+ } else {
+ throw new ConfigurationException("Malformed provides : the specification attribute is mandatory", getCompositeManager().getFactory().getName());
+ }
+ }
+ }
+
+ /**
* 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.CompositeHandler#configure(org.apache.felix.ipojo.CompositeManager,
* org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
*/
- public void configure(CompositeManager im, Element metadata, Dictionary configuration) {
- m_manager = im;
- m_context = im.getContext();
+ public void configure(Element metadata, Dictionary configuration) {
+ m_context = getCompositeManager().getContext();
// Get composition metadata
Element[] provides = metadata.getElements("provides", "");
- if (provides.length == 0) {
- return;
- }
-
+ if (provides.length == 0) { return; }
+
for (int i = 0; i < provides.length; i++) {
ProvidedService ps = new ProvidedService(this, provides[i], "" + i);
m_managedServices.add(ps);
- // Check requirements against the service specification
- if (!checkServiceSpecification(ps)) {
- return;
- }
- im.getComponentDescription().addProvidedServiceSpecification(ps.getSpecification());
}
-
- // Compute imports and instances
- computeAvailableServices();
- computeAvailableTypes();
-
-
- im.register(this);
}
/**
@@ -118,26 +121,22 @@
* @see org.apache.felix.ipojo.CompositeHandler#start()
*/
public void start() {
+ // Compute imports and instances
+ computeAvailableServices();
+ computeAvailableTypes();
+
for (int i = 0; i < m_managedServices.size(); i++) {
ProvidedService ps = (ProvidedService) m_managedServices.get(i);
try {
+ checkServiceSpecification(ps);
ps.start();
} catch (CompositionException e) {
- m_manager.getFactory().getLogger().log(Logger.ERROR, "Cannot start the provided service handler", e);
- m_valid = false;
+ log(Logger.ERROR, "Cannot start the provided service handler", e);
+ if (m_valid) { m_valid = false; }
return;
}
}
- m_valid = true;
- }
-
- /**
- * Check the handler validity.
- * @return true if the handler is valid.
- * @see org.apache.felix.ipojo.CompositeHandler#isValid()
- */
- public boolean isValid() {
- return m_valid;
+ m_valid = true;
}
/**
@@ -166,7 +165,7 @@
return;
}
- // If the new state is VALID => regiter all the services
+ // If the new state is VALID => register all the services
if (state == ComponentInstance.VALID) {
for (int i = 0; i < m_managedServices.size(); i++) {
ProvidedService ps = (ProvidedService) m_managedServices.get(i);
@@ -176,10 +175,6 @@
}
}
- protected CompositeManager getManager() {
- return m_manager;
- }
-
/**
* Build the list of available specification.
* @return the list of available specification.
@@ -193,15 +188,15 @@
*/
private void computeAvailableServices() {
// Get instantiated services :
- ImportExportHandler ih = (ImportExportHandler) m_manager.getCompositeHandler(ImportExportHandler.class.getName());
- ServiceInstantiatorHandler sh = (ServiceInstantiatorHandler) m_manager.getCompositeHandler(ServiceInstantiatorHandler.class.getName());
-
+ ImportHandler ih = (ImportHandler) getHandler(IPojoConfiguration.IPOJO_NAMESPACE + ":requires");
+ ServiceInstantiatorHandler sh = (ServiceInstantiatorHandler) getHandler(IPojoConfiguration.IPOJO_NAMESPACE + ":service");
+
for (int i = 0; sh != null && i < sh.getInstances().size(); i++) {
SvcInstance svc = (SvcInstance) sh.getInstances().get(i);
String itf = svc.getSpecification();
boolean agg = svc.isAggregate();
boolean opt = svc.isOptional();
-
+
SpecificationMetadata sm = new SpecificationMetadata(itf, m_context, agg, opt, this);
m_services.add(sm);
}
@@ -211,26 +206,23 @@
String itf = si.getSpecification();
boolean agg = si.isAggregate();
boolean opt = si.isOptional();
-
+
SpecificationMetadata sm = new SpecificationMetadata(itf, m_context, agg, opt, this);
m_services.add(sm);
}
}
-
+
/**
* Check composite requirement against service specification requirement is available.
* @param ps : the provided service to check
- * @return true if the composite is a correct implementation of the service
+ * @throws CompositionException : occurs if the specification field of the service specification cannot be analyzed correctly.
*/
- private boolean checkServiceSpecification(ProvidedService ps) {
+ private void checkServiceSpecification(ProvidedService ps) throws CompositionException {
try {
- Class spec = m_manager.getFactory().loadClass(ps.getSpecification());
- Field specField = spec.getField("specification");
+ Class spec = getCompositeManager().getFactory().loadClass(ps.getSpecification());
+ Field specField = spec.getField("specification");
Object o = specField.get(null);
- if (!(o instanceof String)) {
- m_manager.getFactory().getLogger().log(Logger.ERROR, "[" + m_manager.getInstanceName() + "] The specification field of the service specification " + ps.getSpecification() + " need to be a String");
- return false;
- } else {
+ if (o instanceof String) {
Element specification = ManifestMetadataParser.parse((String) o);
Element[] reqs = specification.getElements("requires");
for (int j = 0; j < reqs.length; j++) {
@@ -239,128 +231,135 @@
// Fix service-level dependency flag
imp.setServiceLevelDependency();
}
- if (!isRequirementCorrect(imp, reqs[j])) {
- return false;
- }
+ checkRequirement(imp, reqs[j]);
}
+ } else {
+ log(Logger.ERROR, "[" + getCompositeManager().getInstanceName() + "] The specification field of the service specification " + ps.getSpecification() + " need to be a String");
+ throw new CompositionException("Service Specification checking failed : The specification field of the service specification " + ps.getSpecification() + " need to be a String");
}
} catch (NoSuchFieldException e) {
- return true; // No specification field
+ return; // No specification field
} catch (ClassNotFoundException e) {
- m_manager.getFactory().getLogger().log(Logger.ERROR, "[" + m_manager.getInstanceName() + "] The service specification " + ps.getSpecification() + " cannot be load");
- return false;
+ log(Logger.ERROR, "[" + getCompositeManager().getInstanceName() + "] The service specification " + ps.getSpecification() + " cannot be load");
+ throw new CompositionException("The service specification " + ps.getSpecification() + " cannot be load : " + e.getMessage());
} catch (IllegalArgumentException e) {
- m_manager.getFactory().getLogger().log(Logger.ERROR, "[" + m_manager.getInstanceName() + "] The field 'specification' of the service specification " + ps.getSpecification() + " is not accessible : " + e.getMessage());
- return false;
+ log(Logger.ERROR, "[" + getCompositeManager().getInstanceName() + "] The field 'specification' of the service specification " + ps.getSpecification() + " is not accessible : " + e.getMessage());
+ throw new CompositionException("The field 'specification' of the service specification " + ps.getSpecification() + " is not accessible : " + e.getMessage());
} catch (IllegalAccessException e) {
- m_manager.getFactory().getLogger().log(Logger.ERROR, "[" + m_manager.getInstanceName() + "] The field 'specification' of the service specification " + ps.getSpecification() + " is not accessible : " + e.getMessage());
- return false;
+ log(Logger.ERROR, "[" + getCompositeManager().getInstanceName() + "] The field 'specification' of the service specification " + ps.getSpecification() + " is not accessible : " + e.getMessage());
+ throw new CompositionException("The field 'specification' of the service specification " + ps.getSpecification() + " is not accessible : " + e.getMessage());
} catch (ParseException e) {
- m_manager.getFactory().getLogger().log(Logger.ERROR, "[" + m_manager.getInstanceName() + "] The field 'specification' of the service specification " + ps.getSpecification() + " does not contain a valid String : " + e.getMessage());
- return false;
+ log(Logger.ERROR, "[" + getCompositeManager().getInstanceName() + "] The field 'specification' of the service specification " + ps.getSpecification() + " does not contain a valid String : " + e.getMessage());
+ throw new CompositionException("The field 'specification' of the service specification " + ps.getSpecification() + " does not contain a valid String : " + e.getMessage());
}
- return true;
}
-
+
/**
* Look for the implementation (i.e. composite) requirement for the given service-level requirement metadata.
* @param element : the service-level requirement metadata
* @return the ServiceImporter object, null if not found or if the DependencyHandler is not plugged to the instance
*/
private ServiceImporter getAttachedRequirement(Element element) {
- ImportExportHandler ih = (ImportExportHandler) m_manager.getCompositeHandler(ImportExportHandler.class.getName());
- if (ih == null) {
- return null;
- }
-
+ ImportHandler ih = (ImportHandler) getHandler(IPojoConfiguration.IPOJO_NAMESPACE + ":requires");
+ if (ih == null) { return null; }
+
if (element.containsAttribute("id")) {
// Look for dependency Id
String id = element.getAttribute("id");
for (int i = 0; i < ih.getRequirements().size(); i++) {
ServiceImporter imp = (ServiceImporter) ih.getRequirements().get(i);
- if (imp.getId().equals(id)) {
- return imp;
- }
+ if (imp.getId().equals(id)) { return imp; }
}
}
-
+
// If not found or no id, look for a dependency with the same specification
String requirement = element.getAttribute("specification");
for (int i = 0; i < ih.getRequirements().size(); i++) {
ServiceImporter imp = (ServiceImporter) ih.getRequirements().get(i);
- if (imp.getSpecification().equals(requirement)) {
- return imp;
- }
+ if (imp.getSpecification().equals(requirement)) { return imp; }
}
-
return null;
}
-
+
/**
* Check the correctness of the composite requirement against the service level dependency.
* @param imp : requirement to check
* @param elem : service-level dependency metadata
- * @return true if the dependency is correct, false otherwise
+ * @throws CompositionException : occurs if the requirement does not match with service-level specification requirement
*/
- private boolean isRequirementCorrect(ServiceImporter imp, Element elem) {
+ private void checkRequirement(ServiceImporter imp, Element elem) throws CompositionException {
boolean opt = false;
if (elem.containsAttribute("optional") && elem.getAttribute("optional").equalsIgnoreCase("true")) {
opt = true;
}
-
+
boolean agg = false;
if (elem.containsAttribute("aggregate") && elem.getAttribute("aggregate").equalsIgnoreCase("true")) {
agg = true;
}
if (imp == null) {
- //TODO do we need to add the requirement for optional service-level dependency ?
// Add the missing requirement
- ImportExportHandler ih = (ImportExportHandler) m_manager.getCompositeHandler(ImportExportHandler.class.getName());
+ ImportHandler ih = (ImportHandler) getHandler(IPojoConfiguration.IPOJO_NAMESPACE + ":requires");
if (ih == null) {
- // Add the importer handler
- ih = new ImportExportHandler();
- ih.configure(m_manager, new Element("composite", "") , null); // Enter fake info in the configure method.
- m_manager.register(ih);
+ // Look for the import handler factory
+ HandlerManager ci = null;
+ try {
+ ServiceReference[] refs = m_context.getServiceReferences(Factory.class.getName(), "(&(handler.name=requires)(handler.namespace=" + IPojoConfiguration.IPOJO_NAMESPACE + ")(handler.type=composite))");
+ Factory factory = (Factory) m_context.getService(refs[0]);
+ ci = (HandlerManager) factory.createComponentInstance(null, getCompositeManager().getServiceContext());
+ } catch (Exception e) {
+ e.printStackTrace(); // Should not happen
+ }
+ // Add the required handler
+ try {
+ ci.init(getCompositeManager(), new Element("composite", ""), null);
+ } catch (ConfigurationException e) {
+ log(Logger.ERROR, "Internal error : cannot configure the Import Handler : " + e.getMessage());
+ throw new CompositionException("Internal error : cannot configure the Import Handler : " + e.getMessage());
+ }
+ ih = (ImportHandler) ci.getHandler();
+ getCompositeManager().addCompositeHandler(ci);
}
String spec = elem.getAttribute("specification");
- String filter = "(&(objectClass=" + spec + ")(!(service.pid=" + m_manager.getInstanceName() + ")))"; // Cannot import yourself
+ String filter = "(&(objectClass=" + spec + ")(!(instance.name=" + getCompositeManager().getInstanceName() + ")))"; // Cannot import yourself
if (elem.containsAttribute("filter")) {
- if (!elem.getAttribute("filter").equals("")) {
+ if (!"".equals(elem.getAttribute("filter"))) {
filter = "(&" + filter + elem.getAttribute("filter") + ")";
}
}
- ServiceImporter si = new ServiceImporter(spec, filter, agg, opt, m_manager.getContext(), m_manager.getServiceContext(), PolicyServiceContext.LOCAL, null, ih);
+
+ ServiceImporter si = new ServiceImporter(spec, filter, agg, opt, getCompositeManager().getContext(), getCompositeManager().getServiceContext(), PolicyServiceContext.LOCAL, null, ih);
ih.getRequirements().add(si);
- return true;
+ SpecificationMetadata sm = new SpecificationMetadata(spec, m_context, agg, opt, this);
+ m_services.add(sm); // Update the available types
+ return;
}
-
+
if (imp.isAggregate() && !agg) {
- getManager().getFactory().getLogger().log(Logger.ERROR, "[" + getManager().getInstanceName() + "] The requirement " + elem.getAttribute("specification") + " is aggregate in the implementation and is declared as a simple service-level requirement");
- return false;
+ log(Logger.ERROR, "[" + getCompositeManager().getInstanceName() + "] The requirement " + elem.getAttribute("specification") + " is aggregate in the implementation and is declared as a simple service-level requirement");
+ throw new CompositionException("The requirement " + elem.getAttribute("specification") + " is aggregate in the implementation and is declared as a simple service-level requirement");
}
-
+
if (elem.containsAttribute("filter")) {
String filter = elem.getAttribute("filter");
String filter2 = imp.getFilter();
if (filter2 == null || !filter2.equalsIgnoreCase(filter)) {
- getManager().getFactory().getLogger().log(Logger.ERROR, "[" + getManager().getInstanceName() + "] The specification requirement " + elem.getAttribute("specification") + " as not the same filter as declared in the service-level requirement");
- return false;
+ log(Logger.ERROR, "[" + getCompositeManager().getInstanceName() + "] The specification requirement " + elem.getAttribute("specification") + " as not the same filter as declared in the service-level requirement");
+ throw new CompositionException("The specification requirement " + elem.getAttribute("specification") + " as not the same filter as declared in the service-level requirement");
}
}
-
- return true;
}
public HandlerDescription getDescription() {
- return new ProvidedServiceHandlerDescription(true, m_managedServices);
+ return new ProvidedServiceHandlerDescription(this, m_managedServices);
}
-
+
/**
* Build available instance types.
*/
private void computeAvailableTypes() {
- InstanceHandler ih = (InstanceHandler) m_manager.getCompositeHandler(InstanceHandler.class.getName());
+ InstanceHandler ih = (InstanceHandler) getHandler(IPojoConfiguration.IPOJO_NAMESPACE + ":instance");
if (ih == null) {
m_types = new ArrayList();
} else {
Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedServiceHandlerDescription.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedServiceHandlerDescription.java?rev=579239&r1=579238&r2=579239&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedServiceHandlerDescription.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedServiceHandlerDescription.java Tue Sep 25 06:27:49 2007
@@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.List;
+import org.apache.felix.ipojo.CompositeHandler;
import org.apache.felix.ipojo.architecture.HandlerDescription;
import org.apache.felix.ipojo.metadata.Attribute;
import org.apache.felix.ipojo.metadata.Element;
@@ -40,11 +41,11 @@
/**
* Constructor.
*
- * @param isValid : the validity of the provided service handler.
+ * @param h : composite handler.
* @param ps : The list of Provided Service.
*/
- public ProvidedServiceHandlerDescription(boolean isValid, List ps) {
- super(ProvidedServiceHandler.class.getName(), isValid);
+ public ProvidedServiceHandlerDescription(CompositeHandler h, List ps) {
+ super(h);
m_providedServices = ps;
}
Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ServiceExporter.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ServiceExporter.java?rev=579239&r1=579238&r2=579239&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ServiceExporter.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/ServiceExporter.java Tue Sep 25 06:27:49 2007
@@ -180,7 +180,7 @@
/**
* Transform service reference property in a dictionary.
- * Service.PID and Factory.PID are injected too.
+ * instance.name and factory.name are injected too.
* @param ref : the service reference.
* @return the dictionary containing all property of the given service reference.
*/
@@ -191,8 +191,8 @@
prop.put(keys[i], ref.getProperty(keys[i]));
}
- prop.put(Constants.SERVICE_PID, m_ps.getManager().getInstanceName());
- prop.put("factory.pid", m_ps.getManager().getFactory().getName());
+ prop.put("instance.name", m_ps.getManager().getInstanceName());
+ prop.put("factory.name", m_ps.getManager().getFactory().getName());
return prop;
}
@@ -215,7 +215,6 @@
}
m_records.clear();
-
}
/**
@@ -293,7 +292,7 @@
* Manage the arrival of a service.
* @param ref : the new service reference.
*/
- private synchronized void arrivalManagement(ServiceReference ref) {
+ private void arrivalManagement(ServiceReference ref) {
// Check if the new service match
if (m_filter.match(ref)) {
// Add it to the record list
@@ -319,7 +318,7 @@
* Manage the departure of a service.
* @param ref : the new service reference.
*/
- private synchronized void departureManagement(ServiceReference ref) {
+ private void departureManagement(ServiceReference ref) {
List l = getRecordsByRef(ref);
for (int i = 0; i < l.size(); i++) { // Stop the implied record
Record rec = (Record) l.get(i);
Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/SpecificationMetadata.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/SpecificationMetadata.java?rev=579239&r1=579238&r2=579239&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/SpecificationMetadata.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/service/provides/SpecificationMetadata.java Tue Sep 25 06:27:49 2007
@@ -64,7 +64,7 @@
private boolean m_isInterface = true;
/**
- * Componenet Type.
+ * Component Type.
*/
private String m_componentType = null;
@@ -97,7 +97,7 @@
cr.accept(msv, ClassReader.SKIP_FRAMES);
is.close();
} catch (IOException e) {
- m_handler.getManager().getFactory().getLogger().log(Logger.ERROR, "Cannot open " + name + " : " + e.getMessage());
+ m_handler.log(Logger.ERROR, "Cannot open " + name + " : " + e.getMessage());
return;
}
@@ -126,7 +126,7 @@
/**
* Constructor.
* @param c : class
- * @param type : componenet type
+ * @param type : component type
* @param psd : the parent handler
*/
public SpecificationMetadata(Class c, String type, ProvidedServiceHandler psd) {
Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/architecture/ArchitectureHandler.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/architecture/ArchitectureHandler.java?rev=579239&r1=579238&r2=579239&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/architecture/ArchitectureHandler.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/architecture/ArchitectureHandler.java Tue Sep 25 06:27:49 2007
@@ -19,34 +19,19 @@
package org.apache.felix.ipojo.handlers.architecture;
import java.util.Dictionary;
-import java.util.Properties;
-import org.apache.felix.ipojo.Handler;
-import org.apache.felix.ipojo.InstanceManager;
+import org.apache.felix.ipojo.PrimitiveHandler;
import org.apache.felix.ipojo.architecture.Architecture;
import org.apache.felix.ipojo.architecture.InstanceDescription;
import org.apache.felix.ipojo.metadata.Element;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceRegistration;
+import org.apache.felix.ipojo.util.Logger;
/**
- * Achtiecture Handler : do reflection on your component.
+ * Architecture Handler : do reflection on your component.
*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
-public class ArchitectureHandler extends Handler implements Architecture {
-
- /**
- * Instance Manager.
- */
- private InstanceManager m_manager;
-
- /**
- * Service Registration of the Architecture service provided by this
- * handler.
- */
- private ServiceRegistration m_sr;
+public class ArchitectureHandler extends PrimitiveHandler implements Architecture {
/**
* Name of the component.
@@ -56,56 +41,26 @@
/**
* 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)
+ * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
*/
- 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);
- }
- }
-
+ public void configure(Element metadata, Dictionary configuration) {
m_name = (String) configuration.get("name");
-
- m_manager = im;
}
/**
* Stop method.
- * Unregister the service if published.
* @see org.apache.felix.ipojo.Handler#stop()
*/
- public void stop() {
- if (m_sr != null) {
- m_sr.unregister();
- m_sr = null;
- }
- }
+ public void stop() { }
/**
* 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();
- }
-
- // Register the ManagedService
- BundleContext bc = m_manager.getContext();
- Dictionary properties = new Properties();
- properties.put("Component.Implementation.Class", m_manager.getClassName());
- properties.put(Constants.SERVICE_PID, m_name);
-
- m_sr = bc.registerService(Architecture.class.getName(), this, properties);
-
+ public void start() {
+ log(Logger.INFO, "Start architecture handler with " + m_name + " name");
}
/**
@@ -114,7 +69,6 @@
* @see org.apache.felix.ipojo.architecture.Architecture#getDescription()
*/
public InstanceDescription getInstanceDescription() {
- return m_manager.getInstanceDescription();
+ return getInstanceManager().getInstanceDescription();
}
-
}