blob: e887eabb20ee5df64f403e3e2fb18bcc50d47185 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.jmx.snmp.agent;
27
28import java.io.Serializable;
29import java.util.Enumeration;
30import java.util.logging.Level;
31
32import javax.management.ObjectName;
33import javax.management.MBeanServer;
34
35import static com.sun.jmx.defaults.JmxProperties.SNMP_ADAPTOR_LOGGER;
36import com.sun.jmx.snmp.SnmpStatusException;
37import com.sun.jmx.snmp.SnmpDefinitions;
38import com.sun.jmx.snmp.SnmpVarBind;
39
40/**
41 * A simple MIB agent that implements SNMP calls (get, set, getnext and getbulk) in a way that only errors or exceptions are returned. Every call done on this agent fails. Error handling is done according to the manager's SNMP protocol version.
42 * <P>It is used by <CODE>SnmpAdaptorServer</CODE> for its default agent behavior. When a received Oid doesn't match, this agent is called to fill the result list with errors.</P>
43 * <p><b>This API is a Sun Microsystems internal API and is subject
44 * to change without notice.</b></p>
45 * @since 1.5
46 *
47 */
48
49public class SnmpErrorHandlerAgent extends SnmpMibAgent
50 implements Serializable {
51 private static final long serialVersionUID = 7751082923508885650L;
52
53 public SnmpErrorHandlerAgent() {}
54
55 /**
56 * Initializes the MIB (with no registration of the MBeans into the
57 * MBean server). Does nothing.
58 *
59 * @exception IllegalAccessException The MIB cannot be initialized.
60 */
61
62 public void init() throws IllegalAccessException {
63 }
64
65 /**
66 * Initializes the MIB but each single MBean representing the MIB
67 * is inserted into the MBean server.
68 *
69 * @param server The MBean server to register the service with.
70 * @param name The object name.
71 *
72 * @return The passed name paramter.
73 *
74 * @exception java.lang.Exception
75 */
76
77 public ObjectName preRegister(MBeanServer server, ObjectName name)
78 throws Exception {
79 return name;
80 }
81
82 /**
83 * Gets the root object identifier of the MIB.
84 * <P>The root object identifier is the object identifier uniquely
85 * identifying the MIB.
86 *
87 * @return The returned oid is null.
88 */
89
90 public long[] getRootOid() {
91 return null;
92 }
93
94 /**
95 * Processes a <CODE>get</CODE> operation. It will throw an exception for V1 requests or it will set exceptions within the list for V2 requests.
96 *
97 * @param inRequest The SnmpMibRequest object holding the list of variable to be retrieved.
98 *
99 * @exception SnmpStatusException An error occured during the operation.
100 */
101
102 public void get(SnmpMibRequest inRequest) throws SnmpStatusException {
103
104 SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
105 SnmpErrorHandlerAgent.class.getName(),
106 "get", "Get in Exception");
107
108 if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
109 throw new SnmpStatusException(SnmpStatusException.noSuchName);
110
111 Enumeration l = inRequest.getElements();
112 while(l.hasMoreElements()) {
113 SnmpVarBind varbind = (SnmpVarBind) l.nextElement();
114 varbind.setNoSuchObject();
115 }
116 }
117
118 /**
119 * Checks if a <CODE>set</CODE> operation can be performed.
120 * If the operation can not be performed, the method should emit a
121 * <CODE>SnmpStatusException</CODE>.
122 *
123 * @param inRequest The SnmpMibRequest object holding the list of variables to
124 * be set. This list is composed of
125 * <CODE>SnmpVarBind</CODE> objects.
126 *
127 * @exception SnmpStatusException The <CODE>set</CODE> operation
128 * cannot be performed.
129 */
130
131 public void check(SnmpMibRequest inRequest) throws SnmpStatusException {
132
133 SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
134 SnmpErrorHandlerAgent.class.getName(),
135 "check", "Check in Exception");
136
137 throw new SnmpStatusException(SnmpDefinitions.snmpRspNotWritable);
138 }
139
140 /**
141 * Processes a <CODE>set</CODE> operation. Should never be called (check previously called having failed).
142 *
143 * @param inRequest The SnmpMibRequest object holding the list of variable to be set.
144 *
145 * @exception SnmpStatusException An error occured during the operation.
146 */
147
148 public void set(SnmpMibRequest inRequest) throws SnmpStatusException {
149
150 SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
151 SnmpErrorHandlerAgent.class.getName(),
152 "set", "Set in Exception, CANNOT be called");
153
154 throw new SnmpStatusException(SnmpDefinitions.snmpRspNotWritable);
155 }
156
157 /**
158 * Processes a <CODE>getNext</CODE> operation. It will throw an exception for V1 requests or it will set exceptions within the list for V2 requests..
159 *
160 * @param inRequest The SnmpMibRequest object holding the list of variables to be retrieved.
161 *
162 * @exception SnmpStatusException An error occured during the operation.
163 */
164
165 public void getNext(SnmpMibRequest inRequest) throws SnmpStatusException {
166
167 SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
168 SnmpErrorHandlerAgent.class.getName(),
169 "getNext", "GetNext in Exception");
170
171 if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
172 throw new SnmpStatusException(SnmpStatusException.noSuchName);
173
174 Enumeration l = inRequest.getElements();
175 while(l.hasMoreElements()) {
176 SnmpVarBind varbind = (SnmpVarBind) l.nextElement();
177 varbind.setEndOfMibView();
178 }
179 }
180
181 /**
182 * Processes a <CODE>getBulk</CODE> operation. It will throw an exception if the request is a V1 one or it will set exceptions within the list for V2 ones.
183 *
184 * @param inRequest The SnmpMibRequest object holding the list of variable to be retrieved.
185 *
186 * @exception SnmpStatusException An error occured during the operation.
187 */
188
189 public void getBulk(SnmpMibRequest inRequest, int nonRepeat, int maxRepeat)
190 throws SnmpStatusException {
191
192 SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
193 SnmpErrorHandlerAgent.class.getName(),
194 "getBulk", "GetBulk in Exception");
195
196 if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
197 throw new SnmpStatusException(SnmpDefinitions.snmpRspGenErr, 0);
198
199 Enumeration l = inRequest.getElements();
200 while(l.hasMoreElements()) {
201 SnmpVarBind varbind = (SnmpVarBind) l.nextElement();
202 varbind.setEndOfMibView();
203 }
204 }
205
206}