Author: norman
Date: Sat Jul  2 21:10:45 2011
New Revision: 1142304

URL: http://svn.apache.org/viewvc?rev=1142304&view=rev
Log:
Register Tracker services for UsersRepository, RecipientRewriteTable and 
DomainList implementations. This takes care of dynamic loading/unloading of 
such implementations without the need of the user/dev to register such a 
service by his own. See JAMES-835

Added:
    
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/
    
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/AbstractServiceTracker.java
    
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/DomainListTracker.java
    
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/OSGIConfigurationProvider.java
    
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/RecipientRewriteTableTracker.java
    
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/UsersRepositoryTracker.java
Modified:
    james/server/trunk/container-spring/pom.xml
    
james/server/trunk/container-spring/src/main/resources/META-INF/spring/loaders-context.xml

Modified: james/server/trunk/container-spring/pom.xml
URL: 
http://svn.apache.org/viewvc/james/server/trunk/container-spring/pom.xml?rev=1142304&r1=1142303&r2=1142304&view=diff
==============================================================================
--- james/server/trunk/container-spring/pom.xml (original)
+++ james/server/trunk/container-spring/pom.xml Sat Jul  2 21:10:45 2011
@@ -36,10 +36,10 @@
     </james.osgi.export>
     <!-- exclude spring web support and commons-daemon for now -->
     <james.osgi.import>
+      *
       !org.apache.commons.daemon.*,
       !org.apache.james.mailbox.copier.*,
       !org.springframework.web.*,
-      *
     </james.osgi.import>
     <james.osgi.dynamic>
       *
@@ -139,6 +139,14 @@
       <artifactId>spring-web</artifactId>
     </dependency>
     <dependency>
+      <groupId>org.springframework.osgi</groupId>
+      <artifactId>spring-osgi-extender</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+    </dependency>
+    <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
       <scope>provided</scope>

Added: 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/AbstractServiceTracker.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/AbstractServiceTracker.java?rev=1142304&view=auto
==============================================================================
--- 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/AbstractServiceTracker.java
 (added)
+++ 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/AbstractServiceTracker.java
 Sat Jul  2 21:10:45 2011
