1 8 package org.codehaus.spice.jndikit.rmi.server; 9 10 import java.io.IOException ; 11 import java.io.InterruptedIOException ; 12 import java.io.ObjectOutputStream ; 13 import java.net.ServerSocket ; 14 import java.net.Socket ; 15 import java.rmi.MarshalledObject ; 16 import java.rmi.Remote ; 17 import java.rmi.server.UnicastRemoteObject ; 18 import org.codehaus.spice.jndikit.DefaultNameParser; 19 import org.codehaus.spice.jndikit.DefaultNamespace; 20 import org.codehaus.spice.jndikit.memory.MemoryContext; 21 22 29 public class Main 30 implements Runnable 31 { 32 private final boolean m_debug; 34 private final int m_port; 35 36 private boolean m_isRunning; 38 private boolean m_isInitialized; 39 40 private RMINamingProviderImpl m_server; 42 private ServerSocket m_serverSocket; 43 private MarshalledObject 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 54 { 55 if( m_isInitialized ) 56 { 57 return; 58 } 59 60 try 61 { 62 m_serverSocket = new ServerSocket ( 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 ioe ) 68 { 69 debug( "Failed starting server" ); 70 throw ioe; 71 } 72 } 73 74 public void start() 75 throws Exception 76 { 77 init(); 78 export(); 79 } 80 81 public void export() 82 throws Exception 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 try 92 { 93 debug( "Exporting RMI object." ); 95 final Remote remote = 96 UnicastRemoteObject.exportObject( m_server ); 97 m_serverStub = new MarshalledObject ( remote ); 98 } 99 catch( final IOException 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 114 { 115 debug( "Shutting down server" ); 116 m_isRunning = false; 117 final ServerSocket serverSocket = m_serverSocket; 118 m_serverSocket = null; 119 serverSocket.close(); 120 debug( "Server shutdown" ); 121 } 122 123 public void stop() 124 throws Exception 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 try 146 { 147 final Socket socket = m_serverSocket.accept(); 148 debug( "Accepted Connection" ); 149 final ObjectOutputStream output = 150 new ObjectOutputStream ( socket.getOutputStream() ); 151 152 output.writeObject( m_serverStub ); 153 154 socket.close(); 155 } 156 catch( final InterruptedIOException iioe ) 157 { 158 continue; 159 } 160 catch( final IOException ioe ) 161 { 162 if( !m_isRunning ) 163 { 164 break; 165 } 166 ioe.printStackTrace(); 167 } 168 } 169 } 170 171 private void debug( final String message ) 172 { 173 if( m_debug ) 174 { 175 System.out.println( "RNC: " + message ); 176 } 177 } 178 } 179 | Popular Tags |