KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - DefaultMOFactory.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 org.snmp4j.agent.*;
25 import org.snmp4j.smi.*;
26 import java.util.Map JavaDoc;
27 import org.snmp4j.agent.mo.snmp.tc.TextualConvention;
28 import java.util.HashMap JavaDoc;
29 import org.snmp4j.agent.mo.snmp.SNMPv2TC;
30 import java.util.*;
31
32 /**
33  * The <code>DefaultMOFactory</code> is the default factory for creating
34  * ManagedObjects. The default factory creates columnar and scalar objects
35  * based on SNMPv2-TC textual conventions with appropriate constraints.
36  * Other textual conventions can be added too.
37  *
38  * @author Frank Fock
39  * @version 1.1
40  */

41 public class DefaultMOFactory implements MOFactory {
42
43   private Map JavaDoc textualConventions = new HashMap JavaDoc();
44
45   private static MOFactory instance;
46
47   protected DefaultMOFactory() {
48   }
49
50   /**
51    * Returns the factory singleton with default support for SNMPv2-TC textual
52    * conventions.
53    *
54    * @return
55    * a MOFactory instance.
56    */

57   public static MOFactory getInstance() {
58     if (instance == null) {
59       instance = new DefaultMOFactory();
60       addSNMPv2TCs(instance);
61     }
62     return instance;
63   }
64
65   /**
66    * Sets the singleton factory.
67    * @param factory
68    * a MOFactory instance.
69    */

70   public static void setInstance(MOFactory factory) {
71     instance = factory;
72   }
73
74   /**
75    * Adds support for SNMPv2TC textual conventions to the supplied ManagedObject
76    * factory.
77    * @param factory
78    * a MOFactory instance.
79    */

80   public static void addSNMPv2TCs(MOFactory factory) {
81     Collection tcs = new SNMPv2TC().getTextualConventions();
82     for (Iterator it = tcs.iterator(); it.hasNext(); ) {
83       TextualConvention tc = (TextualConvention) it.next();
84       factory.addTextualConvention(tc);
85     }
86   }
87
88   protected Map JavaDoc getTextualConventions() {
89     return textualConventions;
90   }
91
92   /**
93    * Adds a textual convention to this factory which can then be used by the
94    * factory to create appropriate value constraints for columnar and scalar
95    * managed objects.
96    * @param tc
97    * a TextualConvention instance.
98    */

99   public synchronized void addTextualConvention(TextualConvention tc) {
100     Map JavaDoc tcMap = (Map JavaDoc) textualConventions.get(tc.getModuleName());
101     if (tcMap == null) {
102       tcMap = new HashMap JavaDoc(10);
103       textualConventions.put(tc.getModuleName(), tcMap);
104     }
105     tcMap.put(tc.getName(), tc);
106   }
107
108   public synchronized void removeTextualConvention(TextualConvention tc) {
109     Map JavaDoc tcMap = (Map JavaDoc) 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
119       getTextualConvention(String JavaDoc moduleName, String JavaDoc name) {
120     Map JavaDoc tcMap = (Map JavaDoc) 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, MOAccess access) {
128     return new MOMutableColumn(columnID, syntax, access);
129   }
130
131   public MOColumn createColumn(int columnID, int syntax, MOAccess access,
132                                Variable defaultValue, boolean mutableInService) {
133     return new MOMutableColumn(columnID, syntax, access,
134                                defaultValue, mutableInService);
135   }
136
137   public MOTableRelation createTableRelation(MOTable baseTable,
138                                              MOTable dependentTable) {
139     return new MOTableRelation(baseTable, dependentTable);
140   }
141
142   public MOTableRow createRow(OID index, Variable[] values) throws
143       UnsupportedOperationException JavaDoc {
144     return new DefaultMOMutableRow2PC(index, values);
145   }
146
147   public MOScalar createScalar(OID id, MOAccess access, Variable value) {
148     return new MOScalar(id, access, value);
149   }
150
151   public MOTable createTable(OID oid, MOTableIndex indexDef, MOColumn[] columns) {
152     return new DefaultMOTable(oid, indexDef, columns,
153                               createTableModel(oid, indexDef, columns));
154   }
155
156   public MOTable createTable(OID oid, MOTableIndex indexDef, MOColumn[] columns,
157                              MOTableModel model) {
158     return new DefaultMOTable(oid, indexDef, columns, model);
159   }
160
161   public MOTableModel createTableModel(OID tableOID,
162                                        MOTableIndex indexDef,
163                                        MOColumn[] columns) {
164     return new DefaultMOMutableTableModel();
165   }
166
167   public void freeRow(MOTableRow row) {
168   }
169
170   public MOTableIndex createIndex(MOTableSubIndex[] subIndexes,
171                                   boolean impliedLength) {
172     return new MOTableIndex(subIndexes, impliedLength);
173   }
174
175   public MOTableSubIndex createSubIndex(int smiSyntax) {
176     return new MOTableSubIndex(smiSyntax);
177   }
178
179   public MOTableSubIndex createSubIndex(OID oid, int smiSyntax) {
180     return new MOTableSubIndex(oid, smiSyntax);
181   }
182
183   public MOTableSubIndex createSubIndex(int smiSyntax, int minLength,
184                                         int maxLength) {
185     return new MOTableSubIndex(smiSyntax, minLength, maxLength);
186   }
187
188   public MOTableSubIndex createSubIndex(OID oid, int smiSyntax, int minLength,
189                                         int maxLength) {
190     return new MOTableSubIndex(oid, smiSyntax, minLength, maxLength);
191   }
192
193   public MOTableIndex createIndex(MOTableSubIndex[] subIndexes,
194                                   boolean impliedLength,
195                                   MOTableIndexValidator validator) {
196     return new MOTableIndex(subIndexes, impliedLength, validator);
197   }
198
199   public MOColumn createColumn(int columnID, int syntax, MOAccess access,
200                                String JavaDoc tcModuleName, String JavaDoc textualConvention) {
201     TextualConvention tc =
202         getTextualConvention(tcModuleName, textualConvention);
203     if (tc != null) {
204       return tc.createColumn(columnID, syntax, access, null, true);
205     }
206     return createColumn(columnID, syntax, access);
207   }
208
209   public MOColumn createColumn(int columnID, int syntax, MOAccess access,
210                                Variable defaultValue, boolean mutableInService,
211                                String JavaDoc tcModuleName, String JavaDoc textualConvention) {
212     TextualConvention tc =
213         getTextualConvention(tcModuleName, textualConvention);
214     if (tc != null) {
215       return tc.createColumn(columnID, syntax, access,
216                              defaultValue, mutableInService);
217     }
218     return createColumn(columnID, syntax, access,
219                         defaultValue, mutableInService);
220   }
221
222   public MOScalar createScalar(OID id, MOAccess access, Variable value,
223                                String JavaDoc tcModuleName, String JavaDoc textualConvention) {
224     TextualConvention tc =
225         getTextualConvention(tcModuleName, textualConvention);
226     if (tc != null) {
227       return tc.createScalar(id, access, value);
228     }
229     return createScalar(id, access, value);
230   }
231
232   public MOAccess createAccess(int moAccess) {
233     return MOAccessImpl.getInstance(moAccess);
234   }
235
236 }
237
Popular Tags