KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > jndi > registry > RMIFixedPortFirewallSocketFactory


1 /**
2  * Copyright (C) 2005 - Bull S.A.
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: RMIFixedPortFirewallSocketFactory.java,v 1.3 2005/03/15 09:55:03 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.carol.jndi.registry;
26
27 import java.io.IOException JavaDoc;
28 import java.net.ServerSocket JavaDoc;
29 import java.net.Socket JavaDoc;
30 import java.rmi.RemoteException JavaDoc;
31 import java.rmi.server.RMISocketFactory JavaDoc;
32
33 /**
34  * Socket factory allowing to use a fixed port instead of a random port (when
35  * it's 0). This is useful for firewall issues.
36  * @author Florent Benoit
37  */

38 public class RMIFixedPortFirewallSocketFactory extends RMISocketFactory JavaDoc {
39
40     /**
41      * Rmi socket factory instance
42      */

43     private static RMISocketFactory JavaDoc factory = null;
44
45     /**
46      * Server socket used for exported objects
47      */

48     private static ServerSocket JavaDoc exportedObjectfixedSocket = null;
49
50     /**
51      * port number for exporting Objects
52      */

53     private int exportedObjectsPort;
54
55     /**
56      * Constructor
57      * @param port exported objects port number
58      */

59     private RMIFixedPortFirewallSocketFactory(int port) {
60         super();
61         this.exportedObjectsPort = port;
62     }
63
64     /**
65      * Create a server socket on the specified port (port 0 indicates an
66      * anonymous port).
67      * @param port the port number
68      * @return the server socket on the specified port
69      * @exception IOException if an I/O error occurs during server socket
70      * creation
71      * @see java.rmi.server.RMISocketFactory#createServerSocket(int)
72      */

73     public ServerSocket JavaDoc createServerSocket(int port) throws IOException JavaDoc {
74         if (port == 0 && exportedObjectfixedSocket != null) {
75             return exportedObjectfixedSocket;
76         }
77         ServerSocket JavaDoc ss = new ServerSocket JavaDoc(port);
78         // Keep the socket for the exported object port
79
if (port == exportedObjectsPort) {
80             exportedObjectfixedSocket = ss;
81         }
82         return ss;
83     }
84
85     /**
86      * Creates a client socket connected to the specified host and port.
87      * @param host the host name
88      * @param port the port number
89      * @return a socket connected to the specified host and port.
90      * @exception IOException if an I/O error occurs during socket creation
91      * @see java.rmi.server.RMISocketFactory#createSocket(java.lang.String, int)
92      */

93     public Socket JavaDoc createSocket(String JavaDoc host, int port) throws IOException JavaDoc {
94         return new Socket JavaDoc(host, port);
95     }
96
97     /**
98      * Register the factory
99      * @return the factory which was created
100      * @param port given port number for exporting objects
101      * @throws RemoteException if the registration is not possible
102      */

103     public static RMISocketFactory JavaDoc register(int port) throws RemoteException JavaDoc {
104         if (factory == null) {
105             factory = new RMIFixedPortFirewallSocketFactory(port);
106
107             // Registring as default socket factory
108
try {
109                 RMISocketFactory.setSocketFactory(factory);
110
111             } catch (IOException JavaDoc ioe) {
112                 throw new RemoteException JavaDoc("Cannot set the default registry factory :", ioe);
113             }
114
115
116         }
117         return factory;
118     }
119
120 }
121
Popular Tags