Author: athimel Date: 2011-01-19 17:45:00 +0100 (Wed, 19 Jan 2011) New Revision: 2021 Url: http://nuiton.org/repositories/revision/nuiton-utils/2021 Log: Evolution #1209 : Add a method to unregister a service ; Implement tests Added: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/ trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/AnyException.java trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/RmiExporterAndProxyTest.java trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestBean.java trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestService.java trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestServiceImpl.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/RemoteProxyFactory.java trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/ServiceExporter.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/RemoteProxyFactory.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/RemoteProxyFactory.java 2011-01-18 07:06:31 UTC (rev 2020) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/RemoteProxyFactory.java 2011-01-19 16:45:00 UTC (rev 2021) @@ -1,23 +1,23 @@ /* * #%L * Nuiton Utils - * * + * * $Id$ * $HeadURL$ * %% * Copyright (C) 2004 - 2010 CodeLutin * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * <http://www.gnu.org/licenses/lgpl-3.0.html>. * #L% @@ -47,7 +47,7 @@ // TODO AThimel 12/01/2011 This settings has to be externalized protected final static int PORT = 12345; - protected final static String REGISTRY_IP = "10.1.1.85"; + protected final static String REGISTRY_IP = "127.0.0.1"; /** * Create a RMI proxy on the wanted service interface. The default RMI name Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/ServiceExporter.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/ServiceExporter.java 2011-01-18 07:06:31 UTC (rev 2020) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/ServiceExporter.java 2011-01-19 16:45:00 UTC (rev 2021) @@ -1,23 +1,23 @@ /* * #%L * Nuiton Utils - * * + * * $Id$ * $HeadURL$ * %% * Copyright (C) 2004 - 2010 CodeLutin * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * <http://www.gnu.org/licenses/lgpl-3.0.html>. * #L% @@ -28,6 +28,7 @@ import org.apache.commons.logging.LogFactory; import java.rmi.ConnectException; +import java.rmi.NotBoundException; import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; @@ -41,7 +42,7 @@ * * @author Arnaud Thimel <thimel@codelutin.com> */ -public class ServiceExporter { +public final class ServiceExporter { private final static Log log = LogFactory.getLog(ServiceExporter.class); @@ -51,7 +52,7 @@ /** * Does some checks on RMI configuration */ - protected void testRmiConfig() { + protected static void testRmiConfig() { String rmiHost = System.getProperty("java.rmi.server.hostname"); if ((rmiHost == null || "".equals(rmiHost.trim())) && log.isWarnEnabled()) { @@ -67,7 +68,7 @@ * @return the registry found or created * @throws RemoteException in case it is not possible to get the registry */ - protected Registry getRegistry() throws RemoteException { + protected static Registry getRegistry() throws RemoteException { Registry result; try { result = LocateRegistry.getRegistry(PORT); @@ -92,7 +93,7 @@ * @param <E> some interface class * @throws RemoteException in case the registry is not reachable */ - public <E> void registerService(Class<E> serviceInterface, E instance) + public static <E> void registerService(Class<E> serviceInterface, E instance) throws RemoteException { String rmiName = serviceInterface.getName(); registerService(rmiName, instance); @@ -106,7 +107,7 @@ * @param <E> some interface class * @throws RemoteException in case the registry is not reachable */ - public <E> void registerService(String rmiName, E instance) + public static <E> void registerService(String rmiName, E instance) throws RemoteException { testRmiConfig(); @@ -121,4 +122,33 @@ registry.rebind(rmiName, stub); } + /** + * Will unregister a service using the default name. + * + * @param serviceInterface the interface used to unbind the service. The RMI + * name will be generated from this class name + * @throws RemoteException in case the registry is not reachable + * @throws NotBoundException in case the given name is not bound + */ + public static void unregisterService(Class<?> serviceInterface) + throws RemoteException, NotBoundException { + String rmiName = serviceInterface.getName(); + unregisterService(rmiName); + } + + /** + * Will unregister a service using the given RMI name. + * + * @param rmiName the RMI name used to unbind the service in the registry + * @throws RemoteException in case the registry is not reachable + * @throws NotBoundException in case the given name is not bound + */ + public static void unregisterService(String rmiName) + throws RemoteException, NotBoundException { + + // Bind into the registry + Registry registry = getRegistry(); + registry.unbind(rmiName); + } + } Added: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/AnyException.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/AnyException.java (rev 0) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/AnyException.java 2011-01-19 16:45:00 UTC (rev 2021) @@ -0,0 +1,36 @@ +/* + * #%L + * Nuiton Utils + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.util.rmi; + +/** + * @author Arnaud Thimel <thimel@codelutin.com> + */ +public class AnyException extends Exception { + + public AnyException(String message, Throwable cause) { + super(message, cause); + } + +} Property changes on: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/AnyException.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/RmiExporterAndProxyTest.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/RmiExporterAndProxyTest.java (rev 0) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/RmiExporterAndProxyTest.java 2011-01-19 16:45:00 UTC (rev 2021) @@ -0,0 +1,118 @@ +package org.nuiton.util.rmi; + +import junit.framework.TestCase; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.rmi.NotBoundException; +import java.security.InvalidParameterException; + +/** + * @author Arnaud Thimel <thimel@codelutin.com> + */ +public class RmiExporterAndProxyTest { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(RmiExporterAndProxyTest.class); + + @Test(expected = NullPointerException.class) + public void testExportNullService() throws Exception { + + // It is mandatory to provide a service instance + ServiceExporter.registerService(TestService.class, null); + } + + @Test(expected = NotBoundException.class) + public void testProxyWithoutService() throws Exception { + + // Register and unregister (to make sure RMI registry is created) + TestServiceImpl impl = new TestServiceImpl(); + ServiceExporter.registerService(TestService.class, impl); + ServiceExporter.unregisterService(TestService.class); + + // This will throw an exception because service is not registered + RemoteProxyFactory.createProxy(TestService.class); + } + + @Test + public void testObjectIdentity() throws Exception { + + // Create and bind the service + TestServiceImpl impl = new TestServiceImpl(); + int realServiceIdentity = System.identityHashCode(impl); + ServiceExporter.registerService(TestService.class, impl); + + // Get a proxy on this service and make sure this is not the same object + TestService clientSide = RemoteProxyFactory.createProxy(TestService.class); + int proxyIdentity = System.identityHashCode(clientSide); + Assert.assertNotSame(proxyIdentity, realServiceIdentity); + + // Get the remote identifier and compare + int serviceIdentityFromProxy = clientSide.getInstanceId(); + Assert.assertEquals(realServiceIdentity, serviceIdentityFromProxy); + + ServiceExporter.unregisterService(TestService.class); + } + + @Test + public void testComplexType() throws Exception { + + // Create and bind the service + TestServiceImpl impl = new TestServiceImpl(); + int realServiceIdentity = System.identityHashCode(impl); + ServiceExporter.registerService(TestService.class, impl); + + // Get a proxy on this service and do the call to createComplexTypeObject + TestService clientSide = RemoteProxyFactory.createProxy(TestService.class); + TestBean bean = clientSide.createComplexTypeObject(); + + // Now check that the bean is exactly as expected + TestBean father = bean.getFather(); + Assert.assertNotNull(father); + Assert.assertEquals("I'm your father !", father.getMessage()); + Assert.assertEquals(realServiceIdentity, father.getCreatedBy()); + Assert.assertNull(father.getFather()); + Assert.assertNull(father.getCreatedOn()); // transient value + Assert.assertEquals(0L, father.getNumber()); // transient value + + Assert.assertEquals("Son", bean.getMessage()); + Assert.assertEquals(realServiceIdentity, bean.getCreatedBy()); + Assert.assertNull(bean.getCreatedOn()); // transient value + Assert.assertEquals(0L, bean.getNumber()); // transient value + + ServiceExporter.unregisterService(TestService.class); + } + + @Test + public void testExceptionPropagation() throws Exception { + + // Create and bind the service + TestServiceImpl impl = new TestServiceImpl(); + ServiceExporter.registerService(TestService.class, impl); + + // Get a proxy on this service and do the call to createComplexTypeObject + TestService clientSide = RemoteProxyFactory.createProxy(TestService.class); + + // Does not fail + clientSide.throwAnExceptionIfFalse(true); + + try { + clientSide.throwAnExceptionIfFalse(false); + Assert.fail("An axeption should have been thrown"); + } catch (AnyException ae) { + + Assert.assertEquals("Please provide 'true'", ae.getMessage()); + + // Exception is the good one. Now check its cause + InvalidParameterException cause = (InvalidParameterException)ae.getCause(); + Assert.assertEquals("Wrong parameter !", cause.getMessage()); + } + + ServiceExporter.unregisterService(TestService.class); + } + +} Added: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestBean.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestBean.java (rev 0) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestBean.java 2011-01-19 16:45:00 UTC (rev 2021) @@ -0,0 +1,88 @@ +/* + * #%L + * Nuiton Utils + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.util.rmi; + +import java.io.Serializable; +import java.util.Date; + +/** + * @author Arnaud Thimel <thimel@codelutin.com> + */ +public class TestBean implements Serializable { + + protected TestBean father; + protected String message; + protected int createdBy; + protected transient Date createdOn; + protected transient long number; + + public TestBean(String message, Object creator, long number) { + this.message = message; + this.createdBy = System.identityHashCode(creator); + this.createdOn = new Date(); + this.number = number; + } + + public TestBean getFather() { + return father; + } + + public void setFather(TestBean father) { + this.father = father; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public int getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(int createdBy) { + this.createdBy = createdBy; + } + + public Date getCreatedOn() { + return createdOn; + } + + public void setCreatedOn(Date createdOn) { + this.createdOn = createdOn; + } + + public long getNumber() { + return number; + } + + public void setNumber(long number) { + this.number = number; + } + +} Property changes on: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestBean.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestService.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestService.java (rev 0) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestService.java 2011-01-19 16:45:00 UTC (rev 2021) @@ -0,0 +1,57 @@ +/* + * #%L + * Nuiton Utils + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.util.rmi; + +/** + * Any service interface which will be used to test the RMI proxy. + * + * @author Arnaud Thimel <thimel@codelutin.com> + */ +public interface TestService { + + /** + * This method will return a String that can only be known by itself + * + * @return an instance identifier + */ + int getInstanceId(); + + /** + * This method will return a complex object to validate that this is not + * working only with primitive types + * + * @return a newly created TestBean + */ + TestBean createComplexTypeObject(); + + /** + * Will throw an AnyException to test exception propagation + * + * @param bool true : will not fail, false will fail + * @throws AnyException if the given boolean is false + */ + void throwAnExceptionIfFalse(Boolean bool) throws AnyException; + +} Property changes on: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestServiceImpl.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestServiceImpl.java (rev 0) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestServiceImpl.java 2011-01-19 16:45:00 UTC (rev 2021) @@ -0,0 +1,57 @@ +/* + * #%L + * Nuiton Utils + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.util.rmi; + +import com.sun.javaws.exceptions.InvalidArgumentException; + +import java.security.InvalidParameterException; + +/** + * @author Arnaud Thimel <thimel@codelutin.com> + */ +public class TestServiceImpl implements TestService{ + + @Override + public int getInstanceId() { + return System.identityHashCode(this); + } + + @Override + public TestBean createComplexTypeObject() { + TestBean father = new TestBean("I'm your father !", this, 123); + TestBean result = new TestBean("Son", this, 456); + result.setFather(father); + return result; + } + + @Override + public void throwAnExceptionIfFalse(Boolean bool) throws AnyException { + if (!bool) { + InvalidParameterException ipe = new InvalidParameterException("Wrong parameter !"); + throw new AnyException("Please provide 'true'", ipe); + } + } + +} Property changes on: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/TestServiceImpl.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL
participants (1)
-
athimelï¼ users.nuiton.org