KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - SnmpTagList.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 org.snmp4j.mp.SnmpConstants;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Collection JavaDoc;
34
35 public class SnmpTagList extends MOMutableColumn {
36
37   private static final OctetString TAG_DELIMITER = new OctetString(" \t\r\n");
38
39   public SnmpTagList(int columnID, MOAccess access,
40                      Variable defaultValue, boolean mutableInService) {
41     super(columnID, SMIConstants.SYNTAX_OCTET_STRING,
42           access, defaultValue, mutableInService);
43   }
44
45   public synchronized int validate(Variable newValue, Variable oldValue) {
46     int status = super.validate(newValue, oldValue);
47     if (status == SnmpConstants.SNMP_ERROR_SUCCESS) {
48       return isValidTagList(newValue);
49     }
50     return status;
51   }
52
53   public static Set JavaDoc getTags(OctetString tagList) {
54     Collection JavaDoc tags = OctetString.split(tagList, TAG_DELIMITER);
55     HashSet JavaDoc list = new HashSet JavaDoc(tags);
56     return list;
57   }
58
59   public static int isValidTagList(Variable newValue) {
60     if (!(newValue instanceof OctetString)) {
61       return SnmpConstants.SNMP_ERROR_WRONG_TYPE;
62     }
63     OctetString os = (OctetString)newValue;
64     if (os.length() > 255) {
65       return SnmpConstants.SNMP_ERROR_WRONG_LENGTH;
66     }
67     else if (os.length() > 0) {
68       if (SnmpTagValue.isDelimiter(os.get(0)) ||
69           SnmpTagValue.isDelimiter(os.get(os.length()-1))) {
70         return SnmpConstants.SNMP_ERROR_BAD_VALUE;
71       }
72       boolean lastWasDelimiter = false;
73       for (int i = 0; i < os.length()-1; i++) {
74         boolean isDelimiter = SnmpTagValue.isDelimiter(os.get(i));
75         if (lastWasDelimiter && isDelimiter) {
76           return SnmpConstants.SNMP_ERROR_BAD_VALUE;
77         }
78         lastWasDelimiter = isDelimiter;
79       }
80     }
81     return SnmpConstants.SNMP_ERROR_SUCCESS;
82   }
83 }
84
Popular Tags