KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > common > NetUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.common;
25
26 import java.util.Random JavaDoc;
27
28 import java.net.ServerSocket JavaDoc;
29 import java.io.IOException JavaDoc;
30 import com.sun.enterprise.admin.common.exception.PortInUseException;
31 import com.sun.enterprise.admin.util.Debug;
32
33 // i18n import
34
import com.sun.enterprise.admin.util.SOMLocalStringsManager;
35
36 /**
37 A utility class to check the port availability etc.
38 */

39
40
41 public class NetUtil
42 {
43     public static final int kMaxPortNo = 65535;
44     
45     /**
46         Static method to check whether the port provided is available to connect to.
47         Uses ServerSocket for this check.
48         @throws PortInUseException
49     */

50     
51     public static void checkPortAvailability(int port) throws PortInUseException
52     {
53         ServerSocket JavaDoc trialSocket = null;
54         try
55         {
56             trialSocket = new ServerSocket JavaDoc(port);
57         }
58         catch(IOException JavaDoc ioe)
59         {
60             String JavaDoc msg = localizedStrMgr.getString( "admin.common.port_in_use", new String JavaDoc( port + "" ) );
61             throw new PortInUseException( msg );
62         }
63         finally
64         {
65             try
66             {
67                 if (trialSocket != null)
68                 {
69                     trialSocket.close();
70                 }
71             }
72             catch(Exception JavaDoc e)
73             {
74                 Debug.printStackTrace(e);
75             }
76         }
77     }
78     
79     /**
80         Returns whether the given port number is available at the time of
81         call.
82      
83         @param portNo integer specifying the port.
84     */

85     
86     public static boolean isPortAvalable(int portNo)
87     {
88         boolean available = true;
89         
90         try
91         {
92             checkPortAvailability(portNo);
93         }
94         catch(Exception JavaDoc e)
95         {
96             available = false;
97         }
98         return ( available );
99     }
100
101     private static Random JavaDoc random = new Random JavaDoc();
102
103     // i18n SOMLocalStringsManager
104
private static SOMLocalStringsManager localizedStrMgr =
105         SOMLocalStringsManager.getManager( NetUtil.class );
106
107     public static int getFreePort()
108     {
109         synchronized(NetUtil.class)
110         {
111             /* scared of using infinite while loop */
112             for (int i = 0; i < 1024; i++)
113             {
114                 int nextInt = random.nextInt(kMaxPortNo);
115                 if (nextInt <= 1024)
116                 {
117                     continue;
118                 }
119                 else if (isPortAvalable(nextInt))
120                 {
121                     return nextInt;
122                 }
123             }
124         }
125         return 0;
126     }
127 }
128
Popular Tags