Commit the new iPOJO version (0.7.6).
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@642265 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/extender.pattern.handler/metadata.xml b/ipojo/extender.pattern.handler/metadata.xml
new file mode 100644
index 0000000..cb45ffc
--- /dev/null
+++ b/ipojo/extender.pattern.handler/metadata.xml
@@ -0,0 +1,6 @@
+<ipojo>
+ <handler
+ classname="org.apache.felix.ipojo.handler.extender.ExtenderModelHandler"
+ name="extender" namespace="org.apache.felix.ipojo.extender">
+ </handler>
+</ipojo>
\ No newline at end of file
diff --git a/ipojo/extender.pattern.handler/pom.xml b/ipojo/extender.pattern.handler/pom.xml
new file mode 100644
index 0000000..a89fbd0
--- /dev/null
+++ b/ipojo/extender.pattern.handler/pom.xml
@@ -0,0 +1,91 @@
+<!--
+ 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.
+-->
+<project>
+ <parent>
+ <artifactId>iPOJO</artifactId>
+ <groupId>org.apache.felix</groupId>
+ <version>0.7.6-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <packaging>bundle</packaging>
+ <name>Apache Felix iPOJO Extender Pattern Handler</name>
+ <artifactId>
+ org.apache.felix.ipojo.handler.extender.pattern
+ </artifactId>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.ipojo</artifactId>
+ <version>${pom.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.ipojo.metadata</artifactId>
+ <version>${pom.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <version>1.0.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ <version>1.0.0</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Private-Package>
+ org.apache.felix.ipojo.handler.extender
+ </Private-Package>
+ <Bundle-Name>${pom.name}</Bundle-Name>
+ <Bundle-SymbolicName>
+ org.apache.felix.ipojo.handler.extender.pattern
+ </Bundle-SymbolicName>
+ </instructions>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-ipojo-plugin</artifactId>
+ <version>${pom.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>ipojo-bundle</goal>
+ </goals>
+ <configuration>
+ <metadata>metadata.xml</metadata>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/ipojo/extender.pattern.handler/src/main/java/org/apache/felix/ipojo/handler/extender/ExtenderManager.java b/ipojo/extender.pattern.handler/src/main/java/org/apache/felix/ipojo/handler/extender/ExtenderManager.java
new file mode 100644
index 0000000..aa8341a
--- /dev/null
+++ b/ipojo/extender.pattern.handler/src/main/java/org/apache/felix/ipojo/handler/extender/ExtenderManager.java
@@ -0,0 +1,178 @@
+/*
+ * 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.handler.extender;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.List;
+
+import org.apache.felix.ipojo.PrimitiveHandler;
+import org.apache.felix.ipojo.util.Callback;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.SynchronousBundleListener;
+
+/**
+ * Track and manage extensions.
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class ExtenderManager implements SynchronousBundleListener {
+
+ /**
+ * Looked extension.
+ */
+ private String m_extension;
+
+ /**
+ * OnArrival method.
+ */
+ private Callback m_onArrival;
+
+ /**
+ * OnDeparture method.
+ */
+ private Callback m_onDeparture;
+
+ /**
+ * Attached handler.
+ */
+ private PrimitiveHandler m_handler;
+
+ /**
+ * Bundle context.
+ */
+ private BundleContext m_context;
+
+ /**
+ * List of managed bundles.
+ */
+ private List m_bundles = new ArrayList();
+
+ /**
+ * Constructor.
+ * @param handler : attached handler.
+ * @param extension : looked extension.
+ * @param bind : onArrival method
+ * @param unbind : onDeparture method.
+ */
+ public ExtenderManager(ExtenderModelHandler handler, String extension, String bind, String unbind) {
+ m_handler = handler;
+ m_onArrival = new Callback(bind, new Class[] {Bundle.class, String.class}, false, m_handler.getInstanceManager());
+ m_onDeparture = new Callback(unbind, new Class[] {Bundle.class}, false, m_handler.getInstanceManager());
+ m_extension = extension;
+ m_context = handler.getInstanceManager().getContext();
+ }
+
+ /**
+ * Start method.
+ * Look for already presents bundle and register a (synchronous) bundle listener.
+ */
+ public void start() {
+ synchronized (this) {
+ // listen to any changes in bundles.
+ m_context.addBundleListener(this);
+ // compute already started bundles.
+ for (int i = 0; i < m_context.getBundles().length; i++) {
+ if (m_context.getBundles()[i].getState() == Bundle.ACTIVE) {
+ onArrival(m_context.getBundles()[i]);
+ }
+ }
+ }
+ }
+
+ /**
+ * Manage a bundle arrival:
+ * Check the extension and manage it if present.
+ * @param bundle : bundle.
+ */
+ private void onArrival(Bundle bundle) {
+ Dictionary headers = bundle.getHeaders();
+ String header = (String) headers.get(m_extension);
+ if (header != null) {
+ m_bundles.add(bundle);
+ try {
+ m_onArrival.call(new Object[] {bundle, header});
+ } catch (NoSuchMethodException e) {
+ m_handler.error("The onArrival method " + m_onArrival.getMethod() + " does not exist in the class", e);
+ m_handler.getInstanceManager().stop();
+ } catch (IllegalAccessException e) {
+ m_handler.error("The onArrival method " + m_onArrival.getMethod() + " cannot be called", e);
+ m_handler.getInstanceManager().stop();
+ } catch (InvocationTargetException e) {
+ m_handler.error("The onArrival method " + m_onArrival.getMethod() + " has thrown an exception", e.getTargetException());
+ m_handler.getInstanceManager().stop();
+ }
+ }
+ }
+
+ /**
+ * Stop method.
+ * Remove the bundle listener.
+ */
+ public void stop() {
+ m_context.removeBundleListener(this);
+ m_bundles.clear();
+ }
+
+ /**
+ * Bundle listener.
+ * @param event : event.
+ * @see org.osgi.framework.BundleListener#bundleChanged(org.osgi.framework.BundleEvent)
+ */
+ public void bundleChanged(BundleEvent event) {
+ switch (event.getType()) {
+ case BundleEvent.STARTED:
+ onArrival(event.getBundle());
+ break;
+ case BundleEvent.STOPPING:
+ onDeparture(event.getBundle());
+ break;
+ default:
+ break;
+ }
+
+ }
+
+ /**
+ * Manage a bundle departure.
+ * If the bundle was managed, invoke the OnDeparture callback, and remove the bundle from the list.
+ * @param bundle : bundle.
+ */
+ private void onDeparture(Bundle bundle) {
+ if (m_bundles.contains(bundle)) {
+ try {
+ m_onDeparture.call(new Object[] {bundle});
+ } catch (NoSuchMethodException e) {
+ m_handler.error("The onDeparture method " + m_onDeparture.getMethod() + " does not exist in the class", e);
+ m_handler.getInstanceManager().stop();
+ } catch (IllegalAccessException e) {
+ m_handler.error("The onDeparture method " + m_onDeparture.getMethod() + " cannot be called", e);
+ m_handler.getInstanceManager().stop();
+ } catch (InvocationTargetException e) {
+ m_handler.error("The onDeparture method " + m_onDeparture.getMethod() + " has thrown an exception", e.getTargetException());
+ m_handler.getInstanceManager().stop();
+ }
+ }
+ }
+
+
+
+}
diff --git a/ipojo/extender.pattern.handler/src/main/java/org/apache/felix/ipojo/handler/extender/ExtenderModelHandler.java b/ipojo/extender.pattern.handler/src/main/java/org/apache/felix/ipojo/handler/extender/ExtenderModelHandler.java
new file mode 100644
index 0000000..8fc50b9
--- /dev/null
+++ b/ipojo/extender.pattern.handler/src/main/java/org/apache/felix/ipojo/handler/extender/ExtenderModelHandler.java
@@ -0,0 +1,94 @@
+/*
+ * 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.handler.extender;
+
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.List;
+
+import org.apache.felix.ipojo.ConfigurationException;
+import org.apache.felix.ipojo.PrimitiveHandler;
+import org.apache.felix.ipojo.metadata.Element;
+
+/**
+ * Handler automating extender pattern. The component using this handler is notified
+ * when an handler with a special manifest extension is detected, the component is notified.
+ * When a managed handler leaves, the component is also notified.
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class ExtenderModelHandler extends PrimitiveHandler {
+
+ /**
+ * Handler namespace.
+ */
+ public static final String NAMESPACE = "org.apache.felix.ipojo.extender";
+
+ /**
+ * Extension manager list.
+ */
+ private List m_managers = new ArrayList(1);
+
+ /**
+ * Configure method.
+ * @param elem : component type element.
+ * @param dict : instance configuration.
+ * @throws ConfigurationException : the configuration is not valid.
+ * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
+ */
+ public void configure(Element elem, Dictionary dict) throws ConfigurationException {
+ Element[] elems = elem.getElements("extender", NAMESPACE);
+ for (int i = 0; i < elems.length; i++) {
+ String extension = elems[i].getAttribute("extension");
+ String onArrival = elems[i].getAttribute("onArrival");
+ String onDeparture = elems[i].getAttribute("onDeparture");
+
+ if (extension == null) {
+ throw new ConfigurationException("The extender element requires an 'extender' attribute");
+ }
+ if (onArrival == null || onDeparture == null) {
+ throw new ConfigurationException("The extender element requires the onArrival and onDeparture attributes");
+ }
+
+ ExtenderManager wbm = new ExtenderManager(this, extension, onArrival, onDeparture);
+ m_managers.add(wbm);
+ }
+
+ }
+
+ /**
+ * Start the handler.
+ * @see org.apache.felix.ipojo.Handler#start()
+ */
+ public void start() {
+ for (int i = 0; i < m_managers.size(); i++) {
+ ((ExtenderManager) m_managers.get(i)).start();
+ }
+ }
+
+ /**
+ * Stop the handler.
+ * @see org.apache.felix.ipojo.Handler#stop()
+ */
+ public void stop() {
+ for (int i = 0; i < m_managers.size(); i++) {
+ ((ExtenderManager) m_managers.get(i)).stop();
+ }
+ }
+
+}