Source Code Cross Referenced for NetworkUtil.java in  » Portal » Open-Portal » com » sun » portal » fabric » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » Open Portal » com.sun.portal.fabric.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.