felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickh...@apache.org
Subject svn commit: r1305394 - in /felix/trunk/examples/extenderbased.host: ./ src/main/java/org/apache/felix/example/extenderbased/host/ src/main/java/org/apache/felix/example/extenderbased/host/launch/
Date Mon, 26 Mar 2012 15:23:42 GMT
Author: rickhall
Date: Mon Mar 26 15:23:41 2012
New Revision: 1305394

URL: http://svn.apache.org/viewvc?rev=1305394&view=rev
Log:
Apply patch FELIX-3401 to use OSGi BundleTracker in example instead of custom one.

Added:
    felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/ShapeBundleTracker.java
Removed:
    felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/BundleTracker.java
    felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/ShapeTracker.java
Modified:
    felix/trunk/examples/extenderbased.host/pom.xml
    felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/Activator.java
    felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/Application.java
    felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/ConfigUtil.java

Modified: felix/trunk/examples/extenderbased.host/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/examples/extenderbased.host/pom.xml?rev=1305394&r1=1305393&r2=1305394&view=diff
==============================================================================
--- felix/trunk/examples/extenderbased.host/pom.xml (original)
+++ felix/trunk/examples/extenderbased.host/pom.xml Mon Mar 26 15:23:41 2012
@@ -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>

Modified: felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/Activator.java
URL: http://svn.apache.org/viewvc/felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/Activator.java?rev=1305394&r1=1305393&r2=1305394&view=diff
==============================================================================
--- felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/Activator.java
(original)
+++ felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/Activator.java
Mon Mar 26 15:23:41 2012
@@ -20,10 +20,8 @@ package org.apache.felix.example.extende
 
 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 @@ import org.osgi.framework.BundleExceptio
 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 @@ public class Activator implements Bundle
 
                 m_frame.setVisible(true);
 
-                m_shapetracker = new ShapeTracker(context, m_frame);
+                m_shapetracker = new ShapeBundleTracker(context, m_frame);
                 m_shapetracker.open();
             }
         });

Added: felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/ShapeBundleTracker.java
URL: http://svn.apache.org/viewvc/felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/ShapeBundleTracker.java?rev=1305394&view=auto
==============================================================================
--- felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/ShapeBundleTracker.java
(added)
+++ felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/ShapeBundleTracker.java
Mon Mar 26 15:23:41 2012
@@ -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

Modified: felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/Application.java
URL: http://svn.apache.org/viewvc/felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/Application.java?rev=1305394&r1=1305393&r2=1305394&view=diff
==============================================================================
--- felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/Application.java
(original)
+++ felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/Application.java
Mon Mar 26 15:23:41 2012
@@ -20,7 +20,6 @@ package org.apache.felix.example.extende
 
 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 @@ import org.osgi.framework.launch.Framewo
  * 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 @@ public class Application
      * 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 @@ public class Application
     }
 
     /**
-     * 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 @@ public class Application
         }
         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 @@ public class Application
             addition.start();
         }
     }
-}
+}
\ No newline at end of file

Modified: felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/ConfigUtil.java
URL: http://svn.apache.org/viewvc/felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/ConfigUtil.java?rev=1305394&r1=1305393&r2=1305394&view=diff
==============================================================================
--- felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/ConfigUtil.java
(original)
+++ felix/trunk/examples/extenderbased.host/src/main/java/org/apache/felix/example/extenderbased/host/launch/ConfigUtil.java
Mon Mar 26 15:23:41 2012
@@ -22,7 +22,6 @@ import java.io.File;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
-
 import org.osgi.framework.Constants;
 
 /**
@@ -32,21 +31,22 @@ final class ConfigUtil
 {
 
     /**
-     * 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 @@ final class ConfigUtil
     }
 
     /**
-     * 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 @@ final class ConfigUtil
     }
 
     /**
-     * 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 @@ final class ConfigUtil
         }
         file.delete();
     }
-}
+}
\ No newline at end of file



Mime
View raw message