001: /*_############################################################################
002: _##
003: _## SNMP4J-Agent - DefaultMOFactory.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 org.snmp4j.agent.*;
024: import org.snmp4j.smi.*;
025: import java.util.Map;
026: import org.snmp4j.agent.mo.snmp.tc.TextualConvention;
027: import java.util.HashMap;
028: import org.snmp4j.agent.mo.snmp.SNMPv2TC;
029: import java.util.*;
030:
031: /**
032: * The <code>DefaultMOFactory</code> is the default factory for creating
033: * ManagedObjects. The default factory creates columnar and scalar objects
034: * based on SNMPv2-TC textual conventions with appropriate constraints.
035: * Other textual conventions can be added too.
036: *
037: * @author Frank Fock
038: * @version 1.1
039: */
040: public class DefaultMOFactory implements MOFactory {
041:
042: private Map textualConventions = new HashMap();
043:
044: private static MOFactory instance;
045:
046: protected DefaultMOFactory() {
047: }
048:
049: /**
050: * Returns the factory singleton with default support for SNMPv2-TC textual
051: * conventions.
052: *
053: * @return
054: * a MOFactory instance.
055: */
056: public static MOFactory getInstance() {
057: if (instance == null) {
058: instance = new DefaultMOFactory();
059: addSNMPv2TCs(instance);
060: }
061: return instance;
062: }
063:
064: /**
065: * Sets the singleton factory.
066: * @param factory
067: * a MOFactory instance.
068: */
069: public static void setInstance(MOFactory factory) {
070: instance = factory;
071: }
072:
073: /**
074: * Adds support for SNMPv2TC textual conventions to the supplied ManagedObject
075: * factory.
076: * @param factory
077: * a MOFactory instance.
078: */
079: public static void addSNMPv2TCs(MOFactory factory) {
080: Collection tcs = new SNMPv2TC().getTextualConventions();
081: for (Iterator it = tcs.iterator(); it.hasNext();) {
082: TextualConvention tc = (TextualConvention) it.next();
083: factory.addTextualConvention(tc);
084: }
085: }
086:
087: protected Map getTextualConventions() {
088: return textualConventions;
089: }
090:
091: /**
092: * Adds a textual convention to this factory which can then be used by the
093: * factory to create appropriate value constraints for columnar and scalar
094: * managed objects.
095: * @param tc
096: * a TextualConvention instance.
097: */
098: public synchronized void addTextualConvention(TextualConvention tc) {
099: Map tcMap = (Map) textualConventions.get(tc.getModuleName());
100: if (tcMap == null) {
101: tcMap = new HashMap(10);
102: textualConventions.put(tc.getModuleName(), tcMap);
103: }
104: tcMap.put(tc.getName(), tc);
105: }
106:
107: public synchronized void removeTextualConvention(
108: TextualConvention tc) {
109: Map tcMap = (Map) textualConventions.get(tc.getModuleName());
110: if (tcMap != null) {
111: tcMap.remove(tc.getName());
112: if (tcMap.isEmpty()) {
113: textualConventions.remove(tc.getModuleName());
114: }
115: }
116: }
117:
118: public synchronized TextualConvention getTextualConvention(
119: String moduleName, String name) {
120: Map tcMap = (Map) textualConventions.get(moduleName);
121: if (tcMap != null) {
122: return (TextualConvention) tcMap.get(name);
123: }
124: return null;
125: }
126:
127: public MOColumn createColumn(int columnID, int syntax,
128: MOAccess access) {
129: return new MOMutableColumn(columnID, syntax, access);
130: }
131:
132: public MOColumn createColumn(int columnID, int syntax,
133: MOAccess access, Variable defaultValue,
134: boolean mutableInService) {
135: return new MOMutableColumn(columnID, syntax, access,
136: defaultValue, mutableInService);
137: }
138:
139: public MOTableRelation createTableRelation(MOTable baseTable,
140: MOTable dependentTable) {
141: return new MOTableRelation(baseTable, dependentTable);
142: }
143:
144: public MOTableRow createRow(OID index, Variable[] values)
145: throws UnsupportedOperationException {
146: return new DefaultMOMutableRow2PC(index, values);
147: }
148:
149: public MOScalar createScalar(OID id, MOAccess access, Variable value) {
150: return new MOScalar(id, access, value);
151: }
152:
153: public MOTable createTable(OID oid, MOTableIndex indexDef,
154: MOColumn[] columns) {
155: return new DefaultMOTable(oid, indexDef, columns,
156: createTableModel(oid, indexDef, columns));
157: }
158:
159: public MOTable createTable(OID oid, MOTableIndex indexDef,
160: MOColumn[] columns, MOTableModel model) {
161: return new DefaultMOTable(oid, indexDef, columns, model);
162: }
163:
164: public MOTableModel createTableModel(OID tableOID,
165: MOTableIndex indexDef, MOColumn[] columns) {
166: return new DefaultMOMutableTableModel();
167: }
168:
169: public void freeRow(MOTableRow row) {
170: }
171:
172: public MOTableIndex createIndex(MOTableSubIndex[] subIndexes,
173: boolean impliedLength) {
174: return new MOTableIndex(subIndexes, impliedLength);
175: }
176:
177: public MOTableSubIndex createSubIndex(int smiSyntax) {
178: return new MOTableSubIndex(smiSyntax);
179: }
180:
181: public MOTableSubIndex createSubIndex(OID oid, int smiSyntax) {
182: return new MOTableSubIndex(oid, smiSyntax);
183: }
184:
185: public MOTableSubIndex createSubIndex(int smiSyntax, int minLength,
186: int maxLength) {
187: return new MOTableSubIndex(smiSyntax, minLength, maxLength);
188: }
189:
190: public MOTableSubIndex createSubIndex(OID oid, int smiSyntax,
191: int minLength, int maxLength) {
192: return new MOTableSubIndex(oid, smiSyntax, minLength, maxLength);
193: }
194:
195: public MOTableIndex createIndex(MOTableSubIndex[] subIndexes,
196: boolean impliedLength, MOTableIndexValidator validator) {
197: return new MOTableIndex(subIndexes, impliedLength, validator);
198: }
199:
200: public MOColumn createColumn(int columnID, int syntax,
201: MOAccess access, String tcModuleName,
202: String textualConvention) {
203: TextualConvention tc = getTextualConvention(tcModuleName,
204: textualConvention);
205: if (tc != null) {
206: return tc
207: .createColumn(columnID, syntax, access, null, true);
208: }
209: return createColumn(columnID, syntax, access);
210: }
211:
212: public MOColumn createColumn(int columnID, int syntax,
213: MOAccess access, Variable defaultValue,
214: boolean mutableInService, String tcModuleName,
215: String textualConvention) {
216: TextualConvention tc = getTextualConvention(tcModuleName,
217: textualConvention);
218: if (tc != null) {
219: return tc.createColumn(columnID, syntax, access,
220: defaultValue, mutableInService);
221: }
222: return createColumn(columnID, syntax, access, defaultValue,
223: mutableInService);
224: }
225:
226: public MOScalar createScalar(OID id, MOAccess access,
227: Variable value, String tcModuleName,
228: String textualConvention) {
229: TextualConvention tc = getTextualConvention(tcModuleName,
230: textualConvention);
231: if (tc != null) {
232: return tc.createScalar(id, access, value);
233: }
234: return createScalar(id, access, value);
235: }
236:
237: public MOAccess createAccess(int moAccess) {
238: return MOAccessImpl.getInstance(moAccess);
239: }
240:
241: }
|