KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > snmp > DateAndTime


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - DateAndTime.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (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 package org.snmp4j.agent.mo.snmp;
23
24 import org.snmp4j.agent.mo.MOMutableColumn;
25 import org.snmp4j.agent.MOAccess;
26 import org.snmp4j.smi.Variable;
27 import org.snmp4j.smi.SMIConstants;
28 import org.snmp4j.smi.OctetString;
29 import java.util.GregorianCalendar JavaDoc;
30 import java.util.Calendar JavaDoc;
31 import java.util.TimeZone JavaDoc;
32 import org.snmp4j.mp.SnmpConstants;
33 import org.snmp4j.agent.mo.MOScalar;
34 import org.snmp4j.agent.request.SubRequest;
35 import org.snmp4j.smi.OID;
36 // JavaDoc DateAndTimeTC
37
import org.snmp4j.agent.mo.snmp.tc.DateAndTimeTC;
38
39 /**
40  * The <code>DateAndTime</code> implements the DateAndTime textual convention
41  * (TC) from the SNMPv2-TC MIB specificion for columnar objects.
42  * <p/>
43  * <code>DateAndTime</code> subclasses <code>MOMutableColumn</code> and can thus
44  * directly be used with tables. To use this TC implementation as
45  * <code>MOScalar</code> create the corresponding instance using
46  * {@link #createMOScalar} or even better use the {@link DateAndTimeTC} textual
47  * convention in conjunction with a <code>MOFactory</code>.
48  *
49  * @author Frank Fock
50  * @version 1.0
51  */

52 public class DateAndTime extends MOMutableColumn {
53
54   public DateAndTime(int columnID, MOAccess access,
55                      Variable defaultValue, boolean mutableInService) {
56     super(columnID, SMIConstants.SYNTAX_OCTET_STRING,
57           access, defaultValue, mutableInService);
58   }
59
60   public DateAndTime(int columnID, MOAccess access,
61                      Variable defaultValue) {
62     super(columnID, SMIConstants.SYNTAX_OCTET_STRING,
63           access, defaultValue);
64   }
65
66   /**
67    * Tests a variable for DateAndTime conformance.
68    * @param dateAndTime
69    * a Variable.
70    * @return
71    * {@link SnmpConstants#SNMP_ERROR_SUCCESS} if <code>dateAndTime</code>
72    * is valid or an appropriate SNMP error code if not.
73    */

74   public static int validateDateAndTime(Variable dateAndTime) {
75     if (dateAndTime instanceof OctetString) {
76       OctetString os = (OctetString)dateAndTime;
77       if ((os.length() != 8) && (os.length() != 11)) {
78         return SnmpConstants.SNMP_ERROR_WRONG_LENGTH;
79       }
80       int month = (os.get(2) & 0xFF );
81       int date = (os.get(3) & 0xFF );
82       int hour = (os.get(4) & 0xFF );
83       int minute = (os.get(5) & 0xFF );
84       int second = (os.get(6) & 0xFF );
85       int deci = (os.get(7) & 0xFF );
86       if ((month < 1) || (month > 12) ||
87           (date < 1) || (date > 31) || (hour > 23) || (second > 59) ||
88           (minute > 59) || (deci > 9)) {
89         return SnmpConstants.SNMP_ERROR_WRONG_VALUE;
90       }
91       if (os.length() == 11) {
92         if ((os.get(8) != '+') && (os.get(8) != '-')) {
93           return SnmpConstants.SNMP_ERROR_WRONG_VALUE;
94         }
95       }
96       return SnmpConstants.SNMP_ERROR_SUCCESS;
97     }
98     return SnmpConstants.SNMP_ERROR_WRONG_TYPE;
99   }
100
101   /**
102    * Creates a DatenAndTime <code>OctetString</code> value from a
103    * <code>GregorianCalendar</code>.
104    * @param dateAndTime
105    * a <code>GregorianCalendar</code> instance.
106    * @return
107    * the corresponding DateAndTime <code>OctetString</code>.
108    */

109   public static OctetString makeDateAndTime(GregorianCalendar JavaDoc dateAndTime) {
110     OctetString os = new OctetString();
111     os.append((byte)(dateAndTime.get(Calendar.YEAR)/256));
112     os.append((byte)(dateAndTime.get(Calendar.YEAR)%256));
113     os.append((byte)(dateAndTime.get(Calendar.MONTH)+1));
114     os.append((byte)(dateAndTime.get(Calendar.DAY_OF_MONTH)));
115     os.append((byte)(dateAndTime.get(Calendar.HOUR_OF_DAY)));
116     os.append((byte)(dateAndTime.get(Calendar.MINUTE)));
117     os.append((byte)(dateAndTime.get(Calendar.SECOND)));
118     os.append((byte)(dateAndTime.get(Calendar.MILLISECOND)/100));
119     if (dateAndTime.getTimeZone() != null) {
120       TimeZone JavaDoc tz = dateAndTime.getTimeZone();
121       os.append((tz.getRawOffset()>=0) ? "+":"-");
122       os.append((byte)(tz.getOffset(dateAndTime.getTimeInMillis())/3600000));
123       os.append((byte)(tz.getOffset(dateAndTime.getTimeInMillis())%3600000));
124     }
125     return os;
126   }
127
128   /**
129    * Creates a <code>GregorianCalendar</code> from a properly formatted
130    * DateAndTime <code>OctetString</code>.
131    * @param dateAndTimeValue
132    * an OctetString conforming to the DateAndTime TC.
133    * @return
134    * the corresponding <code>GregorianCalendar</code> instance.
135    */

136   public static GregorianCalendar JavaDoc makeCalendar(OctetString dateAndTimeValue) {
137     int year = (dateAndTimeValue.get(0) & 0xFF ) * 256 +
138         (dateAndTimeValue.get(1) & 0xFF );
139     int month = (dateAndTimeValue.get(2) & 0xFF );
140     int date = (dateAndTimeValue.get(3) & 0xFF );
141     int hour = (dateAndTimeValue.get(4) & 0xFF );
142     int minute = (dateAndTimeValue.get(5) & 0xFF );
143     int second = (dateAndTimeValue.get(6) & 0xFF );
144     int deci = (dateAndTimeValue.get(7) & 0xFF );
145     String JavaDoc timezone = "GMT"+dateAndTimeValue.get(8)+
146                       dateAndTimeValue.get(9)+":"+dateAndTimeValue.get(10);
147     GregorianCalendar JavaDoc gc =
148         new GregorianCalendar JavaDoc(year, month-1, date, hour, minute, second);
149     gc.set(Calendar.MILLISECOND, deci*100);
150     GregorianCalendar JavaDoc tgc =
151         new GregorianCalendar JavaDoc(TimeZone.getTimeZone(timezone));
152     tgc.setTimeInMillis(gc.getTimeInMillis());
153     return tgc;
154   }
155
156   public synchronized int validate(Variable newValue, Variable oldValue) {
157     return validateDateAndTime(newValue);
158   }
159
160   /**
161    * Create a <code>MOScalar</code> DateAndTime instance.
162    * @param oid
163    * the OID of the scalar (including the .0 suffix).
164    * @param access
165    * the <code>MOAccess</code> instance defining the maximum access rights.
166    * @param value
167    * the initial value.
168    * @param localtime
169    * if <code>true</code> the returned DateAndTime instance will always
170    * return the local time (does only makes sense for a read-only instance).
171    * Otherwise the value last set will be returned on GET like requests.
172    * @return
173    * the <code>MOScalar</code> instance.
174    */

175   public static MOScalar createMOScalar(final OID oid,
176                                         final MOAccess access,
177                                         final OctetString value,
178                                         final boolean localtime) {
179     return new MOScalar(oid, access, value) {
180       public int isValueOK(SubRequest sreq) {
181         return validateDateAndTime(sreq.getVariableBinding().getVariable());
182       }
183
184       public Variable getValue() {
185         Variable value = super.getValue();
186         if (localtime) {
187           value = makeDateAndTime(new GregorianCalendar JavaDoc());
188         }
189         return value;
190       }
191     };
192   }
193 }
194
Popular Tags