KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > spice > jndikit > rmi > server > Main


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.server;
9
10 import java.io.IOException JavaDoc;
11 import java.io.InterruptedIOException JavaDoc;
12 import java.io.ObjectOutputStream JavaDoc;
13 import java.net.ServerSocket JavaDoc;
14 import java.net.Socket JavaDoc;
15 import java.rmi.MarshalledObject JavaDoc;
16 import java.rmi.Remote JavaDoc;
17 import java.rmi.server.UnicastRemoteObject JavaDoc;
18 import org.codehaus.spice.jndikit.DefaultNameParser;
19 import org.codehaus.spice.jndikit.DefaultNamespace;
20 import org.codehaus.spice.jndikit.memory.MemoryContext;
21
22 /**
23  * This is a simple test name server and should NOT be used in a
24  * production system.
25  *
26  * @author Peter Donald
27  * @version $Revision: 1.2 $
28  */

29 public class Main
30     implements Runnable JavaDoc
31 {
32     //Config settings
33
private final boolean m_debug;
34     private final int m_port;
35
36     //Runtime flags
37
private boolean m_isRunning;
38     private boolean m_isInitialized;
39
40     //Server facet
41
private RMINamingProviderImpl m_server;
42     private ServerSocket JavaDoc m_serverSocket;
43     private MarshalledObject JavaDoc m_serverStub;
44
45     public Main( final boolean debug,
46                  final int port )
47     {
48         m_debug = debug;
49         m_port = port;
50     }
51
52     public void init()
53         throws Exception JavaDoc
54     {
55         if( m_isInitialized )
56         {
57             return;
58         }
59
60         try
61         {
62             m_serverSocket = new ServerSocket JavaDoc( m_port );
63             m_serverSocket.setSoTimeout( 100 );
64             debug( "Started server on port " + m_serverSocket.getLocalPort() );
65             m_isInitialized = true;
66         }
67         catch( final IOException JavaDoc ioe )
68         {
69             debug( "Failed starting server" );
70             throw ioe;
71         }
72     }
73
74     public void start()
75         throws Exception JavaDoc
76     {
77         init();
78         export();
79     }
80
81     public void export()
82         throws Exception JavaDoc
83     {
84         final DefaultNameParser parser = new DefaultNameParser();
85         final DefaultNamespace namespace = new DefaultNamespace( parser );
86         final MemoryContext context =
87             new MemoryContext( namespace, null, null );
88         m_server = new RMINamingProviderImpl( context );
89
90         // Start listener
91
try
92         {
93             // Export server
94
debug( "Exporting RMI object." );
95             final Remote JavaDoc remote =
96                 UnicastRemoteObject.exportObject( m_server );
97             m_serverStub = new MarshalledObject JavaDoc( remote );
98         }
99         catch( final IOException JavaDoc ioe )
100         {
101             debug( "Failed exporting object" );
102             ioe.printStackTrace();
103             throw ioe;
104         }
105     }
106
107     public void run()
108     {
109         accept();
110     }
111
112     public void dispose()
113         throws Exception JavaDoc
114     {
115         debug( "Shutting down server" );
116         m_isRunning = false;
117         final ServerSocket JavaDoc serverSocket = m_serverSocket;
118         m_serverSocket = null;
119         serverSocket.close();
120         debug( "Server shutdown" );
121     }
122
123     public void stop()
124         throws Exception JavaDoc
125     {
126         debug( "Stopping" );
127         m_isRunning = false;
128         debug( "Unexporting object" );
129         UnicastRemoteObject.unexportObject( m_server, true );
130         m_serverStub = null;
131         debug( "Server stopped" );
132     }
133
134     public boolean isRunning()
135     {
136         return m_isRunning;
137     }
138
139     public void accept()
140     {
141         m_isRunning = true;
142         while( m_isRunning )
143         {
144             // Accept a connection
145
try
146             {
147                 final Socket JavaDoc socket = m_serverSocket.accept();
148                 debug( "Accepted Connection" );
149                 final ObjectOutputStream JavaDoc output =
150                     new ObjectOutputStream JavaDoc( socket.getOutputStream() );
151
152                 output.writeObject( m_serverStub );
153
154                 socket.close();
155             }
156             catch( final InterruptedIOException JavaDoc iioe )
157             {
158                 continue;
159             }
160             catch( final IOException JavaDoc ioe )
161             {
162                 if( !m_isRunning )
163                 {
164                     break;
165                 }
166                 ioe.printStackTrace();
167             }
168         }
169     }
170
171     private void debug( final String JavaDoc message )
172     {
173         if( m_debug )
174         {
175             System.out.println( "RNC: " + message );
176         }
177     }
178 }
179
Popular Tags