001: /**
002: * Copyright 2004 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.fabric.util;
013:
014: import java.io.InputStream;
015: import java.io.OutputStream;
016: import java.net.ConnectException;
017: import java.net.InetAddress;
018: import java.net.Socket;
019: import java.net.UnknownHostException;
020: import java.util.logging.Level;
021: import java.util.logging.LogRecord;
022: import java.util.logging.Logger;
023:
024: import com.sun.portal.admin.common.util.AdminUtil;
025: import com.sun.portal.log.common.PortalLogger;
026:
027: public class NetworkUtil {
028:
029: private static Logger configLogger = PortalLogger
030: .getLogger(NetworkUtil.class);
031:
032: private static LogRecord record;
033:
034: /**
035: * check if port is alive
036: * <br><br>
037: *
038: * @param server
039: * server name
040: *
041: * @param portno
042: * port number
043: *
044: * @return true if port is alive
045: */
046:
047: public static boolean isPortValid(String host, String port) {
048:
049: int portno;
050: try {
051: portno = Integer.parseInt(port);
052: Socket socket = new Socket(host, portno);
053: } catch (ConnectException ex) {
054: configLogger.log(Level.INFO, "PSFB_CSPFU0012",
055: new String[] { port, host });
056: return false;
057: } catch (Exception ex) {
058: configLogger.log(Level.INFO, "PSFB_CSPFU0012", ex);
059: return false;
060: }
061:
062: configLogger.log(Level.SEVERE, "PSFB_CSPFU0011", new String[] {
063: port, host });
064: return true;
065: }
066:
067: static public boolean isPortUsed(String host, int port) {
068:
069: if (port <= 0 || port > 65535) {
070: configLogger.log(Level.SEVERE, "PSFB_CSPFU0013",
071: new Integer(port).toString());
072: return false;
073: }
074:
075: try {
076: Socket socket = new Socket(host, port);
077: OutputStream theOutputStream = socket.getOutputStream();
078: InputStream theInputStream = socket.getInputStream();
079: theOutputStream.close();
080: theOutputStream = null;
081: theInputStream.close();
082: theInputStream = null;
083: socket.close();
084: socket = null;
085: } catch (Exception a) {
086: configLogger
087: .log(Level.INFO, "PSFB_CSPFU0011", new String[] {
088: host, new Integer(port).toString() });
089: return true;
090: }
091: configLogger.log(Level.INFO, "PSFB_CSPFU0012", new String[] {
092: host, new Integer(port).toString() });
093: return false;
094: }
095:
096: /**
097: * Return the next available unused port.
098: * Take 50 tries, if not return error.
099: * <br><br>
100: *
101: * @param server
102: * server name
103: *
104: * @param port
105: * port number
106: *
107: * @return String version of next availabe unused port. On error return null.
108: */
109: static public String getNextAvailablePort(String server, String port) {
110: String invalid = null;
111: int portno = 0;
112: if (port == null || port.length() == 0 || port.length() > 5)
113: return invalid;
114:
115: try {
116: portno = Integer.parseInt(port);
117: } catch (NumberFormatException nfe) {
118: return invalid;
119: }
120:
121: int trys = 50;
122: for (int idx = 0; idx < trys; idx++) {
123: if (isPortUsed(server, portno + idx)) {
124: return new Integer(portno + idx).toString();
125: }
126: }
127: return invalid;
128: }
129:
130: /**
131: * Checks whether the given host is a valid host or not
132: *
133: * @return - true if the host is valid
134: * - false if the host is not valid
135: */
136: public static boolean isHostValid(String host) {
137:
138: boolean bRet = true;
139: try {
140: InetAddress.getByName(host).getHostName();
141: } catch (Exception ex) {
142: if (configLogger.isLoggable(Level.SEVERE)) {
143: record = new LogRecord(Level.SEVERE, "PSFB_CSPFU0015");
144: record.setLoggerName(configLogger.getName());
145: record.setParameters(new String[] { host });
146: record.setThrown(ex);
147: record.setLoggerName(configLogger.getName());
148: configLogger.log(record);
149: }
150: bRet = false;
151: }
152:
153: if (bRet) {
154: configLogger.log(Level.INFO, "PSFB_CSPFU0014", host);
155: } else {
156: configLogger.log(Level.INFO, "PSFB_CSPFU0031", host);
157: }
158:
159: return bRet;
160: }
161:
162: /**
163: * Checks whether the given host is a local host or not
164: *
165: * @return - true if it is a local host
166: * - false if it is not a local host
167: */
168: public static boolean isLocalHost(String host) {
169:
170: boolean bRet = false;
171: try {
172: if (isHostValid(host)) {
173: bRet = AdminUtil.isLocal(host);
174: }
175: } catch (UnknownHostException e) {
176: if (configLogger.isLoggable(Level.SEVERE)) {
177: record = new LogRecord(Level.SEVERE, "PSFB_CSPFU0015");
178: record.setLoggerName(configLogger.getName());
179: record.setParameters(new String[] { host });
180: record.setThrown(e);
181: record.setLoggerName(configLogger.getName());
182: configLogger.log(record);
183: }
184: }
185:
186: if (bRet) {
187: configLogger.log(Level.INFO, "PSFB_CSPFU0032", host);
188: } else {
189: configLogger.log(Level.INFO, "PSFB_CSPFU0033", host);
190: }
191:
192: return bRet;
193: }
194:
195: /**
196: * Check if IPAddress and HostName are in sync
197: */
198: public static boolean validateIPAddressAndHost(String host,
199: String ipaddress) {
200:
201: if ((ipaddress == null) || (ipaddress.length() < 7)) {
202:
203: configLogger.log(Level.INFO, "PSFB_CSPFU0018",
204: new String[] { ipaddress });
205: return false;
206: }
207:
208: boolean bRetVal = false;
209:
210: try {
211: //Get the List of all IPAddresses for a given host
212: InetAddress[] hostIPAddressList = InetAddress
213: .getAllByName(host);
214:
215: for (int i = 0; i < hostIPAddressList.length; i++) {
216: String sIPAddress = hostIPAddressList[i]
217: .getHostAddress();
218: if ((sIPAddress != null)
219: && sIPAddress.equalsIgnoreCase(ipaddress)) {
220: configLogger.log(Level.INFO, "PSFB_CSPFU0016",
221: new String[] { host, ipaddress });
222: bRetVal = true;
223: break;
224: }
225: }
226: } catch (Exception e) {
227: if (configLogger.isLoggable(Level.SEVERE)) {
228:
229: record = new LogRecord(Level.SEVERE, "PSFB_CSPFU0017");
230: record.setLoggerName(configLogger.getName());
231: record.setParameters(new String[] { host, ipaddress });
232: record.setThrown(e);
233: record.setLoggerName(configLogger.getName());
234: configLogger.log(record);
235: }
236: }
237:
238: return bRetVal;
239: }
240: }
|