Author: veithen
Date: Sun May 25 10:59:31 2014
New Revision: 1597409

URL: http://svn.apache.org/r1597409
Log:
AXIS2-5646: Merged revisions 1593690,1593765,1594378,1594701 to the 1.6 branch.

Modified:
    axis/axis2/java/core/branches/1_6/   (props changed)
    
axis/axis2/java/core/branches/1_6/modules/osgi-tests/src/test/java/OSGiTest.java
    
axis/axis2/java/core/branches/1_6/modules/osgi/src/org/apache/axis2/osgi/OSGiAxisServlet.java
    
axis/axis2/java/core/branches/1_6/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiConfigurationContextFactory.java

Propchange: axis/axis2/java/core/branches/1_6/
------------------------------------------------------------------------------
  Merged /axis/axis2/java/core/trunk:r1593690,1593765,1594378,1594701

Modified: 
axis/axis2/java/core/branches/1_6/modules/osgi-tests/src/test/java/OSGiTest.java
URL: 
http://svn.apache.org/viewvc/axis/axis2/java/core/branches/1_6/modules/osgi-tests/src/test/java/OSGiTest.java?rev=1597409&r1=1597408&r2=1597409&view=diff
==============================================================================
--- 
axis/axis2/java/core/branches/1_6/modules/osgi-tests/src/test/java/OSGiTest.java
 (original)
+++ 
axis/axis2/java/core/branches/1_6/modules/osgi-tests/src/test/java/OSGiTest.java
 Sun May 25 10:59:31 2014
@@ -23,6 +23,8 @@ import static org.ops4j.pax.exam.CoreOpt
 import static org.ops4j.pax.exam.CoreOptions.url;
 import static org.ops4j.pax.tinybundles.core.TinyBundles.bundle;
 
+import java.util.concurrent.CountDownLatch;
+
 import org.apache.axiom.om.OMAbstractFactory;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMFactory;
@@ -41,11 +43,17 @@ import org.junit.Test;
 import org.ops4j.pax.exam.ExamSystem;
 import org.ops4j.pax.exam.nat.internal.NativeTestContainer;
 import org.ops4j.pax.exam.spi.DefaultExamSystem;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
 import org.osgi.framework.Constants;
