From 33cf44b2ca09724ab08769b887750f8b3807fc9e Mon Sep 17 00:00:00 2001 From: wjaniszewski Date: Tue, 12 Aug 2008 15:17:26 +0000 Subject: Added Transient Name Server JSE Corba host. Adjusted existing bindings and integration tests and removed 'provideNameServer' flag from binding.corba configuration git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@685180 13f79535-47bb-0310-9956-ffa450edef68 --- .../corba/impl/CorbaServiceBindingProvider.java | 21 +- .../sca/binding/corba/impl/util/SocketUtil.java | 225 --------------------- .../tuscany/sca/binding/corba/CorbaBinding.java | 4 - .../sca/binding/corba/impl/CorbaBindingImpl.java | 9 - .../binding/corba/impl/CorbaBindingProcessor.java | 8 - .../corba/impl/CorbaSCAServiceBindingProvider.java | 14 -- .../tuscany/sca/host/corba/jee/JEECorbaHost.java | 8 - java/sca/modules/host-corba-jse-tns/LICENSE | 205 +++++++++++++++++++ java/sca/modules/host-corba-jse-tns/NOTICE | 6 + java/sca/modules/host-corba-jse-tns/pom.xml | 71 +++++++ .../corba/jse/tns/CorbaRuntimeModuleActivator.java | 53 +++++ .../host/corba/jse/tns/TnsDefaultCorbaHost.java | 115 +++++++++++ .../org.apache.tuscany.sca.core.ModuleActivator | 18 ++ .../corba/testing/TnsDefaultCorbaHostTestCase.java | 96 +++++++++ .../corba/jse/CorbaRuntimeModuleActivator.java | 9 +- .../sca/host/corba/jse/DefaultCorbaHost.java | 49 ----- .../corba/testing/DefaultCorbaHostTestCase.java | 35 ---- .../apache/tuscany/sca/host/corba/CorbaHost.java | 16 -- .../sca/host/corba/ExtensibleCorbaHost.java | 8 - .../apache/tuscany/sca/host/corba/SocketUtil.java | 225 +++++++++++++++++++++ java/sca/modules/pom.xml | 1 + 21 files changed, 795 insertions(+), 401 deletions(-) delete mode 100755 java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/util/SocketUtil.java create mode 100644 java/sca/modules/host-corba-jse-tns/LICENSE create mode 100644 java/sca/modules/host-corba-jse-tns/NOTICE create mode 100644 java/sca/modules/host-corba-jse-tns/pom.xml create mode 100644 java/sca/modules/host-corba-jse-tns/src/main/java/org/apache/tuscany/sca/host/corba/jse/tns/CorbaRuntimeModuleActivator.java create mode 100644 java/sca/modules/host-corba-jse-tns/src/main/java/org/apache/tuscany/sca/host/corba/jse/tns/TnsDefaultCorbaHost.java create mode 100644 java/sca/modules/host-corba-jse-tns/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator create mode 100644 java/sca/modules/host-corba-jse-tns/src/test/java/org/apache/tuscany/sca/host/corba/testing/TnsDefaultCorbaHostTestCase.java create mode 100755 java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/SocketUtil.java (limited to 'java/sca/modules') diff --git a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/CorbaServiceBindingProvider.java b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/CorbaServiceBindingProvider.java index 516605ec8f..0465dbe7e9 100644 --- a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/CorbaServiceBindingProvider.java +++ b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/CorbaServiceBindingProvider.java @@ -24,9 +24,7 @@ import org.apache.tuscany.sca.binding.corba.impl.service.ComponentInvocationProx import org.apache.tuscany.sca.binding.corba.impl.service.DynaCorbaServant; import org.apache.tuscany.sca.binding.corba.impl.service.InvocationProxy; import org.apache.tuscany.sca.binding.corba.impl.types.util.Utils; -import org.apache.tuscany.sca.binding.corba.impl.util.SocketUtil; 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.interfacedef.java.JavaInterface; import org.apache.tuscany.sca.provider.ServiceBindingProvider; @@ -42,20 +40,11 @@ public class CorbaServiceBindingProvider implements ServiceBindingProvider { private CorbaHost host; private RuntimeComponentService service; private DynaCorbaServant servant; - private boolean isLocalhost; - private int port; - + public CorbaServiceBindingProvider(CorbaBinding binding, CorbaHost host, RuntimeComponentService service) { this.binding = binding; this.host = host; this.service = service; - if (binding.isProvideNameServer()) { - CorbanameURL details = new CorbanameURL(binding.getCorbaname()); - isLocalhost = SocketUtil.isLocalhost(details.getHost()); - if (isLocalhost) { - port = details.getPort(); - } - } } /** @@ -74,9 +63,6 @@ public class CorbaServiceBindingProvider implements ServiceBindingProvider { InvocationProxy proxy = new ComponentInvocationProxy(service, service.getRuntimeWire(binding), javaClass); servant = new DynaCorbaServant(proxy, Utils.getTypeId(javaClass)); servant.setIds(new String[] {binding.getId()}); - if (binding.isProvideNameServer() && isLocalhost) { - host.createLocalNameServer(port); - } host.registerServant(binding.getCorbaname(), servant); } catch (Exception e) { throw new ServiceRuntimeException(e); @@ -89,10 +75,7 @@ public class CorbaServiceBindingProvider implements ServiceBindingProvider { */ public void stop() { try { - if (binding.isProvideNameServer() && isLocalhost) { - host.releaseLocalNameServer(port); - } - host.unregisterServant(binding.getCorbaname()); + host.unregisterServant(binding.getCorbaname()); } catch (Exception e) { throw new ServiceRuntimeException(e); } diff --git a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/util/SocketUtil.java b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/util/SocketUtil.java deleted file mode 100755 index 3541fd92cf..0000000000 --- a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/util/SocketUtil.java +++ /dev/null @@ -1,225 +0,0 @@ -/* - * 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.corba.impl.util; - -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 localHostCache; - private static Set notLocalHostCache = new HashSet(); - private static Map threadMap = new HashMap(); - - private static Set addressCache; - - static class CacheThread extends Thread { - private Set currentAddresses; - private Set addressList; - private String host; - private Set nonAddressList; - private Map threadMap2; - - public CacheThread(String host, - Set currentAddresses, - Set addressList, - Set 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 currentAddresses = new HashSet(); - currentAddresses.add(InetAddress.getLocalHost()); - Enumeration nis = NetworkInterface.getNetworkInterfaces(); - while (nis.hasMoreElements()) { - NetworkInterface inter = (NetworkInterface)nis.nextElement(); - Enumeration 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(); - localHostCache = new HashSet(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; - } - -} diff --git a/java/sca/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/CorbaBinding.java b/java/sca/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/CorbaBinding.java index bf3bc09461..0b79065cc3 100644 --- a/java/sca/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/CorbaBinding.java +++ b/java/sca/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/CorbaBinding.java @@ -43,8 +43,4 @@ public interface CorbaBinding extends Binding { void setId(String id); String getCorbaname(); - - boolean isProvideNameServer(); - - void setProvideNameServer(boolean provideNameServer); } diff --git a/java/sca/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/impl/CorbaBindingImpl.java b/java/sca/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/impl/CorbaBindingImpl.java index 167fe50d7b..54e98d4117 100644 --- a/java/sca/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/impl/CorbaBindingImpl.java +++ b/java/sca/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/impl/CorbaBindingImpl.java @@ -38,7 +38,6 @@ public class CorbaBindingImpl implements CorbaBinding, PolicySetAttachPoint { private String host; private int port; private String id; - private boolean provideNameServer; private List requiredIntents = new ArrayList(); private List policySets = new ArrayList(); @@ -130,12 +129,4 @@ public class CorbaBindingImpl implements CorbaBinding, PolicySetAttachPoint { return CorbaHostUtils.isValidCorbanameURI(getURI()) ? getURI(): CorbaHostUtils.createCorbanameURI(getHost(), getPort(), getName()); } - public boolean isProvideNameServer() { - return provideNameServer; - } - - public void setProvideNameServer(boolean provideNameServer) { - this.provideNameServer = provideNameServer; - } - } diff --git a/java/sca/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/impl/CorbaBindingProcessor.java b/java/sca/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/impl/CorbaBindingProcessor.java index a7be836437..0bb38d3682 100644 --- a/java/sca/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/impl/CorbaBindingProcessor.java +++ b/java/sca/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/impl/CorbaBindingProcessor.java @@ -86,10 +86,6 @@ public class CorbaBindingProcessor implements StAXArtifactProcessor + + + 4.0.0 + + org.apache.tuscany.sca + tuscany-modules + 1.4-SNAPSHOT + + tuscany-host-corba-jse-tns + Apache Tuscany SCA Transient Name Server CORBA Host + + + + + org.apache.tuscany.sca + tuscany-host-corba + 1.4-SNAPSHOT + + + + org.apache.tuscany.sca + tuscany-host-corba-jse + 1.4-SNAPSHOT + + + + org.apache.tuscany.sca + tuscany-binding-corba-runtime + 1.4-SNAPSHOT + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + + ${tuscany.version} + org.apache.tuscany.sca.host.corba.jse.tns + ${pom.name} + org.apache.tuscany.sca.host.corba.jse.tns* + + + + + + + diff --git a/java/sca/modules/host-corba-jse-tns/src/main/java/org/apache/tuscany/sca/host/corba/jse/tns/CorbaRuntimeModuleActivator.java b/java/sca/modules/host-corba-jse-tns/src/main/java/org/apache/tuscany/sca/host/corba/jse/tns/CorbaRuntimeModuleActivator.java new file mode 100644 index 0000000000..312d739c53 --- /dev/null +++ b/java/sca/modules/host-corba-jse-tns/src/main/java/org/apache/tuscany/sca/host/corba/jse/tns/CorbaRuntimeModuleActivator.java @@ -0,0 +1,53 @@ +/* + * 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.host.corba.jse.tns; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.ModuleActivator; +import org.apache.tuscany.sca.host.corba.CorbaHostExtensionPoint; +import org.apache.tuscany.sca.host.corba.jse.DefaultCorbaHost; + +/** + * @version $Rev: 683329 $ $Date: 2008-08-06 19:06:39 +0200 (śro, 06 sie 2008) $ + */ +public class CorbaRuntimeModuleActivator implements ModuleActivator { + // private static final Logger logger = + // Logger.getLogger(CorbaRuntimeModuleActivator.class.getName()); + + private TnsDefaultCorbaHost server; + + public void start(ExtensionPointRegistry extensionPointRegistry) { + // Register our Corba host + CorbaHostExtensionPoint corbaHosts = extensionPointRegistry.getExtensionPoint(CorbaHostExtensionPoint.class); + server = new TnsDefaultCorbaHost(); + if (corbaHosts.getCorbaHosts().size() > 0 && corbaHosts.getCorbaHosts().get(0) instanceof DefaultCorbaHost) { + // Set TnsDefaultCorbaHost as default when DefaultCorbaHost was registered before + corbaHosts.getCorbaHosts().add(0, server); + } else { + corbaHosts.getCorbaHosts().add(server); + } + } + + public void stop(ExtensionPointRegistry registry) { + if (server != null) { + server.stop(); + } + } + +} diff --git a/java/sca/modules/host-corba-jse-tns/src/main/java/org/apache/tuscany/sca/host/corba/jse/tns/TnsDefaultCorbaHost.java b/java/sca/modules/host-corba-jse-tns/src/main/java/org/apache/tuscany/sca/host/corba/jse/tns/TnsDefaultCorbaHost.java new file mode 100644 index 0000000000..c322b5cfee --- /dev/null +++ b/java/sca/modules/host-corba-jse-tns/src/main/java/org/apache/tuscany/sca/host/corba/jse/tns/TnsDefaultCorbaHost.java @@ -0,0 +1,115 @@ +/* + * 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.host.corba.jse.tns; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.tuscany.sca.host.corba.CorbaHost; +import org.apache.tuscany.sca.host.corba.CorbaHostException; +import org.apache.tuscany.sca.host.corba.CorbanameURL; +import org.apache.tuscany.sca.host.corba.SocketUtil; +import org.apache.tuscany.sca.host.corba.jse.DefaultCorbaHost; +import org.apache.tuscany.sca.host.corba.naming.TransientNameServer; +import org.apache.tuscany.sca.host.corba.naming.TransientNameService; +import org.omg.CORBA.Object; + +/** + * Default implementation of CORBA host + */ +public class TnsDefaultCorbaHost implements CorbaHost { + //private static final Logger logger = Logger.getLogger(TnsDefaultCorbaHost.class.getName()); + + private Map localServers = new ConcurrentHashMap(); + private Map clientsCount = new ConcurrentHashMap(); + private CorbaHost targetHost = new DefaultCorbaHost(); + + public void registerServant(String uri, Object servantObject) throws CorbaHostException { + CorbanameURL details = new CorbanameURL(uri); + if (SocketUtil.isLocalhost(details.getHost())) { + createLocalNameServer(details.getPort()); + } + targetHost.registerServant(uri, servantObject); + } + + public void unregisterServant(String uri) throws CorbaHostException { + targetHost.unregisterServant(uri); + CorbanameURL details = new CorbanameURL(uri); + if (SocketUtil.isLocalhost(details.getHost())) { + releaseLocalNameServer(details.getPort()); + } + } + + public Object lookup(String uri) throws CorbaHostException { + return targetHost.lookup(uri); + } + + /** + * Starts transient name server under given port. If TNS was previously + * spawned it increments clients counter. + */ + synchronized private 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 private 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 + } + + } + + public void stop() { + for (TransientNameServer tns : localServers.values()) { + tns.stop(); + } + } + +} diff --git a/java/sca/modules/host-corba-jse-tns/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator b/java/sca/modules/host-corba-jse-tns/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator new file mode 100644 index 0000000000..906ec4efce --- /dev/null +++ b/java/sca/modules/host-corba-jse-tns/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator @@ -0,0 +1,18 @@ +# 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. + +org.apache.tuscany.sca.host.corba.jse.tns.CorbaRuntimeModuleActivator diff --git a/java/sca/modules/host-corba-jse-tns/src/test/java/org/apache/tuscany/sca/host/corba/testing/TnsDefaultCorbaHostTestCase.java b/java/sca/modules/host-corba-jse-tns/src/test/java/org/apache/tuscany/sca/host/corba/testing/TnsDefaultCorbaHostTestCase.java new file mode 100644 index 0000000000..06bc14f4e3 --- /dev/null +++ b/java/sca/modules/host-corba-jse-tns/src/test/java/org/apache/tuscany/sca/host/corba/testing/TnsDefaultCorbaHostTestCase.java @@ -0,0 +1,96 @@ +/* + * 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.host.corba.testing; + +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + +import java.net.Socket; +import java.net.SocketException; + +import org.apache.tuscany.sca.binding.corba.impl.service.DynaCorbaServant; +import org.apache.tuscany.sca.host.corba.jse.tns.TnsDefaultCorbaHost; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * General tests + */ +public class TnsDefaultCorbaHostTestCase { + + private static TnsDefaultCorbaHost host; + + @BeforeClass + public static void start() { + try { + host = new TnsDefaultCorbaHost(); + } catch (Throwable e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + @AfterClass + public static void stop() { + host.stop(); + } + + /** + * Test for creating and releasing local name server + */ + @Test + public void test_localNameServer() { + int testPort = 5070; + try { + String testUri1 = "corbaname::localhost:" + testPort + "#Test1"; + String testUri2 = "corbaname::localhost:" + testPort + "#Test2"; + String testUri3 = "corbaname::localhost:" + testPort + "#Test3"; + DynaCorbaServant servant = new DynaCorbaServant(null, "IDL:org/apache/tuscany:1.0"); + host.registerServant(testUri1, servant); + host.registerServant(testUri2, servant); + host.registerServant(testUri3, servant); + Thread.sleep(1000); + // make test connection to name server + Socket socket = new Socket("localhost", testPort); + socket.close(); + // and stop server + host.unregisterServant(testUri1); + host.unregisterServant(testUri2); + // after releasing 2 clients 3rd should still hold the server + socket = new Socket("localhost", testPort); + socket.close(); + host.unregisterServant(testUri3); + Thread.sleep(1000); + } 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); + } + } +} + diff --git a/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jse/CorbaRuntimeModuleActivator.java b/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jse/CorbaRuntimeModuleActivator.java index 4194e733a4..252a1512e4 100644 --- a/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jse/CorbaRuntimeModuleActivator.java +++ b/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jse/CorbaRuntimeModuleActivator.java @@ -18,8 +18,6 @@ */ package org.apache.tuscany.sca.host.corba.jse; -import java.util.logging.Logger; - import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.ModuleActivator; import org.apache.tuscany.sca.host.corba.CorbaHostExtensionPoint; @@ -28,16 +26,15 @@ import org.apache.tuscany.sca.host.corba.CorbaHostExtensionPoint; * @version $Rev$ $Date$ */ public class CorbaRuntimeModuleActivator implements ModuleActivator { - private static final Logger logger = Logger.getLogger(CorbaRuntimeModuleActivator.class.getName()); + // private static final Logger logger = + // Logger.getLogger(CorbaRuntimeModuleActivator.class.getName()); private DefaultCorbaHost server; public void start(ExtensionPointRegistry extensionPointRegistry) { // Register our Corba host - CorbaHostExtensionPoint corbaHosts = - extensionPointRegistry.getExtensionPoint(CorbaHostExtensionPoint.class); - + CorbaHostExtensionPoint corbaHosts = extensionPointRegistry.getExtensionPoint(CorbaHostExtensionPoint.class); server = new DefaultCorbaHost(); corbaHosts.addCorbaHost(server); } diff --git a/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jse/DefaultCorbaHost.java b/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jse/DefaultCorbaHost.java index 14ef2eb908..e0cee52c3d 100644 --- a/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jse/DefaultCorbaHost.java +++ b/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jse/DefaultCorbaHost.java @@ -29,8 +29,6 @@ import org.apache.tuscany.sca.host.corba.CorbaHost; 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; @@ -48,8 +46,6 @@ public class DefaultCorbaHost implements CorbaHost { private static final Logger logger = Logger.getLogger(DefaultCorbaHost.class.getName()); private Map orbs = new ConcurrentHashMap(); - private Map localServers = new ConcurrentHashMap(); - private Map clientsCount = new ConcurrentHashMap(); private void validatePort(int port) throws IllegalArgumentException { if (port < 1) { @@ -181,49 +177,4 @@ public class DefaultCorbaHost implements CorbaHost { } } - /** - * 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 - } - - } - } diff --git a/java/sca/modules/host-corba-jse/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCase.java b/java/sca/modules/host-corba-jse/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCase.java index 6b98f1c949..8084e39956 100644 --- a/java/sca/modules/host-corba-jse/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCase.java +++ b/java/sca/modules/host-corba-jse/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCase.java @@ -247,39 +247,4 @@ public class DefaultCorbaHostTestCase { } } - /** - * 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); - Thread.sleep(1000); - // 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); - Thread.sleep(1000); - } 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); - } - } } diff --git a/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/CorbaHost.java b/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/CorbaHost.java index 17e9566e17..01a27ba371 100644 --- a/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/CorbaHost.java +++ b/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/CorbaHost.java @@ -52,20 +52,4 @@ public interface CorbaHost { * @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; } diff --git a/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/ExtensibleCorbaHost.java b/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/ExtensibleCorbaHost.java index 1c36fca57d..12112fea04 100644 --- a/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/ExtensibleCorbaHost.java +++ b/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/ExtensibleCorbaHost.java @@ -51,12 +51,4 @@ public class ExtensibleCorbaHost implements CorbaHost { 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); - } - } diff --git a/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/SocketUtil.java b/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/SocketUtil.java new file mode 100755 index 0000000000..b076e023da --- /dev/null +++ b/java/sca/modules/host-corba/src/main/java/org/apache/tuscany/sca/host/corba/SocketUtil.java @@ -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.host.corba; + +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 localHostCache; + private static Set notLocalHostCache = new HashSet(); + private static Map threadMap = new HashMap(); + + private static Set addressCache; + + static class CacheThread extends Thread { + private Set currentAddresses; + private Set addressList; + private String host; + private Set nonAddressList; + private Map threadMap2; + + public CacheThread(String host, + Set currentAddresses, + Set addressList, + Set 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 currentAddresses = new HashSet(); + currentAddresses.add(InetAddress.getLocalHost()); + Enumeration nis = NetworkInterface.getNetworkInterfaces(); + while (nis.hasMoreElements()) { + NetworkInterface inter = (NetworkInterface)nis.nextElement(); + Enumeration 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(); + localHostCache = new HashSet(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; + } + +} diff --git a/java/sca/modules/pom.xml b/java/sca/modules/pom.xml index 0ef4e87786..aed561953b 100644 --- a/java/sca/modules/pom.xml +++ b/java/sca/modules/pom.xml @@ -112,6 +112,7 @@ host-corba host-corba-jee host-corba-jse + host-corba-jse-tns host-ejb host-embedded host-http -- cgit v1.2.3