KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jacorb.orb.factory;
2
3 /*
4  * Written for JacORB - a free Java ORB
5  *
6  * Copyright (C) 2000-2004 Nicolas Noffke, 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.util.*;
30 import org.jacorb.orb.*;
31
32 public class PortRangeSocketFactory
33     extends PortRangeFactory
34     implements SocketFactory
35 {
36     public static final String JavaDoc MIN_PROP = "jacorb.net.socket_factory.port.min";
37     public static final String JavaDoc MAX_PROP = "jacorb.net.socket_factory.port.max";
38
39     private Logger logger;
40
41     public void configure(Configuration configuration)
42         throws ConfigurationException
43     {
44         this.configuration = (org.jacorb.config.Configuration)configuration;
45         logger = this.configuration.getNamedLogger("jacorb.orb.port_range_fcty");
46
47        // Get configured max and min port numbers
48
portMin = getPortProperty(MIN_PROP);
49         portMax = getPortProperty(MAX_PROP);
50
51         // Check min < max
52
if (portMin > portMax)
53         {
54             throw new ConfigurationException("PortRangeFactory: minimum port number not less than or equal to maximum");
55         }
56     }
57
58     public Socket createSocket (String JavaDoc host, int port)
59         throws IOException JavaDoc, UnknownHostException
60     {
61         int localPort;
62         InetAddress localHost = InetAddress.getLocalHost ();
63         Socket socket;
64
65         for (localPort = portMin; localPort <= portMax; localPort++)
66         {
67             try
68             {
69                 socket = new Socket (host, port, localHost, localPort);
70                 if (logger.isDebugEnabled())
71                     logger.debug("PortRangeSocketFactory: Created server socket at "
72                                  + ":" + localPort);
73
74                 return socket;
75             }
76             catch (IOException JavaDoc ex)
77             {
78                 // Ignore and continue
79
}
80         }
81
82         if (logger.isDebugEnabled())
83             logger.debug("Cannot bind socket between ports " + portMin + " and "
84                          + portMax + " to target " + host + ":" + port);
85
86         throw new BindException ("PortRangeSocketFactory: no free port between "
87                                  + portMin + " and " + portMax);
88     }
89
90     public boolean isSSL (Socket socket)
91     {
92         return false;
93     }
94 }
95
Popular Tags