001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Nam Nguyen
028: */
029:
030: package com.caucho.server.snmp.types;
031:
032: /*
033: * SNMP type
034: */
035: abstract public class SnmpValue {
036: public static final int EOC = 0x00;
037: public static final int INTEGER = 0x02;
038: public static final int OCTET_STRING = 0x04;
039: public static final int NULL = 0x05;
040: public static final int OBJECT_IDENTIFIER = 0x06;
041: public static final int SEQUENCE = 0x30;
042: public static final int IP_ADDRESS = 0x40;
043: public static final int COUNTER = 0x41;
044: public static final int GAUGE = 0x42;
045: public static final int TIME_TICKS = 0x43;
046: public static final int OPAQUE = 0x44;
047: public static final int GET_REQUEST_PDU = 0xA0;
048: public static final int GET_NEXT_REQUEST_PDU = 0xA1;
049: public static final int GET_RESPONSE_PDU = 0xA2;
050: public static final int SET_REQUEST_PDU = 0xA3;
051: public static final int TRAP_PDU = 0xA4;
052:
053: abstract public void toAsn1(StringBuilder sb);
054:
055: abstract public int getType();
056:
057: public static SnmpValue create(Object obj) {
058: if (obj instanceof Number)
059: return new IntegerValue(((Number) obj).intValue());
060: else if (obj instanceof Boolean)
061: return new IntegerValue(((Boolean) obj).booleanValue() ? 1
062: : 0);
063: else
064: return new OctetStringValue(obj.toString());
065: }
066:
067: public static SnmpValue create(Object obj, String typeStr) {
068: if (typeStr == null)
069: return create(obj);
070:
071: int type = SnmpValue.OCTET_STRING;
072:
073: if (typeStr.equalsIgnoreCase("INTEGER"))
074: type = INTEGER;
075: else if (typeStr.equalsIgnoreCase("TIME_TICKS"))
076: type = TIME_TICKS;
077: else if (typeStr.equalsIgnoreCase("GAUGE"))
078: type = GAUGE;
079: else if (typeStr.equalsIgnoreCase("COUNTER"))
080: type = COUNTER;
081: else if (typeStr.equalsIgnoreCase("OBJECT_IDENTIFIER"))
082: type = OBJECT_IDENTIFIER;
083:
084: return create(obj, type);
085: }
086:
087: public static SnmpValue create(Object obj, int type) {
088: if (obj instanceof Number) {
089: switch (type) {
090: case INTEGER:
091: return new IntegerValue(((Number) obj).intValue());
092: case COUNTER:
093: return new CounterValue(((Number) obj).longValue());
094: case GAUGE:
095: return new GaugeValue(((Number) obj).longValue());
096: case TIME_TICKS:
097: // time ticks are in hundredths of a second
098: return new TimeTicksValue(
099: ((Number) obj).longValue() / 10);
100: }
101: }
102:
103: switch (type) {
104: case OBJECT_IDENTIFIER:
105: return new ObjectIdentifierValue(obj.toString());
106: case IP_ADDRESS:
107: return new IpAddressValue(obj.toString());
108: }
109:
110: return new OctetStringValue(obj.toString());
111: }
112:
113: public final String toAsn1() {
114: StringBuilder sb = new StringBuilder();
115:
116: toAsn1(sb);
117:
118: return sb.toString();
119: }
120:
121: final protected void header(StringBuilder sb, int len) {
122: sb.append((char) getType());
123:
124: if (len < 0x7F) {
125: sb.append((char) len);
126: return;
127: }
128:
129: int bytes = 1;
130:
131: if ((len >> 24) > 0)
132: bytes = 4;
133: else if ((len >> 16) > 0)
134: bytes = 3;
135: else if ((len >> 8) > 0)
136: bytes = 2;
137:
138: // size of length field
139: sb.append((char) (0x80 | bytes));
140:
141: while (bytes > 0) {
142: switch (bytes) {
143: case 1:
144: sb.append((char) (len & 0xFF));
145: break;
146: case 2:
147: sb.append((char) ((len >> 8) & 0xFF));
148: break;
149: case 3:
150: sb.append((char) ((len >> 16) & 0xFF));
151: break;
152: case 4:
153: sb.append((char) ((len >> 24) & 0xFF));
154: break;
155: }
156:
157: bytes--;
158: }
159:
160: }
161:
162: public long getLong() {
163: throw new UnsupportedOperationException();
164: }
165:
166: public String getString() {
167: throw new UnsupportedOperationException();
168: }
169:
170: public static String typeName(int identifier) {
171: switch (identifier) {
172: case EOC:
173: return "EOC";
174: case INTEGER:
175: return "INTEGER";
176: case OCTET_STRING:
177: return "OCTET_STRING";
178: case NULL:
179: return "NULL";
180: case OBJECT_IDENTIFIER:
181: return "OBJECT_IDENTIFIER";
182: case SEQUENCE:
183: return "SEQUENCE";
184: case IP_ADDRESS:
185: return "IP_ADDRESS";
186: case COUNTER:
187: return "COUNTER";
188: case GAUGE:
189: return "GAUGE";
190: case TIME_TICKS:
191: return "TIME_TICKS";
192: case OPAQUE:
193: return "OPAQUE";
194: case GET_REQUEST_PDU:
195: return "GET_REQUEST_PDU";
196: case GET_NEXT_REQUEST_PDU:
197: return "GET_NEXT_REQUEST_PDU";
198: case GET_RESPONSE_PDU:
199: return "GET_RESPONSE_PDU";
200: case SET_REQUEST_PDU:
201: return "SET_REQUEST_PDU";
202: case TRAP_PDU:
203: return "TRAP_PDU";
204: default:
205: return "UNKNOWN";
206: }
207: }
208:
209: }
|