tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wjaniszew...@apache.org
Subject svn commit: r681871 - in /tuscany/java/sca: itest/corba/src/test/java/org/apache/tuscany/sca/test/corba/ itest/corba/src/test/java/org/apache/tuscany/sca/test/corba/types/ itest/corba/src/test/resources/ modules/binding-corba-runtime/src/main/java/org/...
Date Fri, 01 Aug 2008 22:27:34 GMT
Author: wjaniszewski
Date: Fri Aug  1 15:27:33 2008
New Revision: 681871

URL: http://svn.apache.org/viewvc?rev=681871&view=rev
Log:
1. SCA default binding over CORBA - now transient name server is beeing created automatically
for localhost service bindings. 2. Code cleanup

Added:
    tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/SocketUtil.java
  (with props)
Removed:
    tuscany/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/TuscanyServant.java
Modified:
    tuscany/java/sca/itest/corba/src/test/java/org/apache/tuscany/sca/test/corba/ScenarioFourTestCase.java
    tuscany/java/sca/itest/corba/src/test/java/org/apache/tuscany/sca/test/corba/types/ScenarioFourException.java
    tuscany/java/sca/itest/corba/src/test/resources/ScenarioFour.composite
    tuscany/java/sca/modules/binding-sca-corba/pom.xml
    tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/CorbaSCAInvocationProxy.java
    tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/CorbaSCAServiceBindingProvider.java
    tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/WrappedSCAException.java
    tuscany/java/sca/modules/host-corba-jdk/src/main/java/org/apache/tuscany/sca/host/corba/jdk/DefaultCorbaHost.java
    tuscany/java/sca/modules/host-corba-jdk/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCase.java
    tuscany/java/sca/modules/host-corba-jee/src/main/java/org/apache/tuscany/sca/host/corba/jee/JEECorbaHost.java
    tuscany/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/CorbaHost.java
    tuscany/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/ExtensibleCorbaHost.java

Modified: tuscany/java/sca/itest/corba/src/test/java/org/apache/tuscany/sca/test/corba/ScenarioFourTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/itest/corba/src/test/java/org/apache/tuscany/sca/test/corba/ScenarioFourTestCase.java?rev=681871&r1=681870&r2=681871&view=diff
==============================================================================
--- tuscany/java/sca/itest/corba/src/test/java/org/apache/tuscany/sca/test/corba/ScenarioFourTestCase.java
(original)
+++ tuscany/java/sca/itest/corba/src/test/java/org/apache/tuscany/sca/test/corba/ScenarioFourTestCase.java
Fri Aug  1 15:27:33 2008
@@ -23,8 +23,6 @@
 import static org.junit.Assert.fail;
 import junit.framework.Assert;
 
-import org.apache.tuscany.sca.host.corba.naming.TransientNameServer;
-import org.apache.tuscany.sca.host.corba.naming.TransientNameService;
 import org.apache.tuscany.sca.host.embedded.SCADomain;
 import org.apache.tuscany.sca.test.corba.scenariofour.ScenarioFourFactory;
 import org.apache.tuscany.sca.test.corba.scenariofour.ScenarioFourSdo;
