Fix FELIX-2594 Have a way to create new custom iPojo handler without having to specify a handler usage
Allow to auto-attach primitive handlers to primitive components.
Handlers an be auto-attached if they are contained in the 'org.apache.felix.ipojo.handler.auto.primitive' system property. This property's value must be the list (command separated) of full handler name (namespace:name).
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1006281 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java b/ipojo/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
index ade79b1..a91362f 100644
--- a/ipojo/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
+++ b/ipojo/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
@@ -1,4 +1,4 @@
-/*
+/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -30,6 +30,7 @@
import org.apache.felix.ipojo.architecture.ComponentTypeDescription;
import org.apache.felix.ipojo.metadata.Attribute;
import org.apache.felix.ipojo.metadata.Element;
+import org.apache.felix.ipojo.parser.ParseUtils;
import org.apache.felix.ipojo.parser.PojoMetadata;
import org.apache.felix.ipojo.util.Logger;
import org.apache.felix.ipojo.util.Tracker;
@@ -49,6 +50,15 @@
*/
public class ComponentFactory extends IPojoFactory implements TrackerCustomizer {
+ /**
+ * System property set to automatically attach primitive handlers to primitive
+ * component types.
+ * The value is a String parsed as a list (comma separated). Each element is
+ * the fully qualified name of the handler <code>namespace:name</code>.
+ */
+ public static final String HANDLER_AUTO_PRIMITIVE = "org.apache.felix.ipojo.handler.auto.primitive";
+
+
/**
* The tracker used to track required handler factories.
* Immutable once set.
@@ -272,6 +282,8 @@
* Computes required handlers.
* This method does not manipulate any non-immutable fields,
* so does not need to be synchronized.
+ * This method checks the {@link ComponentFactory#HANDLER_AUTO_PRIMITIVE}
+ * system property to add the listed handlers to the required handler set.
* @return the required handler list.
*/
public List getRequiredHandlerList() {
@@ -316,6 +328,29 @@
list.add(reqCallback);
}
+ // Manage auto attached handler.
+ String v = System.getProperty(HANDLER_AUTO_PRIMITIVE);
+ if (v != null && v.length() != 0) {
+ String[] hs = ParseUtils.split(v, ",");
+ for (int i = 0; i < hs.length; i++) {
+ String h = hs[i].trim();
+ String[] segments = h.split(":");
+ RequiredHandler rq = null;
+ if (segments.length == 2) { // External handler
+ rq = new RequiredHandler(segments[1], segments[0]);
+ } else if (segments.length == 1) { // Core handler
+ rq = new RequiredHandler(segments[1], null);
+ } // Others case are ignored.
+
+ if (rq != null) {
+ // Check it's not already contained
+ if (! list.contains(rq)) {
+ list.add(rq);
+ }
+ }
+ }
+ }
+
return list;
}
@@ -407,7 +442,7 @@
public String getVersion() {
return m_version;
}
-
+
public ClassLoader getBundleClassLoader() {
return m_classLoader;
}
diff --git a/ipojo/core/src/main/java/org/apache/felix/ipojo/IPojoFactory.java b/ipojo/core/src/main/java/org/apache/felix/ipojo/IPojoFactory.java
index e0454de..3ffa2b2 100644
--- a/ipojo/core/src/main/java/org/apache/felix/ipojo/IPojoFactory.java
+++ b/ipojo/core/src/main/java/org/apache/felix/ipojo/IPojoFactory.java
@@ -1,4 +1,4 @@
-/*
+/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -166,7 +166,7 @@
}
m_requiredHandlers = getRequiredHandlerList(); // Call sub-class to get the list of required handlers.
-
+
m_logger.log(Logger.INFO, "New factory created : " + m_factoryName);
}
@@ -307,7 +307,7 @@
m_logger.log(Logger.ERROR, e.getMessage());
throw new ConfigurationException(e.getMessage(), m_factoryName);
}
-
+
}
@@ -545,7 +545,7 @@
m_described = false;
m_componentDesc = null;
m_componentInstances.clear();
-
+
m_logger.log(Logger.INFO, "Factory " + m_factoryName + " stopped");
}
@@ -586,17 +586,26 @@
if (m_isPublic) {
// Exposition of the factory service
- BundleContext bc = SecurityHelper.selectContextToRegisterServices(m_componentDesc.getFactoryInterfacesToPublish(),
+ BundleContext bc = SecurityHelper.selectContextToRegisterServices(m_componentDesc.getFactoryInterfacesToPublish(),
m_context, getIPOJOBundleContext());
m_sr =
bc.registerService(m_componentDesc.getFactoryInterfacesToPublish(), this, m_componentDesc
.getPropertiesToPublish());
}
-
+
m_logger.log(Logger.INFO, "Factory " + m_factoryName + " started");
}
-
+
+ /**
+ * For testing purpose <b>ONLY</b>.
+ * This method recomputes the required handler list.
+ */
+ public void restart() {
+ // Call sub-class to get the list of required handlers.
+ m_requiredHandlers = getRequiredHandlerList();
+ }
+
/**
* Gets the iPOJO Bundle Context.
* @return the iPOJO Bundle Context
@@ -774,7 +783,7 @@
String name = (String) ref.getProperty(Handler.HANDLER_NAME_PROPERTY);
String namespace = (String) ref.getProperty(Handler.HANDLER_NAMESPACE_PROPERTY);
if (HandlerFactory.IPOJO_NAMESPACE.equals(namespace)) {
- return name.equalsIgnoreCase(req.getName()) && req.getNamespace() == null;
+ return name.equalsIgnoreCase(req.getName()) && req.getNamespace() == null;
}
return name.equalsIgnoreCase(req.getName()) && namespace.equalsIgnoreCase(req.getNamespace());
}
diff --git a/ipojo/tests/core/external-handlers/pom.xml b/ipojo/tests/core/external-handlers/pom.xml
index 727f2f4..f182bf2 100644
--- a/ipojo/tests/core/external-handlers/pom.xml
+++ b/ipojo/tests/core/external-handlers/pom.xml
@@ -70,16 +70,16 @@
<instructions>
<Export-Package>
org.apache.felix.ipojo.test.scenarios.eh.service
- </Export-Package>
+ </Export-Package>
<Bundle-SymbolicName>
${project.artifactId}
- </Bundle-SymbolicName>
+ </Bundle-SymbolicName>
<Private-Package>
org.apache.felix.ipojo.test*
- </Private-Package>
+ </Private-Package>
<Test-Suite>
org.apache.felix.ipojo.test.scenarios.eh.ExternalHandlerTestSuite
- </Test-Suite>
+ </Test-Suite>
</instructions>
</configuration>
</plugin>
@@ -105,6 +105,19 @@
<target>1.4</target>
</configuration>
</plugin>
+
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-junit4osgi-plugin</artifactId>
+ <version>1.1.0-SNAPSHOT</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>test</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
</plugins>
</build>
</project>
diff --git a/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceHandler.java b/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceHandler.java
index cd43545..7bdcfb0 100644
--- a/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceHandler.java
+++ b/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceHandler.java
@@ -1,4 +1,4 @@
-/*
+/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -30,21 +30,21 @@
import org.osgi.framework.ServiceRegistration;
public class CheckServiceHandler extends PrimitiveHandler implements CheckService {
-
+
ServiceRegistration sr;
boolean isValid;
int changes = 0;
static final String NAMESPACE = "org.apache.felix.ipojo.test.handler.checkservice";
-
+
Properties props = new Properties();
public void configure(Element metadata, Dictionary configuration) {
Element[] meta = metadata.getElements("check", NAMESPACE);
- if(meta == null) { return; }
- // Get handler props
+ if(meta == null) { return; }
+ // Get handler props
props.put("instance.name", configuration.get("instance.name"));
if(configuration.get("csh.simple") != null) { props.put("Simple", configuration.get("csh.simple")); }
- if(configuration.get("csh.map") != null) {
+ if(configuration.get("csh.map") != null) {
Dictionary m = (Dictionary) configuration.get("csh.map");
if (m.size() > 0) {
props.put("Map1", m.get("a"));
@@ -53,28 +53,28 @@
}
}
props.put("changes", new Integer(changes));
-
+
}
-
+
public void initializeComponentFactory(ComponentTypeDescription cd, Element metadata) {
cd.addProperty(new PropertyDescription("csh.simple", "java.lang.String", null));
cd.addProperty(new PropertyDescription("csh.map", "java.util.Dictionary", null));
}
-
+
public void start() {
if(sr == null) {
sr = getInstanceManager().getContext().registerService(CheckService.class.getName(), this, props);
}
isValid = true;
}
-
+
public void stop() {
isValid = false;
synchronized(this) {
if(sr != null) { sr.unregister(); }
}
}
-
+
public boolean check() {
if(isValid) { isValid = false;}
else { isValid = true; }
@@ -84,7 +84,7 @@
public Properties getProps() {
return props;
}
-
+
public void stateChanged(int state) {
if (sr != null) {
changes++;
@@ -96,7 +96,7 @@
public String getName() {
return NAMESPACE;
}
-
+
public HandlerDescription getDescription() {
return new CheckServiceHandlerDescription(this);
}
diff --git a/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/AutoHandlerTest.java b/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/AutoHandlerTest.java
new file mode 100644
index 0000000..48a3ec9
--- /dev/null
+++ b/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/AutoHandlerTest.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.ipojo.test.scenarios.eh;
+
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentFactory;
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.architecture.HandlerDescription;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.scenarios.component.CheckServiceHandlerDescription;
+import org.apache.felix.ipojo.test.scenarios.util.Utils;
+
+public class AutoHandlerTest extends OSGiTestCase {
+
+ private static final String ORG_APACHE_FELIX_IPOJO_HANDLER_AUTO_PRIMITIVE = "org.apache.felix.ipojo.handler.auto.primitive";
+
+ ComponentInstance instance;
+
+ ComponentFactory factory;
+
+ public void setUp() {
+ factory = (ComponentFactory) Utils.getFactoryByName(getContext(), "HANDLER-HandlerTesterWO");
+ System.clearProperty(ORG_APACHE_FELIX_IPOJO_HANDLER_AUTO_PRIMITIVE);
+ }
+
+ public void tearDown() {
+ if (instance != null) {
+ instance.dispose();
+ }
+ instance = null;
+
+ System.clearProperty(ORG_APACHE_FELIX_IPOJO_HANDLER_AUTO_PRIMITIVE);
+ }
+
+ public void testRequiredHandlerList() {
+ List list = factory.getRequiredHandlers();
+ assertFalse(list.contains("org.apache.felix.ipojo.test.handler.checkservice:check"));
+
+ String v = "org.apache.felix.ipojo.test.handler.checkservice:check";
+ System.setProperty(ORG_APACHE_FELIX_IPOJO_HANDLER_AUTO_PRIMITIVE, v);
+
+ factory.stop();
+ factory.restart();
+ factory.start();
+
+ list = factory.getRequiredHandlers();
+ assertTrue(list.contains("org.apache.felix.ipojo.test.handler.checkservice:check"));
+
+ }
+
+ public void testInstanceCreation() throws Exception {
+ String v = "org.apache.felix.ipojo.test.handler.checkservice:check";
+ System.setProperty(ORG_APACHE_FELIX_IPOJO_HANDLER_AUTO_PRIMITIVE, v);
+
+ factory.stop();
+ factory.restart();
+ factory.start();
+
+ instance = factory.createComponentInstance(new Properties());
+ assertEquals(ComponentInstance.VALID, instance.getState());
+
+ HandlerDescription hd = instance.getInstanceDescription().getHandlerDescription(v);
+ assertNotNull(hd);
+ assertTrue(hd instanceof CheckServiceHandlerDescription);
+
+ }
+}
diff --git a/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/ExternalHandlerTestSuite.java b/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/ExternalHandlerTestSuite.java
index 9965643..4f66028 100644
--- a/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/ExternalHandlerTestSuite.java
+++ b/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/ExternalHandlerTestSuite.java
@@ -1,4 +1,4 @@
-/*
+/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -29,6 +29,7 @@
public static Test suite(BundleContext bc) {
OSGiTestSuite ots = new OSGiTestSuite("External Handler Test Suite", bc);
ots.addTestSuite(HandlerTest.class);
+ ots.addTestSuite(AutoHandlerTest.class);
return ots;
}
diff --git a/ipojo/tests/core/external-handlers/src/main/resources/metadata.xml b/ipojo/tests/core/external-handlers/src/main/resources/metadata.xml
index 343244e..b7da090 100644
--- a/ipojo/tests/core/external-handlers/src/main/resources/metadata.xml
+++ b/ipojo/tests/core/external-handlers/src/main/resources/metadata.xml
@@ -29,5 +29,11 @@
<!-- Empty dictionary -->
</property>
</instance>
-
+
+ <!-- The handler will be added using the auto handler property -->
+ <component
+ classname="org.apache.felix.ipojo.test.scenarios.component.FooProviderType1"
+ name="HANDLER-HandlerTesterWO" architecture="true">
+ </component>
+
</ipojo>