From 70589e54de3f16a711c47a971676b65f0a6c5ed1 Mon Sep 17 00:00:00 2001 From: wjaniszewski Date: Wed, 6 Aug 2008 17:06:39 +0000 Subject: renaming due to JIRA TUSCANY-2468, JSE CORBA host tests enabled back to see what happen (JIRA TUSCANY-2526) git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@683329 13f79535-47bb-0310-9956-ffa450edef68 --- .../corba/jdk/CorbaRuntimeModuleActivator.java | 51 ---- .../sca/host/corba/jdk/DefaultCorbaHost.java | 229 ----------------- .../corba/jse/CorbaRuntimeModuleActivator.java | 51 ++++ .../sca/host/corba/jse/DefaultCorbaHost.java | 229 +++++++++++++++++ .../org.apache.tuscany.sca.core.ModuleActivator | 2 +- .../corba/testing/DefaultCorbaHostTestCase.java | 285 +++++++++++++++++++++ .../testing/DefaultCorbaHostTestCaseFIXME.java | 283 -------------------- 7 files changed, 566 insertions(+), 564 deletions(-) delete mode 100644 java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jdk/CorbaRuntimeModuleActivator.java delete mode 100644 java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jdk/DefaultCorbaHost.java create mode 100644 java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jse/CorbaRuntimeModuleActivator.java create mode 100644 java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jse/DefaultCorbaHost.java create mode 100644 java/sca/modules/host-corba-jse/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCase.java delete mode 100644 java/sca/modules/host-corba-jse/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCaseFIXME.java (limited to 'java/sca') diff --git a/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jdk/CorbaRuntimeModuleActivator.java b/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jdk/CorbaRuntimeModuleActivator.java deleted file mode 100644 index 1bf86ccec9..0000000000 --- a/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jdk/CorbaRuntimeModuleActivator.java +++ /dev/null @@ -1,51 +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.host.corba.jdk; - -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; - -/** - * @version $Rev$ $Date$ - */ -public class CorbaRuntimeModuleActivator implements ModuleActivator { - 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); - - server = new DefaultCorbaHost(); - corbaHosts.addCorbaHost(server); - } - - public void stop(ExtensionPointRegistry registry) { - if (server != null) { - server.stop(); - } - } - -} diff --git a/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jdk/DefaultCorbaHost.java b/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jdk/DefaultCorbaHost.java deleted file mode 100644 index fc7889f5e9..0000000000 --- a/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jdk/DefaultCorbaHost.java +++ /dev/null @@ -1,229 +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.host.corba.jdk; - -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; -import java.util.logging.Logger; - -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; -import org.omg.CosNaming.NamingContext; -import org.omg.CosNaming.NamingContextExt; -import org.omg.CosNaming.NamingContextExtHelper; -import org.omg.CosNaming.NamingContextHelper; -import org.omg.CosNaming.NamingContextPackage.InvalidName; -import org.omg.CosNaming.NamingContextPackage.NotFound; - -/** - * Default implementation of CORBA host - */ -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) { - throw new IllegalArgumentException("Port value should be > 0"); - } - } - - private NamingContextExt getNamingContext(ORB orb, String nameService) throws Exception { - org.omg.CORBA.Object objRef = orb.resolve_initial_references(nameService); - return NamingContextExtHelper.narrow(objRef); - } - - private void handleException(Exception e) throws CorbaHostException { - // The cause of the Exception is JDK specific - if (e instanceof NotFound) { - throw new CorbaHostException(CorbaHostException.NO_SUCH_OBJECT, e); - } else if (e instanceof InvalidName) { - throw new CorbaHostException(e); - } else { - throw new CorbaHostException(e); - } - } - - public void registerServant(String uri, Object servantObject) throws CorbaHostException { - CorbanameURL details = CorbaHostUtils.getServiceDetails(uri); - ORB orb = createORB(details.getHost(), details.getPort(), false); - try { - NamingContext namingCtx = getNamingContext(orb, details.getNameService()); - for (int i = 0; i < details.getNamePath().size() - 1; i++) { - NameComponent nc = new NameComponent(details.getNamePath().get(i), ""); - NameComponent[] path = new NameComponent[] {nc}; - try { - namingCtx = NamingContextHelper.narrow(namingCtx.resolve(path)); - } catch (Exception e) { - namingCtx = namingCtx.bind_new_context(path); - } - } - 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 - // under this name, we need to crash here - throw new CorbaHostException(CorbaHostException.BINDING_IN_USE); - } catch (NotFound e) { - namingCtx.bind(new NameComponent[] {finalName}, servantObject); - } - } catch (CorbaHostException e) { - throw e; - } catch (Exception e) { - handleException(e); - } - } - - public void unregisterServant(String uri) throws CorbaHostException { - CorbanameURL details = CorbaHostUtils.getServiceDetails(uri); - ORB orb = createORB(details.getHost(), details.getPort(), false); - try { - NamingContextExt namingCtx = getNamingContext(orb, details.getNameService()); - namingCtx.unbind(namingCtx.to_name(details.getName())); - } catch (Exception e) { - handleException(e); - } - } - - public Object lookup(String uri) throws CorbaHostException { - Object result = null; - try { - CorbanameURL url = new CorbanameURL(uri); - ORB orb = createORB(url.getHost(), url.getPort(), false); - NamingContextExt context = getNamingContext(orb, url.getNameService()); - result = context.resolve_str(url.getName()); - } catch (Exception e) { - handleException(e); - } - if (result == null) { - throw new CorbaHostException(CorbaHostException.NO_SUCH_OBJECT); - } - return result; - } - - public ORB createORB(String host, int port, boolean server) throws CorbaHostException { - validatePort(port); - - String key = host + ":" + port; - ORB orb = orbs.get(key); - if (orb != null) { - return orb; - } - // Create an ORB object - Properties props = new Properties(); - props.putAll(System.getProperties()); - - String portStr = String.valueOf(port); - props.put("org.omg.CORBA.ORBServerId", "1000000"); - props.put("org.omg.CORBA.ORBInitialHost", host); - props.put("org.omg.CORBA.ORBInitialPort", portStr); - - // STEP 1: Set ORBPeristentServerPort property - // Set the proprietary property to open up a port to listen to - // INS requests. - - if (server) { - props.put("com.sun.CORBA.POA.ORBPersistentServerPort", portStr); - props.put("com.ibm.CORBA.ListenerPort", portStr); - props.put("gnu.CORBA.ListenerPort", portStr); - // props.put("org.omg.CORBA.ORBClass", - // "org.apache.yoko.orb.CORBA.ORB"); - // props.put("org.omg.CORBA.ORBSingletonClass", - // "org.apache.yoko.orb.CORBA.ORBSingleton"); - props.put("yoko.orb.oa.endpoint", "iiop --host " + host + " --port " + port); - props.put("yoko.orb.poamanager.TNameService.endpoint", "iiop --host " + host); - } - - String[] args = {"-ORBInitialHost", host, "-ORBInitialPort", "" + port}; - orb = ORB.init(args, props); - orbs.put(key, orb); - return orb; - } - - public void stop() { - for (ORB orb : orbs.values()) { - try { - orb.shutdown(true); - orb.destroy(); - } catch (Throwable e) { - logger.log(Level.SEVERE, e.getMessage(), e); - } - } - } - - /** - * 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/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 new file mode 100644 index 0000000000..4194e733a4 --- /dev/null +++ b/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jse/CorbaRuntimeModuleActivator.java @@ -0,0 +1,51 @@ +/* + * 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; + +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; + +/** + * @version $Rev$ $Date$ + */ +public class CorbaRuntimeModuleActivator implements ModuleActivator { + 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); + + server = new DefaultCorbaHost(); + corbaHosts.addCorbaHost(server); + } + + public void stop(ExtensionPointRegistry registry) { + if (server != null) { + server.stop(); + } + } + +} 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 new file mode 100644 index 0000000000..14ef2eb908 --- /dev/null +++ b/java/sca/modules/host-corba-jse/src/main/java/org/apache/tuscany/sca/host/corba/jse/DefaultCorbaHost.java @@ -0,0 +1,229 @@ +/* + * 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; + +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +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; +import org.omg.CosNaming.NamingContext; +import org.omg.CosNaming.NamingContextExt; +import org.omg.CosNaming.NamingContextExtHelper; +import org.omg.CosNaming.NamingContextHelper; +import org.omg.CosNaming.NamingContextPackage.InvalidName; +import org.omg.CosNaming.NamingContextPackage.NotFound; + +/** + * Default implementation of CORBA host + */ +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) { + throw new IllegalArgumentException("Port value should be > 0"); + } + } + + private NamingContextExt getNamingContext(ORB orb, String nameService) throws Exception { + org.omg.CORBA.Object objRef = orb.resolve_initial_references(nameService); + return NamingContextExtHelper.narrow(objRef); + } + + private void handleException(Exception e) throws CorbaHostException { + // The cause of the Exception is JDK specific + if (e instanceof NotFound) { + throw new CorbaHostException(CorbaHostException.NO_SUCH_OBJECT, e); + } else if (e instanceof InvalidName) { + throw new CorbaHostException(e); + } else { + throw new CorbaHostException(e); + } + } + + public void registerServant(String uri, Object servantObject) throws CorbaHostException { + CorbanameURL details = CorbaHostUtils.getServiceDetails(uri); + ORB orb = createORB(details.getHost(), details.getPort(), false); + try { + NamingContext namingCtx = getNamingContext(orb, details.getNameService()); + for (int i = 0; i < details.getNamePath().size() - 1; i++) { + NameComponent nc = new NameComponent(details.getNamePath().get(i), ""); + NameComponent[] path = new NameComponent[] {nc}; + try { + namingCtx = NamingContextHelper.narrow(namingCtx.resolve(path)); + } catch (Exception e) { + namingCtx = namingCtx.bind_new_context(path); + } + } + 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 + // under this name, we need to crash here + throw new CorbaHostException(CorbaHostException.BINDING_IN_USE); + } catch (NotFound e) { + namingCtx.bind(new NameComponent[] {finalName}, servantObject); + } + } catch (CorbaHostException e) { + throw e; + } catch (Exception e) { + handleException(e); + } + } + + public void unregisterServant(String uri) throws CorbaHostException { + CorbanameURL details = CorbaHostUtils.getServiceDetails(uri); + ORB orb = createORB(details.getHost(), details.getPort(), false); + try { + NamingContextExt namingCtx = getNamingContext(orb, details.getNameService()); + namingCtx.unbind(namingCtx.to_name(details.getName())); + } catch (Exception e) { + handleException(e); + } + } + + public Object lookup(String uri) throws CorbaHostException { + Object result = null; + try { + CorbanameURL url = new CorbanameURL(uri); + ORB orb = createORB(url.getHost(), url.getPort(), false); + NamingContextExt context = getNamingContext(orb, url.getNameService()); + result = context.resolve_str(url.getName()); + } catch (Exception e) { + handleException(e); + } + if (result == null) { + throw new CorbaHostException(CorbaHostException.NO_SUCH_OBJECT); + } + return result; + } + + public ORB createORB(String host, int port, boolean server) throws CorbaHostException { + validatePort(port); + + String key = host + ":" + port; + ORB orb = orbs.get(key); + if (orb != null) { + return orb; + } + // Create an ORB object + Properties props = new Properties(); + props.putAll(System.getProperties()); + + String portStr = String.valueOf(port); + props.put("org.omg.CORBA.ORBServerId", "1000000"); + props.put("org.omg.CORBA.ORBInitialHost", host); + props.put("org.omg.CORBA.ORBInitialPort", portStr); + + // STEP 1: Set ORBPeristentServerPort property + // Set the proprietary property to open up a port to listen to + // INS requests. + + if (server) { + props.put("com.sun.CORBA.POA.ORBPersistentServerPort", portStr); + props.put("com.ibm.CORBA.ListenerPort", portStr); + props.put("gnu.CORBA.ListenerPort", portStr); + // props.put("org.omg.CORBA.ORBClass", + // "org.apache.yoko.orb.CORBA.ORB"); + // props.put("org.omg.CORBA.ORBSingletonClass", + // "org.apache.yoko.orb.CORBA.ORBSingleton"); + props.put("yoko.orb.oa.endpoint", "iiop --host " + host + " --port " + port); + props.put("yoko.orb.poamanager.TNameService.endpoint", "iiop --host " + host); + } + + String[] args = {"-ORBInitialHost", host, "-ORBInitialPort", "" + port}; + orb = ORB.init(args, props); + orbs.put(key, orb); + return orb; + } + + public void stop() { + for (ORB orb : orbs.values()) { + try { + orb.shutdown(true); + orb.destroy(); + } catch (Throwable e) { + logger.log(Level.SEVERE, e.getMessage(), e); + } + } + } + + /** + * 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/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator b/java/sca/modules/host-corba-jse/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator index 7bd1cf7d40..a96baed33e 100644 --- a/java/sca/modules/host-corba-jse/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator +++ b/java/sca/modules/host-corba-jse/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator @@ -15,4 +15,4 @@ # specific language governing permissions and limitations # under the License. -org.apache.tuscany.sca.host.corba.jdk.CorbaRuntimeModuleActivator \ No newline at end of file +org.apache.tuscany.sca.host.corba.jse.CorbaRuntimeModuleActivator \ No newline at end of file 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 new file mode 100644 index 0000000000..6b98f1c949 --- /dev/null +++ b/java/sca/modules/host-corba-jse/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCase.java @@ -0,0 +1,285 @@ +/* + * 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.assertEquals; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + +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; +import org.apache.tuscany.sca.host.corba.CorbaHostUtils; +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.apache.tuscany.sca.host.corba.testing.general.TestInterface; +import org.apache.tuscany.sca.host.corba.testing.general.TestInterfaceHelper; +import org.apache.tuscany.sca.host.corba.testing.servants.TestInterfaceServant; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +/** + * General tests + */ +public class DefaultCorbaHostTestCase { + + private static final String LOCALHOST = "localhost"; + private static final int DEFAULT_PORT = 11100; // 1050; + + private static CorbaHost host; + + private static TransientNameServer server; + + @BeforeClass + public static void start() { + try { + server = new TransientNameServer(LOCALHOST, DEFAULT_PORT, TransientNameService.DEFAULT_SERVICE_NAME); + Thread t = server.start(); + if (t == null) { + fail("The naming server cannot be started"); + } + host = new DefaultCorbaHost(); + } catch (Throwable e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + @AfterClass + public static void stop() { + server.stop(); + } + + /** + * Tests registering and lookup CORBA services + */ + @Test + public void test_registerServant() { + try { + String uri = CorbaHostUtils.createCorbanameURI(LOCALHOST, DEFAULT_PORT, "Nested/Test"); + TestInterface servant = new TestInterfaceServant(); + host.registerServant(uri, servant); + TestInterface ref = TestInterfaceHelper.narrow(host.lookup(uri)); + assertEquals(2, ref.getInt(2)); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + /** + * Tests unregistering servants + */ + @Test + public void test_unregisterServant() { + try { + String objName = "Unregistering/Test"; + String uri = CorbaHostUtils.createCorbanameURI(LOCALHOST, DEFAULT_PORT, objName); + TestInterface servant = new TestInterfaceServant(); + + // creating and releasing using corbaname URI + host.registerServant(uri, servant); + host.unregisterServant(uri); + host.registerServant(uri, servant); + host.unregisterServant(uri); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + /** + * Tests situation when name is already registered + */ + @Test + public void test_nameAlreadyRegistered() { + // test using URI + try { + TestInterface servant = new TestInterfaceServant(); + String uri = CorbaHostUtils.createCorbanameURI(LOCALHOST, DEFAULT_PORT, "AlreadyRegisteredTest2"); + host.registerServant(uri, servant); + host.registerServant(uri, servant); + fail(); + } catch (CorbaHostException e) { + assertTrue(e.getMessage().equals(CorbaHostException.BINDING_IN_USE)); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + /** + * Tests getting non existing reference + */ + @Test + public void test_getNonExistingObject() { + // try to fetch object with corbaname URI + try { + host.lookup(CorbaHostUtils.createCorbanameURI("NonExistingOne", DEFAULT_PORT, LOCALHOST)); + fail(); + } catch (CorbaHostException e) { + // The message is JDK-specific + // assertTrue(e.getMessage().equals(CorbaHostException.NO_SUCH_OBJECT)); + } catch (Exception e) { + // e.printStackTrace(); + fail(); + } + } + + /** + * Tests unregistering non existing reference + */ + @Test + public void test_unregisterNonExistentObject() { + // test using URI + try { + String uri = CorbaHostUtils.createCorbanameURI(LOCALHOST, DEFAULT_PORT, "NonExistingReference1"); + host.unregisterServant(uri); + fail(); + } catch (CorbaHostException e) { + assertTrue(e.getMessage().equals(CorbaHostException.NO_SUCH_OBJECT)); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + /** + * Tests registering under invalid host + */ + @Test + public void test_invalidHost() { + try { + String url = CorbaHostUtils.createCorbanameURI("not_" + LOCALHOST, DEFAULT_PORT, "Name"); + TestInterface servant = new TestInterfaceServant(); + host.registerServant(url, servant); + fail(); + } catch (CorbaHostException e) { + // Expected + } catch (Exception e) { + // e.printStackTrace(); + fail(e.getMessage()); + } + } + + /** + * Tests registering under invalid port + */ + @Test + public void test_invalidPort() { + try { + String url = CorbaHostUtils.createCorbanameURI(LOCALHOST, DEFAULT_PORT + 1, "Name"); + TestInterface servant = new TestInterfaceServant(); + host.registerServant(url, servant); + fail(); + } catch (CorbaHostException e) { + // Expected + } catch (Exception e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + /** + * Tests registering under invalid name + */ + @Test + @Ignore("SUN JDK 6 is happy with all kind of names") + public void test_invalidBindingName() { + try { + TestInterface servant = new TestInterfaceServant(); + host.registerServant("---", servant); + fail(); + } catch (CorbaHostException e) { + assertTrue(e.getMessage().equals(CorbaHostException.WRONG_NAME)); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + /** + * + */ + @Test + public void test_ensureORBStopped() { + try { + int innerORBPort = 11102; + TransientNameServer innerServer = + new TransientNameServer(LOCALHOST, innerORBPort, TransientNameService.DEFAULT_SERVICE_NAME); + innerServer.start(); + innerServer.stop(); + try { + Thread.sleep(500); + } catch (Exception e) { + } + new Socket(LOCALHOST, innerORBPort); + fail(); + } catch (Exception e) { + if (e instanceof ConnectException) { + assertTrue(true); + } else { + e.printStackTrace(); + } + } + } + + /** + * 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-jse/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCaseFIXME.java b/java/sca/modules/host-corba-jse/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCaseFIXME.java deleted file mode 100644 index e4548ce021..0000000000 --- a/java/sca/modules/host-corba-jse/src/test/java/org/apache/tuscany/sca/host/corba/testing/DefaultCorbaHostTestCaseFIXME.java +++ /dev/null @@ -1,283 +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.host.corba.testing; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertTrue; -import static junit.framework.Assert.fail; - -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; -import org.apache.tuscany.sca.host.corba.CorbaHostUtils; -import org.apache.tuscany.sca.host.corba.jdk.DefaultCorbaHost; -import org.apache.tuscany.sca.host.corba.naming.TransientNameServer; -import org.apache.tuscany.sca.host.corba.naming.TransientNameService; -import org.apache.tuscany.sca.host.corba.testing.general.TestInterface; -import org.apache.tuscany.sca.host.corba.testing.general.TestInterfaceHelper; -import org.apache.tuscany.sca.host.corba.testing.servants.TestInterfaceServant; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -/** - * General tests - */ -public class DefaultCorbaHostTestCaseFIXME { - - private static final String LOCALHOST = "localhost"; - private static final int DEFAULT_PORT = 11100; // 1050; - - private static CorbaHost host; - - private static TransientNameServer server; - - @BeforeClass - public static void start() { - try { - server = new TransientNameServer(LOCALHOST, DEFAULT_PORT, TransientNameService.DEFAULT_SERVICE_NAME); - Thread t = server.start(); - if (t == null) { - fail("The naming server cannot be started"); - } - host = new DefaultCorbaHost(); - } catch (Throwable e) { - e.printStackTrace(); - fail(e.getMessage()); - } - } - - @AfterClass - public static void stop() { - server.stop(); - } - - /** - * Tests registering and lookup CORBA services - */ - @Test - public void test_registerServant() { - try { - String uri = CorbaHostUtils.createCorbanameURI(LOCALHOST, DEFAULT_PORT, "Nested/Test"); - TestInterface servant = new TestInterfaceServant(); - host.registerServant(uri, servant); - TestInterface ref = TestInterfaceHelper.narrow(host.lookup(uri)); - assertEquals(2, ref.getInt(2)); - } catch (Exception e) { - e.printStackTrace(); - fail(); - } - } - - /** - * Tests unregistering servants - */ - @Test - public void test_unregisterServant() { - try { - String objName = "Unregistering/Test"; - String uri = CorbaHostUtils.createCorbanameURI(LOCALHOST, DEFAULT_PORT, objName); - TestInterface servant = new TestInterfaceServant(); - - // creating and releasing using corbaname URI - host.registerServant(uri, servant); - host.unregisterServant(uri); - host.registerServant(uri, servant); - host.unregisterServant(uri); - } catch (Exception e) { - e.printStackTrace(); - fail(); - } - } - - /** - * Tests situation when name is already registered - */ - @Test - public void test_nameAlreadyRegistered() { - // test using URI - try { - TestInterface servant = new TestInterfaceServant(); - String uri = CorbaHostUtils.createCorbanameURI(LOCALHOST, DEFAULT_PORT, "AlreadyRegisteredTest2"); - host.registerServant(uri, servant); - host.registerServant(uri, servant); - fail(); - } catch (CorbaHostException e) { - assertTrue(e.getMessage().equals(CorbaHostException.BINDING_IN_USE)); - } catch (Exception e) { - e.printStackTrace(); - fail(); - } - } - - /** - * Tests getting non existing reference - */ - @Test - public void test_getNonExistingObject() { - // try to fetch object with corbaname URI - try { - host.lookup(CorbaHostUtils.createCorbanameURI("NonExistingOne", DEFAULT_PORT, LOCALHOST)); - fail(); - } catch (CorbaHostException e) { - // The message is JDK-specific - // assertTrue(e.getMessage().equals(CorbaHostException.NO_SUCH_OBJECT)); - } catch (Exception e) { - // e.printStackTrace(); - fail(); - } - } - - /** - * Tests unregistering non existing reference - */ - @Test - public void test_unregisterNonExistentObject() { - // test using URI - try { - String uri = CorbaHostUtils.createCorbanameURI(LOCALHOST, DEFAULT_PORT, "NonExistingReference1"); - host.unregisterServant(uri); - fail(); - } catch (CorbaHostException e) { - assertTrue(e.getMessage().equals(CorbaHostException.NO_SUCH_OBJECT)); - } catch (Exception e) { - e.printStackTrace(); - fail(); - } - } - - /** - * Tests registering under invalid host - */ - @Test - public void test_invalidHost() { - try { - String url = CorbaHostUtils.createCorbanameURI("not_" + LOCALHOST, DEFAULT_PORT, "Name"); - TestInterface servant = new TestInterfaceServant(); - host.registerServant(url, servant); - fail(); - } catch (CorbaHostException e) { - // Expected - } catch (Exception e) { - // e.printStackTrace(); - fail(e.getMessage()); - } - } - - /** - * Tests registering under invalid port - */ - @Test - public void test_invalidPort() { - try { - String url = CorbaHostUtils.createCorbanameURI(LOCALHOST, DEFAULT_PORT + 1, "Name"); - TestInterface servant = new TestInterfaceServant(); - host.registerServant(url, servant); - fail(); - } catch (CorbaHostException e) { - // Expected - } catch (Exception e) { - e.printStackTrace(); - fail(e.getMessage()); - } - } - - /** - * Tests registering under invalid name - */ - @Test - @Ignore("SUN JDK 6 is happy with all kind of names") - public void test_invalidBindingName() { - try { - TestInterface servant = new TestInterfaceServant(); - host.registerServant("---", servant); - fail(); - } catch (CorbaHostException e) { - assertTrue(e.getMessage().equals(CorbaHostException.WRONG_NAME)); - } catch (Exception e) { - e.printStackTrace(); - fail(); - } - } - - /** - * - */ - @Test - public void test_ensureORBStopped() { - try { - int innerORBPort = 11102; - TransientNameServer innerServer = - new TransientNameServer(LOCALHOST, innerORBPort, TransientNameService.DEFAULT_SERVICE_NAME); - innerServer.start(); - innerServer.stop(); - try { - Thread.sleep(500); - } catch (Exception e) { - } - new Socket(LOCALHOST, innerORBPort); - fail(); - } catch (Exception e) { - if (e instanceof ConnectException) { - assertTrue(true); - } else { - e.printStackTrace(); - } - } - } - - /** - * 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); - } - } -} -- cgit v1.2.3