001: /*_############################################################################
002: _##
003: _## SNMP4J-Agent - MOMutableColumn.java
004: _##
005: _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.agent.mo;
022:
023: import java.util.*;
024:
025: import org.snmp4j.*;
026: import org.snmp4j.agent.*;
027: import org.snmp4j.agent.request.*;
028: import org.snmp4j.mp.*;
029: import org.snmp4j.smi.*;
030:
031: /**
032: * The <code>MOMutableColumn</code> class represents mutable columnar SMI
033: * objects. It represents all instances of a table's column not only a
034: * single instance (cell).
035: * <p>
036: * Objects represented by <code>MOMutableColumn</code> can be modified via SNMP,
037: * thus <code>MOColumn</code> supports read-only, read-write, and read-create
038: * maximum access.
039: *
040: * @see MOColumn
041: * @author Frank Fock
042: * @version 1.0
043: */
044: public class MOMutableColumn extends MOColumn {
045:
046: private Vector validators;
047: private Variable defaultValue;
048: private boolean mutableInService = true;
049:
050: public MOMutableColumn(int columnID, int syntax) {
051: super (columnID, syntax);
052: }
053:
054: public MOMutableColumn(int columnID, int syntax, MOAccess access) {
055: super (columnID, syntax, access);
056: }
057:
058: public MOMutableColumn(int columnID, int syntax, MOAccess access,
059: Variable defaultValue) {
060: super (columnID, syntax, access);
061: this .defaultValue = defaultValue;
062: }
063:
064: public MOMutableColumn(int columnID, int syntax, MOAccess access,
065: Variable defaultValue, boolean mutableInService) {
066: super (columnID, syntax, access);
067: this .defaultValue = defaultValue;
068: this .mutableInService = mutableInService;
069: }
070:
071: public synchronized void addMOValueValidationListener(
072: MOValueValidationListener validator) {
073: if (validators == null) {
074: validators = new Vector(2);
075: }
076: validators.add(validator);
077: }
078:
079: public synchronized void removeMOValueValidationListener(
080: MOValueValidationListener validator) {
081: if (validators != null) {
082: validators.remove(validator);
083: }
084: }
085:
086: public synchronized int validate(Variable newValue,
087: Variable oldValue) {
088: int status = SnmpConstants.SNMP_ERROR_SUCCESS;
089: if (validators != null) {
090: for (Iterator it = validators.iterator(); it.hasNext();) {
091: MOValueValidationListener v = (MOValueValidationListener) it
092: .next();
093: MOValueValidationEvent event = new MOValueValidationEvent(
094: this , oldValue, newValue);
095: v.validate(event);
096: if (event.getValidationStatus() != SnmpConstants.SNMP_ERROR_SUCCESS) {
097: status = event.getValidationStatus();
098: break;
099: }
100: }
101: }
102: return status;
103: }
104:
105: protected boolean validateSetRequest(SubRequest subRequest,
106: MOTableRow row, int column) {
107: Variable value = subRequest.getVariableBinding().getVariable();
108: if (value.getSyntax() != getSyntax()) {
109: subRequest.getStatus().setErrorStatus(PDU.wrongType);
110: }
111: int status = validate(value, (row.size() > column) ? row
112: .getValue(column) : null);
113: if (status != SnmpConstants.SNMP_ERROR_SUCCESS) {
114: subRequest.getStatus().setErrorStatus(status);
115: return false;
116: }
117: return true;
118: }
119:
120: public void prepare(SubRequest subRequest, MOTableRow row,
121: MOTableRow changeSet, int column) {
122: if (row instanceof MOMutableRow2PC) {
123: if (validateSetRequest(subRequest, row, column)) {
124: ((MOMutableRow2PC) row).prepare(subRequest, changeSet,
125: column);
126: }
127: } else if (row instanceof MOMutableTableRow) {
128: if (validateSetRequest(subRequest, row, column)) {
129: subRequest.completed();
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,
141: column);
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()
148: .getVariable().clone());
149: subRequest.completed();
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,
163: (Variable) subRequest.getUndoValue());
164: subRequest.completed();
165: } else {
166: // should never be reached!
167: subRequest.getStatus().setErrorStatus(PDU.undoFailed);
168: }
169: }
170:
171: public void cleanup(SubRequest subRequest, MOTableRow row,
172: 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)
205: && (getAccess().isAccessibleForCreate());
206: }
207:
208: public String toString() {
209: return this .getClass().getName() + "[columnID=" + getColumnID()
210: + ",syntax=" + getSyntax() + ",default="
211: + getDefaultValue() + ",mode=]";
212: }
213:
214: }
|