You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by sa...@apache.org on 2008/07/19 10:23:47 UTC

svn commit: r678117 - in /webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi: deployment/ deployment/tracker/ internal/

Author: saminda
Date: Sat Jul 19 01:23:47 2008
New Revision: 678117

URL: http://svn.apache.org/viewvc?rev=678117&view=rev
Log:
Cleaning up code and providing the extension from the registry concept. Some of the concepts are adhered from Eclipse 
http registry and jsp registry 

Added:
    webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/tracker/
    webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/tracker/BundleTracker.java
Modified:
    webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/AbstractRegistry.java
    webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/ModuleRegistry.java
    webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiConfigurationContextFactory.java
    webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/Registry.java
    webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/ServiceRegistry.java
    webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/internal/Activator.java

Modified: webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/AbstractRegistry.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/AbstractRegistry.java?rev=678117&r1=678116&r2=678117&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/AbstractRegistry.java (original)
+++ webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/AbstractRegistry.java Sat Jul 19 01:23:47 2008
@@ -15,17 +15,14 @@
  */
 package org.apache.axis2.osgi.deployment;
 
+import org.apache.axis2.context.ConfigurationContext;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
-import org.apache.axis2.AxisFault;
-import org.apache.axis2.context.ConfigurationContext;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 
-import java.util.Map;
-import java.util.List;
 import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
@@ -35,8 +32,6 @@
  */
 public abstract class AbstractRegistry<V> implements Registry {
 
-    private Log log = LogFactory.getLog(AbstractRegistry.class);
-
     protected Map<Bundle, List<V>> resolvedBundles = new ConcurrentHashMap<Bundle, List<V>>();
 
     protected List<Bundle> unreslovedBundles = new ArrayList<Bundle>();
@@ -52,35 +47,10 @@
         this.configCtx = configCtx;
     }
 
