Source Code Cross Referenced for IPAddressUtils.java in  » Web-Services » xins » org » xins » common » net » 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 » Web Services » xins » org.xins.common.net 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: IPAddressUtils.java,v 1.38 2007/09/11 12:39:57 agoubard Exp $
003:         *
004:         * Copyright 2003-2007 Orange Nederland Breedband B.V.
005:         * See the COPYRIGHT file for redistribution and use restrictions.
006:         */
007:        package org.xins.common.net;
008:
009:        import java.net.InetAddress;
010:        import java.net.UnknownHostException;
011:        import java.util.NoSuchElementException;
012:        import java.util.StringTokenizer;
013:
014:        import org.xins.common.MandatoryArgumentChecker;
015:        import org.xins.common.Utils;
016:        import org.xins.common.text.ParseException;
017:
018:        /**
019:         * IP address-related utility functions.
020:         *
021:         * @version $Revision: 1.38 $ $Date: 2007/09/11 12:39:57 $
022:         * @author <a href="mailto:ernst@ernstdehaan.com">Ernst de Haan</a>
023:         *
024:         * @since XINS 1.0.0
025:         */
026:        public final class IPAddressUtils {
027:
028:            /**
029:             * Constructs a new <code>IPAddressUtils</code> object.
030:             */
031:            private IPAddressUtils() {
032:                // empty
033:            }
034:
035:            /**
036:             * Converts an IP address in the form <em>a.b.c.d</em> to an
037:             * <code>int</code>.
038:             *
039:             * @param ip
040:             *    the IP address, must be in the form:
041:             *    <em>a.a.a.a.</em>, where <em>a</em> is a number between 0 and 255,
042:             *    with no leading zeroes; cannot be <code>null</code>.
043:             *
044:             * @return
045:             *    the IP address as an <code>int</code>.
046:             *
047:             * @throws IllegalArgumentException
048:             *    if <code>ip == null</code>.
049:             *
050:             * @throws ParseException
051:             *    if <code>ip</code> cannot be parsed as an IP address.
052:             */
053:            public static int ipToInt(String ip)
054:                    throws IllegalArgumentException, ParseException {
055:                MandatoryArgumentChecker.check("ip", ip);
056:
057:                int value;
058:
059:                // Tokenize the string
060:                StringTokenizer tokenizer = new StringTokenizer(ip, ".", false);
061:
062:                try {
063:
064:                    // Token 1 must be an IP address part
065:                    value = ipPartToInt(ip, tokenizer.nextToken());
066:
067:                    // Token 3 must be an IP address part
068:                    value <<= 8;
069:                    value += ipPartToInt(ip, tokenizer.nextToken());
070:
071:                    // Token 5 must be an IP address part
072:                    value <<= 8;
073:                    value += ipPartToInt(ip, tokenizer.nextToken());
074:
075:                    // Token 7 must be an IP address part
076:                    value <<= 8;
077:                    value += ipPartToInt(ip, tokenizer.nextToken());
078:
079:                } catch (NoSuchElementException nsee) {
080:                    throw newParseException(ip);
081:                }
082:                if (tokenizer.hasMoreTokens()) {
083:                    throw newParseException(ip);
084:                }
085:
086:                return value;
087:            }
088:
089:            /**
090:             * Converts the specified component of an IP address to a number between 0
091:             * and 255.
092:             *
093:             * @param ip
094:             *    the complete IP address, needed when throwing a
095:             *    {@link ParseException}, should not be <code>null</code>; if it is,
096:             *    then the behaviour is undefined.
097:             *
098:             * @param part
099:             *    the part to convert to an <code>int</code> number, should not be
100:             *    <code>null</code>; if it is, then the behaviour is undefined.
101:             *
102:             * @return
103:             *    the <code>int</code> value of the part, between 0 and 255
104:             *    (inclusive).
105:             *
106:             * @throws ParseException
107:             *    if the part cannot be parsed.
108:             */
109:            private static int ipPartToInt(String ip, String part)
110:                    throws ParseException {
111:
112:                char[] partString = part.toCharArray();
113:                int length = partString.length;
114:
115:                if (length == 1) {
116:                    char c0 = partString[0];
117:                    if (c0 >= '0' && c0 <= '9') {
118:                        return c0 - '0';
119:                    }
120:
121:                } else if (length == 2) {
122:                    char c0 = partString[0];
123:                    char c1 = partString[1];
124:
125:                    if (c0 >= '1' && c0 <= '9' && c1 >= '0' && c1 <= '9') {
126:                        return ((c0 - '0') * 10) + (c1 - '0');
127:                    }
128:
129:                } else if (length == 3) {
130:                    char c0 = partString[0];
131:                    char c1 = partString[1];
132:                    char c2 = partString[2];
133:
134:                    if (c0 >= '1' && c0 <= '2' && c1 >= '0' && c1 <= '9'
135:                            && c2 >= '0' && c2 <= '9') {
136:
137:                        int value = ((c0 - '0') * 100) + ((c1 - '0') * 10)
138:                                + (c2 - '0');
139:                        if (value <= 255) {
140:                            return value;
141:                        }
142:                    }
143:                }
144:
145:                throw newParseException(ip);
146:            }
147:
148:            /**
149:             * Retrieves the localhost IP address.
150:             *
151:             * @return
152:             *    if possible the IP address for localhost, otherwise
153:             *    the string <code>"127.0.0.1"</code>.
154:             *
155:             * @since XINS 1.3.0
156:             */
157:            public static String getLocalHostIPAddress() {
158:                try {
159:                    return InetAddress.getLocalHost().getHostAddress();
160:                } catch (UnknownHostException exception) {
161:                    return "127.0.0.1";
162:                }
163:            }
164:
165:            /**
166:             * Retrieves the localhost host name. This method applies several
167:             * techniques to attempt to retrieve the localhost host name.
168:             *
169:             * @return
170:             *    if possible the fully qualified host name for localhost, otherwise if
171:             *    possible the non-qualified host name for the localhost, otherwise
172:             *    the string <code>"localhost"</code>.
173:             */
174:            public static String getLocalHost() {
175:                if (Utils.getJavaVersion() < 1.4) {
176:                    return getLocalHostJava13();
177:                } else {
178:                    return getLocalHostJava14();
179:                }
180:            }
181:
182:            /**
183:             * Retrieves the localhost host name, on Java 1.3.
184:             *
185:             * @return
186:             *    if possible the fully qualified host name for localhost, otherwise if
187:             *    possible the non-qualified host name for the localhost, otherwise
188:             *    the string <code>"localhost"</code>.
189:             */
190:            private static String getLocalHostJava13() {
191:                try {
192:                    return InetAddress.getLocalHost().getHostName();
193:                } catch (UnknownHostException exception) {
194:                    return "localhost";
195:                }
196:            }
197:
198:            /**
199:             * Retrieves the localhost host name, on Java 1.4 and up. This method
200:             * applies several techniques to attempt to retrieve the localhost host
201:             * name.
202:             *
203:             * @return
204:             *    if possible the fully qualified host name for localhost, otherwise if
205:             *    possible the non-qualified host name for the localhost, otherwise
206:             *    the string <code>"localhost"</code>.
207:             */
208:            private static String getLocalHostJava14() {
209:
210:                String hostname = "localhost";
211:
212:                try {
213:                    hostname = InetAddress.getLocalHost()
214:                            .getCanonicalHostName();
215:                } catch (UnknownHostException unknownHostException) {
216:                    String unknownMessage = unknownHostException.getMessage();
217:                    int twoDotPos = unknownMessage.indexOf(':');
218:                    if (twoDotPos != -1) {
219:                        hostname = unknownMessage.substring(0, twoDotPos);
220:                    }
221:                } catch (SecurityException securityException) {
222:                    // fall through
223:                }
224:
225:                return hostname;
226:            }
227:
228:            /**
229:             * Constructs a new <code>ParseException</code> for the specified malformed
230:             * IP address.
231:             *
232:             * @param ip
233:             *    the malformed IP address, not <code>null</code>.
234:             *
235:             * @return
236:             *    the {@link ParseException} to throw.
237:             */
238:            private static ParseException newParseException(String ip) {
239:
240:                // Construct the message for the exception
241:                String detail = "The string \"" + ip
242:                        + "\" is not a valid IP address.";
243:
244:                // Return the exception
245:                return new ParseException(detail);
246:            }
247:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.