activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dej...@apache.org
Subject svn commit: r901177 - in /activemq/branches/activemq-5.3: ./ activemq-core/ activemq-core/src/main/java/org/apache/activemq/util/ activemq-core/src/main/java/org/apache/activemq/util/osgi/ activemq-core/src/main/java/org/apache/activemq/xbean/
Date Wed, 20 Jan 2010 13:12:00 GMT
Author: dejanb
Date: Wed Jan 20 13:11:59 2010
New Revision: 901177

URL: http://svn.apache.org/viewvc?rev=901177&view=rev
Log:
merging 816444 and 901171 - osgi support and https://issues.apache.org/activemq/browse/AMQ-2574
- stop osgi bundle on application context close

Added:
    activemq/branches/activemq-5.3/activemq-core/src/main/java/org/apache/activemq/util/osgi/
      - copied from r816444, activemq/trunk/activemq-core/src/main/java/org/apache/activemq/util/osgi/
    activemq/branches/activemq-5.3/activemq-core/src/main/java/org/apache/activemq/util/osgi/Activator.java
      - copied unchanged from r816444, activemq/trunk/activemq-core/src/main/java/org/apache/activemq/util/osgi/Activator.java
Modified:
    activemq/branches/activemq-5.3/activemq-core/pom.xml
    activemq/branches/activemq-5.3/activemq-core/src/main/java/org/apache/activemq/util/FactoryFinder.java
    activemq/branches/activemq-5.3/activemq-core/src/main/java/org/apache/activemq/xbean/XBeanBrokerService.java
    activemq/branches/activemq-5.3/pom.xml

Modified: activemq/branches/activemq-5.3/activemq-core/pom.xml
URL: http://svn.apache.org/viewvc/activemq/branches/activemq-5.3/activemq-core/pom.xml?rev=901177&r1=901176&r2=901177&view=diff
==============================================================================
--- activemq/branches/activemq-5.3/activemq-core/pom.xml (original)
+++ activemq/branches/activemq-5.3/activemq-core/pom.xml Wed Jan 20 13:11:59 2010
@@ -85,11 +85,23 @@
       <groupId>org.apache.activemq.protobuf</groupId>
       <artifactId>activemq-protobuf</artifactId>
       <optional>false</optional>
-    </dependency>  
-    
+    </dependency>
+
     <!-- =============================== -->
     <!-- Optional Dependencies           -->
     <!-- =============================== -->
+
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+    </dependency>
+    
+    <dependency>
+        <groupId>org.springframework.osgi</groupId>
+        <artifactId>spring-osgi-core</artifactId>
+    </dependency>
+    
+
     <dependency>
       <groupId>org.apache.activemq</groupId>
       <artifactId>activemq-jaas</artifactId>

Modified: activemq/branches/activemq-5.3/activemq-core/src/main/java/org/apache/activemq/util/FactoryFinder.java
URL: http://svn.apache.org/viewvc/activemq/branches/activemq-5.3/activemq-core/src/main/java/org/apache/activemq/util/FactoryFinder.java?rev=901177&r1=901176&r2=901177&view=diff
==============================================================================
--- activemq/branches/activemq-5.3/activemq-core/src/main/java/org/apache/activemq/util/FactoryFinder.java
(original)
+++ activemq/branches/activemq-5.3/activemq-core/src/main/java/org/apache/activemq/util/FactoryFinder.java
Wed Jan 20 13:11:59 2010
@@ -22,89 +22,129 @@
 import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
 