@@ -43,30 +41,16 @@
 public class ScenarioFourTestCase {
 
     // note that those values are also used in resources/*.composite file
-    private static int ORB_INITIAL_PORT = 5060;
     private static SCADomain domain;
-    private static TransientNameServer server;
     private static ScenarioFourComponent scenarioFourComponent;
     private static ScenarioFour scenarioFour;
 
     /**
-     * Sets up name service, creates and registers traditional CORBA service,
-     * obtains SCADomain
+     * Initial configuration
      */
     @BeforeClass
     public static void setUp() {
         try {
-            try {
-                server =
-                    new TransientNameServer("localhost", ORB_INITIAL_PORT, TransientNameService.DEFAULT_SERVICE_NAME);
-                Thread t = server.start();
-                if (t == null) {
-                    Assert.fail("The naming server cannot be started");
-                }
-            } catch (Throwable e) {
-                e.printStackTrace();
-                Assert.fail(e.getMessage());
-            }
             // obtain domain
             domain = SCADomain.newInstance("ScenarioFour.composite");
             scenarioFourComponent = domain.getService(ScenarioFourComponent.class, "ScenarioFour");
@@ -77,11 +61,11 @@
     }
 
     /**
-     * Kills previously spawned name service.
+     * Test cleanup
      */
     @AfterClass
     public static void tearDown() {
-        server.stop();
+        // do nothing
     }
 
     /**
@@ -133,5 +117,20 @@
             e.printStackTrace();
             fail();
         }
-    }    
+    }
+    
+    /**
+     * Tests reusing local name server with multiple bindings 
+     */
+    @Test
+    public void test_nameServerReuse() {
+        try {
+            ScenarioFour scenarioFour = domain.getService(ScenarioFourComponent.class, "ScenarioFourReuse").getScenarioFour();
+            ScenarioFourStruct struct = new ScenarioFourStruct();
+            scenarioFour.setStruct(struct);
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
 }

Modified: tuscany/java/sca/itest/corba/src/test/java/org/apache/tuscany/sca/test/corba/types/ScenarioFourException.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/itest/corba/src/test/java/org/apache/tuscany/sca/test/corba/types/ScenarioFourException.java?rev=681871&r1=681870&r2=681871&view=diff
==============================================================================
--- tuscany/java/sca/itest/corba/src/test/java/org/apache/tuscany/sca/test/corba/types/ScenarioFourException.java
(original)
+++ tuscany/java/sca/itest/corba/src/test/java/org/apache/tuscany/sca/test/corba/types/ScenarioFourException.java
Fri Aug  1 15:27:33 2008
@@ -19,6 +19,9 @@
 
 package org.apache.tuscany.sca.test.corba.types;
 
+/**
+ * Exception for ScenarioFour tests
+ */
 public class ScenarioFourException extends Exception {
 
     public static final String DEFAULT_CONTENT = "Test";

Modified: tuscany/java/sca/itest/corba/src/test/resources/ScenarioFour.composite
URL: http://svn.apache.org/viewvc/tuscany/java/sca/itest/corba/src/test/resources/ScenarioFour.composite?rev=681871&r1=681870&r2=681871&view=diff
==============================================================================
--- tuscany/java/sca/itest/corba/src/test/resources/ScenarioFour.composite (original)
+++ tuscany/java/sca/itest/corba/src/test/resources/ScenarioFour.composite Fri Aug  1 15:27:33
2008
@@ -30,13 +30,26 @@
     </component>
 	<service name="ScenarioFour" promote="ScenarioFourService">
     	<interface.java interface="org.apache.tuscany.sca.test.corba.types.ScenarioFour"/>
-        <binding.sca uri="corbaname::localhost:5060#ScenarioFour"/>
+        <binding.sca uri="corbaname::localhost:5080#ScenarioFour"/>
     </service>
     
     <component name="ScenarioFour">
         <implementation.java class="org.apache.tuscany.sca.test.corba.types.ScenarioFourComponent"
/>
         <reference name="scenarioFour">
-			<binding.sca uri="corbaname::localhost:5060#ScenarioFour"/>        
+			<binding.sca uri="corbaname::localhost:5080#ScenarioFour"/>        
+        </reference>
+    </component>
+    
+    <!-- doubled service and reference to test reuse of name server --> 
+    <service name="ScenarioFourDoubled" promote="ScenarioFourService">
+    	<interface.java interface="org.apache.tuscany.sca.test.corba.types.ScenarioFour"/>
+        <binding.sca uri="corbaname::localhost:5080#ScenarioFourReuse"/>
+    </service>
+    
+    <component name="ScenarioFourReuse">
+        <implementation.java class="org.apache.tuscany.sca.test.corba.types.ScenarioFourComponent"
/>
+        <reference name="scenarioFour">
+			<binding.sca uri="corbaname::localhost:5080#ScenarioFourReuse"/>        
         </reference>
     </component>
     

Modified: tuscany/java/sca/modules/binding-sca-corba/pom.xml
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-sca-corba/pom.xml?rev=681871&r1=681870&r2=681871&view=diff
==============================================================================
--- tuscany/java/sca/modules/binding-sca-corba/pom.xml (original)
+++ tuscany/java/sca/modules/binding-sca-corba/pom.xml Fri Aug  1 15:27:33 2008
@@ -71,29 +71,11 @@
 			<version>1.4-SNAPSHOT</version>
 		</dependency>
 
-        <dependency>
-            <groupId>org.apache.tuscany.sca</groupId>
-            <artifactId>tuscany-databinding-axiom</artifactId>
-            <version>1.4-SNAPSHOT</version>
-        </dependency>
-
-       <!--
 		<dependency>
-			<groupId>org.apache.ws.commons.axiom</groupId>
-			<artifactId>axiom-api</artifactId>
-			<exclusions>
-				<exclusion>
-					<groupId>xerces</groupId>
-					<artifactId>xercesImpl</artifactId>
-				</exclusion>
-				<exclusion>
-					<groupId>stax</groupId>
-					<artifactId>stax-api</artifactId>
-				</exclusion>
-			</exclusions>
-			<version>1.2.5</version>
+			<groupId>org.apache.tuscany.sca</groupId>
+			<artifactId>tuscany-databinding-axiom</artifactId>
+			<version>1.4-SNAPSHOT</version>
 		</dependency>
-        -->
 
 		<dependency>
 			<groupId>org.apache.tuscany.sca</groupId>

Modified: tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/CorbaSCAInvocationProxy.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/CorbaSCAInvocationProxy.java?rev=681871&r1=681870&r2=681871&view=diff
==============================================================================
--- tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/CorbaSCAInvocationProxy.java
(original)
+++ tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/CorbaSCAInvocationProxy.java
Fri Aug  1 15:27:33 2008
@@ -75,9 +75,7 @@
                     break;
                 }
             }
-            if (componentOperation == null) {
-                // TODO: throw an exception - no such operation
-            } else {
+            if (componentOperation != null) {
                 Message msg = messageFactory.createMessage();
                 msg.setOperation(componentOperation);
                 Object[] args = new Object[1];

Modified: tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/CorbaSCAServiceBindingProvider.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/CorbaSCAServiceBindingProvider.java?rev=681871&r1=681870&r2=681871&view=diff
==============================================================================
--- tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/CorbaSCAServiceBindingProvider.java
(original)
+++ tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/CorbaSCAServiceBindingProvider.java
Fri Aug  1 15:27:33 2008
@@ -28,6 +28,7 @@
 import org.apache.tuscany.sca.binding.ws.wsdlgen.BindingWSDLGenerator;
 import org.apache.tuscany.sca.core.ExtensionPointRegistry;
 import org.apache.tuscany.sca.host.corba.CorbaHost;
+import org.apache.tuscany.sca.host.corba.CorbanameURL;
 import org.apache.tuscany.sca.interfacedef.InterfaceContract;
 import org.apache.tuscany.sca.invocation.MessageFactory;
 import org.apache.tuscany.sca.provider.ServiceBindingProvider;
@@ -35,7 +36,6 @@
 import org.apache.tuscany.sca.runtime.RuntimeComponentService;
 import org.osoa.sca.ServiceRuntimeException;
 
-
 /**
  * Service binding provider for SCA default binding over CORBA binding
  */
@@ -47,6 +47,8 @@
     private DynaCorbaServant servant;
     private MessageFactory messageFactory;
     private InterfaceContract wsdlInterfaceContract;
+    private boolean isLocalhost;
+    private int bindingPort;
 
     public CorbaSCAServiceBindingProvider(SCABinding binding,
                                           CorbaHost host,
@@ -57,8 +59,12 @@
         this.host = host;
         this.service = service;
         
-        messageFactory = extensions.getExtensionPoint(MessageFactory.class);
+        CorbanameURL details = new CorbanameURL(binding.getURI());
+        isLocalhost = SocketUtil.isLocalhost(details.getHost());
+        bindingPort = details.getPort();
         
+        messageFactory = extensions.getExtensionPoint(MessageFactory.class);
+
         WebServiceBindingFactory wsFactory = extensions.getExtensionPoint(WebServiceBindingFactory.class);
         WebServiceBinding wsBinding = wsFactory.createWebServiceBinding();
         BindingWSDLGenerator.generateWSDL(component, service, wsBinding, extensions, null);
@@ -76,8 +82,13 @@
 
     public void start() {
         try {
-            InvocationProxy proxy = new CorbaSCAInvocationProxy(service.getRuntimeWire(binding),
service.getInterfaceContract().getInterface(), messageFactory);
+            InvocationProxy proxy =
+                new CorbaSCAInvocationProxy(service.getRuntimeWire(binding), service.getInterfaceContract()
+                    .getInterface(), messageFactory);
             servant = new DynaCorbaServant(proxy, "IDL:org/apache/tuscany/sca/binding/sca/corba/Service:1.0");
+            if (isLocalhost) {
+                host.createLocalNameServer(bindingPort);
+            }
             host.registerServant(binding.getURI(), servant);
         } catch (Exception e) {
             throw new ServiceRuntimeException(e);
@@ -87,6 +98,9 @@
     public void stop() {
         try {
             host.unregisterServant(binding.getURI());
+            if (isLocalhost) {
+                host.releaseLocalNameServer(bindingPort);
+            }
         } catch (Exception e) {
             throw new ServiceRuntimeException(e);
         }

Added: tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/SocketUtil.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/SocketUtil.java?rev=681871&view=auto
==============================================================================
--- tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/SocketUtil.java
(added)
+++ tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/SocketUtil.java
Fri Aug  1 15:27:33 2008
@@ -0,0 +1,225 @@
+/*
+ * 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.tuscany.sca.binding.sca.corba.impl;
+
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.UnknownHostException;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Class partially copied from eclipse wst project
+ * (http://repo1.maven.org/maven2/org/eclipse/wst/server/core/1.0.205-v20070829b/).
+ * Finally we should use jar from maven dependency. Problem described in
+ * http://www.fornax-platform.org/cp/display/fornax/2.+Hello+World+Tutorial+(CSC)?replyToComment=2098#comment-2098
+ * needs to be fixed.
+ */
+public class SocketUtil {
+
+    protected static final Object lock = new Object();
+
+    private static Set<String> localHostCache;
+    private static Set<String> notLocalHostCache = new HashSet<String>();
+    private static Map<String, CacheThread> threadMap = new HashMap<String, CacheThread>();
+
+    private static Set<InetAddress> addressCache;
+
+    static class CacheThread extends Thread {
+        private Set<InetAddress> currentAddresses;
+        private Set<String> addressList;
+        private String host;
+        private Set<String> nonAddressList;
+        private Map threadMap2;
+
+        public CacheThread(String host,
+                           Set<InetAddress> currentAddresses,
+                           Set<String> addressList,
+                           Set<String> nonAddressList,
+                           Map threadMap2) {
+            super("Caching localhost information");
+            this.host = host;
+            this.currentAddresses = currentAddresses;
+            this.addressList = addressList;
+            this.nonAddressList = nonAddressList;
+            this.threadMap2 = threadMap2;
+        }
+
+        public void run() {
+            if (currentAddresses != null) {
+                Iterator iter2 = currentAddresses.iterator();
+                while (iter2.hasNext()) {
+                    InetAddress addr = (InetAddress)iter2.next();
+                    String hostname = addr.getHostName();
+                    String hostname2 = addr.getCanonicalHostName();
+                    synchronized (lock) {
+                        if (hostname != null && !addressList.contains(hostname))
+                            addressList.add(hostname);
+                        if (hostname2 != null && !addressList.contains(hostname2))
+                            addressList.add(hostname2);
+                    }
+                }
+            }
+
+            try {
+                InetAddress[] addrs = InetAddress.getAllByName(host);
+                int length = addrs.length;
+                for (int j = 0; j < length; j++) {
+                    InetAddress addr = addrs[0];
+                    String hostname = addr.getHostName();
+                    String hostname2 = addr.getCanonicalHostName();
+                    synchronized (lock) {
+                        if (addr.isLoopbackAddress()) {
+                            if (hostname != null && !addressList.contains(hostname))
+                                addressList.add(hostname);
+                            if (hostname2 != null && !addressList.contains(hostname2))
+                                addressList.add(hostname2);
+                        } else {
+                            if (hostname != null && !nonAddressList.contains(hostname))
+                                nonAddressList.add(hostname);
+                            if (hostname2 != null && !nonAddressList.contains(hostname2))
+                                nonAddressList.add(hostname2);
+                        }
+                    }
+                }
+            } catch (UnknownHostException e) {
+                synchronized (lock) {
+                    if (host != null && !nonAddressList.contains(host))
+                        nonAddressList.add(host);
+                }
+            }
+            synchronized (lock) {
+                threadMap2.remove(host);
+            }
+        }
+    }
+
+    public static boolean isLocalhost(final String host) {
+        if (host == null || host.equals(""))
+            return false;
+
+        if ("localhost".equals(host) || "127.0.0.1".equals(host))
+            return true;
+
+        // check simple cases
+        try {
+            InetAddress localHostaddr = InetAddress.getLocalHost();
+            if (localHostaddr.getHostName().equals(host) || host.equals(localHostaddr.getCanonicalHostName())
+                || localHostaddr.getHostAddress().equals(host))
+                return true;
+        } catch (Exception e) {
+
+        }
+
+        // check for current thread and wait if necessary
+        boolean currentThread = false;
+        try {
+            Thread t = null;
+            synchronized (lock) {
+                t = threadMap.get(host);
+            }
+            if (t != null && t.isAlive()) {
+                currentThread = true;
+                t.join(30);
+            }
+        } catch (Exception e) {
+
+        }
+
+        // check if cache is still ok
+        boolean refreshedCache = false;
+        try {
+            // get network interfaces
+            final Set<InetAddress> currentAddresses = new HashSet<InetAddress>();
+            currentAddresses.add(InetAddress.getLocalHost());
+            Enumeration nis = NetworkInterface.getNetworkInterfaces();
+            while (nis.hasMoreElements()) {
+                NetworkInterface inter = (NetworkInterface)nis.nextElement();
+                Enumeration<InetAddress> ias = inter.getInetAddresses();
+                while (ias.hasMoreElements())
+                    currentAddresses.add(ias.nextElement());
+            }
+
+            // check if cache is empty or old and refill it if necessary
+            if (addressCache == null || !addressCache.containsAll(currentAddresses)
+                || !currentAddresses.containsAll(addressCache)) {
+                CacheThread cacheThread = null;
+                refreshedCache = true;
+
+                synchronized (lock) {
+                    addressCache = currentAddresses;
+                    notLocalHostCache = new HashSet<String>();
+                    localHostCache = new HashSet<String>(currentAddresses.size() *
3);
+
+                    Iterator iter = currentAddresses.iterator();
+                    while (iter.hasNext()) {
+                        InetAddress addr = (InetAddress)iter.next();
+                        String a = addr.getHostAddress();
+                        if (a != null && !localHostCache.contains(a))
+                            localHostCache.add(a);
+                    }
+
+                    cacheThread = new CacheThread(host, currentAddresses, localHostCache,
notLocalHostCache, threadMap);
+                    threadMap.put(host, cacheThread);
+                    cacheThread.setDaemon(true);
+                    cacheThread.setPriority(Thread.NORM_PRIORITY - 1);
+                    cacheThread.start();
+                }
+                cacheThread.join(200);
+            }
+        } catch (Exception e) {
+        }
+
+        synchronized (lock) {
+            if (localHostCache.contains(host))
+                return true;
+            if (notLocalHostCache.contains(host))
+                return false;
+        }
+
+        // if the cache hasn't been cleared, maybe we still need to lookup the
+        // host
+        if (!refreshedCache && !currentThread) {
+            try {
+                CacheThread cacheThread = null;
+                synchronized (lock) {
+                    cacheThread = new CacheThread(host, null, localHostCache, notLocalHostCache,
threadMap);
+                    threadMap.put(host, cacheThread);
+                    cacheThread.setDaemon(true);
+                    cacheThread.setPriority(Thread.NORM_PRIORITY - 1);
+                    cacheThread.start();
+                }
+                cacheThread.join(75);
+
+                synchronized (lock) {
+                    if (localHostCache.contains(host))
+                        return true;
+                }
+            } catch (Exception e) {
+            }
+        }
+        return false;
+    }
+
+}

Propchange: tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/SocketUtil.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/WrappedSCAException.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/WrappedSCAException.java?rev=681871&r1=681870&r2=681871&view=diff
==============================================================================
--- tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/WrappedSCAException.java
(original)
+++ tuscany/java/sca/modules/binding-sca-corba/src/main/java/org/apache/tuscany/sca/binding/sca/corba/impl/WrappedSCAException.java
Fri Aug  1 15:27:33 2008
@@ -19,15 +19,28 @@
 
 package org.apache.tuscany.sca.binding.sca.corba.impl;
 
+/**
+ * Exception holder for SCA default binding over CORBA. Holds fault message in
+ * XML
+ */
 public class WrappedSCAException extends Exception {
 
     private static final long serialVersionUID = 1L;
     public String fault;
 
+    /**
+     * Creates exception holder with null fault message. This no argument
+     * constructor is needed by CORBA binding.
+     */
     public WrappedSCAException() {
 
     }
 
+    /**
+     * Creates exception holder with fault message
+     * 
+     * @param fault XML fault message
+     */
     public WrappedSCAException(String fault) {
         this.fault = fault;
     }

Modified: tuscany/java/sca/modules/host-corba-jdk/src/main/java/org/apache/tuscany/sca/host/corba/jdk/DefaultCorbaHost.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/host-corba-jdk/src/main/java/org/apache/tuscany/sca/host/corba/jdk/DefaultCorbaHost.java?rev=681871&r1=681870&r2=681871&view=diff
==============================================================================
--- tuscany/java/sca/modules/host-corba-jdk/src/main/java/org/apache/tuscany/sca/host/corba/jdk/DefaultCorbaHost.java
(original)
+++ tuscany/java/sca/modules/host-corba-jdk/src/main/java/org/apache/tuscany/sca/host/corba/jdk/DefaultCorbaHost.java
Fri Aug  1 15:27:33 2008
@@ -29,6 +29,8 @@
 import org.apache.tuscany.sca.host.corba.CorbaHostException;
 import org.apache.tuscany.sca.host.corba.CorbaHostUtils;
 import org.apache.tuscany.sca.host.corba.CorbanameURL;
+import org.apache.tuscany.sca.host.corba.naming.TransientNameServer;
+import org.apache.tuscany.sca.host.corba.naming.TransientNameService;
 import org.omg.CORBA.ORB;
 import org.omg.CORBA.Object;
 import org.omg.CosNaming.NameComponent;
@@ -46,6 +48,8 @@
     private static final Logger logger = Logger.getLogger(DefaultCorbaHost.class.getName());
 
     private Map<String, ORB> orbs = new ConcurrentHashMap<String, ORB>();
+    private Map<Integer, TransientNameServer> localServers = new ConcurrentHashMap<Integer,
TransientNameServer>();
+    private Map<Integer, Integer> clientsCount = new ConcurrentHashMap<Integer,
Integer>();
 
     private void validatePort(int port) throws IllegalArgumentException {
         if (port < 1) {
@@ -83,7 +87,8 @@
                     namingCtx = namingCtx.bind_new_context(path);
                 }
             }
-            NameComponent finalName = new NameComponent(details.getNamePath().get(details.getNamePath().size()
- 1), "");
+            NameComponent finalName =
+                new NameComponent(details.getNamePath().get(details.getNamePath().size()
- 1), "");
             try {
                 namingCtx.resolve(new NameComponent[] {finalName});
                 // no exception means that some object is already registered
@@ -109,7 +114,7 @@
             handleException(e);
         }
     }
-    
+
     public Object lookup(String uri) throws CorbaHostException {
         Object result = null;
         try {
@@ -118,7 +123,6 @@
             NamingContextExt context = getNamingContext(orb, url.getNameService());
             result = context.resolve_str(url.getName());
         } catch (Exception e) {
-            // e.printStackTrace();
             handleException(e);
         }
         if (result == null) {
@@ -177,4 +181,49 @@
         }
     }
 
+    /**
+     * Starts transient name server under given port. If TNS was previously
+     * spawned it increments clients counter.
+     */
+    synchronized public void createLocalNameServer(int port) throws CorbaHostException {
+        int useCount = clientsCount.containsKey(port) ? clientsCount.get(port) : 0;
+        // no server previously spawned
+        if (useCount == 0) {
+            TransientNameServer server =
+                new TransientNameServer("localhost", port, TransientNameService.DEFAULT_SERVICE_NAME);
+            Thread thread = server.start();
+            if (thread == null) {
+                throw new CorbaHostException("TransientNameServer couldn't be started");
+            } else {
+                localServers.put(port, server);
+            }
+        }
+        clientsCount.put(port, ++useCount);
+    }
+
+    /**
+     * Stops transient name server if there is only one client left using such
+     * TNS. Decrements clients counter if TNS is used by 2 or more clients.
+     */
+    synchronized public void releaseLocalNameServer(int port) throws CorbaHostException {
+        int useCount = clientsCount.containsKey(port) ? clientsCount.get(port) : 0;
+        if (useCount == 1) {
+            // last client executed stop, cleaning up
+            TransientNameServer server = localServers.get(port);
+            if (server != null) {
+                server.stop();
+                clientsCount.remove(port);
+                localServers.remove(port);
+            } else {
+                // FIXME: should we throw exception when expecting not null
+                // server object?
+            }
+        } else if (useCount > 1) {
+            clientsCount.put(port, --useCount);
+        } else {
+            // ignoring request to stop non existing name server
+        }
+
+    }
+
 }

Modified: tuscany/java/sca/modules/host-corba-jdk/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/host-corba-jdk/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCase.java?rev=681871&r1=681870&r2=681871&view=diff
==============================================================================
--- tuscany/java/sca/modules/host-corba-jdk/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCase.java
(original)
+++ tuscany/java/sca/modules/host-corba-jdk/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCase.java
Fri Aug  1 15:27:33 2008
@@ -25,6 +25,7 @@
 
 import java.net.ConnectException;
 import java.net.Socket;
+import java.net.SocketException;
 
 import org.apache.tuscany.sca.host.corba.CorbaHost;
 import org.apache.tuscany.sca.host.corba.CorbaHostException;
@@ -220,6 +221,9 @@
         }
     }
 
+    /**
+     * 
+     */
     @Test
     public void test_ensureORBStopped() {
         try {
@@ -242,4 +246,38 @@
             }
         }
     }
+
+    /**
+     * Test for creating and releasing local name server
+     */
+    @Test
+    public void test_localNameServer() {
+        int testPort = 5070;
+        try {
+            host.createLocalNameServer(testPort);
+            host.createLocalNameServer(testPort);
+            host.createLocalNameServer(testPort);
+            // make test connection to name server
+            Socket socket = new Socket("localhost", testPort);
+            socket.close();
+            // and stop server
+            host.releaseLocalNameServer(testPort);
+            host.releaseLocalNameServer(testPort);
+            // after releasing 2 clients 3rd should still hold the server
+            socket = new Socket("localhost", testPort);
+            socket.close();
+            host.releaseLocalNameServer(testPort);
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail();
+        }
+        try {
+            // previously made 3rd stop so there should be no name server under
+            // this port
+            new Socket("localhost", testPort);
+            fail();
+        } catch (Exception e) {
+            assertTrue(e instanceof SocketException);
+        }
+    }
 }

Modified: tuscany/java/sca/modules/host-corba-jee/src/main/java/org/apache/tuscany/sca/host/corba/jee/JEECorbaHost.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/host-corba-jee/src/main/java/org/apache/tuscany/sca/host/corba/jee/JEECorbaHost.java?rev=681871&r1=681870&r2=681871&view=diff
==============================================================================
--- tuscany/java/sca/modules/host-corba-jee/src/main/java/org/apache/tuscany/sca/host/corba/jee/JEECorbaHost.java
(original)
+++ tuscany/java/sca/modules/host-corba-jee/src/main/java/org/apache/tuscany/sca/host/corba/jee/JEECorbaHost.java
Fri Aug  1 15:27:33 2008
@@ -158,4 +158,12 @@
         }
     }
 
+    public void createLocalNameServer(int port) throws CorbaHostException {
+        // no need to start - server is provided by environment
+    }
+
+    public void releaseLocalNameServer(int port) throws CorbaHostException {
+        // no need to release - server is provided by environment
+    }
+
 }

