KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > adaptor > snmp > agent > SnmpVarBindFactory


1 /*
2  * Copyright (c) 2003, Intracom S.A. - www.intracom.com
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * This package and its source code is available at www.jboss.org
19 **/

20 package org.jboss.jmx.adaptor.snmp.agent;
21
22 import java.util.Date JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.jboss.logging.Logger;
27 import org.opennms.protocols.snmp.SnmpCounter64;
28 import org.opennms.protocols.snmp.SnmpInt32;
29 import org.opennms.protocols.snmp.SnmpObjectId;
30 import org.opennms.protocols.snmp.SnmpOctetString;
31 import org.opennms.protocols.snmp.SnmpVarBind;
32 /**
33  * <tt>SnmpVarBindFactory</tt> implements the infrastructure required to
34  * generate SNMP variable bindings from generic Object instances.
35  * For each handled type (integer, string, e.t.c.) a corresponding maker class
36  * is present that "knows" how to make and populate the coresponding variable
37  * binding (SnmpInt32, SnmpOctetString). The mapping between types and makers
38  * is held in a hash map for optimised performance.
39  *
40  * @version $Revision: 44604 $
41  *
42  * @author <a HREF="mailto:spol@intracom.gr">Spyros Pollatos</a>
43  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
44 **/

45 public class SnmpVarBindFactory
46 {
47    /** The logger object */
48    private static final Logger log = Logger.getLogger(SnmpVarBindFactory.class);
49    
50    /** Contains "type - maker" tupples */
51    private Map JavaDoc makers = new HashMap JavaDoc();
52
53    /** Default Maker */
54    private final Maker defaultMaker = new SnmpObjectMaker();
55    
56    /**
57     * CTOR - Initialises the factory with the known handled types and maker
58     * instances
59    **/

60    public SnmpVarBindFactory()
61    {
62       makers.put("java.lang.String", new SnmpOctetStringMaker());
63       makers.put("java.lang.Integer", new SnmpInt32Maker());
64       makers.put("java.lang.Long", new SnmpCounter64Maker());
65       makers.put("java.math.BigInteger", new SnmpCounter64Maker());
66       makers.put("java.util.Date", new SnmpDateMaker());
67    }
68     
69    /**
70     * The factory method. A lookup is performed based on the type of the
71     * provided value, as this is returned by "getClass().getName()". If a
72     * match is found the call is delegated to the returned maker.
73    **/

74    public SnmpVarBind make(String JavaDoc oid, Object JavaDoc value)
75       throws MappingFailedException
76    {
77       // Get value type and locate the maker
78
String JavaDoc type = value.getClass().getName();
79       Maker m = (Maker)this.makers.get(type);
80         
81       // Delegate where type match is found. If not use generic varbind maker
82
if (m == null) {
83          log.warn("Value type \"" + type + "\" for OID " + oid +
84                   " encountered. Using default VarBind maker");
85          
86          return defaultMaker.make(oid, value);
87       }
88       else
89          return m.make(oid, value);
90    }
91     
92    /**
93     * The generic interface that should be implemented by all makers.
94    **/

95    interface Maker
96    {
97       public SnmpVarBind make(String JavaDoc oid, Object JavaDoc value)
98          throws MappingFailedException;
99    }
100
101    /**
102     * Generates unsigned integer SNMP variable bindings
103    **/

104    class SnmpInt32Maker
105       implements Maker
106    {
107       public SnmpVarBind make(String JavaDoc oid, Object JavaDoc value)
108          throws MappingFailedException
109       {
110          Integer JavaDoc i = (Integer JavaDoc)value;
111             
112          return new SnmpVarBind(new SnmpObjectId(oid),
113                                 new SnmpInt32(i));
114       }
115    } // class SnmpInt32Maker
116

117    /**
118     * Generates unsigned long integer SNMP variable bindings
119    **/

120    class SnmpCounter64Maker
121       implements Maker
122    {
123       public SnmpVarBind make(String JavaDoc oid, Object JavaDoc value)
124          throws MappingFailedException
125       {
126          Long JavaDoc l = (Long JavaDoc)value;
127             
128          return new SnmpVarBind(new SnmpObjectId(oid),
129                                 new SnmpCounter64(l.longValue()));
130       }
131    } // class SnmpCounter64Maker
132

133    /**
134     * Generates octet string SNMP variable bindings
135    **/

136    class SnmpOctetStringMaker
137       implements Maker
138    {
139       public SnmpVarBind make(String JavaDoc oid, Object JavaDoc value)
140          throws MappingFailedException
141       {
142          String JavaDoc s = (String JavaDoc)value;
143             
144          return new SnmpVarBind(new SnmpObjectId(oid),
145                                 new SnmpOctetString(s.getBytes()));
146       }
147    } // class OctetStringMaker
148

149    /**
150     * Generates octet string SNMP variable bindings from dates
151    **/

152    class SnmpDateMaker
153       implements Maker
154    {
155       public SnmpVarBind make(String JavaDoc oid, Object JavaDoc value)
156          throws MappingFailedException
157       {
158          Date JavaDoc d = (Date JavaDoc)value;
159          SnmpOctetStringMaker sMaker = new SnmpOctetStringMaker();
160
161          return sMaker.make(oid, d.toString());
162       }
163    } // class SnmpDateMaker
164

165    /**
166     * Generates octet string SNMP variable bindings from objects
167    **/

168    class SnmpObjectMaker
169       implements Maker
170    {
171       public SnmpVarBind make(String JavaDoc oid, Object JavaDoc value)
172          throws MappingFailedException
173       {
174          SnmpOctetStringMaker sMaker = new SnmpOctetStringMaker();
175
176          return sMaker.make(oid, value.toString());
177       }
178    } // class SnmpDateMaker
179

180 } // class SnmpVarBindFactory
181

182
183
Popular Tags