1 8 package org.codehaus.spice.jndikit.rmi.test; 9 10 import java.util.Hashtable ; 11 import java.util.Random ; 12 import javax.naming.Context ; 13 import javax.naming.spi.InitialContextFactory ; 14 15 import org.codehaus.spice.jndikit.rmi.RMIInitialContextFactory; 16 import org.codehaus.spice.jndikit.rmi.server.Main; 17 18 25 public class RMITestSetup 26 { 27 28 private Main m_server; 29 private Thread m_serverThread; 30 private static final Random RANDOM = new Random (); 31 private int m_port; 32 private final InitialContextFactory m_factory; 33 34 public RMITestSetup() 35 { 36 this( new RMIInitialContextFactory() ); 37 } 38 39 public RMITestSetup( RMIInitialContextFactory factory ) 40 { 41 m_factory = factory; 42 43 } 44 45 public void setUp() throws Exception 46 { 47 m_port = 1500 + Math.abs( RANDOM.nextInt() % 1000 ); 48 49 startServer(); 50 } 51 52 public Context getRoot() throws Exception 53 { 54 final Hashtable environment = new Hashtable (); 55 environment.put( Context.PROVIDER_URL, "rmi://localhost:" + m_port ); 56 final Context root = m_factory.getInitialContext( environment ); 57 return root; 58 } 59 60 public void tearDown() 61 throws Exception 62 { 63 try 64 { 65 stopServer(); 66 } 67 catch( Exception e ) 68 { 69 e.printStackTrace(); 70 } 71 } 72 73 private void startServer() 74 throws Exception 75 { 76 m_server = new Main( true, m_port ); 77 m_server.start(); 78 79 m_serverThread = new Thread ( m_server ); 80 m_serverThread.start(); 81 while( !m_server.isRunning() ) 82 { 83 Thread.yield(); 84 } 85 } 86 87 private void stopServer() 88 throws Exception 89 { 90 m_server.stop(); 91 m_server.dispose(); 92 m_serverThread.interrupt(); 93 } 94 95 96 } 97 | Popular Tags |