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 2010/10/10 14:31:18 UTC

svn commit: r1006281 - in /felix/trunk/ipojo: core/src/main/java/org/apache/felix/ipojo/ tests/core/external-handlers/ tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ tests/core/external-handlers/src/main/jav...

Author: clement
Date: Sun Oct 10 12:31:17 2010
New Revision: 1006281

URL: http://svn.apache.org/viewvc?rev=1006281&view=rev
Log:
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).

Added:
    felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/AutoHandlerTest.java
Modified:
    felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
    felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/IPojoFactory.java
    felix/trunk/ipojo/tests/core/external-handlers/pom.xml
    felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceHandler.java
    felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/ExternalHandlerTestSuite.java
    felix/trunk/ipojo/tests/core/external-handlers/src/main/resources/metadata.xml

Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java?rev=1006281&r1=1006280&r2=1006281&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java Sun Oct 10 12:31:17 2010
@@ -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 java.util.Map;
 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 @@ import org.osgi.framework.ServiceReferen
  */
 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 @@ public class ComponentFactory extends IP
      * 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 @@ public class ComponentFactory extends IP
             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 class ComponentFactory extends IP
     public String getVersion() {
         return m_version;
     }
-    
+
     public ClassLoader getBundleClassLoader() {
         return m_classLoader;
     }

Modified: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/IPojoFactory.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/IPojoFactory.java?rev=1006281&r1=1006280&r2=1006281&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/IPojoFactory.java (original)
+++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/IPojoFactory.java Sun Oct 10 12:31:17 2010
@@ -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 @@ public abstract class IPojoFactory imple
         }
 
         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 @@ public abstract class IPojoFactory imple
             m_logger.log(Logger.ERROR, e.getMessage());
             throw new ConfigurationException(e.getMessage(), m_factoryName);
         }
-        
+
 
     }
 
@@ -545,7 +545,7 @@ public abstract class IPojoFactory imple
         m_described = false;
         m_componentDesc = null;
         m_componentInstances.clear();
-        
+
         m_logger.log(Logger.INFO, "Factory " + m_factoryName + " stopped");
 
     }
@@ -586,17 +586,26 @@ public abstract class IPojoFactory imple
 
         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 @@ public abstract class IPojoFactory imple
         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());
     }

Modified: felix/trunk/ipojo/tests/core/external-handlers/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/core/external-handlers/pom.xml?rev=1006281&r1=1006280&r2=1006281&view=diff
==============================================================================
--- felix/trunk/ipojo/tests/core/external-handlers/pom.xml (original)
+++ felix/trunk/ipojo/tests/core/external-handlers/pom.xml Sun Oct 10 12:31:17 2010
@@ -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>

Modified: felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceHandler.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceHandler.java?rev=1006281&r1=1006280&r2=1006281&view=diff
==============================================================================
--- felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceHandler.java (original)
+++ felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceHandler.java Sun Oct 10 12:31:17 2010
@@ -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.apache.felix.ipojo.test.scena
 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 @@ public class CheckServiceHandler extends
             }
 		}
 		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 class CheckServiceHandler extends
 	public Properties getProps() {
 		return props;
 	}
-	
+
 	public void stateChanged(int state) {
 		if (sr != null) {
 		    changes++;
@@ -96,7 +96,7 @@ public class CheckServiceHandler extends
 	public String getName() {
 		return NAMESPACE;
 	}
-	
+
 	public HandlerDescription getDescription() {
 		return new CheckServiceHandlerDescription(this);
 	}

Added: felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/AutoHandlerTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/AutoHandlerTest.java?rev=1006281&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/AutoHandlerTest.java (added)
+++ felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/AutoHandlerTest.java Sun Oct 10 12:31:17 2010
@@ -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);
+
+	}
+}

Modified: felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/ExternalHandlerTestSuite.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/ExternalHandlerTestSuite.java?rev=1006281&r1=1006280&r2=1006281&view=diff
==============================================================================
--- felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/ExternalHandlerTestSuite.java (original)
+++ felix/trunk/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/ExternalHandlerTestSuite.java Sun Oct 10 12:31:17 2010
@@ -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 class ExternalHandlerTestSuite ex
 	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;
 	}
 

Modified: felix/trunk/ipojo/tests/core/external-handlers/src/main/resources/metadata.xml
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/core/external-handlers/src/main/resources/metadata.xml?rev=1006281&r1=1006280&r2=1006281&view=diff
==============================================================================
--- felix/trunk/ipojo/tests/core/external-handlers/src/main/resources/metadata.xml (original)
+++ felix/trunk/ipojo/tests/core/external-handlers/src/main/resources/metadata.xml Sun Oct 10 12:31:17 2010
@@ -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>