KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > transport > TransportMappings


1 /*_############################################################################
2   _##
3   _## SNMP4J - TransportMappings.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
22
23 package org.snmp4j.transport;
24
25 import org.snmp4j.smi.Address;
26 import org.snmp4j.TransportMapping;
27 import java.io.InputStream JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import org.snmp4j.log.*;
33 import java.lang.reflect.Constructor JavaDoc;
34 import org.snmp4j.SNMP4JSettings;
35
36 /**
37  * The <code>TransportMappings</code> factory can be used to create a transport
38  * mapping for an address class.
39  *
40  * @author Frank Fock
41  * @version 1.1
42  * @since 1.1
43  */

44 public class TransportMappings {
45
46   private static final LogAdapter logger = LogFactory.getLogger(TransportMappings.class);
47
48   public static final String JavaDoc TRANSPORT_MAPPINGS =
49       "org.snmp4j.transportMappings";
50   private static final String JavaDoc TRANSPORT_MAPPINGS_DEFAULT =
51       "transports.properties";
52
53   private static TransportMappings instance = null;
54   private Hashtable JavaDoc transportMappings = null;
55
56   protected TransportMappings() {
57   }
58
59   /**
60    * Returns the <code>TransportMappings</code> singleton.
61    * @return
62    * the <code>TransportMappings</code> instance.
63    */

64   public static TransportMappings getInstance() {
65     if (instance == null) {
66       instance = new TransportMappings();
67     }
68     return instance;
69   }
70
71   /**
72    * Returns a <code>TransportMapping</code> instance that is initialized with
73    * the supplied transport address.
74    * If no such mapping exists, <code>null</code> is returned. To register
75    * third party transport mappings, please set the system property
76    * {@link #TRANSPORT_MAPPINGS} to a transport mappings registration file,
77    * before calling this method for the first time.
78    *
79    * @param transportAddress
80    * an <code>Address</code> instance that the transport mapping to lookup
81    * has to support.
82    * @return
83    * a <code>TransportMapping</code> that supports the specified
84    * <code>transportAddress</code> or <code>null</code> if such a mapping
85    * cannot be found.
86    */

87   public TransportMapping createTransportMapping(Address transportAddress) {
88     if (transportMappings == null) {
89       registerTransportMappings();
90     }
91     Class JavaDoc c =
92         (Class JavaDoc) transportMappings.get(transportAddress.getClass().getName());
93     if (c == null) {
94       return null;
95     }
96     Class JavaDoc[] params = new Class JavaDoc[1];
97     params[0] = transportAddress.getClass();
98     Constructor JavaDoc constructor = null;
99     try {
100       constructor = c.getConstructor(params);
101       return (TransportMapping)
102           constructor.newInstance(new Object JavaDoc[] { transportAddress });
103     }
104     catch (Exception JavaDoc ex) {
105       if (logger.isDebugEnabled()) {
106         ex.printStackTrace();
107       }
108       logger.error(ex);
109       return null;
110     }
111   }
112
113   protected synchronized void registerTransportMappings() {
114     if (SNMP4JSettings.isExtensibilityEnabled()) {
115       String JavaDoc transports =
116           System.getProperty(TRANSPORT_MAPPINGS, TRANSPORT_MAPPINGS_DEFAULT);
117       InputStream JavaDoc is = TransportMappings.class.getResourceAsStream(transports);
118       if (is == null) {
119         throw new InternalError JavaDoc("Could not read '" + transports +
120                                 "' from classpath!");
121       }
122       Properties JavaDoc props = new Properties JavaDoc();
123       try {
124         props.load(is);
125         Hashtable JavaDoc t = new Hashtable JavaDoc(props.size());
126         for (Enumeration JavaDoc en = props.propertyNames(); en.hasMoreElements(); ) {
127           String JavaDoc addressClassName = (String JavaDoc) en.nextElement();
128           String JavaDoc className = props.getProperty(addressClassName);
129           try {
130             Class JavaDoc c = Class.forName(className);
131             t.put(addressClassName, c);
132           }
133           catch (ClassNotFoundException JavaDoc cnfe) {
134             logger.error(cnfe);
135           }
136         }
137         // atomic syntax registration
138
transportMappings = t;
139       }
140       catch (IOException JavaDoc iox) {
141         String JavaDoc txt = "Could not read '" + transports + "': " +
142             iox.getMessage();
143         logger.error(txt);
144         throw new InternalError JavaDoc(txt);
145       }
146       finally {
147         try {
148           is.close();
149         }
150         catch (IOException JavaDoc ex) {
151           logger.warn(ex);
152         }
153       }
154     }
155     else {
156       Hashtable JavaDoc t = new Hashtable JavaDoc(2);
157       t.put("org.snmp4j.smi.UdpAddress", DefaultUdpTransportMapping.class);
158       t.put("org.snmp4j.smi.TcpAddress", DefaultTcpTransportMapping.class);
159       transportMappings = t;
160     }
161   }
162
163 }
164
Popular Tags