KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J - GenericAddress.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.util.*;
25 import org.snmp4j.log.*;
26 import org.snmp4j.asn1.BERInputStream;
27 import org.snmp4j.SNMP4JSettings;
28
29 /**
30  * The <code>GenericAddress</code> implements the decorator and factory
31  * design pattern to provide a generic address type.
32  * <p>
33  * To register address types other than the default, set the system property
34  * {@link #ADDRESS_TYPES_PROPERTIES} before calling the {@link #parse} method
35  * for the first time.
36  *
37  * @author Frank Fock
38  * @version 1.8
39  */

40 public class GenericAddress extends SMIAddress {
41
42   static final long serialVersionUID = -6102594326202231511L;
43
44   /**
45    * Default address type identifier for an UpdAddress.
46    */

47   public static final String JavaDoc TYPE_UDP = "udp";
48   /**
49    * Default address type identifier for a TcpAddress.
50    */

51   public static final String JavaDoc TYPE_TCP = "tcp";
52   /**
53    * Default address type identifier for an IpAddress.
54    */

55   public static final String JavaDoc TYPE_IP = "ip";
56
57   public static final String JavaDoc ADDRESS_TYPES_PROPERTIES =
58       "org.snmp4j.addresses";
59   private static final String JavaDoc ADDRESS_TYPES_PROPERTIES_DEFAULT =
60       "address.properties";
61
62   private static final LogAdapter logger = LogFactory.getLogger(GenericAddress.class);
63
64   private SMIAddress address;
65   private static Map knownAddressTypes = null;
66
67   public GenericAddress() {
68   }
69
70   public GenericAddress(SMIAddress address) {
71     this.address = address;
72   }
73
74   public int getSyntax() {
75     return address.getSyntax();
76   }
77
78   public boolean isValid() {
79     if (address == null) {
80       return false;
81     }
82     return address.isValid();
83   }
84
85   public String JavaDoc toString() {
86     return address.toString();
87   }
88
89   public int hashCode() {
90     return address.hashCode();
91   }
92
93   public int compareTo(Object JavaDoc o) {
94     return address.compareTo(o);
95   }
96
97   public boolean equals(Object JavaDoc o) {
98     return address.equals(o);
99   }
100
101   public void decodeBER(BERInputStream inputStream) throws java.io.IOException JavaDoc {
102     throw new UnsupportedOperationException JavaDoc();
103   }
104   public void encodeBER(OutputStream outputStream) throws java.io.IOException JavaDoc {
105     address.encodeBER(outputStream);
106   }
107
108   public int getBERLength() {
109     return address.getBERLength();
110   }
111
112   public void setAddress(SMIAddress address) {
113     this.address = address;
114   }
115
116   public Address getAddress() {
117     return address;
118   }
119
120   /**
121    * Register Address classes from a properties file. The registered
122    * address types are used by the {@link parse} method to type-safe
123    * instantiate sub-classes from <code>Address</code> from a
124    * <code>String</code>.
125    */

126   private synchronized static void registerAddressTypes() {
127     if (SNMP4JSettings.isExtensibilityEnabled()) {
128       String JavaDoc addresses = System.getProperty(ADDRESS_TYPES_PROPERTIES,
129                                             ADDRESS_TYPES_PROPERTIES_DEFAULT);
130       InputStream is = Variable.class.getResourceAsStream(addresses);
131       if (is == null) {
132         throw new InternalError JavaDoc("Could not read '" + addresses +
133                                 "' from classpath!");
134       }
135       Properties props = new Properties();
136       try {
137         props.load(is);
138         Map h = new TreeMap();
139         for (Enumeration en = props.propertyNames(); en.hasMoreElements(); ) {
140           String JavaDoc id = (String JavaDoc) en.nextElement();
141           String JavaDoc className = props.getProperty(id);
142           try {
143             Class JavaDoc c = Class.forName(className);
144             h.put(id, c);
145           }
146           catch (ClassNotFoundException JavaDoc cnfe) {
147             logger.error(cnfe);
148           }
149         }
150         knownAddressTypes = h;
151       }
152       catch (IOException iox) {
153         String JavaDoc txt = "Could not read '" + addresses + "': " + iox.getMessage();
154         logger.error(txt);
155         throw new InternalError JavaDoc(txt);
156       }
157       finally {
158         try {
159           is.close();
160         }
161         catch (IOException ex) {
162           // ignore
163
logger.warn(ex);
164         }
165       }
166     }
167     else {
168       Map h = new TreeMap();
169       h.put(TYPE_UDP, UdpAddress.class);
170       h.put(TYPE_TCP, TcpAddress.class);
171       h.put(TYPE_IP, IpAddress.class);
172       knownAddressTypes = h;
173     }
174   }
175
176   /**
177    * Parses a given transport protocol dependent address string into an
178    * <code>Address</code> instance that is subsumed by this
179    * <code>GenericAddress</code> object.
180    *
181    * @param address
182    * an address string with a leading type specifier as defined in the
183    * "address.properties". The format is <code>"type:address"</code> where
184    * the format of <code>address</code> depends on <code>type</code>.
185    * Valid values for <code>type</code> are, for example, "udp" and "tcp".
186    * @return
187    * a <code>Address</code> instance of the address classes specified
188    * in "address.properties" whose type ID matched the specified ID in
189    * <code>address</code>. If <code>address</code> cannot be parsed,
190    * <code>null</code> is returned.
191    * @throws IllegalArgumentException
192    * if the address type indicator supplied is not know.
193    */

194   public static Address parse(String JavaDoc address) {
195     if (knownAddressTypes == null) {
196       registerAddressTypes();
197     }
198     String JavaDoc type = TYPE_UDP;
199     int sep = address.indexOf(':');
200     if (sep > 0) {
201       type = address.substring(0, sep);
202       address = address.substring(sep+1);
203     }
204     type = type.toLowerCase();
205     Class JavaDoc c = (Class JavaDoc)knownAddressTypes.get(type);
206     if (c == null) {
207       throw new IllegalArgumentException JavaDoc("Address type " + type + " unknown");
208     }
209     try {
210       Address addr = (Address)c.newInstance();
211       if (addr.parseAddress(address)) {
212         return addr;
213       }
214       return null;
215     }
216     catch (Exception JavaDoc ex) {
217       logger.warn(ex);
218     }
219     return null;
220   }
221
222   /**
223    * Parse an address form the supplied string.
224    * @param address
225    * an address string known by the GenericAddress.
226    * @return boolean
227    * @see #parse(String address)
228    */

229   public boolean parseAddress(String JavaDoc address) {
230     Address addr = parse(address);
231     if (addr instanceof SMIAddress) {
232       setAddress((SMIAddress)addr);
233       return true;
234     }
235     return false;
236   }
237
238   public Object JavaDoc clone() {
239     return new GenericAddress(address);
240   }
241
242   public int toInt() {
243     throw new UnsupportedOperationException JavaDoc();
244   }
245
246   public long toLong() {
247     throw new UnsupportedOperationException JavaDoc();
248   }
249
250   public OID toSubIndex(boolean impliedLength) {
251     throw new UnsupportedOperationException JavaDoc();
252   }
253
254   public void fromSubIndex(OID subIndex, boolean impliedLength) {
255     throw new UnsupportedOperationException JavaDoc();
256   }
257
258   public void setValue(String JavaDoc value) {
259     if (!parseAddress(value)) {
260       throw new IllegalArgumentException JavaDoc(value+" cannot be parsed by "+
261                                          getClass().getName());
262     }
263   }
264 }
265
266
Popular Tags