KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > factory > PortRangeServerSocketFactory


1 package org.jacorb.orb.factory;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 2000-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.net.*;
24 import java.io.IOException JavaDoc;
25
26 import org.apache.avalon.framework.logger.Logger;
27 import org.apache.avalon.framework.configuration.*;
28
29 import org.jacorb.orb.*;
30
31 public class PortRangeServerSocketFactory
32     extends PortRangeFactory
33     implements ServerSocketFactory
34 {
35     public static final String JavaDoc MIN_PROP = "jacorb.net.server_socket_factory.port.min";
36     public static final String JavaDoc MAX_PROP = "jacorb.net.server_socket_factory.port.max";
37
38     private Logger logger;
39
40     public void configure(Configuration configuration)
41         throws ConfigurationException
42     {
43         this.configuration = (org.jacorb.config.Configuration)configuration;
44         logger = this.configuration.getNamedLogger("jacorb.orb.port_rang_fctry");
45
46        // Get configured max and min port numbers
47
portMin = getPortProperty(MIN_PROP);
48         portMax = getPortProperty(MAX_PROP);
49
50         // Check min < max
51
if (portMin > portMax)
52         {
53             throw new ConfigurationException("PortRangeFactory: minimum port number not less than or equal to maximum");
54         }
55     }
56
57     public ServerSocket createServerSocket (int port, int backlog)
58         throws IOException JavaDoc
59     {
60         int localPort;
61         ServerSocket socket;
62
63         for (localPort = portMin; localPort <= portMax; localPort++)
64         {
65             try
66             {
67                 socket = new ServerSocket (localPort, backlog);
68                 if (logger.isDebugEnabled())
69                     logger.debug("PortRangeServerSocketFactory: Created server socket at "
70                                  + ":" + localPort);
71                 return socket;
72             }
73             catch (IOException JavaDoc ex)
74             {
75                 // Ignore and continue
76
}
77         }
78
79         if (logger.isDebugEnabled())
80         {
81             logger.debug("Cannot create server socket between ports " +
82                          portMin + " and " + portMax);
83         }
84
85         throw new BindException ("PortRangeServerSocketFactory: no free port between "
86                                  + portMin + " and " + portMax);
87     }
88
89     public ServerSocket createServerSocket
90         (int port, int backlog, InetAddress ifAddress)
91         throws IOException JavaDoc
92     {
93         int localPort;
94         ServerSocket socket;
95
96         for (localPort = portMin; localPort <= portMax; localPort++)
97         {
98             try
99             {
100                 socket = new ServerSocket(localPort, backlog, ifAddress);
101                 if (logger.isDebugEnabled())
102                 {
103                     logger.debug("Created server socket at "
104                                  + ":" + localPort);
105                 }
106                 return socket;
107             }
108             catch (IOException JavaDoc ex)
109             {
110                 // Ignore and continue
111
}
112         }
113
114         if (logger.isDebugEnabled())
115         {
116             logger.debug("Cannot create server socket between ports " +
117                          portMin + " and " + portMax);
118         }
119
120         throw new BindException ("PortRangeServerSocketFactory: no free port between "
121                                  + portMin + " and " + portMax);
122     }
123
124     public ServerSocket createServerSocket(int port)
125         throws IOException JavaDoc
126     {
127         int localPort;
128         ServerSocket socket;
129
130         for (localPort = portMin; localPort <= portMax; localPort++)
131         {
132             try
133             {
134                 socket = new ServerSocket(localPort);
135                 if (logger.isDebugEnabled())
136                 {
137                     logger.debug("Created server socket at "
138                                  + ":" + localPort);
139                 }
140                 return socket;
141             }
142             catch (IOException JavaDoc ex)
143             {
144                 // Ignore and continue
145
}
146         }
147
148         if (logger.isDebugEnabled())
149         {
150             logger.debug("Cannot create server socket between ports " +
151                          portMin + " and " + portMax);
152         }
153         throw new BindException ("PortRangeServerSocketFactory: no free port between "
154                                  + portMin + " and " + portMax);
155     }
156 }
157
158
Popular Tags