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/06/20 19:26:40 UTC

svn commit: r669998 - in /webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi: InitServlet.java deployment/OSGiAxis2Constants.java deployment/OSGiConfigurationContextFactory.java internal/Activator.java tx/ tx/HttpListener.java

Author: saminda
Date: Fri Jun 20 10:26:40 2008
New Revision: 669998

URL: http://svn.apache.org/viewvc?rev=669998&view=rev
Log:
1. Added a HttpListener that sync with underlying open connector
2. Added TransportListeners as services
3. Improves the code 
4. At this point onward this Axis2 OSGi imple is ready to deploy in Equinox and Knopflerfish OSGi 
implementations. Need to do a small workaround to work this with Felix. I have made this a low priority for the 
moment.   


Added:
    webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/tx/
    webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/tx/HttpListener.java
Modified:
    webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/InitServlet.java
    webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiAxis2Constants.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/internal/Activator.java

Modified: webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/InitServlet.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/InitServlet.java?rev=669998&r1=669997&r2=669998&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/InitServlet.java (original)
+++ webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/InitServlet.java Fri Jun 20 10:26:40 2008
@@ -15,18 +15,16 @@
  */
 package org.apache.axis2.osgi;
 
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.engine.AxisConfigurator;
-import org.apache.axis2.engine.ListenerManager;
-import org.apache.axis2.osgi.deployment.OSGiServerConfigurator;
 import org.apache.axis2.osgi.deployment.OSGiConfigurationContextFactory;
-import org.apache.axis2.context.ConfigurationContext;
-import org.apache.axis2.AxisFault;
+import org.apache.axis2.osgi.deployment.OSGiServerConfigurator;
 import org.osgi.framework.BundleContext;
 
-import javax.servlet.http.HttpServlet;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
-import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServlet;
 
 /**
  * InitServlet is used only to initialize Axis2 environment
@@ -47,10 +45,6 @@
             ConfigurationContext configCtx = OSGiConfigurationContextFactory
                     .createConfigurationContext(configurator, context);
             //regiser the ConfigurationContext as an service.
-            ListenerManager listenerManager = new ListenerManager();
-            listenerManager.init(configCtx);
-            listenerManager.start();
-            ListenerManager.defaultConfigurationContext = configCtx;
             context.registerService(ConfigurationContext.class.getName(), configCtx, null);
         } catch (AxisFault e) {
             String msg = "Error while creating the ConfigurationContext";

Modified: webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiAxis2Constants.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiAxis2Constants.java?rev=669998&r1=669997&r2=669998&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiAxis2Constants.java (original)
+++ webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/deployment/OSGiAxis2Constants.java Fri Jun 20 10:26:40 2008
@@ -21,4 +21,8 @@
 public final class OSGiAxis2Constants {
 
     public static String MODULE_NOT_FOUND_ERROR = "Error 1: Required module is not found. Module name : ";
+
+    public static String PROTOCOL = "protocol";
+
+    public static String  AXIS2_OSGi_ROOT_CONTEXT = "axis2.osgi.root.context";
 }

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=669998&r1=669997&r2=669998&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 Fri Jun 20 10:26:40 2008
@@ -15,12 +15,26 @@
  */
 package org.apache.axis2.osgi.deployment;
 
-import org.apache.axis2.context.ConfigurationContextFactory;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.deployment.util.Utils;
 import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.context.ConfigurationContextFactory;
+import org.apache.axis2.description.Parameter;
+import org.apache.axis2.description.TransportInDescription;
+import org.apache.axis2.description.AxisService;
 import org.apache.axis2.engine.AxisConfigurator;