-    public void bundleChanged(BundleEvent event) {
-        Bundle bundle = event.getBundle();
-        try {
-            switch (event.getType()) {
-                case BundleEvent.STARTED:
-                    if (context.getBundle() != bundle) {
-                        register(event.getBundle());
-                    }
-                    break;
-
-                case BundleEvent.STOPPED:
-                    if (context.getBundle() != bundle) {
-                        unRegister(event.getBundle(), false);
-                    }
-                    break;
-                case BundleEvent.UNINSTALLED:
-                    if (context.getBundle() != bundle) {
-                        remove(bundle);
-                    }
-                    break;
-            }
-        } catch (AxisFault e) {
-            String msg = "Error while registering the bundle in AxisConfiguration";
-            log.error(msg, e);
-        }
-    }
-
-    public void resolve() throws AxisFault {
-        for (Bundle bundle : unreslovedBundles) {
+    public void resolve() {
+        //to avoid concurrent modification
+        Bundle[] bundles = unreslovedBundles.toArray(new Bundle[unreslovedBundles.size()]);
+        for (Bundle bundle : bundles) {
             register(bundle);
         }
     }

Modified: webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/ModuleRegistry.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/ModuleRegistry.java?rev=678117&r1=678116&r2=678117&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/ModuleRegistry.java (original)
+++ webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/ModuleRegistry.java Sat Jul 19 01:23:47 2008
@@ -16,6 +16,7 @@
 package org.apache.axis2.osgi.deployment;
 
 import org.apache.axis2.AxisFault;
+import org.apache.axis2.util.Utils;
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.deployment.DeploymentEngine;
 import org.apache.axis2.deployment.ModuleBuilder;
@@ -50,11 +51,10 @@
         this.serviceRegistry = serviceRegistry;
     }
 
-    public void register(Bundle bundle) throws AxisFault {
+    public void register(Bundle bundle) {
         lock.lock();
         try {
             addModules(bundle);
-            serviceRegistry.resolve();
         } finally {
             lock.unlock();
         }
@@ -124,12 +124,7 @@
                 for (Long bundleId : stopBundleList) {
                     Bundle unRegBundle = context.getBundle(bundleId);
                     if (unRegBundle != null) {
-                        try {
-                            serviceRegistry.unRegister(unRegBundle, false);
-                        } catch (AxisFault e) {
-                            String msg = "Erro while stopping the bundle";
-                            log.error(msg, e);
-                        }
+                        serviceRegistry.unRegister(unRegBundle, false);
                     }
                 }
             }
@@ -164,9 +159,9 @@
                     ClassLoader loader =
                             new BundleClassLoader(bundle, Registry.class.getClassLoader());
                     axismodule.setModuleClassLoader(loader);
+                    AxisConfiguration axisConfig = configCtx.getAxisConfiguration();
                     ModuleBuilder builder =
-                            new ModuleBuilder(url.openStream(), axismodule,
-                                              configCtx.getAxisConfiguration());
+                            new ModuleBuilder(url.openStream(), axismodule, axisConfig);
                     Dictionary headers = bundle.getHeaders();
                     String bundleSymbolicName = (String) headers.get("Bundle-SymbolicName");
                     if (bundleSymbolicName != null && bundleSymbolicName.length() != 0) {
@@ -200,11 +195,10 @@
                         axismodule.setVersion(moduleVersion);
                     }
                     builder.populateModule();
-                    axismodule.setParent(configCtx.getAxisConfiguration());
-                    AxisModule module =
-                            configCtx.getAxisConfiguration().getModule(axismodule.getName());
+                    axismodule.setParent(axisConfig);
+                    AxisModule module = axisConfig.getModule(axismodule.getName());
                     if (module == null) {
-                        DeploymentEngine.addNewModule(axismodule, configCtx.getAxisConfiguration());
+                        DeploymentEngine.addNewModule(axismodule, axisConfig);
                         //initialze the module if the module contains Module interface.
                         Module moduleObj = axismodule.getModule();
                         if (moduleObj != null) {
@@ -219,6 +213,9 @@
                         log.info("[ModuleRegistry] Module : " + axismodule.getName() +
                                  " is already available.");
                     }
+                    // set in default map if necessary
+                    Utils.calculateDefaultModuleVersion(axisConfig.getModules(), axisConfig);
+                    serviceRegistry.resolve();
                 } catch (IOException e) {
                     String msg = "Error while reading module.xml";
                     log.error(msg, e);
@@ -232,7 +229,7 @@
 
     }
 
-    public void remove(Bundle bundle) throws AxisFault {
+    public void remove(Bundle bundle) {
         unRegister(bundle, true);
     }
 }

Modified: webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiConfigurationContextFactory.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiConfigurationContextFactory.java?rev=678117&r1=678116&r2=678117&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiConfigurationContextFactory.java (original)
+++ webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiConfigurationContextFactory.java Sat Jul 19 01:23:47 2008
@@ -26,6 +26,7 @@
 import org.apache.axis2.description.TransportInDescription;
 import org.apache.axis2.engine.*;
 import static org.apache.axis2.osgi.deployment.OSGiAxis2Constants.*;
+import org.apache.axis2.osgi.deployment.tracker.BundleTracker;
 import org.apache.axis2.osgi.tx.HttpListener;
 import org.apache.axis2.transport.MessageFormatter;
 import org.apache.axis2.transport.TransportListener;
@@ -58,9 +59,11 @@
 
     private ServiceRegistration configCtxServiceRegistration;
 
+    private BundleTracker bundleTracker;
 
-    public synchronized void init(BundleContext context) {
+    public synchronized void start(BundleContext context) {
         this.context = context;
+        bundleTracker = new BundleTracker(context);
         Dictionary props = new Properties();
         props.put(Constants.SERVICE_PID, "org.apache.axis2.osgi");
         mngServiceRegistration =
@@ -71,6 +74,7 @@
         if (mngServiceRegistration != null) {
             mngServiceRegistration.unregister();
         }
+        bundleTracker.close();
         if (configCtx != null) {
             try {
                 configCtx.terminate();
@@ -102,32 +106,12 @@
             configCtxServiceRegistration =
                     context.registerService(ConfigurationContext.class.getName(), configCtx, null);
 
-            // first check (bundlestarts at the end or partially) {
-            //      // loop  and add axis*
-            // } then {
-            //      // stat the bundle early
-            // }
             Registry servicesRegistry = new ServiceRegistry(context, configCtx);
             Registry moduleRegistry = new ModuleRegistry(context, configCtx, servicesRegistry);
-            Bundle[] bundles = context.getBundles();
-            if (bundles != null) {
-                for (Bundle bundle : bundles) {
-                    if (bundle != context.getBundle()) {
-                        if (bundle.getState() == Bundle.ACTIVE) {
-                            moduleRegistry.register(bundle);
-                        }
-                    }
-                }
-                for (Bundle bundle : bundles) {
-                    if (bundle != context.getBundle()) {
-                        if (bundle.getState() == Bundle.ACTIVE) {
-                            servicesRegistry.register(bundle);
-                        }
-                    }
-                }
-            }
-            context.addBundleListener(moduleRegistry);
-            context.addBundleListener(servicesRegistry);
+            bundleTracker.addRegistry(servicesRegistry);
+            bundleTracker.addRegistry(moduleRegistry);
+            bundleTracker.open();
+
             context.addServiceListener(new AxisConfigServiceListener(configCtx, context));
             context.addServiceListener(new WSListener(configCtx, context));
 
@@ -136,8 +120,6 @@
             //adding the default listener
             context.registerService(TransportListener.class.getName(), new HttpListener(context),
                                     prop);
-
-
             log.info("Axis2 environment has started.");
         } catch (AxisFault e) {
             String msg = "Error while creating ConfigurationContext";

Modified: webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/Registry.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/Registry.java?rev=678117&r1=678116&r2=678117&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/Registry.java (original)
+++ webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/Registry.java Sat Jul 19 01:23:47 2008
@@ -16,20 +16,18 @@
 package org.apache.axis2.osgi.deployment;
 
 import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleListener;
-import org.apache.axis2.AxisFault;
 
 /**
  *  This interface will be used to register the extension/file
  * that needed be processed and create the relevant Axis* object.
  */
-public interface Registry extends BundleListener{
+public interface Registry{
 
-    void register(Bundle bundle) throws AxisFault;
+    void register(Bundle bundle);
 
-    void unRegister(Bundle bundle, boolean uninstall) throws AxisFault;
+    void unRegister(Bundle bundle, boolean uninstall);
 
-    void resolve() throws AxisFault;
+    void resolve();
 
-    void remove(Bundle bundle) throws AxisFault;
+    void remove(Bundle bundle);
 }

Modified: webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/ServiceRegistry.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/ServiceRegistry.java?rev=678117&r1=678116&r2=678117&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/ServiceRegistry.java (original)
+++ webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/ServiceRegistry.java Sat Jul 19 01:23:47 2008
@@ -21,7 +21,8 @@
 import org.apache.axis2.deployment.*;
 import org.apache.axis2.description.AxisService;
 import org.apache.axis2.description.AxisServiceGroup;
-import static org.apache.axis2.osgi.deployment.OSGiAxis2Constants.*;
+import static org.apache.axis2.osgi.deployment.OSGiAxis2Constants.MODULE_NOT_FOUND_ERROR;
+import static org.apache.axis2.osgi.deployment.OSGiAxis2Constants.OSGi_BUNDLE_ID;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.osgi.framework.Bundle;
@@ -42,7 +43,7 @@
         super(context, configCtx);
     }
 
-    public void register(Bundle bundle) throws AxisFault {
+    public void register(Bundle bundle) {
         lock.lock();
         try {
             addServices(bundle);
@@ -94,7 +95,7 @@
                                 new OSGiServiceBuilder(configCtx, axisService);
                         serviceBuilder.setWsdlServiceMap(wsdlServicesMap);
                         AxisService service = serviceBuilder.populateService(rootElement);
-                        ArrayList serviceList = new ArrayList();
+                        ArrayList<AxisService> serviceList = new ArrayList<AxisService>();
                         serviceList.add(service);
                         DeploymentEngine.addServiceGroup(serviceGroup,
                                                          serviceList,
@@ -136,6 +137,8 @@
                                     log.info("A service being found with unmeant module " +
                                              "dependency. Hence, moved it to UNRESOLVED state.");
                                     unreslovedBundles.add(bundle);
+                                } else {
+                                    log.info("A service being found in UNRESOLVED state.");
                                 }
                             } else {
                                 log.error(msg, e);
@@ -158,7 +161,7 @@
 
     }
 
-    public void unRegister(Bundle bundle, boolean uninstall) throws AxisFault {
+    public void unRegister(Bundle bundle, boolean uninstall) {
         lock.lock();
         try {
             List<AxisServiceGroup> axisServiceGroupList = resolvedBundles.get(bundle);
@@ -174,14 +177,14 @@
                         for (Iterator iterator = axisServiceGroup.getServices();
                              iterator.hasNext();) {
                             AxisService service = (AxisService) iterator.next();
-                            System.out.println("[Axis2/OSGi] Service - " + service.getName());
+                            log.info("[Axis2/OSGi] Service - " + service.getName());
                         }
                         configCtx.getAxisConfiguration()
                                 .removeServiceGroup(axisServiceGroup.getServiceGroupName());
-                        System.out.println("[Axis2/OSGi] Stopping " +
-                                           axisServiceGroup.getServiceGroupName() +
-                                           " service group in Bundle - " +
-                                           bundle.getSymbolicName());
+                        log.info("[Axis2/OSGi] Stopping " +
+                                 axisServiceGroup.getServiceGroupName() +
+                                 " service group in Bundle - " +
+                                 bundle.getSymbolicName());
                     } catch (AxisFault e) {
                         String msg = "Error while removing the service group";
                         log.error(msg, e);
@@ -194,7 +197,7 @@
         }
     }
 
-    public void remove(Bundle bundle) throws AxisFault {
+    public void remove(Bundle bundle) {
         unRegister(bundle, true);
         lock.lock();
         try {

Added: webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/tracker/BundleTracker.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/tracker/BundleTracker.java?rev=678117&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/tracker/BundleTracker.java (added)
+++ webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/tracker/BundleTracker.java Sat Jul 19 01:23:47 2008
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.axis2.osgi.deployment.tracker;
+
+import org.apache.axis2.osgi.deployment.Registry;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * BundleTracker listen to bundle events and class the registered Registry
+ * objects to deal with them. One be able to add Registry objects to BundleTracker and remove
+ * them as necessary. When open() is called BundleTracker will open the tracking and when close()
+ * is called close the tracking.
+ */
+public class BundleTracker {
+
+    private List<Registry> registyList = new ArrayList<Registry>();
+
+    private List<Bundle> bundleList = new ArrayList<Bundle>();
+
+    private boolean open;
+
+    private BundleContext context;
+
+    private BundleListener bundleListener;
+
+    private static Log log = LogFactory.getLog(BundleTracker.class);
+
+    private final Lock lock = new ReentrantLock();
+
+    public BundleTracker(final BundleContext context) {
+        this.context = context;
+        bundleListener = new BundleListener() {
+            public void bundleChanged(BundleEvent event) {
+                lock.lock();
+                try {
+                    if (!open) {
+                        return;
+                    }
+                    Bundle bundle = event.getBundle();
+                    switch (event.getType()) {
+                        case BundleEvent.STARTED:
+                            if (!bundleList.contains(event.getBundle())) {
+                                bundleList.add(event.getBundle());
+                                // logic
+                                for (Registry registry : registyList) {
+                                    registry.register(bundle);
+                                }
+                            }
+                            break;
+                        case BundleEvent.STOPPED:
+                            if (context.getBundle() != bundle) {
+                                if (bundleList.contains(event.getBundle())) {
+                                    bundleList.remove(event.getBundle());
+                                    //logic
+                                    for (Registry registry : registyList) {
+                                        registry.unRegister(bundle, false);
+                                    }
+
+                                }
+                            }
+                            break;
+                        case BundleEvent.UNINSTALLED:
+                            if (context.getBundle() != bundle) {
+                                if (bundleList.contains(event.getBundle())) {
+                                    bundleList.remove(event.getBundle());
+                                    //logic
+                                    for (Registry registry : registyList) {
+                                        registry.remove(bundle);
+                                    }
+                                }
+                            }
+                            break;
+                    }
+                } finally {
+                    lock.unlock();
+                }
+
+            }
+        };
+
+
+    }
+
+    public void addRegistry(Registry registry) {
+        registyList.add(registry);
+    }
+
+    public void open() {
+        lock.lock();
+        try {
+            if (!open) {
+                open = true;
+                log.info("Bundle tracker is opened");
+                Bundle[] bundles = context.getBundles();
+                for (Bundle bundle : bundles) {
+                    if (bundle.getState() == Bundle.ACTIVE) {
+                        bundleList.add(bundle);
+                        for (Registry registry : registyList) {
+                            registry.register(bundle);
+                        }
+                    }
+                }
+                context.addBundleListener(bundleListener);
+            }
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    public void close() {
+        lock.lock();
+        try {
+            if (open) {
+                open = false;
+                context.removeBundleListener(bundleListener);
+                Bundle[] bundles = bundleList.toArray(new Bundle[bundleList.size()]);
+                for (Bundle bundle : bundles) {
+                    if (bundleList.remove(bundle)) {
+                        for (Registry registry : registyList) {
+                            registry.remove(bundle);
+                        }
+                    }
+                }
+                log.info("Bundle tracker is closed");
+            }
+        } finally {
+            lock.unlock();
+        }
+    }
+
+
+}

Modified: webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/internal/Activator.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/internal/Activator.java?rev=678117&r1=678116&r2=678117&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/internal/Activator.java (original)
+++ webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/internal/Activator.java Sat Jul 19 01:23:47 2008
@@ -44,7 +44,7 @@
     }
 
     public void start(BundleContext context) throws Exception {
-        managedService.init(context);
+        managedService.start(context);
         managedService.updated(null);
         tracker = new HttpServiceTracker(context);
         tracker.open();