1 7 package org.jboss.remoting.transport.rmi; 8 9 import org.apache.log4j.Level; 10 import org.jboss.logging.Logger; 11 import org.jboss.remoting.AbstractInvokerTest; 12 import org.jboss.remoting.InvokerLocator; 13 import org.jboss.remoting.ServerInvocationHandler; 14 import org.jboss.remoting.transport.Connector; 15 import org.jboss.remoting.transport.mock.MockServerInvocationHandler; 16 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Random ; 21 import java.util.Set ; 22 23 24 29 public class RMIInvokerServerTest extends AbstractInvokerTest 30 { 31 private static final Logger log = Logger.getLogger(RMIInvokerServerTest.class); 32 33 public RMIInvokerServerTest(String name) 34 { 35 super(name); 36 } 37 38 public RMIInvokerServerTest(int instances) 39 { 40 super(RMIInvokerServerTest.class.getName(), instances); 41 } 42 43 public RMIInvokerServerTest(String transport, int port) 44 { 45 super(RMIInvokerServerTest.class.getName(), transport, port); 46 } 47 48 public RMIInvokerServerTest(String transport, int port, int instances) 49 { 50 super(RMIInvokerServerTest.class.getName(), transport, port, instances); 51 } 52 53 public void init(Map metatdata) throws Exception 54 { 55 if(port < 0) 56 { 57 port = Math.abs(new Random ().nextInt(2000)); 58 } 59 log.debug("port = " + port); 60 61 Connector connector = new Connector(); 64 InvokerLocator locator = new InvokerLocator(buildLocatorURI(metatdata)); 65 connector.setInvokerLocator(locator.getLocatorURI()); 66 connector.start(); 67 connector.addInvocationHandler(getSubsystem(), getServerInvocationHandler()); 68 } 69 70 private String buildLocatorURI(Map metadata) 71 { 72 if(metadata == null || metadata.size() == 0) 73 { 74 return getTransport() + "://localhost:" + port; 75 } 76 else 77 { 78 StringBuffer uriBuffer = new StringBuffer (getTransport() + "://localhost:" + port + "/?"); 79 80 Set keys = metadata.keySet(); 81 Iterator itr = keys.iterator(); 82 while(itr.hasNext()) 83 { 84 String key = (String ) itr.next(); 85 String value = (String ) metadata.get(key); 86 uriBuffer.append(key + "=" + value + ","); 87 } 88 return uriBuffer.substring(0, uriBuffer.length() - 1); 89 } 90 } 91 92 protected String getSubsystem() 93 { 94 return "mock"; 95 } 96 97 protected ServerInvocationHandler getServerInvocationHandler() 98 { 99 return new MockServerInvocationHandler(); 100 } 101 102 public void serverTest(Map metadata) throws Exception 103 { 104 try 105 { 106 init(metadata); 107 startup(getNumberOfInstances()); 108 shutdown(); 109 } 110 catch(Exception e) 111 { 112 log.error("Error in serverTest() of " + this.getClass().getName(), e); 113 throw e; 114 } 115 } 116 117 public static void main(String [] args) 118 { 119 org.apache.log4j.BasicConfigurator.configure(); 120 org.apache.log4j.Category.getRoot().setLevel(Level.INFO); 121 org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.INFO); 122 org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG); 123 org.apache.log4j.Category.getInstance("test").setLevel(Level.DEBUG); 124 125 126 Map metadata = new HashMap (); 127 int port = RMIServerInvoker.DEFAULT_REGISTRY_PORT - 1; 128 String transport = "rmi"; 129 130 RMIInvokerServerTest server = null; 131 if(args.length == 1) 132 { 133 port = Integer.parseInt(args[0]); 134 server = new RMIInvokerServerTest(transport, port); 135 } 136 else if(args.length == 2) 137 { 138 int instances = Integer.parseInt(args[0]); 139 port = Integer.parseInt(args[1]); 140 server = new RMIInvokerServerTest(transport, port, instances); 141 142 } 143 else 144 { 145 server = new RMIInvokerServerTest(transport, port); 146 System.out.println("Using default transport (" + server.getTransport() + 147 ") and default port (" + server.getPort() + ") and " + 148 "default number of instances (" + server.getNumberOfInstances() + ")" + 149 "\nCan enter port and instances via command line."); 150 } 151 152 metadata.put(RMIServerInvoker.REGISTRY_PORT_KEY, String.valueOf(port + 1)); 153 154 try 155 { 156 server.serverTest(metadata); 157 } 158 catch(Exception e) 159 { 160 e.printStackTrace(); 161 System.exit(1); 162 } 163 System.exit(0); 164 } 165 166 } 167 | Popular Tags |