KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > NetUtils


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: NetUtils.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.net.InetAddress JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26
27 import org.opensubsystems.core.error.OSSException;
28
29 /**
30  * Class containing methods for working with network
31  *
32  * @version $Id: NetUtils.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
33  * @author Julo Legeny
34  * @code.reviewer Miro Halas
35  * @code.reviewed Initial revision
36  */

37 public final class NetUtils
38 {
39    // Constructors /////////////////////////////////////////////////////////////
40

41    /**
42     * Private constructor since this class cannot be instantiated
43     */

44    private NetUtils(
45    )
46    {
47       // Do nothing
48
}
49    
50    // Public methods ///////////////////////////////////////////////////////////
51

52    /**
53     * Get textual representation just of the server machine IP address.
54     *
55     * @return strAddress - IP address of the server
56     * @throws OSSException - an error occured while getting server IP address
57     */

58    public static String JavaDoc getServerIPAddress(
59    ) throws OSSException
60    {
61       String JavaDoc strAddress;
62        
63       try
64       {
65          strAddress = InetAddress.getLocalHost().getHostAddress();
66       }
67       catch (UnknownHostException JavaDoc eExc)
68       {
69          // MHALAS: On my Linux box it cannot find the address, but that
70
// is not reason to do not start the server
71
// throw new OSSInternalErrorException("Localhost IP address could not be determined.");
72
strAddress = "Unknown address";
73       }
74       
75       return strAddress;
76    }
77
78    /**
79     * Get textual representation of the server machine IP address together with
80     * its name.
81     *
82     * @return strAddress - IP address of the server
83     * @throws OSSException - an error occured while getting server IP address
84     */

85    public static String JavaDoc getServerIPAddressAndName(
86    ) throws OSSException
87    {
88       String JavaDoc strAddress;
89        
90       try
91       {
92          strAddress = InetAddress.getLocalHost().toString();
93       }
94       catch (UnknownHostException JavaDoc eExc)
95       {
96          // MHALAS: On my Linux box it cannot find the address, but that
97
// is not reason to do not start the server
98
// throw new OSSInternalErrorException("Localhost IP address could not be determined.");
99
strAddress = "Unknown address";
100       }
101       
102       return strAddress;
103    }
104 }
105
Popular Tags