-import org.apache.axis2.AxisFault;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.Bundle;
+import org.apache.axis2.engine.ListenerManager;
+import static org.apache.axis2.osgi.deployment.OSGiAxis2Constants.PROTOCOL;
+import org.apache.axis2.osgi.tx.HttpListener;
+import org.apache.axis2.transport.TransportListener;
+import org.osgi.framework.*;
+
+import java.util.Dictionary;
+import java.util.Properties;
+import java.util.Set;
+import java.util.Iterator;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 
 /**
  *
@@ -31,6 +45,10 @@
             AxisConfigurator axisConfigurator, BundleContext context) throws AxisFault {
         ConfigurationContext configCtx =
                 ConfigurationContextFactory.createConfigurationContext(axisConfigurator);
+        ListenerManager listenerManager = new ListenerManager();
+        listenerManager.init(configCtx);
+        listenerManager.start();
+        ListenerManager.defaultConfigurationContext = configCtx;
 
         // first check (bundlestarts at the end or partially) {
         //      // loop  and add axis*
@@ -58,10 +76,94 @@
         }
         context.addBundleListener(moduleRegistry);
         context.addBundleListener(servicesRegistry);
+        context.addServiceListener(new AxisConfigServiceListener(configCtx, context));
+
+        Dictionary prop = new Properties();
+        prop.put(PROTOCOL, "http");
+        //adding the default listener
+        context.registerService(TransportListener.class.getName(), new HttpListener(context), prop);
 
         return configCtx;
 
     }
 
+    private static class AxisConfigServiceListener implements ServiceListener {
+
+        private ConfigurationContext configCtx;
+
+        private BundleContext context;
+
+        private Lock lock = new ReentrantLock();
+
+        public AxisConfigServiceListener(ConfigurationContext configCtx, BundleContext context) {
+            this.configCtx = configCtx;
+            this.context = context;
+        }
+
+        public void serviceChanged(ServiceEvent event) {
+            ServiceReference reference = event.getServiceReference();
+            Object service = context.getService(reference);
+            if (service instanceof TransportListener) {
+                String protocol = (String) reference.getProperty(PROTOCOL);
+                if (protocol == null || protocol.length() == 0) {
+                    throw new RuntimeException(
+                            "Protocol is not found for the trnasport object");
+                }
+                if (event.getType() == ServiceEvent.REGISTERED) {
+                    TransportListener txListener =
+                            (TransportListener) service;
+
+                    TransportInDescription txInDes = new TransportInDescription(protocol);
+                    txInDes.setReceiver(txListener);
+                    String[] keys = reference.getPropertyKeys();
+                    if (keys != null) {
+                        for (String key : keys) {
+                            if (key.equals(PROTOCOL)) {
+                                continue;
+                            }
+                            //TODO: assume String properties at this moment.
+                            try {
+                                Object propObj = reference.getProperty(key);
+                                if (propObj instanceof String) {
+                                    String value = (String) propObj;
+                                    Parameter param = new Parameter(key, value);
+                                    txInDes.addParameter(param);
+                                }
+                            } catch (AxisFault e) {
+                                String msg = "Error while reading transport properties from :" +
+                                             txListener.toString();
+                                throw new RuntimeException(msg, e);
+                            }
+                        }
+                    }
+                    try {
+                        configCtx.getListenerManager().addListener(txInDes, false);
+                        //Now update the AxisService endpoint map
+                        lock.lock();
+                        try {
+                            for (Iterator iterator =
+                                    configCtx.getAxisConfiguration().getServices().keySet()
+                                            .iterator();
+                                 iterator.hasNext();) {
+                                String serviceName = (String) iterator.next();
+                                AxisService axisService =
+                                        configCtx.getAxisConfiguration().getService(serviceName);
+                                Utils.addEndpointsToService(axisService,
+                                                            configCtx.getAxisConfiguration());
+                            }
+                        } finally {
+                            lock.unlock();
+                        }
+                    } catch (AxisFault e) {
+                        String msg = "Error while intiating and starting the listener";
+                        throw new RuntimeException(msg, e);
+                    }
+                }
+
+            }
+
+        }
+    }
+
 
 }

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=669998&r1=669997&r2=669998&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 Fri Jun 20 10:26:40 2008
@@ -29,6 +29,8 @@
 
 import javax.servlet.ServletException;
 
+import static org.apache.axis2.osgi.deployment.OSGiAxis2Constants.*;
+
 /**
  * Activator will set the necessary parameters that initiate Axis2 OSGi integration
  * TODO: TBD; yet the structure is being formed
@@ -76,15 +78,16 @@
                 ServiceReference axisConfigRef =
                         context.getServiceReference(AxisConfiguration.class.getName());
                 AxisConfiguration axisConfig = (AxisConfiguration)context.getService(axisConfigRef);
-                Object obj = axisConfig.getParameterValue("servicePath");
-                String servicepath = "/services";
-                if (obj != null) {
-                    servicepath = (String)obj;
-                    if (!servicepath.startsWith("/")) {
-                        servicepath = "/" + servicepath;
+                String propContextRoot = context.getProperty(AXIS2_OSGi_ROOT_CONTEXT);
+                String contextRoot = "/axis2";
+                if (propContextRoot != null && propContextRoot.length() != 0) {
+                    if (!propContextRoot.startsWith("/")) {
+                        contextRoot = "/" + propContextRoot;
+                    } else {
+                        contextRoot = propContextRoot;
                     }
                 }
-                httpService.registerServlet(servicepath, axisServlet, null, null);
+                httpService.registerServlet(contextRoot, axisServlet, null, null);
             } catch (ServletException e) {
                 String msg = "Error while registering servlets";
                 throw new RuntimeException(msg, e);

Added: webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/tx/HttpListener.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/tx/HttpListener.java?rev=669998&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/tx/HttpListener.java (added)
+++ webservices/axis2/trunk/java/modules/osgi/src/org/apache/axis2/osgi/tx/HttpListener.java Fri Jun 20 10:26:40 2008
@@ -0,0 +1,94 @@
+/*
+ * 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.tx;
+
+import org.apache.axis2.transport.TransportListener;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.context.SessionContext;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.TransportInDescription;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.addressing.EndpointReference;
+import org.osgi.framework.BundleContext;
+
+/**
+ * Default HttpListener that synch with the underlying listerner frameworks.
+ * This implemenation uses org.osgi.service.http.port property to find the port.
+ * <p/>
+ * At the moment this will assume the underlying evn is Equinox. if the prior property is not set
+ * this will default to 80.
+ */
+public class HttpListener implements TransportListener {
+
+    private BundleContext context;
+
+    private ConfigurationContext configCtx;
+
+    public HttpListener(BundleContext context) {
+        this.context = context;
+    }
+
+    public void init(ConfigurationContext configCtx, TransportInDescription transprtIn)
+            throws AxisFault {
+        this.configCtx = configCtx;
+    }
+
+    public void start() throws AxisFault {
+        //ignore
+    }
+
+    public void stop() throws AxisFault {
+        //ignore
+    }
+
+    public EndpointReference getEPRForService(String serviceName, String ip) throws AxisFault {
+        return calculateEndpoint("http", serviceName, ip);
+    }
+
+    public EndpointReference[] getEPRsForService(String serviceName, String ip) throws AxisFault {
+        return new EndpointReference[]{calculateEndpoint("http", serviceName, ip)};
+    }
+
+    public SessionContext getSessionContext(MessageContext messageContext) {
+        //ignore for the moment. This should be able to take the HttpService and attached to it.
+        return null;
+    }
+
+    public void destroy() {
+
+    }
+
+    private EndpointReference calculateEndpoint(String protocol, String serviceName, String ip) {
+        String portS = context.getProperty("org.osgi.service.http.port");
+        int port = 80;
+        if (portS != null || portS.length() != 0) {
+            try {
+                port = Integer.parseInt(portS);
+            } catch (NumberFormatException e) {//ignore
+            }
+        }
+        String servicePath = configCtx.getServicePath();
+        if (servicePath.startsWith("/")) {
+            servicePath = servicePath.substring(1);
+        }
+        String contextRoot = configCtx.getContextRoot();
+        if (contextRoot.equals("/") || contextRoot == null) {
+            contextRoot = ""; 
+        }
+        return new EndpointReference(protocol + "://" + ip + ":" + port + contextRoot + "/" +
+                                     servicePath + "/" + serviceName);
+    }
+}