+import org.osgi.framework.FrameworkEvent;
+import org.osgi.framework.FrameworkListener;
 
 public class OSGiTest {
     @Test
-    public void test() throws Exception {
+    public void test() throws Throwable {
         int httpPort = PortAllocator.allocatePort();
         ExamSystem system = DefaultExamSystem.create(options(
                 
url("link:classpath:META-INF/links/org.ops4j.pax.logging.api.link"),
@@ -106,8 +114,69 @@ public class OSGiTest {
             serviceClient.setOptions(options);
             OMElement result = serviceClient.sendReceive(payload);
             assertEquals("getVersionResponse", result.getLocalName());
+            // Stop the Axis2 bundle explicitly here so that we can test that 
it cleanly shuts down (see AXIS2-5646)
+            stopBundle(getAxis2Bundle(container));
         } finally {
             container.stop();
         }
     }
+    
+    private static Bundle getAxis2Bundle(NativeTestContainer container) {
+        for (Bundle bundle : 
container.getSystemBundle().getBundleContext().getBundles()) {
+            if (bundle.getSymbolicName().equals("org.apache.axis2.osgi")) {
+                return bundle;
+            }
+        }
+        throw new Error("Axis2 bundle not found");
+    }
+
+    static class Listener implements FrameworkListener, BundleListener {
+        private final Bundle bundle;
+        private final CountDownLatch latch = new CountDownLatch(1);
+        private Throwable throwable;
+        
+        Listener(Bundle bundle) {
+            this.bundle = bundle;
+        }
+
+        public void frameworkEvent(FrameworkEvent event) {
+            if (event.getType() == FrameworkEvent.ERROR && event.getSource() 
== bundle && throwable == null) {
+                throwable = event.getThrowable();
+            }
+        }
+
+        public void bundleChanged(BundleEvent event) {
+            if (event.getType() == BundleEvent.STOPPED && event.getSource() == 
bundle) {
+                latch.countDown();
+            }
+        }
+        
+        void check() throws Throwable {
+            latch.await();
+            if (throwable != null) {
+                throw throwable;
+            }
+        }
+    }
+    
+    /**
+     * Stop the given bundle and throw any exception triggered during the stop 
operation.
+     */
+    private static void stopBundle(Bundle bundle) throws Throwable {
+        // The listener must be registered on the system bundle; registering 
it on the bundle
+        // passed as parameter won't work because a stopping bundle can't 
receive asynchronous events.
+        BundleContext systemBundleContext = 
bundle.getBundleContext().getBundle(0).getBundleContext();
+        Listener listener = new Listener(bundle);
+        // Need a framework listener to intercept errors that would otherwise 
end up only being logged
+        systemBundleContext.addFrameworkListener(listener);
+        systemBundleContext.addBundleListener(listener);
+        try {
+            // Note: the stop method may also throw exceptions
+            bundle.stop();
+            listener.check();
+        } finally {
+            systemBundleContext.removeFrameworkListener(listener);
+            systemBundleContext.removeBundleListener(listener);
+        }
+    }
 }

Modified: 
axis/axis2/java/core/branches/1_6/modules/osgi/src/org/apache/axis2/osgi/OSGiAxisServlet.java
URL: 
http://svn.apache.org/viewvc/axis/axis2/java/core/branches/1_6/modules/osgi/src/org/apache/axis2/osgi/OSGiAxisServlet.java?rev=1597409&r1=1597408&r2=1597409&view=diff
==============================================================================
--- 
axis/axis2/java/core/branches/1_6/modules/osgi/src/org/apache/axis2/osgi/OSGiAxisServlet.java
 (original)
+++ 
axis/axis2/java/core/branches/1_6/modules/osgi/src/org/apache/axis2/osgi/OSGiAxisServlet.java
 Sun May 25 10:59:31 2014
@@ -54,4 +54,11 @@ public class OSGiAxisServlet extends Axi
         }
 
     }
+
+    @Override
+    public void destroy() {
+        // Do nothing. This prevents AxisServlet from terminating the 
configuration context.
+        // The configuration context is terminated by 
OSGiConfigurationContextFactory, and
+        // invoking the terminate method twice (potentially concurrently) 
causes problems.
+    }
 }

Modified: 
axis/axis2/java/core/branches/1_6/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiConfigurationContextFactory.java
URL: 
http://svn.apache.org/viewvc/axis/axis2/java/core/branches/1_6/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiConfigurationContextFactory.java?rev=1597409&r1=1597408&r2=1597409&view=diff
==============================================================================
--- 
axis/axis2/java/core/branches/1_6/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiConfigurationContextFactory.java
 (original)
+++ 
axis/axis2/java/core/branches/1_6/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiConfigurationContextFactory.java
 Sun May 25 10:59:31 2014
@@ -56,6 +56,8 @@ public class OSGiConfigurationContextFac
 
     private ServiceRegistration mngServiceRegistration;
 
+    private ServiceRegistration transportServiceRegistration;
+    
     private ConfigurationContext configCtx;
 
     private ServiceRegistration configCtxServiceRegistration;
@@ -72,6 +74,14 @@ public class OSGiConfigurationContextFac
     }
 
     public synchronized void stop() {
+        if (configCtxServiceRegistration != null) {
+            configCtxServiceRegistration.unregister();
+        }
+        
+        if (transportServiceRegistration != null) {
+            transportServiceRegistration.unregister();
+        }
+        
         if (mngServiceRegistration != null) {
             mngServiceRegistration.unregister();
         }
@@ -119,7 +129,7 @@ public class OSGiConfigurationContextFac
             Dictionary prop = new Properties();
             prop.put(PROTOCOL, "http");
             //adding the default listener
-            context.registerService(TransportListener.class.getName(), new 
HttpListener(context),
+            transportServiceRegistration = 
context.registerService(TransportListener.class.getName(), new 
HttpListener(context),
                                     prop);
             log.info("Axis2 environment has started.");
         } catch (AxisFault e) {


Reply via email to