KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > MOMutableColumn


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - MOMutableColumn.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;
23
24 import java.util.*;
25
26 import org.snmp4j.*;
27 import org.snmp4j.agent.*;
28 import org.snmp4j.agent.request.*;
29 import org.snmp4j.mp.*;
30 import org.snmp4j.smi.*;
31
32 /**
33  * The <code>MOMutableColumn</code> class represents mutable columnar SMI
34  * objects. It represents all instances of a table's column not only a
35  * single instance (cell).
36  * <p>
37  * Objects represented by <code>MOMutableColumn</code> can be modified via SNMP,
38  * thus <code>MOColumn</code> supports read-only, read-write, and read-create
39  * maximum access.
40  *
41  * @see MOColumn
42  * @author Frank Fock
43  * @version 1.0
44  */

45 public class MOMutableColumn extends MOColumn {
46
47   private Vector validators;
48   private Variable defaultValue;
49   private boolean mutableInService = true;
50
51   public MOMutableColumn(int columnID, int syntax) {
52     super(columnID, syntax);
53   }
54
55   public MOMutableColumn(int columnID, int syntax, MOAccess access) {
56     super(columnID, syntax, access);
57   }
58
59   public MOMutableColumn(int columnID, int syntax, MOAccess access,
60                      Variable defaultValue) {
61     super(columnID, syntax, access);
62     this.defaultValue = defaultValue;
63   }
64
65   public MOMutableColumn(int columnID, int syntax, MOAccess access,
66                          Variable defaultValue, boolean mutableInService) {
67     super(columnID, syntax, access);
68     this.defaultValue = defaultValue;
69     this.mutableInService = mutableInService;
70   }
71
72   public synchronized void
73       addMOValueValidationListener(MOValueValidationListener validator) {
74     if (validators == null) {
75       validators = new Vector(2);
76     }
77     validators.add(validator);
78   }
79
80   public synchronized void
81       removeMOValueValidationListener(MOValueValidationListener validator) {
82     if (validators != null) {
83       validators.remove(validator);
84     }
85   }
86
87   public synchronized int validate(Variable newValue, Variable oldValue) {
88     int status = SnmpConstants.SNMP_ERROR_SUCCESS;
89     if (validators != null) {
90       for (Iterator it = validators.iterator(); it.hasNext();) {
91         MOValueValidationListener v = (MOValueValidationListener) it.next();
92         MOValueValidationEvent event =
93             new MOValueValidationEvent(this, oldValue, newValue);
94         v.validate(event);
95         if (event.getValidationStatus() != SnmpConstants.SNMP_ERROR_SUCCESS) {
96           status = event.getValidationStatus();
97           break;
98         }
99       }
100     }
101     return status;
102   }
103
104   protected boolean validateSetRequest(SubRequest subRequest,
105                                        MOTableRow row, int column) {
106     Variable value = subRequest.getVariableBinding().getVariable();
107     if (value.getSyntax() != getSyntax()) {
108       subRequest.getStatus().setErrorStatus(PDU.wrongType);
109     }
110     int status =
111         validate(value, (row.size() > column) ? row.getValue(column) : null);
112     if (status != SnmpConstants.SNMP_ERROR_SUCCESS) {
113       subRequest.getStatus().setErrorStatus(status);
114       return false;
115     }
116     return true;
117   }
118
119   public void prepare(SubRequest subRequest, MOTableRow row,
120                       MOTableRow changeSet, int column) {
121     if (row instanceof MOMutableRow2PC) {
122       if (validateSetRequest(subRequest, row, column)) {
123         ((MOMutableRow2PC) row).prepare(subRequest, changeSet, column);
124       }
125     }
126     else if (row instanceof MOMutableTableRow) {
127       if (validateSetRequest(subRequest, row, column)) {
128         subRequest.completed();
129       }
130     }
131     else {
132       // not writable
133
subRequest.getStatus().setErrorStatus(PDU.notWritable);
134     }
135   }
136
137   public void commit(SubRequest subRequest, MOTableRow row,
138                      MOTableRow changeSet, int column) {
139     if (row instanceof MOMutableRow2PC) {
140       ((MOMutableRow2PC)row).commit(subRequest, changeSet, column);
141     }
142     else if (row instanceof MOMutableTableRow) {
143       if (subRequest.getUndoValue() == null) {
144         subRequest.setUndoValue(row.getValue(column));
145       }
146       ((MOMutableTableRow)row).setValue(column,
147           (Variable)subRequest.getVariableBinding().getVariable().clone());
148       subRequest.completed();
149     }
150     else {
151       // should never be reached!
152
subRequest.getStatus().setErrorStatus(PDU.commitFailed);
153     }
154   }
155
156   public void undo(SubRequest subRequest, MOTableRow row, int column) {
157     if (row instanceof MOMutableRow2PC) {
158       ((MOMutableRow2PC)row).undo(subRequest, column);
159     }
160     if ((row instanceof MOMutableTableRow) &&
161         (subRequest.getUndoValue() instanceof Variable)) {
162       ((MOMutableTableRow)row).setValue(column, (Variable)
163                                         subRequest.getUndoValue());
164       subRequest.completed();
165     }
166     else {
167       // should never be reached!
168
subRequest.getStatus().setErrorStatus(PDU.undoFailed);
169     }
170   }
171
172   public void cleanup(SubRequest subRequest, MOTableRow row, int column) {
173     if (row instanceof MOMutableRow2PC) {
174       ((MOMutableRow2PC)row).cleanup(subRequest, column);
175     }
176     subRequest.completed();
177   }
178
179   public void setDefaultValue(Variable defaultValue) {
180     this.defaultValue = defaultValue;
181   }
182
183   public void setMutableInService(boolean mutableInService) {
184
185     this.mutableInService = mutableInService;
186   }
187
188   public Variable getDefaultValue() {
189     return defaultValue;
190   }
191
192   public boolean isMutableInService() {
193     return mutableInService;
194   }
195
196   /**
197    * Returns <code>true</code> if this column must be specified in a SET
198    * request which creates a row.
199    * @return
200    * <code>true</code> if this row has a maximum access of READ-CREATE and
201    * has a <code>null</code> default value, <code>false</code> otherwise.
202    */

203   public boolean isMandatory() {
204     return (defaultValue == null) && (getAccess().isAccessibleForCreate());
205   }
206
207   public String JavaDoc toString() {
208     return this.getClass().getName()+"[columnID="+getColumnID()+",syntax="+
209         getSyntax()+",default="+getDefaultValue()+",mode=]";
210   }
211
212 }
213
Popular Tags