KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > smi > IpAddress


1 /*_############################################################################
2   _##
3   _## SNMP4J - IpAddress.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j.smi;
22
23 import java.io.*;
24 import java.net.*;
25 import org.snmp4j.asn1.BER;
26 import org.snmp4j.asn1.BERInputStream;
27 import org.snmp4j.log.LogFactory;
28 import org.snmp4j.log.LogAdapter;
29
30 /**
31  * The <code>IpAddress</code> class represents an IPv4 address SNMP variable.
32  *
33  * @author Frank Fock
34  * @version 1.7
35  * @since 1.0
36  */

37 public class IpAddress extends SMIAddress {
38
39   private static final long serialVersionUID = -146846354059565449L;
40
41   private static final LogAdapter logger =
42       LogFactory.getLogger(AbstractVariable.class);
43
44   private static final byte[] IPANYADDRESS = { 0,0,0,0 };
45
46   public static final InetAddress ANY_IPADDRESS = createAnyAddress();
47
48   private java.net.InetAddress JavaDoc inetAddress;
49
50   /**
51    * Creates a <code>0.0.0.0</code> IP address.
52    */

53   public IpAddress() {
54     this.inetAddress = ANY_IPADDRESS;
55   }
56
57   /**
58    * Creates an IP address from an <code>InetAddress</code>
59    * @param address
60    * an <code>InetAddress</code> instance (must not necessarily be a IPv4
61    * address).
62    */

63   public IpAddress(InetAddress address) {
64     if (address == null) {
65       throw new NullPointerException JavaDoc();
66     }
67     this.inetAddress = address;
68   }
69
70   /**
71    * Create an IP address from an address string.
72    * @param address
73    * an IP address String.
74    * @see Address#parseAddress(String address)
75    */

76   public IpAddress(String JavaDoc address) {
77     if (!parseAddress(address)) {
78       throw new IllegalArgumentException JavaDoc(address);
79     }
80   }
81
82   public int getSyntax() {
83     return SMIConstants.SYNTAX_IPADDRESS;
84   }
85
86   public boolean isValid() {
87     return (inetAddress != null);
88   }
89
90   public String JavaDoc toString() {
91     String JavaDoc addressString = inetAddress.toString();
92     return addressString.substring(addressString.indexOf('/')+1);
93   }
94
95   public int hashCode() {
96     if (inetAddress != null) {
97       return inetAddress.hashCode();
98     }
99     return 0;
100   }
101
102   /**
103    * Parses an IP address string and returns the corresponding
104    * <code>IpAddress</code> instance.
105    * @param address
106    * an IP address string which may be a host name or a numerical IP address.
107    * @return
108    * an <code>IpAddress</code> instance or <code>null</code> if
109    * <code>address</code> cannot not be parsed.
110    * @see Address#parseAddress(String address)
111    */

112   public static Address parse(String JavaDoc address) {
113     try {
114       InetAddress addr = InetAddress.getByName(address);
115       return new IpAddress(addr);
116     }
117     catch (Exception JavaDoc ex) {
118       logger.error("Unable to parse IpAddress from: "+address, ex);
119       return null;
120     }
121   }
122
123   public boolean parseAddress(String JavaDoc address) {
124     try {
125       inetAddress = InetAddress.getByName(address);
126       return true;
127     }
128     catch (UnknownHostException uhex) {
129       return false;
130     }
131   }
132
133   public int compareTo(Object JavaDoc o) {
134     OctetString a = new OctetString(inetAddress.getAddress());
135     return a.compareTo(new OctetString(((IpAddress)o).getInetAddress().getAddress()));
136   }
137
138   public boolean equals(Object JavaDoc o) {
139     if (!(o instanceof IpAddress)) {
140       return false;
141     }
142     return (compareTo(o) == 0);
143   }
144
145   public void decodeBER(BERInputStream inputStream) throws java.io.IOException JavaDoc {
146     BER.MutableByte type = new BER.MutableByte();
147     byte[] value = BER.decodeString(inputStream, type);
148     if (type.getValue() != BER.IPADDRESS) {
149       throw new IOException("Wrong type encountered when decoding Counter: "+
150                             type.getValue());
151     }
152     if (value.length != 4) {
153       throw new IOException("IpAddress encoding error, wrong length: " +
154                             value.length);
155     }
156     inetAddress = InetAddress.getByAddress(value);
157   }
158
159   public void encodeBER(OutputStream outputStream) throws java.io.IOException JavaDoc {
160     byte[] address = new byte[4];
161     System.arraycopy(inetAddress.getAddress(), 0, address, 0, 4);
162     BER.encodeString(outputStream, BER.IPADDRESS, inetAddress.getAddress());
163   }
164
165   public int getBERLength() {
166     return 6;
167   }
168
169   public void setAddress(byte[] rawValue) throws UnknownHostException {
170     this.inetAddress = InetAddress.getByAddress(rawValue);
171   }
172
173   public void setInetAddress(java.net.InetAddress JavaDoc inetAddress) {
174     this.inetAddress = inetAddress;
175   }
176
177   public InetAddress getInetAddress() {
178     return inetAddress;
179   }
180
181   private static InetAddress createAnyAddress() {
182     try {
183       return InetAddress.getByAddress(IPANYADDRESS);
184     }
185     catch (Exception JavaDoc ex) {
186       logger.error("Unable to create any IpAddress: "+ex.getMessage(), ex);
187     }
188     return null;
189   }
190
191   public Object JavaDoc clone() {
192     return new IpAddress(inetAddress);
193   }
194
195   public int toInt() {
196     throw new UnsupportedOperationException JavaDoc();
197   }
198
199   public long toLong() {
200     throw new UnsupportedOperationException JavaDoc();
201   }
202
203   public OID toSubIndex(boolean impliedLength) {
204     byte[] address = new byte[4];
205     System.arraycopy(inetAddress.getAddress(), 0, address, 0, 4);
206     OID subIndex = new OID(new int[4]);
207     for (int i=0; i<address.length; i++) {
208       subIndex.set(i, address[i] & 0xFF);
209     }
210     return subIndex;
211   }
212
213   public void fromSubIndex(OID subIndex, boolean impliedLength) {
214     byte[] rawValue = new byte[4];
215     for (int i=0; i<rawValue.length; i++) {
216       rawValue[i] = (byte)(subIndex.get(i) & 0xFF);
217     }
218     try {
219       setAddress(rawValue);
220     }
221     catch (UnknownHostException ex) {
222       ex.printStackTrace();
223     }
224   }
225
226   public void setValue(String JavaDoc value) {
227     if (!parseAddress(value)) {
228       throw new IllegalArgumentException JavaDoc(value+" cannot be parsed by "+
229                                          getClass().getName());
230     }
231   }
232
233 }
234
235
Popular Tags