KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > ext > SimMOFactory


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - SimMOFactory.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 package org.snmp4j.agent.mo.ext;
22
23 import org.snmp4j.agent.mo.*;
24 import org.snmp4j.agent.MOAccess;
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 /**
29  * A <code>SimMOFactory</code> object can be used to create non-default MOAccess
30  * instances which support an agent simulation mode which allows the
31  * modification of MIB objects at runtime via SNMP that are not writable in
32  * normal operation but writable in a special config mode (see
33  * AGENTPP-SIMULATION-MIB).
34  * <p>
35  * By creating the ManagedObject instances for a MIB module based on this
36  * factory the injected special MOAccess instances support changing the
37  * operation mode for these instances from normal operation to simulation
38  * mode. In simulation mode, an agent can be filled with data via SNMP
39  * regardless whether the objects are defined as writable or not. Such a
40  * simulation agent can be used for testing/developing management applications
41  * when real agents are not (physically) available.
42  *
43  * @author Frank Fock
44  * @version 1.0
45  */

46 public class SimMOFactory extends DefaultMOFactory {
47
48   private Map JavaDoc accessModes = new HashMap JavaDoc();
49
50   private static boolean simulationModeEnabled;
51   private static SimMOFactory instance;
52
53   private SimMOFactory() {
54   }
55
56   public synchronized static MOFactory getInstance() {
57     if (instance == null) {
58       instance = new SimMOFactory();
59     }
60     return instance;
61   }
62
63   public static void setSimulationModeEnabled(boolean simulationMode) {
64     simulationModeEnabled = simulationMode;
65   }
66
67   public static boolean isSimulationModeEnabled() {
68     return simulationModeEnabled;
69   }
70
71   public synchronized MOAccess createAccess(int moAccess) {
72     MOAccess accessObj = (MOAccess) accessModes.get(new Integer JavaDoc(moAccess));
73     if (accessObj == null) {
74       accessObj = new SimMOAccess(moAccess);
75       accessModes.put(new Integer JavaDoc(moAccess), accessObj);
76     }
77     return accessObj;
78   }
79
80   public class SimMOAccess extends MOAccessImpl {
81
82     public SimMOAccess(int moAccess) {
83       super(moAccess);
84     }
85
86     public boolean isAccessibleForCreate() {
87       if (isSimulationModeEnabled()) {
88         return true;
89       }
90       return super.isAccessibleForCreate();
91     }
92
93     public boolean isAccessibleForWrite() {
94       if (isSimulationModeEnabled()) {
95         return true;
96       }
97       return super.isAccessibleForWrite();
98     }
99   }
100 }
101
Popular Tags