View Javadoc
1 /* 2 * Copyright (C) The Apache Software Foundation. All rights reserved. 3 * 4 * This software is published under the terms of the Apache Software License 5 * version 1.1, a copy of which has been included with this distribution in 6 * the LICENSE file. 7 */ 8 package org.apache.avalon.excalibur.naming.rmi.server; 9 10 import java.io.IOException; 11 import java.io.ObjectOutputStream; 12 import java.net.ServerSocket; 13 import java.net.Socket; 14 import java.rmi.MarshalledObject; 15 import java.rmi.server.UnicastRemoteObject; 16 import org.apache.avalon.excalibur.naming.DefaultNameParser; 17 import org.apache.avalon.excalibur.naming.DefaultNamespace; 18 import org.apache.avalon.excalibur.naming.memory.MemoryContext; 19 import org.apache.avalon.excalibur.naming.memory.MemoryInitialContextFactory; 20 21 /*** 22 * This is a simple test name server and should NOT be used in a production system. 23 * 24 */ 25 public class Main 26 implements Runnable 27 { 28 public static void main( final String[] args ) 29 throws Exception 30 { 31 final Main main = new Main(); 32 main.start(); 33 main.accept(); 34 } 35 36 protected boolean m_debug = true; 37 protected RMINamingProviderImpl m_server; 38 protected ServerSocket m_serverSocket; 39 protected MarshalledObject m_serverStub; 40 protected boolean m_running; 41 protected boolean m_initialized; 42 43 public void init() 44 throws Exception 45 { 46 if( m_initialized ) return; 47 48 try 49 { 50 if( m_debug ) System.out.println( "Starting server on port " + 1977 ); 51 m_serverSocket = new ServerSocket( 1977 ); 52 m_initialized = true; 53 } 54 catch( final IOException ioe ) 55 { 56 if( m_debug ) System.out.println( "Failed starting server" ); 57 throw ioe; 58 } 59 } 60 61 public void start() 62 throws Exception 63 { 64 init(); 65 export(); 66 } 67 68 public void export() 69 throws Exception 70 { 71 final DefaultNameParser parser = new DefaultNameParser(); 72 final DefaultNamespace namespace = new DefaultNamespace( parser ); 73 final MemoryContext context = new MemoryContext( namespace, null, null ); 74 m_server = new RMINamingProviderImpl( context ); 75 76 // Start listener 77 try 78 { 79 // Export server 80 if( m_debug ) System.out.println( "Exporting RMI object on port " + 1099 ); 81 m_serverStub = 82 new MarshalledObject( UnicastRemoteObject.exportObject( m_server, 1099 ) ); 83 } 84 catch( final IOException ioe ) 85 { 86 if( m_debug ) System.out.println( "Failed exporting object" ); 87 throw ioe; 88 } 89 } 90 91 public void dispose() 92 throws Exception 93 { 94 if( m_debug ) System.out.println( "Shutting down server" ); 95 m_running = false; 96 final ServerSocket serverSocket = m_serverSocket; 97 m_serverSocket = null; 98 serverSocket.close(); 99 if( m_debug ) System.out.println( "Server shutdown" ); 100 } 101 102 public void stop() 103 throws Exception 104 { 105 if( m_debug ) System.out.println( "Stopping" ); 106 m_running = false; 107 if( m_debug ) System.out.println( "Unexporting object" ); 108 UnicastRemoteObject.unexportObject( m_server, true ); 109 m_serverStub = null; 110 if( m_debug ) System.out.println( "Server stopped" ); 111 } 112 113 public void accept() 114 { 115 m_running = true; 116 while( m_running ) 117 { 118 // Accept a connection 119 try 120 { 121 final Socket socket = m_serverSocket.accept(); 122 if( m_debug ) System.out.println( "Accepted Connection" ); 123 final ObjectOutputStream output = 124 new ObjectOutputStream( socket.getOutputStream() ); 125 126 output.writeObject( m_serverStub ); 127 128 socket.close(); 129 } 130 catch( final IOException ioe ) 131 { 132 if( false == m_running ) break; 133 ioe.printStackTrace(); 134 } 135 } 136 } 137 138 public void run() 139 { 140 accept(); 141 } 142 }

This page was automatically generated by Maven