@@ -0,0 +1,153 @@
+/****************************************************************
+ * 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.james.container.spring.osgi;
+
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Properties;
+
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import 
org.apache.james.container.spring.provider.configuration.ConfigurationProvider;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.osgi.framework.ServiceRegistration;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanFactoryAware;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.osgi.context.BundleContextAware;
+import 
org.springframework.osgi.service.exporter.support.BeanNameServicePropertiesResolver;
+
+/**
+ * This {@link BundleListener} use the extender pattern to scan all loaded
+ * bundles if a class name with a given name is present. If so it register in
+ * the {@link BeanDefinitionRegistry} and also register it to
+ * {@link BundleContext} as service. This allows to dynamic load and unload 
osgi
+ * bundles
+ * 
+ */
+public abstract class AbstractServiceTracker implements BeanFactoryAware, 
BundleListener, BundleContextAware, InitializingBean, DisposableBean {
+
+    private BundleContext context;
+    private BeanFactory factory;
+    private String configuredClass;
+    private BeanNameServicePropertiesResolver resolver;
+    private ServiceRegistration reg;
+
+    @Override
+    public void setBeanFactory(BeanFactory factory) throws BeansException {
+        this.factory = factory;
+    }
+
+    @Override
+    public void setBundleContext(BundleContext context) {
+        this.context = context;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public void bundleChanged(BundleEvent event) {
+        Bundle b = event.getBundle();
+
+        if (b.equals(this.context.getBundle())) {
+            return;
+        }
+
+        switch (event.getType()) {
+        case BundleEvent.STARTED:
+            Enumeration<?> entrs = b.findEntries("/", "*.class", true);
+            if (entrs != null) {
+
+                while (entrs.hasMoreElements()) {
+                    URL e = (URL) entrs.nextElement();
+                    String file = e.getFile();
+
+                    String className = file.replaceAll("/", 
".").replaceAll(".class", "").replaceFirst(".", "");
+                    if (className.equals(configuredClass)) {
+                        Properties p = new Properties();
+                        
p.putAll(resolver.getServiceProperties(getComponentName()));
+                        Class<?> clazz = getServiceClass();
+                        // Create the definition and register it
+                        BeanDefinitionRegistry registry = 
(BeanDefinitionRegistry) factory;
+                        BeanDefinition def = 
BeanDefinitionBuilder.genericBeanDefinition(className).getBeanDefinition();
+                        registry.registerBeanDefinition(getComponentName(), 
def);
+
+                        reg = 
b.getBundleContext().registerService(clazz.getName(), factory.getBean(clazz), 
p);
+                    }
+                }
+            }
+            break;
+        case BundleEvent.STOPPED:
+            if (reg != null) {
+
+                // Check if we need to unregister the service
+                if (b.equals(reg.getReference().getBundle())) {
+                    reg.unregister();
+                }
+            }
+            break;
+        default:
+            break;
+        }
+
+    }
+
+    @Override
+    public void afterPropertiesSet() throws Exception {
+        ConfigurationProvider confProvider = 
factory.getBean(ConfigurationProvider.class);
+        HierarchicalConfiguration config = 
confProvider.getConfiguration(getComponentName());
+
+        // Setup a resolver
+        resolver = new BeanNameServicePropertiesResolver();
+        resolver.setBundleContext(context);
+
+        // Get the configuration for the class
+        configuredClass = config.getString("[@class]");
+        context.addBundleListener(this);
+    }
+
+    @Override
+    public void destroy() throws Exception {
+        if (context != null) {
+            context.removeBundleListener(this);
+        }
+    }
+
+    /**
+     * Return the name of the component
+     * 
+     * @return name
+     */
+    protected abstract String getComponentName();
+
+    /**
+     * Return the class which will be used to expose the service in the OSGI
+     * registry
+     * 
+     * @return sClass
+     */
+    protected abstract Class<?> getServiceClass();
+
+}

Added: 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/DomainListTracker.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/DomainListTracker.java?rev=1142304&view=auto
==============================================================================
--- 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/DomainListTracker.java
 (added)
+++ 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/DomainListTracker.java
 Sat Jul  2 21:10:45 2011
@@ -0,0 +1,35 @@
+/****************************************************************
+ * 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.james.container.spring.osgi;
+
+import org.apache.james.domainlist.api.DomainList;
+
+public class DomainListTracker extends AbstractServiceTracker{
+
+    @Override
+    protected String getComponentName() {
+        return "domainlist";
+    }
+
+    @Override
+    protected Class<?> getServiceClass() {
+        return DomainList.class;
+    }
+
+}

Added: 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/OSGIConfigurationProvider.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/OSGIConfigurationProvider.java?rev=1142304&view=auto
==============================================================================
--- 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/OSGIConfigurationProvider.java
 (added)
+++ 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/OSGIConfigurationProvider.java
 Sat Jul  2 21:10:45 2011
@@ -0,0 +1,38 @@
+package org.apache.james.container.spring.osgi;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.apache.commons.configuration.XMLConfiguration;
+
+public class OSGIConfigurationProvider implements 
org.apache.james.container.spring.provider.configuration.ConfigurationProvider{
+
+    @Override
+    public void registerConfiguration(String beanName, 
HierarchicalConfiguration conf) {
+        // TODO Auto-generated method stub
+        
+    }
+
+    @Override
+    public HierarchicalConfiguration getConfiguration(String beanName) throws 
ConfigurationException {
+        XMLConfiguration config = new XMLConfiguration();
+        config.setDelimiterParsingDisabled(true);
+        
+        // Don't split attributes which can have bad side-effects with 
matcher-conditions.
+        // See JAMES-1233
+        config.setAttributeSplittingDisabled(true);
+        
+        // Use InputStream so we are not bound to File implementations of the
+        // config
+        try {
+            config.load(new FileInputStream("/tmp/" + beanName + ".xml"));
+        } catch (FileNotFoundException e) {
+            throw new ConfigurationException("Bean " + beanName);
+        }
+        
+        return config;
+    }
+
+}

Added: 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/RecipientRewriteTableTracker.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/RecipientRewriteTableTracker.java?rev=1142304&view=auto
==============================================================================
--- 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/RecipientRewriteTableTracker.java
 (added)
+++ 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/RecipientRewriteTableTracker.java
 Sat Jul  2 21:10:45 2011
@@ -0,0 +1,35 @@
+/****************************************************************
+ * 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.james.container.spring.osgi;
+
+import org.apache.james.rrt.api.RecipientRewriteTable;
+
+public class RecipientRewriteTableTracker extends AbstractServiceTracker{
+
+    @Override
+    protected String getComponentName() {
+        return "recipientrewritetable";
+    }
+
+    @Override
+    protected Class<?> getServiceClass() {
+        return RecipientRewriteTable.class;
+    }
+
+}

Added: 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/UsersRepositoryTracker.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/UsersRepositoryTracker.java?rev=1142304&view=auto
==============================================================================
--- 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/UsersRepositoryTracker.java
 (added)
+++ 
james/server/trunk/container-spring/src/main/java/org/apache/james/container/spring/osgi/UsersRepositoryTracker.java
 Sat Jul  2 21:10:45 2011
@@ -0,0 +1,36 @@
+/****************************************************************
+ * 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.james.container.spring.osgi;
+
+import org.apache.james.user.api.UsersRepository;
+
+public class UsersRepositoryTracker extends AbstractServiceTracker{
+
+    
+    @Override
+    protected String getComponentName() {
+        return "usersrepository";
+    }
+    
+    @Override
+    protected Class<?> getServiceClass() {
+        return UsersRepository.class;
+    }
+
+}

Modified: 
james/server/trunk/container-spring/src/main/resources/META-INF/spring/loaders-context.xml
URL: 
http://svn.apache.org/viewvc/james/server/trunk/container-spring/src/main/resources/META-INF/spring/loaders-context.xml?rev=1142304&r1=1142303&r2=1142304&view=diff
==============================================================================
--- 
james/server/trunk/container-spring/src/main/resources/META-INF/spring/loaders-context.xml
 (original)
+++ 
james/server/trunk/container-spring/src/main/resources/META-INF/spring/loaders-context.xml
 Sat Jul  2 21:10:45 2011
@@ -26,6 +26,12 @@
     <!-- Import PostBeanProcessors -->
     <import 
resource="classpath*:org/apache/james/container/spring/server-context.xml"/>
     
+    <!-- Register the service trackers -->
+    <bean id="usersrepositoryTracker" 
class="org.apache.james.container.spring.osgi.UsersRepositoryTracker"/>
+    <bean id="domainlistTracker" 
class="org.apache.james.container.spring.osgi.DomainListTracker"/>
+    <bean id="recipientrewritetableTracker" 
class="org.apache.james.container.spring.osgi.RecipientRwriteTableTracker"/>
+    
+
     <!-- 
       Mailet and Matcher "Bean-Factory".
      -->



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to