FELIX-3895 (https://issues.apache.org/jira/browse/FELIX-3895) - iPOJO instance is not shown (with the "arch" commands) if constructor is failing
The architecture service is now published even is the instance is stopped. This, in combination of the declaration bindings error (available through the instance declaration service)
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1480353 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/CompositeInstanceDescription.java b/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/CompositeInstanceDescription.java
index 802ddcb..87793fc 100644
--- a/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/CompositeInstanceDescription.java
+++ b/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/CompositeInstanceDescription.java
@@ -50,7 +50,7 @@
/**
- * Gets the list of contained instance in the describe instance.
+ * Gets the list of contained instance in the described instance.
* This list contains only instances who exposed their architecture.
* @return the list of contained instances.
*/
@@ -58,15 +58,15 @@
// Get instances description of internal instance
ServiceContext internal = ((CompositeManager) m_instance).getServiceContext();
try {
- ServiceReference[]refs = internal.getServiceReferences(Architecture.class.getName(), null);
+ ServiceReference[] refs = internal.getServiceReferences(Architecture.class.getName(), null);
if (refs != null) {
- InstanceDescription[] descs = new InstanceDescription[refs.length];
+ InstanceDescription[] desc = new InstanceDescription[refs.length];
for (int i = 0; i < refs.length; i++) {
Architecture arch = (Architecture) internal.getService(refs[i]);
- descs[i] = arch.getInstanceDescription();
+ desc[i] = arch.getInstanceDescription();
internal.ungetService(refs[i]);
}
- return descs;
+ return desc;
}
} catch (InvalidSyntaxException e) {
// Cannot happen
diff --git a/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/architecture/ArchitectureHandler.java b/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/architecture/ArchitectureHandler.java
index 817ec08..8286c86 100644
--- a/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/architecture/ArchitectureHandler.java
+++ b/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/architecture/ArchitectureHandler.java
@@ -19,18 +19,23 @@
package org.apache.felix.ipojo.composite.architecture;
import java.util.Dictionary;
+import java.util.Hashtable;
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.InstanceStateListener;
import org.apache.felix.ipojo.architecture.Architecture;
import org.apache.felix.ipojo.architecture.InstanceDescription;
import org.apache.felix.ipojo.composite.CompositeHandler;
import org.apache.felix.ipojo.metadata.Element;
+import org.osgi.framework.ServiceRegistration;
/**
* Composite Architecture Handler.
*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
-public class ArchitectureHandler extends CompositeHandler implements Architecture {
+public class ArchitectureHandler extends CompositeHandler implements Architecture, InstanceStateListener {
/**
* Name of the component.
@@ -38,37 +43,66 @@
private String m_name;
/**
+ * The Architecture service registration.
+ */
+ private ServiceRegistration m_serviceRegistration;
+
+ /**
* Configure the handler.
- *
- * @param metadata : the metadata of the component
+ *
+ * @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)
+ * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
*/
public void configure(Element metadata, Dictionary configuration) {
- m_name = (String) configuration.get("instance.name");
+ m_name = (String) configuration.get(Factory.INSTANCE_NAME_PROPERTY);
+ Dictionary<String, String> dict = new Hashtable<String, String>();
+ dict.put(ARCHITECTURE_INSTANCE, m_name);
+
+ debug("Registering architecture service for " + m_name);
+ m_serviceRegistration = getCompositeManager().getContext().registerService(Architecture.class.getName(), this, dict);
+
+ // We can't use the regular handler stateChanged method as this method is not called when the instance is
+ // disposed. This handler stays actives until the instance disposal.
+ getCompositeManager().addInstanceStateListener(this);
}
/**
- * Stop the handler.
+ * Stop method.
+ *
* @see org.apache.felix.ipojo.Handler#stop()
*/
public void stop() {
+ // Nothing do do when stopping.
+ }
+
+ /**
+ * Start method.
+ *
+ * @see org.apache.felix.ipojo.Handler#start()
+ */
+ public void start() {
// Nothing to do.
}
/**
- * Start the handler.
- * @see org.apache.felix.ipojo.Handler#start()
+ * The instance lifecycle listener callback.
+ * When we receive the DISPOSED state, the architecture is unregistered from the service registry.
+ * @param instance the changing instance the instance, meaningless in our case.
+ * @param newState the new instance state the new instance state.
*/
- public void start() {
- info("Start composite architecture handler with " + m_name + " name");
+ public void stateChanged(ComponentInstance instance, int newState) {
+ if (newState == ComponentInstance.DISPOSED && m_serviceRegistration != null) {
+ debug("Withdrawing the architecture service of " + m_name + " due to instance disposal");
+ m_serviceRegistration.unregister();
+ m_serviceRegistration = null;
+ }
}
/**
* Get the instance description.
* @return the instance description
- * @see org.apache.felix.ipojo.architecture.Architecture#getDescription()
+ * @see org.apache.felix.ipojo.architecture.Architecture#getInstanceDescription() ()
*/
public InstanceDescription getInstanceDescription() {
return getCompositeManager().getInstanceDescription();
diff --git a/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/instance/InstanceHandler.java b/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/instance/InstanceHandler.java
index f7fe811..20b2a7b 100644
--- a/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/instance/InstanceHandler.java
+++ b/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/instance/InstanceHandler.java
@@ -197,7 +197,8 @@
public void unbindFactory(Factory factory) {
boolean implicated = false;
for (int i = 0; i < m_configurations.length; i++) {
- if (m_configurations[i].getInstance() != null && m_configurations[i].getFactory().equals(factory.getName())) {
+ if (m_configurations[i].getInstance() != null
+ && m_configurations[i].getFactory().equals(factory.getName())) {
m_configurations[i].setInstance(null);
m_configurations[i].setFactory(null);
implicated = true;
@@ -340,7 +341,7 @@
/**
* Check handler validity.
- * The method update the validaity of the handler.
+ * The method updates the validity of the handler.
*/
private void checkValidity() {
for (int i = 0; i < m_configurations.length; i++) {
@@ -362,8 +363,9 @@
public void stateChanged(ComponentInstance instance, int newState) {
switch (newState) {
case ComponentInstance.DISPOSED:
+ break; // Should not happen
case ComponentInstance.STOPPED:
- break; // Should not happen
+ break; // Should not happen
case ComponentInstance.VALID:
if (!getValidity()) {
checkValidity();
@@ -392,7 +394,8 @@
if (m_configurations[i].getInstance().getState() == ComponentInstance.VALID) {
return ((InstanceManager) m_configurations[i].getInstance()).getPojoObject();
} else {
- error("An object cannot be get from the instance of the type " + type + ": invalid instance" + m_configurations[i].getInstance().getInstanceDescription().getDescription());
+ error("An object cannot be get from the instance of the type " + type + ": invalid instance" +
+ m_configurations[i].getInstance().getInstanceDescription().getDescription());
return null;
}
}
diff --git a/ipojo/runtime/composite/src/main/resources/metadata.xml b/ipojo/runtime/composite/src/main/resources/metadata.xml
index 0f6262b..13b2163 100644
--- a/ipojo/runtime/composite/src/main/resources/metadata.xml
+++ b/ipojo/runtime/composite/src/main/resources/metadata.xml
@@ -38,8 +38,6 @@
<handler
classname="org.apache.felix.ipojo.composite.architecture.ArchitectureHandler"
name="architecture" type="composite" architecture="false">
- <provides>
- <property field="m_name" name="architecture.instance"/>
- </provides>
+ <!-- the architecture service is published by the handler manually -->
</handler>
</ipojo>
\ No newline at end of file