KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - DateAndTimeScalar.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 package org.snmp4j.agent.mo.snmp;
22
23 import org.snmp4j.agent.mo.MOScalar;
24 import org.snmp4j.smi.OctetString;
25 import org.snmp4j.smi.OID;
26 import org.snmp4j.agent.MOAccess;
27 import org.snmp4j.smi.Variable;
28 import org.snmp4j.agent.request.SubRequest;
29 import java.util.GregorianCalendar JavaDoc;
30 // JavaDoc
31
import org.snmp4j.agent.mo.snmp.tc.DateAndTimeTC;
32
33 /**
34  * The <code>DateAndTimeScalar</code> implements the DateAndTime textual
35  * convention (TC) from the SNMPv2-TC MIB specificion for scalar objects.
36  * <p/>
37  * <code>DateAndTimeScalar</code> subclasses <code>MOScalar</code> and
38  * can thus directly be used.
39  * <p>
40  * It is recommended to use this TC implementation not directly, instead use
41  * the {@link DateAndTimeTC} textual convention in conjunction with a
42  * <code>MOFactory</code>.
43  *
44  * @author Frank Fock
45  * @version 1.0
46  */

47 public class DateAndTimeScalar extends MOScalar {
48
49   private boolean localtime;
50
51   public DateAndTimeScalar(final OID oid,
52                            final MOAccess access,
53                            final OctetString value) {
54     this(oid, access, value, false);
55   }
56
57
58   public DateAndTimeScalar(final OID oid,
59                            final MOAccess access,
60                            final OctetString value,
61                            final boolean localtime) {
62     super(oid, access, value);
63     this.localtime = localtime;
64   }
65
66   public int isValueOK(SubRequest sreq) {
67     return DateAndTime.validateDateAndTime(sreq.getVariableBinding().getVariable());
68   }
69
70   public Variable getValue() {
71     Variable value = super.getValue();
72     if (localtime) {
73       value = DateAndTime.makeDateAndTime(new GregorianCalendar JavaDoc());
74     }
75     return value;
76   }
77
78   /**
79    * Sets the date and time value (incl. time zone) from a gregorian calendar
80    * value.
81    * @param calendar
82    * a <code>GregorianCalendar</code> instance.
83    */

84   public void setCalendar(GregorianCalendar JavaDoc calendar) {
85     setValue(DateAndTime.makeDateAndTime(calendar));
86   }
87
88   /**
89    * Gets a gregorian calendar instance with the date and time of this scalar.
90    * @return
91    * a <code>GregorianCalendar</code> instance.
92    */

93   public GregorianCalendar JavaDoc getCalendar() {
94     return DateAndTime.makeCalendar((OctetString)getValue());
95   }
96
97 }
98
Popular Tags