KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > GenericTools


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch;
25
26 import java.net.*;
27 import java.util.*;
28 import java.util.regex.*;
29
30 import org.eclipse.swt.graphics.*;
31
32 import java.net.*;
33
34 import net.fenyo.gnetwatch.GUI.VisualElement;
35
36 import org.apache.commons.logging.*;
37 import org.apache.log4j.xml.*;
38
39 /**
40  * General methods not dedicated to a particular application.
41  * @author Alexandre Fenyo
42  * @version $Id: GenericTools.java,v 1.13 2007/03/09 22:44:21 fenyo Exp $
43  */

44
45 public class GenericTools {
46   private static Log log = LogFactory.getLog(GenericTools.class);
47
48   /**
49    * Configure the global logging rules.
50    * @param config Reference to the general configuration instance.
51    * @return void.
52    */

53   static public void initLogEngine(final Config config) {
54     // configure log4j
55
DOMConfigurator.configure(config.getProperty("log4j"));
56   }
57
58   /**
59    * Converts a string to an Inet4Address.
60    * @param str source string.
61    * @return Inet4Address address representing this string.
62    */

63   static public Inet4Address stringToInet4Address(final String JavaDoc str) throws UnknownHostException {
64     /*
65     final Matcher match =
66       Pattern.compile("([0-9]*)\\.([0-9]*)\\.([0-9]*)\\.([0-9]*)").matcher(str);
67     if (match.find() == true) {
68       return (Inet4Address) InetAddress.getByAddress(new byte [] {
69           new Integer(match.group(1)).byteValue(),
70           new Integer(match.group(2)).byteValue(),
71           new Integer(match.group(3)).byteValue(),
72           new Integer(match.group(4)).byteValue(),
73       });
74     } else return stringToInet4Address("0.0.0.0");
75     */

76     // shortest alternative to the previous code:
77
return (Inet4Address) InetAddress.getByName(str);
78   }
79
80   /**
81    * Converts a string to an Inet6Address.
82    * @param str source string.
83    * @return Inet6Address address representing this string.
84    */

85   static public Inet6Address stringToInet6Address(final String JavaDoc str) throws UnknownHostException {
86     try {
87       return (Inet6Address) InetAddress.getByName(str);
88     } catch (final ClassCastException JavaDoc ex) {
89       return null;
90     }
91   }
92
93   /**
94    * Converts a unsigned byte (encoded into a java signed byte) to a positive signed short.
95    * @param ub unsigned byte encoded into a java signed byte.
96    * @return short positive signed short.
97    */

98   static public short unsignedByteToShort(final byte ub) {
99     final int foo = ub;
100     return (short) (foo < 0 ? foo + 256 : foo);
101   }
102
103   /**
104    * Converts an IP address to its string representation.
105    * @param addr ipv4 address.
106    * @return String ascii representation.
107    */

108   static public String JavaDoc inet4AddressToString(final Inet4Address addr) {
109     try {
110       byte bytes[] = addr.getAddress();
111       return InetAddress.getByAddress(bytes).toString().substring(1);
112     } catch (final UnknownHostException ex) {
113       log.error("Exception", ex);
114     }
115     return "";
116   }
117
118   /**
119    * Converts an IP address to its string representation.
120    * @param addr ipv6 address.
121    * @return String ascii representation.
122    */

123   static public String JavaDoc inet6AddressToString(final Inet6Address addr) {
124     try {
125       byte bytes[] = addr.getAddress();
126       return InetAddress.getByAddress(bytes).toString().substring(1);
127     } catch (final UnknownHostException ex) {
128       log.error("Exception", ex);
129     }
130     return "";
131   }
132
133   /**
134    * Returns the class A/B/C network address containing an IP address.
135    * @param addr_str IPv4 address.
136    * @return String network address.
137    */

138   static public String JavaDoc getNetFromAddress(final String JavaDoc addr_str) {
139     try {
140       final InetAddress addr;
141
142       addr = InetAddress.getByName(addr_str);
143
144       if (Inet6Address.class.isInstance(addr)) {
145         // rfc-4291
146
return "IPv6 range";
147       } else if (Inet4Address.class.isInstance(addr)) {
148         byte bytes[] = ((Inet4Address) addr).getAddress();
149         if (unsignedByteToShort(bytes[0]) < 128) {
150           // class A
151
bytes[1] = 0;
152           bytes[2] = 0;
153           bytes[3] = 0;
154           return InetAddress.getByAddress(bytes).toString().substring(1) + "/8";
155         } else if (unsignedByteToShort(bytes[0]) < 192) {
156           // class B
157
bytes[2] = 0;
158           bytes[3] = 0;
159           return InetAddress.getByAddress(bytes).toString().substring(1) + "/16";
160         } else if (unsignedByteToShort(bytes[0]) < 224) {
161           // class C
162
bytes[3] = 0;
163           return InetAddress.getByAddress(bytes).toString().substring(1) + "/24";
164         } else if (unsignedByteToShort(bytes[0]) < 248) {
165           // class D
166
return "224.0.0.0/4";
167         } else {
168           // class E
169
return "248.0.0.0/4";
170         }
171       } else return null;
172     } catch (final UnknownHostException ex) {
173       log.error("Exception (addr_str=" + addr_str + ")", ex);
174       return null;
175     }
176   }
177
178   /**
179    * Removes the part of a graph that is covered by another graph.
180    * Note that the second graph need not to be a subgraph of the first one.
181    * @param addr g1 initial graph.
182    * @param addr g2 graph defining links to remove to the initial graph.
183    * @return void.
184    */

185   static public void substractGraph(java.util.List JavaDoc<Pair<VisualElement, VisualElement>> g1, java.util.List JavaDoc<Pair<VisualElement, VisualElement>> g2) {
186     final java.util.List JavaDoc<Pair<VisualElement, VisualElement>> gtemp = new LinkedList<Pair<VisualElement, VisualElement>>(g1);
187     for (final Pair<VisualElement, VisualElement> p : gtemp)
188       if (g2.contains(p)) g1.remove(p);
189   }
190 }
191
Popular Tags