Modified: tuscany/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/CorbaHost.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/CorbaHost.java?rev=681871&r1=681870&r2=681871&view=diff
==============================================================================
--- tuscany/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/CorbaHost.java
(original)
+++ tuscany/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/CorbaHost.java
Fri Aug  1 15:27:33 2008
@@ -19,7 +19,6 @@
 
 package org.apache.tuscany.sca.host.corba;
 
-import org.omg.CORBA.ORB;
 import org.omg.CORBA.Object;
 
 /**
@@ -29,6 +28,7 @@
 
     /**
      * Registers servant in name server.
+     * 
      * @param uri corbaname URI
      * @param serviceObject
      * @throws CorbaHostException
@@ -37,18 +37,35 @@
 
     /**
      * Removes servant from name server
+     * 
      * @param orb The ORB instance
      * @param name binding name
      * @throws CorbaHostException
      */
     void unregisterServant(String uri) throws CorbaHostException;
-    
+
     /**
      * Gets reference to object
+     * 
      * @param name binding name
      * @return objects reference
      * @throws CorbaHostException
      */
     Object lookup(String uri) throws CorbaHostException;
 
+    /**
+     * Provides name server under given port.
+     * 
+     * @param port
+     * @throws CorbaHostException
+     */
+    void createLocalNameServer(int port) throws CorbaHostException;
+
+    /**
+     * Stops name server but only if every client released previously created NS.
+     * 
+     * @param port
+     * @throws CorbaHostException
+     */
+    void releaseLocalNameServer(int port) throws CorbaHostException;
 }

Modified: tuscany/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/ExtensibleCorbaHost.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/ExtensibleCorbaHost.java?rev=681871&r1=681870&r2=681871&view=diff
==============================================================================
--- tuscany/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/ExtensibleCorbaHost.java
(original)
+++ tuscany/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/ExtensibleCorbaHost.java
Fri Aug  1 15:27:33 2008
@@ -19,7 +19,6 @@
 
 package org.apache.tuscany.sca.host.corba;
 
-import org.omg.CORBA.ORB;
 import org.omg.CORBA.Object;
 
 /**
@@ -52,4 +51,12 @@
         return hosts.getCorbaHosts().get(0);
     }
 
+    public void createLocalNameServer(int port) throws CorbaHostException {
+        getCorbaHost().createLocalNameServer(port);
+    }
+    
+    public void releaseLocalNameServer(int port) throws CorbaHostException {
+        getCorbaHost().releaseLocalNameServer(port);
+    }
+
 }



Mime
View raw message