KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > jmx > types > EnumBitsType


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentJMX - EnumBitsType.java
4   _##
5   _## Copyright (C) 2006-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## This program is free software; you can redistribute it and/or modify
8   _## it under the terms of the GNU General Public License version 2 as
9   _## published by the Free Software Foundation.
10   _##
11   _## This program is distributed in the hope that it will be useful,
12   _## but WITHOUT ANY WARRANTY; without even the implied warranty of
13   _## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   _## GNU General Public License for more details.
15   _##
16   _## You should have received a copy of the GNU General Public License
17   _## along with this program; if not, write to the Free Software
18   _## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19   _## MA 02110-1301 USA
20   _##
21   _##########################################################################*/

22 package org.snmp4j.agent.mo.jmx.types;
23
24 import org.snmp4j.smi.OctetString;
25 import javax.management.*;
26
27 public class EnumBitsType extends TypedAttribute {
28
29   private Enum JavaDoc[] enumValues;
30   private Class JavaDoc enumClass;
31   private int offset = 0;
32
33   public EnumBitsType(String JavaDoc name, Class JavaDoc enumClass, Enum JavaDoc[] enumValues) {
34     super(name, byte[].class);
35     this.enumClass = enumClass;
36     this.enumValues = enumValues;
37   }
38
39   public EnumBitsType(String JavaDoc name, Class JavaDoc enumClass,
40                       Enum JavaDoc[] enumValues, int offset) {
41     this(name, enumClass, enumValues);
42     this.offset = offset;
43   }
44
45   public int getOffset() {
46     return offset;
47   }
48
49   public Object JavaDoc transformFromNative(Object JavaDoc nativeValue, ObjectName objectName) {
50     Enum JavaDoc en;
51     if (nativeValue instanceof Enum JavaDoc) {
52       en = (Enum JavaDoc)nativeValue;
53     }
54     else {
55       en = Enum.valueOf(enumClass, nativeValue.toString());
56     }
57     int o = offset + en.ordinal();
58     StringBuffer JavaDoc buf = new StringBuffer JavaDoc("1");
59     for (int i=0; i<o; i++) {
60       buf.insert(0, "0");
61     }
62     while (buf.length() % 8 > 0) {
63       buf.append("0");
64     }
65     return OctetString.fromString(buf.toString(), 2).toByteArray();
66   }
67
68   public Object JavaDoc transformToNative(Object JavaDoc transformedValue,
69                                   Object JavaDoc oldNativeValue, ObjectName objectName) {
70     OctetString os = new OctetString((byte[])transformedValue);
71     String JavaDoc s = os.toString(2);
72     int pos = s.indexOf("1");
73     if (pos < 0) {
74       return null;
75     }
76     pos -= offset;
77     return enumValues[pos];
78   }
79
80 }
81
Popular Tags