Apply patch FELIX-3401 to use OSGi BundleTracker in example instead of custom one.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1305394 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/examples/extenderbased.host/pom.xml b/examples/extenderbased.host/pom.xml
index 6cf6f2c..e54bd16 100644
--- a/examples/extenderbased.host/pom.xml
+++ b/examples/extenderbased.host/pom.xml
@@ -36,7 +36,9 @@
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.main</artifactId>
- <version>1.8.0</version>
+ <version>4.0.2</version>
+ <type>zip</type>
+ <classifier>source-release</classifier>
</dependency>
</dependencies>
<build>
diff --git a/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/Activator.java b/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/Activator.java
index 7f6a8ca..7e771e7 100644
--- a/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/Activator.java
+++ b/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/Activator.java
@@ -20,10 +20,8 @@
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
-
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
-
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
@@ -39,7 +37,7 @@
public class Activator implements BundleActivator
{
private DrawingFrame m_frame = null;
- private ShapeTracker m_shapetracker = null;
+ private ShapeBundleTracker m_shapetracker = null;
/**
* Displays the applications window and starts extension tracking;
@@ -75,7 +73,7 @@
m_frame.setVisible(true);
- m_shapetracker = new ShapeTracker(context, m_frame);
+ m_shapetracker = new ShapeBundleTracker(context, m_frame);
m_shapetracker.open();
}
});
diff --git a/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/BundleTracker.java b/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/BundleTracker.java
deleted file mode 100644
index a25750f..0000000
--- a/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/BundleTracker.java
+++ /dev/null
@@ -1,158 +0,0 @@
-/*
- * 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.example.extenderbased.host;
-
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.framework.SynchronousBundleListener;
-
-/**
- * This is a very simple bundle tracker utility class that tracks active
- * bundles. The tracker must be given a bundle context upon creation,
- * which it uses to listen for bundle events. The bundle tracker must be
- * opened to track objects and closed when it is no longer needed. This
- * class is abstract, which means in order to use it you must create a
- * subclass of it. Subclasses must implement the <tt>addedBundle()</tt>
- * and <tt>removedBundle()</tt> methods, which can be used to perform some
- * custom action upon the activation or deactivation of bundles. Since this
- * tracker is quite simple, its concurrency control approach is also
- * simplistic. This means that subclasses should take great care to ensure
- * that their <tt>addedBundle()</tt> and <tt>removedBundle()</tt> methods
- * are very simple and do not do anything to change the state of any bundles.
-**/
-public abstract class BundleTracker
-{
- final Set<Bundle> m_bundles = new HashSet<Bundle>();
- protected final BundleContext m_context;
- final SynchronousBundleListener m_listener;
- boolean m_open;
-
- /**
- * Constructs a bundle tracker object that will use the specified
- * bundle context.
- * @param context The bundle context to use to track bundles.
- **/
- public BundleTracker(BundleContext context)
- {
- m_context = context;
- m_listener = new SynchronousBundleListener() {
- public void bundleChanged(BundleEvent evt)
- {
- synchronized (BundleTracker.this)
- {
- if (!m_open)
- {
- return;
- }
-
- if (evt.getType() == BundleEvent.STARTED)
- {
- if (!m_bundles.contains(evt.getBundle()))
- {
- m_bundles.add(evt.getBundle());
- addedBundle(evt.getBundle());
- }
- }
- else if (evt.getType() == BundleEvent.STOPPED)
- {
- if (m_bundles.contains(evt.getBundle()))
- {
- m_bundles.remove(evt.getBundle());
- removedBundle(evt.getBundle());
- }
- }
- }
- }
- };
- }
-
- /**
- * Returns the current set of active bundles.
- * @return The current set of active bundles.
- **/
- public synchronized Bundle[] getBundles()
- {
- return m_bundles.toArray(new Bundle[m_bundles.size()]);
- }
-
- /**
- * Call this method to start the tracking of active bundles.
- **/
- public synchronized void open()
- {
- if (!m_open)
- {
- m_open = true;
-
- m_context.addBundleListener(m_listener);
-
- Bundle[] bundles = m_context.getBundles();
- for (Bundle bundle : bundles)
- {
- if (bundle.getState() == Bundle.ACTIVE)
- {
- m_bundles.add(bundle);
- addedBundle(bundle);
- }
- }
- }
- }
-
- /**
- * Call this method to stop the tracking of active bundles.
- **/
- public synchronized void close()
- {
- if (m_open)
- {
- m_open = false;
-
- m_context.removeBundleListener(m_listener);
-
- for (Iterator<Bundle> itr = m_bundles.iterator(); itr.hasNext();)
- {
- Bundle bundle = itr.next();
- itr.remove();
- removedBundle(bundle);
- }
- }
- }
-
- /**
- * Subclasses must implement this method; it can be used to perform
- * actions upon the activation of a bundle. Subclasses should keep
- * this method implementation as simple as possible and should not
- * cause the change in any bundle state to avoid concurrency issues.
- * @param bundle The bundle being added to the active set.
- **/
- protected abstract void addedBundle(Bundle bundle);
-
- /**
- * Subclasses must implement this method; it can be used to perform
- * actions upon the deactivation of a bundle. Subclasses should keep
- * this method implementation as simple as possible and should not
- * cause the change in any bundle state to avoid concurrency issues.
- * @param bundle The bundle being removed from the active set.
- **/
- protected abstract void removedBundle(Bundle bundle);
-}
\ No newline at end of file
diff --git a/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/ShapeBundleTracker.java b/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/ShapeBundleTracker.java
new file mode 100644
index 0000000..93b3a09
--- /dev/null
+++ b/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/ShapeBundleTracker.java
@@ -0,0 +1,144 @@
+package org.apache.felix.example.extenderbased.host;
+
+import java.util.Dictionary;
+import javax.swing.Icon;
+import javax.swing.ImageIcon;
+import javax.swing.SwingUtilities;
+import org.apache.felix.example.extenderbased.host.extension.SimpleShape;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.util.tracker.BundleTracker;
+
+/**
+ * This is a simple bundle tracker utility class that tracks active
+ * bundles. The tracker must be given a bundle context upon creation,
+ * which it uses to listen for bundle events. The bundle tracker must be
+ * opened to track objects and closed when it is no longer needed.
+ *
+ * @see BundleTracker
+**/
+public class ShapeBundleTracker extends BundleTracker<SimpleShape>
+{
+ // The application object to notify.
+ private final DrawingFrame m_frame;
+
+ /**
+ * Constructs a tracker that uses the specified bundle context to
+ * track extensions and notifies the specified application object about
+ * changes.
+ * @param context The bundle context to be used by the tracker.
+ * @param frame The application object to notify about extension changes.
+ **/
+ public ShapeBundleTracker(BundleContext context, DrawingFrame frame)
+ {
+ // we only want to track active bundles
+ super(context, Bundle.ACTIVE, null);
+ this.m_frame = frame;
+ }
+
+ // Gets called when a bundle in enters the state ACTIVE
+ @Override
+ public SimpleShape addingBundle(Bundle bundle, BundleEvent event)
+ {
+ // Try to get the name of the extension.
+ Dictionary<String, String> dict = bundle.getHeaders();
+ String name = dict.get(SimpleShape.NAME_PROPERTY);
+
+ // if the name is not null, bundle is a ShapeBundle
+ if (name != null)
+ {
+ // Get the icon resource of the extension.
+ String iconPath = dict.get(SimpleShape.ICON_PROPERTY);
+ Icon icon = new ImageIcon(bundle.getResource(iconPath));
+ // Get the class of the extension.
+ String className = dict.get(SimpleShape.CLASS_PROPERTY);
+ SimpleShape shape = new DefaultShape(bundle.getBundleContext(),
+ bundle.getBundleId(), className);
+ processAdd(name, icon, shape);
+ return shape;
+ }
+
+ // bundle is no ShapeBundle, ingore it
+ return null;
+ }
+
+ /**
+ * Util method to process the addition of a new shape.
+ *
+ * @param name The name of the new shape
+ * @param icon The icon of the new shape
+ * @param shape the shape itself
+ */
+ private void processAdd(final String name, final Icon icon, final SimpleShape shape)
+ {
+ try
+ {
+ if (SwingUtilities.isEventDispatchThread())
+ {
+ m_frame.addShape(name, icon, shape);
+ }
+ else
+ {
+ SwingUtilities.invokeAndWait(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ m_frame.addShape(name, icon, shape);
+ }
+ });
+ }
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+
+ // Gets called when a bundle leaves the ACTIVE state
+ @Override
+ public void removedBundle(Bundle bundle, BundleEvent event, SimpleShape object)
+ {
+ // Try to get the name of the extension.
+ Dictionary<String, String> dict = bundle.getHeaders();
+ String name = dict.get(SimpleShape.NAME_PROPERTY);
+
+ // if the name is not null, bundle is a ShapeBundle
+ if (name != null)
+ {
+ prcoessRemove(name);
+ }
+ }
+
+ /**
+ * Util method to process the removal of a shape.
+ *
+ * @param name the name of the shape that is about to be removed.
+ */
+ private void prcoessRemove(final String name)
+ {
+ try
+ {
+ if (SwingUtilities.isEventDispatchThread())
+ {
+ m_frame.removeShape(name);
+ }
+ else
+ {
+ SwingUtilities.invokeAndWait(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ m_frame.removeShape(name);
+ }
+ });
+ }
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+}
\ No newline at end of file
diff --git a/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/ShapeTracker.java b/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/ShapeTracker.java
deleted file mode 100644
index 6f4baf4..0000000
--- a/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/ShapeTracker.java
+++ /dev/null
@@ -1,179 +0,0 @@
-/*
- * 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.example.extenderbased.host;
-
-import java.util.Dictionary;
-import javax.swing.Icon;
-import javax.swing.ImageIcon;
-import javax.swing.SwingUtilities;
-import org.apache.felix.example.extenderbased.host.extension.SimpleShape;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-
-/**
- * Extends the <tt>BundleTracker</tt> to create a tracker for
- * <tt>SimpleShape</tt> extensions. The tracker is responsible for
- * listening for <tt>SimpleShape</tt> extensions and informing the
- * application about the availability of shapes. This tracker forces
- * all notifications to be processed on the Swing event thread to
- * avoid synchronization and redraw issues.
-**/
-public class ShapeTracker extends BundleTracker
-{
- // The application object to notify.
- private final DrawingFrame m_frame;
-
- /**
- * Constructs a tracker that uses the specified bundle context to
- * track extensions and notifies the specified application object about
- * changes.
- * @param context The bundle context to be used by the tracker.
- * @param frame The application object to notify about extension changes.
- **/
- public ShapeTracker(BundleContext context, DrawingFrame frame)
- {
- super(context);
- m_frame = frame;
- }
-
- /**
- * Overrides the <tt>BundleTracker</tt> functionality to inform
- * the application object about the added extensions.
- * @param bundle The activated bundle.
- **/
- @Override
- protected void addedBundle(Bundle bundle)
- {
- processBundleOnEventThread(ShapeEvent.ADDED, bundle);
- }
-
- /**
- * Overrides the <tt>BundleTracker</tt> functionality to inform
- * the application object about removed extensions.
- * @param bundle The inactivated bundle.
- **/
- @Override
- protected void removedBundle(Bundle bundle)
- {
- processBundleOnEventThread(ShapeEvent.REMOVED, bundle);
- }
-
- /**
- * Processes a received bundle notification from the <tt>BundleTracker</tt>,
- * forcing the processing of the notification onto the Swing event thread
- * if it is not already on it.
- * @param event The type of action associated with the notification.
- * @param bundle The bundle of the corresponding extension.
- **/
- private void processBundleOnEventThread(ShapeEvent event, Bundle bundle)
- {
- try
- {
- if (SwingUtilities.isEventDispatchThread())
- {
- processBundle(event, bundle);
- }
- else
- {
- SwingUtilities.invokeAndWait(new BundleRunnable(event, bundle));
- }
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
-
- /**
- * Actually performs the processing of the bundle notification. Invokes
- * the appropriate callback method on the application object depending on
- * the action type of the notification.
- * @param event The type of action associated with the notification.
- * @param bundle The bundle of the corresponding extension.
- **/
- private void processBundle(ShapeEvent event, Bundle bundle)
- {
- // see http://www.osgi.org/javadoc/r4v43/org/osgi/framework/Bundle.html#getHeaders()
- @SuppressWarnings("unchecked")
- Dictionary<String, String> dict = bundle.getHeaders();
-
- // Try to get the name of the extension.
- String name = dict.get(SimpleShape.NAME_PROPERTY);
- // Return immediately if the bundle is not an extension.
- if (name == null)
- {
- return;
- }
-
- switch (event)
- {
- case ADDED:
- // Get the icon resource of the extension.
- String iconPath = dict.get(SimpleShape.ICON_PROPERTY);
- Icon icon = new ImageIcon(bundle.getResource(iconPath));
- // Get the class of the extension.
- String className = dict.get(SimpleShape.CLASS_PROPERTY);
- m_frame.addShape(
- name,
- icon,
- new DefaultShape(m_context, bundle.getBundleId(), className));
- break;
-
- case REMOVED:
- m_frame.removeShape(name);
- break;
- }
- }
-
- /**
- * Simple class used to process bundle notification handling on the
- * Swing event thread.
- **/
- private class BundleRunnable implements Runnable
- {
- private final ShapeEvent m_event;
- private final Bundle m_bundle;
-
- /**
- * Constructs an object with the specified action and bundle
- * object for processing on the Swing event thread.
- * @param event The type of action associated with the notification.
- * @param bundle The bundle of the corresponding extension.
- **/
- public BundleRunnable(ShapeEvent event, Bundle bundle)
- {
- m_event = event;
- m_bundle = bundle;
- }
-
- /**
- * Calls the <tt>processBundle()</tt> method.
- **/
- public void run()
- {
- processBundle(m_event, m_bundle);
- }
- }
-
- private static enum ShapeEvent
- {
- ADDED,
- REMOVED
- }
-}
\ No newline at end of file
diff --git a/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/Application.java b/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/Application.java
index 2f5516e..df52b2f 100644
--- a/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/Application.java
+++ b/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/Application.java
@@ -20,7 +20,6 @@
import java.util.Map;
import java.util.ServiceLoader;
-
import org.apache.felix.example.extenderbased.host.Activator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@@ -35,13 +34,13 @@
* internal extensions to providing drawing functionality. To successfully
* launch the stand-alone application, it must be run from this bundle's
* installation directory using "{@code java -jar}".
- * The locations of any additional extensions that have to be started, have to
- * be passed as command line arguments to this method.
+ * The locations of any additional extensions that have to be started, have to
+ * be passed as command line arguments to this method.
*/
public class Application
{
private static Framework m_framework = null;
-
+
/**
* Enables the bundle to run as a stand-alone application. When this
* static {@code main()} method is invoked, the application creates
@@ -49,33 +48,33 @@
* internal extensions to provide drawing functionality. To successfully
* launch as a stand-alone application, this method should be invoked from
* the bundle's installation directory using "{@code java -jar}".
- * The location of any extension that shall be installed can be passed
+ * The location of any extension that shall be installed can be passed
* as parameters.
* <p>
* For example if you build the bundles inside your workspace, maven will
- * create a target directory in every project. To start the application
- * from within your IDE you should pass:
+ * create a target directory in every project. To start the application
+ * from within your IDE you should pass:
* <p>
* <pre>
- * {@code file:../extenderbased.circle/target/extenderbased.circle-1.0.0.jar
- * file:../extenderbased.square/target/extenderbased.square-1.0.0.jar
+ * {@code file:../extenderbased.circle/target/extenderbased.circle-1.0.0.jar
+ * file:../extenderbased.square/target/extenderbased.square-1.0.0.jar
* file:../extenderbased.triangle/target/extenderbased.triangle-1.0.0.jar}
* </pre>
- *
+ *
* @param args The locations of additional bundles to start.
**/
public static void main(String[] args)
{
// args should never be null if the application is run from the command line. Check it anyway.
String[] locations = args != null ? args : new String[0];
-
+
// Print welcome banner.
System.out.println("\nWelcome to My Launcher");
System.out.println("======================\n");
try
{
- Map<String, Object> config = ConfigUtil.createConfig();
+ Map<String, String> config = ConfigUtil.createConfig();
m_framework = createFramework(config);
m_framework.init();
m_framework.start();
@@ -92,13 +91,13 @@
}
/**
- * Util method for creating an embedded Framework. Tries to create a {@link FrameworkFactory}
+ * Util method for creating an embedded Framework. Tries to create a {@link FrameworkFactory}
* which is then be used to create the framework.
*
* @param config the configuration to create the framework with
* @return a Framework with the given configuration
*/
- private static Framework createFramework(Map<String, Object> config)
+ private static Framework createFramework(Map<String, String> config)
{
ServiceLoader<FrameworkFactory> factoryLoader = ServiceLoader.load(FrameworkFactory.class);
for(FrameworkFactory factory : factoryLoader){
@@ -106,10 +105,10 @@
}
throw new IllegalStateException("Unable to load FrameworkFactory service.");
}
-
+
/**
- * Installs and starts all bundles used by the application. Therefore the host bundle will be started. The locations
- * of extensions for the host bundle can be passed in as parameters.
+ * Installs and starts all bundles used by the application. Therefore the host bundle will be started. The locations
+ * of extensions for the host bundle can be passed in as parameters.
*
* @param bundleLocations the locations where extension for the host bundle are located. Must not be {@code null}!
* @throws BundleException if something went wrong while installing or starting the bundles.
@@ -125,4 +124,4 @@
addition.start();
}
}
-}
+}
\ No newline at end of file
diff --git a/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/ConfigUtil.java b/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/ConfigUtil.java
index b4375f6..dcfdbda 100644
--- a/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/ConfigUtil.java
+++ b/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/ConfigUtil.java
@@ -22,7 +22,6 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
-
import org.osgi.framework.Constants;
/**
@@ -32,21 +31,22 @@
{
/**
- * Creates a configuration for the framework. Therefore this method attempts to create
- * a temporary cache dir. If creation of the cache dir is successful, it will be added
+ * Creates a configuration for the framework. Therefore this method attempts to create
+ * a temporary cache dir. If creation of the cache dir is successful, it will be added
* to the configuration.
- *
+ *
* @return
*/
- public static Map<String, Object> createConfig()
+ public static Map<String, String> createConfig()
{
final File cachedir = createCacheDir();
- Map<String, Object> configMap = new HashMap<String, Object>();
- // Tells the framework to export the extension package, making it accessible for the other shape bundels
+ Map<String, String> configMap = new HashMap<String, String>();
+ // Tells the framework to export the extension package, making it accessible
+ // for the other shape bundels
configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
"org.apache.felix.example.extenderbased.host.extension; version=1.0.0");
-
+
// if we could create a cache dir, we use it. Otherwise the platform default will be used
if (cachedir != null)
{
@@ -57,8 +57,8 @@
}
/**
- * Tries to create a temporay cache dir. If creation of the cache dir is successful,
- * it will be returned. If creation fails, null will be returned.
+ * Tries to create a temporay cache dir. If creation of the cache dir is successful,
+ * it will be returned. If creation fails, null will be returned.
*
* @return a {@code File} object representing the cache dir
*/
@@ -80,7 +80,7 @@
}
/**
- * Adds a shutdown hook to the runtime, that will make sure, that the cache dir will
+ * Adds a shutdown hook to the runtime, that will make sure, that the cache dir will
* be deleted after the application has been terminated.
*/
private static void createShutdownHook(final File cachedir)
@@ -113,4 +113,4 @@
}
file.delete();
}
-}
+}
\ No newline at end of file