KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > spice > jndikit > rmi > test > RMITestSetup


1 /*
2  * Copyright (C) The Spice Group. All rights reserved.
3  *
4  * This software is published under the terms of the Spice
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

8 package org.codehaus.spice.jndikit.rmi.test;
9
10 import java.util.Hashtable JavaDoc;
11 import java.util.Random JavaDoc;
12 import javax.naming.Context JavaDoc;
13 import javax.naming.spi.InitialContextFactory JavaDoc;
14
15 import org.codehaus.spice.jndikit.rmi.RMIInitialContextFactory;
16 import org.codehaus.spice.jndikit.rmi.server.Main;
17
18 /**
19  * Helper for setting up and tearing down the RMI naming provider.
20  *
21  * @author Peter Donald
22  * @author Tim Anderson
23  * @version $Revision: 1.1 $ $Date: 2005/06/30 04:22:16 $
24  */

25 public class RMITestSetup
26 {
27
28     private Main m_server;
29     private Thread JavaDoc m_serverThread;
30     private static final Random JavaDoc RANDOM = new Random JavaDoc();
31     private int m_port;
32     private final InitialContextFactory JavaDoc 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 JavaDoc
46     {
47         m_port = 1500 + Math.abs( RANDOM.nextInt() % 1000 );
48
49         startServer();
50     }
51
52     public Context JavaDoc getRoot() throws Exception JavaDoc
53     {
54         final Hashtable JavaDoc environment = new Hashtable JavaDoc();
55         environment.put( Context.PROVIDER_URL, "rmi://localhost:" + m_port );
56         final Context JavaDoc root = m_factory.getInitialContext( environment );
57         return root;
58     }
59
60     public void tearDown()
61         throws Exception JavaDoc
62     {
63         try
64         {
65             stopServer();
66         }
67         catch( Exception JavaDoc e )
68         {
69             e.printStackTrace();
70         }
71     }
72
73     private void startServer()
74         throws Exception JavaDoc
75     {
76         m_server = new Main( true, m_port );
77         m_server.start();
78
79         m_serverThread = new Thread JavaDoc( 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 JavaDoc
89     {
90         m_server.stop();
91         m_server.dispose();
92         m_serverThread.interrupt();
93     }
94
95
96 }
97
Popular Tags