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