+/**
+ * 
+ */
 public class FactoryFinder {
 
-    private final String path;
-    private final ConcurrentHashMap<String, Class> classMap = new ConcurrentHashMap<String,
Class>();
+    /**
+     * The strategey that the FactoryFinder uses to find load and instanciate Objects
+     * can be chagned out by calling the
+     * {@link org.apache.activemq.util.FactoryFinder#setObjectFactory(org.apache.activemq.util.FactoryFinder.ObjectFactory)}
+     * method with a custom implemenation of ObjectFactory.
+     *
+     * The default ObjectFactory is typically changed out when running in a specialized container
+     * enviorment where service discovery needs to be done via the container system.  For
example,
+     * in an OSGi scenario.
+     */
+    public interface ObjectFactory {
+        /**
+         * @param path the full service path 
+         * @return
+         */
+        public Object create(String path) throws IllegalAccessException, InstantiationException,
IOException, ClassNotFoundException;
 
-    public FactoryFinder(String path) {
-        this.path = path;
     }
 
     /**
-     * Creates a new instance of the given key
-     * 
-     * @param key is the key to add to the path to find a text file containing
-     *                the factory name
-     * @return a newly created instance
+     * The default implementation of Object factory which works well in standalone applications.
      */
-    public Object newInstance(String key) throws IllegalAccessException, InstantiationException,
IOException, ClassNotFoundException {
-        return newInstance(key, null);
-    }
+    protected static class StandaloneObjectFactory implements ObjectFactory {
+        final ConcurrentHashMap<String, Class> classMap = new ConcurrentHashMap<String,
Class>();
 
-    public Object newInstance(String key, String propertyPrefix) throws IllegalAccessException,
InstantiationException, IOException, ClassNotFoundException {
-        if (propertyPrefix == null) {
-            propertyPrefix = "";
+        public Object create(final String path) throws InstantiationException, IllegalAccessException,
ClassNotFoundException, IOException {
+            Class clazz = classMap.get(path);
+            if (clazz == null) {
+                clazz = loadClass(loadProperties(path));
+                classMap.put(path, clazz);
+            }
+            return clazz.newInstance();
         }
 
-        Class clazz = classMap.get(propertyPrefix + key);
-        if (clazz == null) {
-            clazz = newInstance(doFindFactoryProperies(key), propertyPrefix);
-            classMap.put(propertyPrefix + key, clazz);
-        }
-        return clazz.newInstance();
-    }
+        static public Class loadClass(Properties properties) throws ClassNotFoundException,
IOException {
 
-    private Class newInstance(Properties properties, String propertyPrefix) throws ClassNotFoundException,
IOException {
+            String className = properties.getProperty("class");
+            if (className == null) {
+                throw new IOException("Expected property is missing: class");
+            }
+            Class clazz = null;
+            ClassLoader loader = Thread.currentThread().getContextClassLoader();
+            if (loader != null) {
+                try {
+                    clazz = loader.loadClass(className);
+                } catch (ClassNotFoundException e) {
+                    // ignore
+                }
+            }
+            if (clazz == null) {
+                clazz = FactoryFinder.class.getClassLoader().loadClass(className);
+            }
 
-        String className = properties.getProperty(propertyPrefix + "class");
-        if (className == null) {
-            throw new IOException("Expected property is missing: " + propertyPrefix + "class");
+            return clazz;
         }
-        Class clazz = null;
-        ClassLoader loader = Thread.currentThread().getContextClassLoader();
-        if (loader != null) {
+
+        static public Properties loadProperties(String uri) throws IOException {
+            // lets try the thread context class loader first
+            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+            if (classLoader == null) {
+                classLoader = StandaloneObjectFactory.class.getClassLoader();
+            }
+            InputStream in = classLoader.getResourceAsStream(uri);
+            if (in == null) {
+                in = FactoryFinder.class.getClassLoader().getResourceAsStream(uri);
+                if (in == null) {
+                    throw new IOException("Could not find factory class for resource: " +
uri);
+                }
+            }
+
+            // lets load the file
+            BufferedInputStream reader = null;
             try {
-                clazz = loader.loadClass(className);
-            } catch (ClassNotFoundException e) {
-                // ignore
+                reader = new BufferedInputStream(in);
+                Properties properties = new Properties();
+                properties.load(reader);
+                return properties;
+            } finally {
+                try {
+                    reader.close();
+                } catch (Exception e) {
+                }
             }
         }
-        if (clazz == null) {
-            clazz = FactoryFinder.class.getClassLoader().loadClass(className);
-        }
+    }
+
+    // ================================================================
+    // Class methods and properties
+    // ================================================================
+    private static ObjectFactory objectFactory = new StandaloneObjectFactory();
 
-        return clazz;
+    public static ObjectFactory getObjectFactory() {
+        return objectFactory;
     }
 
-    private Properties doFindFactoryProperies(String key) throws IOException {
-        String uri = path + key;
+    public static void setObjectFactory(ObjectFactory objectFactory) {
+        FactoryFinder.objectFactory = objectFactory;
+    }
 
-        // lets try the thread context class loader first
-        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-        if (classLoader == null) {
-            classLoader = getClass().getClassLoader();
-        }
-        InputStream in = classLoader.getResourceAsStream(uri);
-        if (in == null) {
-            in = FactoryFinder.class.getClassLoader().getResourceAsStream(uri);
-            if (in == null) {
-                throw new IOException("Could not find factory class for resource: " + uri);
-            }
-        }
+    // ================================================================
+    // Instance methods and properties
+    // ================================================================
+    private final String path;
 
-        // lets load the file
-        BufferedInputStream reader = null;
-        try {
-            reader = new BufferedInputStream(in);
-            Properties properties = new Properties();
-            properties.load(reader);
-            return properties;
-        } finally {
-            try {
-                reader.close();
-            } catch (Exception e) {
-            }
-        }
+    public FactoryFinder(String path) {
+        this.path = path;
     }
+
+    /**
+     * Creates a new instance of the given key
+     *
+     * @param key is the key to add to the path to find a text file containing
+     *                the factory name
+     * @return a newly created instance
+     */
+    public Object newInstance(String key) throws IllegalAccessException, InstantiationException,
IOException, ClassNotFoundException {
+        return objectFactory.create(path+key);
+    }
+
+    
 }

Modified: activemq/branches/activemq-5.3/activemq-core/src/main/java/org/apache/activemq/xbean/XBeanBrokerService.java
URL: http://svn.apache.org/viewvc/activemq/branches/activemq-5.3/activemq-core/src/main/java/org/apache/activemq/xbean/XBeanBrokerService.java?rev=901177&r1=901176&r2=901177&view=diff
==============================================================================
--- activemq/branches/activemq-5.3/activemq-core/src/main/java/org/apache/activemq/xbean/XBeanBrokerService.java
(original)
+++ activemq/branches/activemq-5.3/activemq-core/src/main/java/org/apache/activemq/xbean/XBeanBrokerService.java
Wed Jan 20 13:11:59 2010
@@ -20,12 +20,16 @@
 
 import org.apache.activemq.broker.BrokerService;
 import org.apache.activemq.usage.SystemUsage;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.osgi.framework.BundleException;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.osgi.context.support.OsgiBundleXmlApplicationContext;
 
 /**
  * An ActiveMQ Message Broker. It consists of a number of transport
@@ -42,7 +46,8 @@
  * @version $Revision: 1.1 $
  */
 public class XBeanBrokerService extends BrokerService implements InitializingBean, DisposableBean,
ApplicationContextAware {
-
+    private static final transient Log LOG = LogFactory.getLog(XBeanBrokerService.class);
+    
     private boolean start = true;
     private ApplicationContext applicationContext = null;
     private boolean destroyApplicationContextOnShutdown = false;
@@ -61,6 +66,14 @@
                     if (applicationContext instanceof ConfigurableApplicationContext) {
 	                    ((ConfigurableApplicationContext) applicationContext).close();
                     }
+                    if (applicationContext instanceof OsgiBundleXmlApplicationContext){
+                        try {
+                            ((OsgiBundleXmlApplicationContext)applicationContext).getBundle().stop();
+                        } catch (BundleException e) {
+                            LOG.info("Error stopping OSGi bundle " + e, e);
+                        }
+                    }
+
                 }
             });
         }

Modified: activemq/branches/activemq-5.3/pom.xml
URL: http://svn.apache.org/viewvc/activemq/branches/activemq-5.3/pom.xml?rev=901177&r1=901176&r2=901177&view=diff
==============================================================================
--- activemq/branches/activemq-5.3/pom.xml (original)
+++ activemq/branches/activemq-5.3/pom.xml Wed Jan 20 13:11:59 2010
@@ -37,6 +37,7 @@
     <!-- base url for site deployment.  See distribution management for full url.  Override
this in settings.xml for staging -->
     <staging.siteURL>scp://people.apache.org/x1/www/activemq.apache.org</staging.siteURL>
     <spring-version>2.5.6</spring-version>
+    <spring-osgi-version>1.2.1</spring-osgi-version>
     <activesoap-version>1.3</activesoap-version>
     <annogen-version>0.1.0</annogen-version>
     <ant-version>1.7.1</ant-version>
@@ -390,6 +391,13 @@
       </dependency>
 
       <dependency>
+        <groupId>org.osgi</groupId>
+        <artifactId>org.osgi.core</artifactId>
+        <version>4.1.0</version>
+        <scope>provided</scope>
+      </dependency>
+
+      <dependency>
         <groupId>org.apache.hadoop.zookeeper</groupId>
         <artifactId>zookeeper</artifactId>
         <version>3.0.0</version>
@@ -579,6 +587,11 @@
         <artifactId>spring-oxm-tiger</artifactId>
         <version>${spring-oxm-version}</version>
       </dependency>
+      <dependency>
+          <groupId>org.springframework.osgi</groupId>
+          <artifactId>spring-osgi-core</artifactId>
+          <version>${spring-osgi-version}</version>
+      </dependency>
 
       <!-- Optional Derby support-->
       <dependency>



Mime
View raw message