KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - MOAccessImpl.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.MOAccess;
25
26 /**
27  * The <code>MOAccessImpl</code> class implements an immutable
28  * <code>MOAccess</code>. For special purposes, it can be sub-classed
29  * to modify access dynamically, for example to build an simulation agent
30  * where objects may be modified in a certain mode even if they are read-only
31  * normally.
32  *
33  * @author Frank Fock
34  * @version 1.0
35  */

36 public class MOAccessImpl implements MOAccess {
37
38   public static final short ACCESSIBLE_FOR_READ = 1;
39   public static final short ACCESSIBLE_FOR_WRITE = 2;
40   public static final short ACCESSIBLE_FOR_CREATE = 4;
41   public static final short ACCESSIBLE_FOR_NOTIFY = 8;
42
43   public static final short ACCESSIBLE_FOR_READ_ONLY =
44       ACCESSIBLE_FOR_READ | ACCESSIBLE_FOR_NOTIFY;
45   public static final short ACCESSIBLE_FOR_READ_WRITE =
46       ACCESSIBLE_FOR_WRITE | ACCESSIBLE_FOR_READ | ACCESSIBLE_FOR_NOTIFY;
47   public static final short ACCESSIBLE_FOR_READ_CREATE =
48       ACCESSIBLE_FOR_WRITE | ACCESSIBLE_FOR_CREATE |
49       ACCESSIBLE_FOR_READ | ACCESSIBLE_FOR_NOTIFY;
50
51   public static final MOAccess ACCESS_READ_ONLY =
52       new MOAccessImpl(ACCESSIBLE_FOR_READ_ONLY);
53   public static final MOAccess ACCESS_FOR_NOTIFY =
54       new MOAccessImpl(ACCESSIBLE_FOR_NOTIFY);
55   public static final MOAccess ACCESS_WRITE_ONLY =
56       new MOAccessImpl(ACCESSIBLE_FOR_WRITE);
57   public static final MOAccess ACCESS_READ_WRITE =
58       new MOAccessImpl(ACCESSIBLE_FOR_READ_WRITE);
59   public static final MOAccess ACCESS_READ_CREATE =
60       new MOAccessImpl(ACCESSIBLE_FOR_READ_CREATE);
61
62   private short access = ACCESSIBLE_FOR_READ | ACCESSIBLE_FOR_NOTIFY;
63
64   public MOAccessImpl(int access) {
65     this.access = (short)(access & 0xFFFF);
66   }
67
68   public boolean isAccessibleForRead() {
69     return ((access & ACCESSIBLE_FOR_READ) > 0);
70   }
71
72   public boolean isAccessibleForWrite() {
73     return ((access & ACCESSIBLE_FOR_WRITE) > 0);
74   }
75
76   public boolean isAccessibleForNotify() {
77     return ((access & ACCESSIBLE_FOR_NOTIFY) > 0);
78   }
79
80   public boolean isAccessibleForCreate() {
81     return ((access & ACCESSIBLE_FOR_CREATE) > 0);
82   }
83
84   /**
85    * Returns the internal access ID.
86    * @return
87    * a short value identifying the configured access level.
88    */

89   public final short getAccess() {
90     return access;
91   }
92
93   /**
94    * Returns the appropriate <code>MOAccess</code> instance for the supplied
95    * access ID. If that ID matches one of the standard access levels defined
96    * by this class, then that instance is returned. Otherwise, a new instance
97    * will be created with that access ID.
98    *
99    * @param moAccess
100    * a bitwise OR combination of the basic access levels defined by this
101    * class.
102    * @return
103    * a MOAccess instance.
104    */

105   public static MOAccess getInstance(int moAccess) {
106     switch (moAccess) {
107       case ACCESSIBLE_FOR_READ_ONLY: {
108         return ACCESS_READ_ONLY;
109       }
110       case ACCESSIBLE_FOR_READ_CREATE: {
111         return ACCESS_READ_CREATE;
112       }
113       case ACCESSIBLE_FOR_NOTIFY: {
114         return ACCESS_FOR_NOTIFY;
115       }
116       case ACCESSIBLE_FOR_READ_WRITE: {
117         return ACCESS_READ_WRITE;
118       }
119       case ACCESSIBLE_FOR_WRITE: {
120         return ACCESS_WRITE_ONLY;
121       }
122       default: {
123         return new MOAccessImpl(moAccess);
124       }
125     }
126   }
127 }
128
Popular Tags