feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,151 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.Vector;
import javax.management.ObjectName;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.NotCompliantMBeanException;
import static com.sun.jmx.defaults.JmxProperties.SNMP_ADAPTOR_LOGGER;
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.SnmpVarBind;
import com.sun.jmx.snmp.SnmpDefinitions;
import com.sun.jmx.snmp.SnmpStatusException;
import com.sun.jmx.snmp.SnmpEngine;
import com.sun.jmx.snmp.SnmpUnknownModelException;
import com.sun.jmx.snmp.internal.SnmpAccessControlModel;
import com.sun.jmx.snmp.internal.SnmpEngineImpl;
/**
* Oid Checker makes use of ACM to check each OID during the getnext process.
*/
class AcmChecker {
SnmpAccessControlModel model = null;
String principal = null;
int securityLevel = -1;
int version = -1;
int pduType = -1;
int securityModel = -1;
byte[] contextName = null;
SnmpEngineImpl engine = null;
LongList l = null;
AcmChecker(SnmpMibRequest req) {
engine = (SnmpEngineImpl) req.getEngine();
//We are in V3 architecture, ACM is in the picture.
if(engine != null) {
if(engine.isCheckOidActivated()) {
try {
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"AcmChecker(SnmpMibRequest)",
"SNMP V3 Access Control to be done");
}
model = (SnmpAccessControlModel)
engine.getAccessControlSubSystem().
getModel(SnmpDefinitions.snmpVersionThree);
principal = req.getPrincipal();
securityLevel = req.getSecurityLevel();
pduType = req.getPdu().type;
version = req.getRequestPduVersion();
securityModel = req.getSecurityModel();
contextName = req.getAccessContextName();
l = new LongList();
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
final StringBuilder strb = new StringBuilder()
.append("Will check oid for : principal : ")
.append(principal)
.append("; securityLevel : ").append(securityLevel)
.append("; pduType : ").append(pduType)
.append("; version : ").append(version)
.append("; securityModel : ").append(securityModel)
.append("; contextName : ").append(contextName);
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"AcmChecker(SnmpMibRequest)", strb.toString());
}
}catch(SnmpUnknownModelException e) {
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"AcmChecker(SnmpMibRequest)",
"Unknown Model, no ACM check.");
}
}
}
}
}
void add(int index, long arc) {
if(model != null)
l.add(index, arc);
}
void remove(int index) {
if(model != null)
l.remove(index);
}
void add(final int at,final long[] src, final int from,
final int count) {
if(model != null)
l.add(at,src,from,count);
}
void remove(final int from, final int count) {
if(model != null)
l.remove(from,count);
}
void checkCurrentOid() throws SnmpStatusException {
if(model != null) {
SnmpOid oid = new SnmpOid(l.toArray());
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, SnmpMib.class.getName(),
"checkCurrentOid", "Checking access for : " + oid);
}
model.checkAccess(version,
principal,
securityLevel,
pduType,
securityModel,
contextName,
oid);
}
}
}

View File

@@ -0,0 +1,235 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.Vector;
import javax.management.ObjectName;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.NotCompliantMBeanException;
import static com.sun.jmx.defaults.JmxProperties.SNMP_ADAPTOR_LOGGER;
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.SnmpVarBind;
import com.sun.jmx.snmp.SnmpDefinitions;
import com.sun.jmx.snmp.SnmpStatusException;
import com.sun.jmx.snmp.SnmpEngine;
import com.sun.jmx.snmp.SnmpUnknownModelException;
import com.sun.jmx.snmp.internal.SnmpAccessControlModel;
import com.sun.jmx.snmp.internal.SnmpEngineImpl;
/**
* This list is used in order to construct the OID during the getnext.
* The constructed oid is checked by the checker AcmChecker.
*/
final class LongList {
public static int DEFAULT_CAPACITY = 10;
public static int DEFAULT_INCREMENT = 10;
private final int DELTA;
private int size;
/**
* The list content. Any access to this variable must be protected
* by a synchronized block on the LongList object.
* Only read-only action should be performed on this object.
**/
public long[] list;
LongList() {
this(DEFAULT_CAPACITY,DEFAULT_INCREMENT);
}
LongList(int initialCapacity) {
this(initialCapacity,DEFAULT_INCREMENT);
}
LongList(int initialCapacity, int delta) {
size = 0;
DELTA = delta;
list = allocate(initialCapacity);
}
/**
* Same behaviour than size() in {@link java.util.List}.
**/
public final int size() { return size;}
/**
* Same behaviour than add(long o) in {@link java.util.List}.
* Any access to this method should be protected in a synchronized
* block on the LongList object.
**/
public final boolean add(final long o) {
if (size >= list.length)
resize();
list[size++]=o;
return true;
}
/**
* Same behaviour than add(int index, long o) in
* {@link java.util.List}.
* Any access to this method should be protected in a synchronized
* block on the LongList object.
**/
public final void add(final int index, final long o) {
if (index > size) throw new IndexOutOfBoundsException();
if (index >= list.length) resize();
if (index == size) {
list[size++]=o;
return;
}
java.lang.System.arraycopy(list,index,list,index+1,size-index);
list[index]=o;
size++;
}
/**
* Adds <var>count</var> elements to the list.
* @param at index at which the elements must be inserted. The
* first element will be inserted at this index.
* @param src An array containing the elements we want to insert.
* @param from Index of the first element from <var>src</var> that
* must be inserted.
* @param count number of elements to insert.
* Any access to this method should be protected in a synchronized
* block on the LongList object.
**/
public final void add(final int at,final long[] src, final int from,
final int count) {
if (count <= 0) return;
if (at > size) throw new IndexOutOfBoundsException();
ensure(size+count);
if (at < size) {
java.lang.System.arraycopy(list,at,list,at+count,size-at);
}
java.lang.System.arraycopy(src,from,list,at,count);
size+=count;
}
/**
* Any access to this method should be protected in a synchronized
* block on the LongList object.
**/
public final long remove(final int from, final int count) {
if (count < 1 || from < 0) return -1;
if (from+count > size) return -1;
final long o = list[from];
final int oldsize = size;
size = size - count;
if (from == size) return o;
java.lang.System.arraycopy(list,from+count,list,from,
size-from);
return o;
}
/**
* Same behaviour than remove(int index) in {@link java.util.List}.
* Any access to this method should be protected in a synchronized
* block on the LongList object.
**/
public final long remove(final int index) {
if (index >= size) return -1;
final long o = list[index];
list[index]=0;
if (index == --size) return o;
java.lang.System.arraycopy(list,index+1,list,index,
size-index);
return o;
}
/**
* Same behaviour than the toArray(long[] a) method in
* {@link java.util.List}.
* Any access to this method should be protected in a synchronized
* block on the LongList object.
**/
public final long[] toArray(long[] a) {
java.lang.System.arraycopy(list,0,a,0,size);
return a;
}
/**
* Same behaviour than the toArray() method in
* {@link java.util.List}.
* Any access to this method should be protected in a synchronized
* block on the LongList object.
**/
public final long[] toArray() {
return toArray(new long[size]);
}
/**
* Resize the list. Increase its capacity by DELTA elements.
* Any call to this method must be protected by a synchronized
* block on this LongList.
**/
private final void resize() {
final long[] newlist = allocate(list.length + DELTA);
java.lang.System.arraycopy(list,0,newlist,0,size);
list = newlist;
}
/**
* Resize the list. Insure that the new length will be at
* least equal to <var>length</var>.
* @param length new minimal length requested.
* Any call to this method must be protected by a synchronized
* block on this LongList.
**/
private final void ensure(int length) {
if (list.length < length) {
final int min = list.length+DELTA;
length=(length<min)?min:length;
final long[] newlist = allocate(length);
java.lang.System.arraycopy(list,0,newlist,0,size);
list = newlist;
}
}
/**
* Allocate a new array of object of specified length.
**/
private final long[] allocate(final int length) {
return new long[length];
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
import com.sun.jmx.snmp.SnmpOid;
/**
* This class only adds a new constructor to SnmpOid...
*
**/
class SnmpEntryOid extends SnmpOid {
private static final long serialVersionUID = 9212653887791059564L;
/**
* Constructs a new <CODE>SnmpOid</CODE> from the specified
* component array, starting at given position.
*
* @param oid The original OID array
* @param start The position at which to begin.
*
**/
public SnmpEntryOid(long[] oid, int start) {
final int subLength = oid.length - start;
final long[] subOid = new long[subLength];
java.lang.System.arraycopy(oid, start, subOid, 0, subLength) ;
components = subOid;
componentCount = subLength;
}
}

View File

@@ -0,0 +1,214 @@
/*
* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.logging.Level;
import javax.management.ObjectName;
import javax.management.MBeanServer;
import static com.sun.jmx.defaults.JmxProperties.SNMP_ADAPTOR_LOGGER;
import com.sun.jmx.snmp.SnmpStatusException;
import com.sun.jmx.snmp.SnmpDefinitions;
import com.sun.jmx.snmp.SnmpVarBind;
/**
* 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.
* <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>
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
* @since 1.5
*
*/
public class SnmpErrorHandlerAgent extends SnmpMibAgent
implements Serializable {
private static final long serialVersionUID = 7751082923508885650L;
public SnmpErrorHandlerAgent() {}
/**
* Initializes the MIB (with no registration of the MBeans into the
* MBean server). Does nothing.
*
* @exception IllegalAccessException The MIB cannot be initialized.
*/
@Override
public void init() throws IllegalAccessException {
}
/**
* Initializes the MIB but each single MBean representing the MIB
* is inserted into the MBean server.
*
* @param server The MBean server to register the service with.
* @param name The object name.
*
* @return The passed name parameter.
*
* @exception java.lang.Exception
*/
@Override
public ObjectName preRegister(MBeanServer server, ObjectName name)
throws Exception {
return name;
}
/**
* Gets the root object identifier of the MIB.
* <P>The root object identifier is the object identifier uniquely
* identifying the MIB.
*
* @return The returned oid is null.
*/
@Override
public long[] getRootOid() {
return null;
}
/**
* 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.
*
* @param inRequest The SnmpMibRequest object holding the list of variable to be retrieved.
*
* @exception SnmpStatusException An error occurred during the operation.
*/
@Override
public void get(SnmpMibRequest inRequest) throws SnmpStatusException {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpErrorHandlerAgent.class.getName(),
"get", "Get in Exception");
if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
throw new SnmpStatusException(SnmpStatusException.noSuchName);
Enumeration<SnmpVarBind> l = inRequest.getElements();
while(l.hasMoreElements()) {
SnmpVarBind varbind = l.nextElement();
varbind.setNoSuchObject();
}
}
/**
* Checks if a <CODE>set</CODE> operation can be performed.
* If the operation can not be performed, the method should emit a
* <CODE>SnmpStatusException</CODE>.
*
* @param inRequest The SnmpMibRequest object holding the list of variables to
* be set. This list is composed of
* <CODE>SnmpVarBind</CODE> objects.
*
* @exception SnmpStatusException The <CODE>set</CODE> operation
* cannot be performed.
*/
@Override
public void check(SnmpMibRequest inRequest) throws SnmpStatusException {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpErrorHandlerAgent.class.getName(),
"check", "Check in Exception");
throw new SnmpStatusException(SnmpDefinitions.snmpRspNotWritable);
}
/**
* Processes a <CODE>set</CODE> operation. Should never be called (check previously called having failed).
*
* @param inRequest The SnmpMibRequest object holding the list of variable to be set.
*
* @exception SnmpStatusException An error occurred during the operation.
*/
@Override
public void set(SnmpMibRequest inRequest) throws SnmpStatusException {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpErrorHandlerAgent.class.getName(),
"set", "Set in Exception, CANNOT be called");
throw new SnmpStatusException(SnmpDefinitions.snmpRspNotWritable);
}
/**
* 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..
*
* @param inRequest The SnmpMibRequest object holding the list of variables to be retrieved.
*
* @exception SnmpStatusException An error occurred during the operation.
*/
@Override
public void getNext(SnmpMibRequest inRequest) throws SnmpStatusException {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpErrorHandlerAgent.class.getName(),
"getNext", "GetNext in Exception");
if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
throw new SnmpStatusException(SnmpStatusException.noSuchName);
Enumeration<SnmpVarBind> l = inRequest.getElements();
while(l.hasMoreElements()) {
SnmpVarBind varbind = l.nextElement();
varbind.setEndOfMibView();
}
}
/**
* 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.
*
* @param inRequest The SnmpMibRequest object holding the list of variable to be retrieved.
*
* @exception SnmpStatusException An error occurred during the operation.
*/
@Override
public void getBulk(SnmpMibRequest inRequest, int nonRepeat, int maxRepeat)
throws SnmpStatusException {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpErrorHandlerAgent.class.getName(),
"getBulk", "GetBulk in Exception");
if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
throw new SnmpStatusException(SnmpDefinitions.snmpRspGenErr, 0);
Enumeration<SnmpVarBind> l = inRequest.getElements();
while(l.hasMoreElements()) {
SnmpVarBind varbind = l.nextElement();
varbind.setEndOfMibView();
}
}
}

View File

@@ -0,0 +1,132 @@
/*
* Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// jmx imports
//
import com.sun.jmx.snmp.SnmpValue;
import com.sun.jmx.snmp.SnmpStatusException;
/**
* <p>
* This interface defines the methods that must be implemented by an
* SNMP metadata object that needs to interact with an
* {@link com.sun.jmx.snmp.agent.SnmpGenericObjectServer} object.
* </p>
*
* <p>
* All these methods are usually generated by <code>mibgen</code> when
* run in generic-metadata mode.
* </p>
*
* <p><b><i>
* This interface is used internally between the generated Metadata and
* the SNMP runtime and you shouldn't need to worry about it, because
* you will never have to use it directly.
* </b></i></p>
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
**/
public interface SnmpGenericMetaServer {
/**
* Construct an attribute value (as returned by Attribute::getValue())
* from an SnmpValue. The returned attribute value can be used to
* construct an Attribute object.
*
* @param id The OID arc identifying the variable for which the
* value is constructed.
* @param value The SnmpValue from which the Attribute::value will be
* constructed.
* @return The attribute value built from the given <code>value</code>.
* @exception SnmpStatusException if the attribute value cannot be built
* from the given SnmpValue <code>value</code>.
*
*/
Object buildAttributeValue(long id, SnmpValue value)
throws SnmpStatusException;
/**
* Construct an SnmpValue from an Attribute value as returned by
* Attribute::getValue().
*
* @param id The OID arc identifying the variable for which the
* value is constructed.
* @param value The attribute value as returned by Attribute::getValue().
*
* @return The SnmpValue built from the given <code>value</code>.
* @exception SnmpStatusException if the SnmpValue cannot be built from
* the given <code>value</code>.
**/
SnmpValue buildSnmpValue(long id, Object value)
throws SnmpStatusException;
/**
* Return the name of the attribute corresponding to the
* SNMP variable identified by the given <code>id</code>.
*
* @param id The OID arc identifying the variable.
*
* @return The name of the variable identified by the given
* <code>id</code>.
*
* @exception SnmpStatusException if the given <code>id</code> does not
* correspond to a known variable.
*/
String getAttributeName(long id)
throws SnmpStatusException;
/**
* Check the access rights for a SET operation.
*
* @param x The new requested value.
* @param id The OID arc identifying the variable for which the SET is
* requested.
* @param data A contextual object containing user-data.
* This object is allocated through the <code>
* {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}</code>
* for each incoming SNMP request.
* @exception SnmpStatusException if the SET operation must be rejected.
*/
void checkSetAccess(SnmpValue x, long id, Object data)
throws SnmpStatusException;
/**
* Check the access rights for a GET operation.
*
* @param id The OID arc identifying the variable for which the SET is
* requested.
* @param data A contextual object containing user-data.
* This object is allocated through the <code>
* {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}</code>
* for each incoming SNMP request.
* @exception SnmpStatusException if the SET operation must be rejected.
*/
void checkGetAccess(long id, Object data)
throws SnmpStatusException;
}

View File

@@ -0,0 +1,572 @@
/*
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// java imports
//
import java.util.Enumeration;
import java.util.Iterator;
// jmx imports
//
import javax.management.AttributeList;
import javax.management.Attribute;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.InstanceNotFoundException;
import javax.management.InvalidAttributeValueException;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.NotCompliantMBeanException;
import javax.management.RuntimeOperationsException;
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.SnmpValue;
import com.sun.jmx.snmp.SnmpVarBind;
import com.sun.jmx.snmp.SnmpStatusException;
/**
* <p>
* This class is a utility class that transforms SNMP GET / SET requests
* into standard JMX getAttributes() setAttributes() requests.
* </p>
*
* <p>
* The transformation relies on the metadata information provided by the
* {@link com.sun.jmx.snmp.agent.SnmpGenericMetaServer} object which is
* passed as the first parameter to every method. This SnmpGenericMetaServer
* object is usually a Metadata object generated by <code>mibgen</code>.
* </p>
*
* <p><b><i>
* This class is used internally by mibgen generated metadata objects and
* you should never need to use it directly.
* </b></i></p>
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
**/
public class SnmpGenericObjectServer {
// ----------------------------------------------------------------------
//
// Protected variables
//
// ----------------------------------------------------------------------
/**
* The MBean server through which the MBeans will be accessed.
**/
protected final MBeanServer server;
// ----------------------------------------------------------------------
//
// Constructors
//
// ----------------------------------------------------------------------
/**
* Builds a new SnmpGenericObjectServer. Usually there will be a single
* object of this type per MIB.
*
* @param server The MBeanServer in which the MBean accessed by this
* MIB are registered.
**/
public SnmpGenericObjectServer(MBeanServer server) {
this.server = server;
}
/**
* Execute an SNMP GET request.
*
* <p>
* This method first builds the list of attributes that need to be
* retrieved from the MBean and then calls getAttributes() on the
* MBean server. Then it updates the SnmpMibSubRequest with the values
* retrieved from the MBean.
* </p>
*
* <p>
* The SNMP metadata information is obtained through the given
* <code>meta</code> object, which usually is an instance of a
* <code>mibgen</code> generated class.
* </p>
*
* <p><b><i>
* This method is called internally by <code>mibgen</code> generated
* objects and you should never need to call it directly.
* </i></b></p>
*
* @param meta The metadata object impacted by the subrequest
* @param name The ObjectName of the MBean impacted by this subrequest
* @param req The SNMP subrequest to execute on the MBean
* @param depth The depth of the SNMP object in the OID tree.
*
* @exception SnmpStatusException whenever an SNMP exception must be
* raised. Raising an exception will abort the request.<br>
* Exceptions should never be raised directly, but only by means of
* <code>
* req.registerGetException(<i>VariableId</i>,<i>SnmpStatusException</i>)
* </code>
**/
public void get(SnmpGenericMetaServer meta, ObjectName name,
SnmpMibSubRequest req, int depth)
throws SnmpStatusException {
// java.lang.System.out.println(">>>>>>>>> GET " + name);
final int size = req.getSize();
final Object data = req.getUserData();
final String[] nameList = new String[size];
final SnmpVarBind[] varList = new SnmpVarBind[size];
final long[] idList = new long[size];
int i = 0;
for (Enumeration<SnmpVarBind> e=req.getElements(); e.hasMoreElements();) {
final SnmpVarBind var= e.nextElement();
try {
final long id = var.oid.getOidArc(depth);
nameList[i] = meta.getAttributeName(id);
varList[i] = var;
idList[i] = id;
// Check the access rights according to the MIB.
// The MBean might be less restrictive (have a getter
// while the MIB defines the variable as AFN)
//
meta.checkGetAccess(id,data);
//java.lang.System.out.println(nameList[i] + " added.");
i++;
} catch(SnmpStatusException x) {
//java.lang.System.out.println("exception for " + nameList[i]);
//x.printStackTrace();
req.registerGetException(var,x);
}
}
AttributeList result = null;
int errorCode = SnmpStatusException.noSuchInstance;
try {
result = server.getAttributes(name,nameList);
} catch (InstanceNotFoundException f) {
//java.lang.System.out.println(name + ": instance not found.");
//f.printStackTrace();
result = new AttributeList();
} catch (ReflectionException r) {
//java.lang.System.out.println(name + ": reflexion error.");
//r.printStackTrace();
result = new AttributeList();
} catch (Exception x) {
result = new AttributeList();
}
final Iterator<?> it = result.iterator();
for (int j=0; j < i; j++) {
if (!it.hasNext()) {
//java.lang.System.out.println(name + "variable[" + j +
// "] absent");
final SnmpStatusException x =
new SnmpStatusException(errorCode);
req.registerGetException(varList[j],x);
continue;
}
final Attribute att = (Attribute) it.next();
while ((j < i) && (! nameList[j].equals(att.getName()))) {
//java.lang.System.out.println(name + "variable[" +j +
// "] not found");
final SnmpStatusException x =
new SnmpStatusException(errorCode);
req.registerGetException(varList[j],x);
j++;
}
if ( j == i) break;
try {
varList[j].value =
meta.buildSnmpValue(idList[j],att.getValue());
} catch (SnmpStatusException x) {
req.registerGetException(varList[j],x);
}
//java.lang.System.out.println(att.getName() + " retrieved.");
}
//java.lang.System.out.println(">>>>>>>>> END GET");
}
/**
* Get the value of an SNMP variable.
*
* <p><b><i>
* You should never need to use this method directly.
* </i></b></p>
*
* @param meta The impacted metadata object
* @param name The ObjectName of the impacted MBean
* @param id The OID arc identifying the variable we're trying to set.
* @param data User contextual data allocated through the
* {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}
*
* @return The value of the variable.
*
* @exception SnmpStatusException whenever an SNMP exception must be
* raised. Raising an exception will abort the request. <br>
* Exceptions should never be raised directly, but only by means of
* <code>
* req.registerGetException(<i>VariableId</i>,<i>SnmpStatusException</i>)
* </code>
**/
public SnmpValue get(SnmpGenericMetaServer meta, ObjectName name,
long id, Object data)
throws SnmpStatusException {
final String attname = meta.getAttributeName(id);
Object result = null;
try {
result = server.getAttribute(name,attname);
} catch (MBeanException m) {
Exception t = m.getTargetException();
if (t instanceof SnmpStatusException)
throw (SnmpStatusException) t;
throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
} catch (Exception e) {
throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
}
return meta.buildSnmpValue(id,result);
}
/**
* Execute an SNMP SET request.
*
* <p>
* This method first builds the list of attributes that need to be
* set on the MBean and then calls setAttributes() on the
* MBean server. Then it updates the SnmpMibSubRequest with the new
* values retrieved from the MBean.
* </p>
*
* <p>
* The SNMP metadata information is obtained through the given
* <code>meta</code> object, which usually is an instance of a
* <code>mibgen</code> generated class.
* </p>
*
* <p><b><i>
* This method is called internally by <code>mibgen</code> generated
* objects and you should never need to call it directly.
* </i></b></p>
*
* @param meta The metadata object impacted by the subrequest
* @param name The ObjectName of the MBean impacted by this subrequest
* @param req The SNMP subrequest to execute on the MBean
* @param depth The depth of the SNMP object in the OID tree.
*
* @exception SnmpStatusException whenever an SNMP exception must be
* raised. Raising an exception will abort the request. <br>
* Exceptions should never be raised directly, but only by means of
* <code>
* req.registerGetException(<i>VariableId</i>,<i>SnmpStatusException</i>)
* </code>
**/
public void set(SnmpGenericMetaServer meta, ObjectName name,
SnmpMibSubRequest req, int depth)
throws SnmpStatusException {
final int size = req.getSize();
final AttributeList attList = new AttributeList(size);
final String[] nameList = new String[size];
final SnmpVarBind[] varList = new SnmpVarBind[size];
final long[] idList = new long[size];
int i = 0;
for (Enumeration<SnmpVarBind> e=req.getElements(); e.hasMoreElements();) {
final SnmpVarBind var= e.nextElement();
try {
final long id = var.oid.getOidArc(depth);
final String attname = meta.getAttributeName(id);
final Object attvalue=
meta.buildAttributeValue(id,var.value);
final Attribute att = new Attribute(attname,attvalue);
attList.add(att);
nameList[i] = attname;
varList[i] = var;
idList[i] = id;
i++;
} catch(SnmpStatusException x) {
req.registerSetException(var,x);
}
}
AttributeList result;
int errorCode = SnmpStatusException.noAccess;
try {
result = server.setAttributes(name,attList);
} catch (InstanceNotFoundException f) {
result = new AttributeList();
errorCode = SnmpStatusException.snmpRspInconsistentName;
} catch (ReflectionException r) {
errorCode = SnmpStatusException.snmpRspInconsistentName;
result = new AttributeList();
} catch (Exception x) {
result = new AttributeList();
}
final Iterator<?> it = result.iterator();
for (int j=0; j < i; j++) {
if (!it.hasNext()) {
final SnmpStatusException x =
new SnmpStatusException(errorCode);
req.registerSetException(varList[j],x);
continue;
}
final Attribute att = (Attribute) it.next();
while ((j < i) && (! nameList[j].equals(att.getName()))) {
final SnmpStatusException x =
new SnmpStatusException(SnmpStatusException.noAccess);
req.registerSetException(varList[j],x);
j++;
}
if ( j == i) break;
try {
varList[j].value =
meta.buildSnmpValue(idList[j],att.getValue());
} catch (SnmpStatusException x) {
req.registerSetException(varList[j],x);
}
}
}
/**
* Set the value of an SNMP variable.
*
* <p><b><i>
* You should never need to use this method directly.
* </i></b></p>
*
* @param meta The impacted metadata object
* @param name The ObjectName of the impacted MBean
* @param x The new requested SnmpValue
* @param id The OID arc identifying the variable we're trying to set.
* @param data User contextual data allocated through the
* {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}
*
* @return The new value of the variable after the operation.
*
* @exception SnmpStatusException whenever an SNMP exception must be
* raised. Raising an exception will abort the request. <br>
* Exceptions should never be raised directly, but only by means of
* <code>
* req.registerSetException(<i>VariableId</i>,<i>SnmpStatusException</i>)
* </code>
**/
public SnmpValue set(SnmpGenericMetaServer meta, ObjectName name,
SnmpValue x, long id, Object data)
throws SnmpStatusException {
final String attname = meta.getAttributeName(id);
final Object attvalue=
meta.buildAttributeValue(id,x);
final Attribute att = new Attribute(attname,attvalue);
Object result = null;
try {
server.setAttribute(name,att);
result = server.getAttribute(name,attname);
} catch(InvalidAttributeValueException iv) {
throw new
SnmpStatusException(SnmpStatusException.snmpRspWrongValue);
} catch (InstanceNotFoundException f) {
throw new
SnmpStatusException(SnmpStatusException.snmpRspInconsistentName);
} catch (ReflectionException r) {
throw new
SnmpStatusException(SnmpStatusException.snmpRspInconsistentName);
} catch (MBeanException m) {
Exception t = m.getTargetException();
if (t instanceof SnmpStatusException)
throw (SnmpStatusException) t;
throw new
SnmpStatusException(SnmpStatusException.noAccess);
} catch (Exception e) {
throw new
SnmpStatusException(SnmpStatusException.noAccess);
}
return meta.buildSnmpValue(id,result);
}
/**
* Checks whether an SNMP SET request can be successfully performed.
*
* <p>
* For each variable in the subrequest, this method calls
* checkSetAccess() on the meta object, and then tries to invoke the
* check<i>AttributeName</i>() method on the MBean. If this method
* is not defined then it is assumed that the SET won't fail.
* </p>
*
* <p><b><i>
* This method is called internally by <code>mibgen</code> generated
* objects and you should never need to call it directly.
* </i></b></p>
*
* @param meta The metadata object impacted by the subrequest
* @param name The ObjectName of the MBean impacted by this subrequest
* @param req The SNMP subrequest to execute on the MBean
* @param depth The depth of the SNMP object in the OID tree.
*
* @exception SnmpStatusException if the requested SET operation must
* be rejected. Raising an exception will abort the request. <br>
* Exceptions should never be raised directly, but only by means of
* <code>
* req.registerCheckException(<i>VariableId</i>,<i>SnmpStatusException</i>)
* </code>
*
**/
public void check(SnmpGenericMetaServer meta, ObjectName name,
SnmpMibSubRequest req, int depth)
throws SnmpStatusException {
final Object data = req.getUserData();
for (Enumeration<SnmpVarBind> e=req.getElements(); e.hasMoreElements();) {
final SnmpVarBind var= e.nextElement();
try {
final long id = var.oid.getOidArc(depth);
// call meta.check() here, and meta.check will call check()
check(meta,name,var.value,id,data);
} catch(SnmpStatusException x) {
req.registerCheckException(var,x);
}
}
}
/**
* Checks whether a SET operation can be performed on a given SNMP
* variable.
*
* @param meta The impacted metadata object
* @param name The ObjectName of the impacted MBean
* @param x The new requested SnmpValue
* @param id The OID arc identifying the variable we're trying to set.
* @param data User contextual data allocated through the
* {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}
*
* <p>
* This method calls checkSetAccess() on the meta object, and then
* tries to invoke the check<i>AttributeName</i>() method on the MBean.
* If this method is not defined then it is assumed that the SET
* won't fail.
* </p>
*
* <p><b><i>
* This method is called internally by <code>mibgen</code> generated
* objects and you should never need to call it directly.
* </i></b></p>
*
* @exception SnmpStatusException if the requested SET operation must
* be rejected. Raising an exception will abort the request. <br>
* Exceptions should never be raised directly, but only by means of
* <code>
* req.registerCheckException(<i>VariableId</i>,<i>SnmpStatusException</i>)
* </code>
*
**/
// XXX xxx ZZZ zzz Maybe we should go through the MBeanInfo here?
public void check(SnmpGenericMetaServer meta, ObjectName name,
SnmpValue x, long id, Object data)
throws SnmpStatusException {
meta.checkSetAccess(x,id,data);
try {
final String attname = meta.getAttributeName(id);
final Object attvalue= meta.buildAttributeValue(id,x);
final Object[] params = new Object[1];
final String[] signature = new String[1];
params[0] = attvalue;
signature[0] = attvalue.getClass().getName();
server.invoke(name,"check"+attname,params,signature);
} catch( SnmpStatusException e) {
throw e;
}
catch (InstanceNotFoundException i) {
throw new
SnmpStatusException(SnmpStatusException.snmpRspInconsistentName);
} catch (ReflectionException r) {
// checkXXXX() not defined => do nothing
} catch (MBeanException m) {
Exception t = m.getTargetException();
if (t instanceof SnmpStatusException)
throw (SnmpStatusException) t;
throw new SnmpStatusException(SnmpStatusException.noAccess);
} catch (Exception e) {
throw new
SnmpStatusException(SnmpStatusException.noAccess);
}
}
public void registerTableEntry(SnmpMibTable meta, SnmpOid rowOid,
ObjectName objname, Object entry)
throws SnmpStatusException {
if (objname == null)
throw new
SnmpStatusException(SnmpStatusException.snmpRspInconsistentName);
try {
if (entry != null && !server.isRegistered(objname))
server.registerMBean(entry, objname);
} catch (InstanceAlreadyExistsException e) {
throw new
SnmpStatusException(SnmpStatusException.snmpRspInconsistentName);
} catch (MBeanRegistrationException e) {
throw new SnmpStatusException(SnmpStatusException.snmpRspNoAccess);
} catch (NotCompliantMBeanException e) {
throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr);
} catch (RuntimeOperationsException e) {
throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr);
} catch(Exception e) {
throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr);
}
}
}

View File

@@ -0,0 +1,191 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// java imports
//
import java.io.Serializable;
import java.util.Vector;
import java.util.Enumeration;
// jmx imports
//
import com.sun.jmx.snmp.SnmpOid;
/**
* Represents a SNMP index.
* An <CODE>SnmpIndex</CODE> is represented as a <CODE>Vector</CODE> of <CODE>SnmpOid</CODE>.
* <P>
* This class is used internally and by the classes generated by <CODE>mibgen</CODE>.
* You should not need to use this class directly.
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
*/
public class SnmpIndex implements Serializable {
private static final long serialVersionUID = 8712159739982192146L;
/**
* Initializes an <CODE>SnmpIndex</CODE> using a vector of object identifiers.
* <P>Following the RFC recommendations, every syntax that is used as a
* table index should have an object identifier representation. There are
* some guidelines on how to map the different syntaxes into an object identifier.
* In the different <CODE>SnmpValue</CODE> classes provided, there is a <CODE>toOid</CODE> method to get
* the object identifier of the value.
*
* @param oidList The list of Object Identifiers.
*/
public SnmpIndex(SnmpOid[] oidList) {
size= oidList.length;
for(int i= 0; i <size; i++) {
// The order is important ...
//
oids.addElement(oidList[i]);
}
}
/**
* Initializes an <CODE>SnmpIndex</CODE> using the specified Object Identifier.
*
* @param oid The Object Identifier.
*/
public SnmpIndex(SnmpOid oid) {
oids.addElement(oid);
size= 1;
}
/**
* Gets the number of Object Identifiers the index is made of.
*
* @return The number of Object Identifiers.
*/
public int getNbComponents() {
return size;
}
/**
* Gets the index as a vector of Object Identifiers.
*
* @return The index as a vector.
*/
public Vector<SnmpOid> getComponents() {
return oids;
}
/**
* Compares two indexes for equality.
*
* @param index The index to compare <CODE>this</CODE> with.
*
* @return <CODE>true</CODE> if the two indexes are equal, <CODE>false</CODE> otherwise.
*/
public boolean equals(SnmpIndex index) {
if (size != index.getNbComponents())
return false;
// The two vectors have the same length.
// Compare each single element ...
//
SnmpOid oid1;
SnmpOid oid2;
Vector<SnmpOid> components= index.getComponents();
for(int i=0; i <size; i++) {
oid1= oids.elementAt(i);
oid2= components.elementAt(i);
if (oid1.equals(oid2) == false)
return false;
}
return true;
}
/**
* Compares two indexes.
*
* @param index The index to compare <CODE>this</CODE> with.
*
* @return The value 0 if the two OID vectors have the same elements, another value otherwise.
*/
public int compareTo(SnmpIndex index) {
int length= index.getNbComponents();
Vector<SnmpOid> components= index.getComponents();
SnmpOid oid1;
SnmpOid oid2;
int comp;
for(int i=0; i < size; i++) {
if ( i > length) {
// There is no more element in the index
//
return 1;
}
// Access the element ...
//
oid1= oids.elementAt(i);
oid2= components.elementAt(i);
comp= oid1.compareTo(oid2);
if (comp == 0)
continue;
return comp;
}
return 0;
}
/**
* Returns a <CODE>String</CODE> representation of the index.
* The different elements are separated by "//".
*
* @return A string representation of the index.
*/
@Override
public String toString() {
final StringBuilder msg= new StringBuilder();
for(Enumeration<SnmpOid> e= oids.elements(); e.hasMoreElements(); ) {
SnmpOid val= e.nextElement();
msg.append("//").append( val.toString());
}
return msg.toString();
}
// PRIVATE VARIABLES
//------------------
/**
* The list of OIDs.
* @serial
*/
private Vector<SnmpOid> oids = new Vector<>();
/**
* The number of elements in the index.
* @serial
*/
private int size = 0;
}

View File

@@ -0,0 +1,737 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.Vector;
import javax.management.ObjectName;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.NotCompliantMBeanException;
import static com.sun.jmx.defaults.JmxProperties.SNMP_ADAPTOR_LOGGER;
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.SnmpVarBind;
import com.sun.jmx.snmp.SnmpDefinitions;
import com.sun.jmx.snmp.SnmpStatusException;
/**
* Abstract class for representing an SNMP MIB.
* <P>
* When compiling a SNMP MIB, among all the classes generated by
* <CODE>mibgen</CODE>, there is one which extends <CODE>SnmpMib</CODE>
* for representing a whole MIB.
* <BR>The class is used by the SNMP protocol adaptor as the entry point in
* the MIB.
*
* <p>This generated class can be subclassed in your code in order to
* plug in your own specific behaviour.
* </p>
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
*/
public abstract class SnmpMib extends SnmpMibAgent implements Serializable {
/**
* Default constructor.
* Initializes the OID tree.
*/
public SnmpMib() {
root= new SnmpMibOid();
}
// --------------------------------------------------------------------
// POLYMORHIC METHODS
// --------------------------------------------------------------------
/**
* <p>
* This callback should return the OID associated to the group
* identified by the given <code>groupName</code>.
* </p>
*
* <p>
* This method is provided as a hook to plug-in some custom
* specific behavior. Although doing so is discouraged you might
* want to subclass this method in order to store & provide more metadata
* information (mapping OID <-> symbolic name) within the agent,
* or to "change" the root of the MIB OID by prefixing the
* defaultOid by an application dependant OID string, for instance.
* </p>
*
* <p>
* The default implementation of this method is to return the given
* <code>defaultOid</code>
* </p>
*
* @param groupName The java-ized name of the SNMP group.
* @param defaultOid The OID defined in the MIB for that group
* (in dot notation).
*
* @return The OID of the group identified by <code>groupName</code>,
* in dot-notation.
*/
protected String getGroupOid(String groupName, String defaultOid) {
return defaultOid;
}
/**
* <p>
* This callback should return the ObjectName associated to the
* group identified by the given <code>groupName</code>.
* </p>
*
* <p>
* This method is provided as a hook to plug-in some custom
* specific behavior. You might want to override this method
* in order to provide a different object naming scheme than
* that proposed by default by <code>mibgen</code>.
* </p>
*
* <p>
* This method is only meaningful if the MIB is registered
* in the MBeanServer, otherwise, it will not be called.
* </p>
*
* <p>
* The default implementation of this method is to return an ObjectName
* built from the given <code>defaultName</code>.
* </p>
*
* @param name The java-ized name of the SNMP group.
* @param oid The OID returned by getGroupOid() - in dot notation.
* @param defaultName The name by default generated by <code>
* mibgen</code>
*
* @return The ObjectName of the group identified by <code>name</code>
*/
protected ObjectName getGroupObjectName(String name, String oid,
String defaultName)
throws MalformedObjectNameException {
return new ObjectName(defaultName);
}
/**
* <p>
* Register an SNMP group and its metadata node in the MIB.
* </p>
*
* <p>
* This method is provided as a hook to plug-in some custom
* specific behavior. You might want to override this method
* if you want to set special links between the MBean, its metadata
* node, its OID or ObjectName etc..
* </p>
*
* <p>
* If the MIB is not registered in the MBeanServer, the <code>
* server</code> and <code>groupObjName</code> parameters will be
* <code>null</code>.<br>
* If the given group MBean is not <code>null</code>, and if the
* <code>server</code> and <code>groupObjName</code> parameters are
* not null, then this method will also automatically register the
* group MBean with the given MBeanServer <code>server</code>.
* </p>
*
* @param groupName The java-ized name of the SNMP group.
* @param groupOid The OID as returned by getGroupOid() - in dot
* notation.
* @param groupObjName The ObjectName as returned by getGroupObjectName().
* This parameter may be <code>null</code> if the
* MIB is not registered in the MBeanServer.
* @param node The metadata node, as returned by the metadata
* factory method for this group.
* @param group The MBean for this group, as returned by the
* MBean factory method for this group.
* @param server The MBeanServer in which the groups are to be
* registered. This parameter will be <code>null</code>
* if the MIB is not registered, otherwise it is a
* reference to the MBeanServer in which the MIB is
* registered.
*
*/
protected void registerGroupNode(String groupName, String groupOid,
ObjectName groupObjName, SnmpMibNode node,
Object group, MBeanServer server)
throws NotCompliantMBeanException, MBeanRegistrationException,
InstanceAlreadyExistsException, IllegalAccessException {
root.registerNode(groupOid,node);
if (server != null && groupObjName != null && group != null)
server.registerMBean(group,groupObjName);
}
/**
* <p>
* Register an SNMP Table metadata node in the MIB.
* </p>
*
* <p>
* <b><i>
* This method is used internally and you should never need to
* call it directly.</i></b><br> It is used to establish the link
* between an SNMP table metadata node and its bean-like counterpart.
* <br>
* The group metadata nodes will create and register their
* underlying table metadata nodes in the MIB using this
* method. <br>
* The metadata nodes will be later retrieved from the MIB by the
* bean-like table objects using the getRegisterTableMeta() method.
* </p>
*
* @param name The java-ized name of the SNMP table.
* @param table The SNMP table metadata node - usually this
* corresponds to a <code>mibgen</code> generated
* object.
*/
public abstract void registerTableMeta(String name, SnmpMibTable table);
/**
* Returns a registered SNMP Table metadata node.
*
* <p><b><i>
* This method is used internally and you should never need to
* call it directly.
* </i></b></p>
*
*/
public abstract SnmpMibTable getRegisteredTableMeta(String name);
// --------------------------------------------------------------------
// PUBLIC METHODS
// --------------------------------------------------------------------
/**
* Processes a <CODE>get</CODE> operation.
*
**/
// Implements the method defined in SnmpMibAgent. See SnmpMibAgent
// for java-doc
//
@Override
public void get(SnmpMibRequest req) throws SnmpStatusException {
// Builds the request tree: creation is not allowed, operation
// is not atomic.
final int reqType = SnmpDefinitions.pduGetRequestPdu;
SnmpRequestTree handlers = getHandlers(req,false,false,reqType);
SnmpRequestTree.Handler h = null;
SnmpMibNode meta = null;
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, SnmpMib.class.getName(),
"get", "Processing handlers for GET... ");
}
// For each sub-request stored in the request-tree, invoke the
// get() method.
for (Enumeration<SnmpRequestTree.Handler> eh=handlers.getHandlers();eh.hasMoreElements();) {
h = eh.nextElement();
// Gets the Meta node. It can be either a Group Meta or a
// Table Meta.
//
meta = handlers.getMetaNode(h);
// Gets the depth of the Meta node in the OID tree
final int depth = handlers.getOidDepth(h);
for (Enumeration<SnmpMibSubRequest> rqs=handlers.getSubRequests(h);
rqs.hasMoreElements();) {
// Invoke the get() operation.
meta.get(rqs.nextElement(),depth);
}
}
}
/**
* Processes a <CODE>set</CODE> operation.
*
*/
// Implements the method defined in SnmpMibAgent. See SnmpMibAgent
// for java-doc
//
@Override
public void set(SnmpMibRequest req) throws SnmpStatusException {
SnmpRequestTree handlers = null;
// Optimization: we're going to get the whole SnmpRequestTree
// built in the "check" method, so that we don't have to rebuild
// it here.
//
if (req instanceof SnmpMibRequestImpl)
handlers = ((SnmpMibRequestImpl)req).getRequestTree();
// Optimization didn't work: we have to rebuild the tree.
//
// Builds the request tree: creation is not allowed, operation
// is atomic.
//
final int reqType = SnmpDefinitions.pduSetRequestPdu;
if (handlers == null) handlers = getHandlers(req,false,true,reqType);
handlers.switchCreationFlag(false);
handlers.setPduType(reqType);
SnmpRequestTree.Handler h;
SnmpMibNode meta;
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, SnmpMib.class.getName(),
"set", "Processing handlers for SET... ");
}
// For each sub-request stored in the request-tree, invoke the
// get() method.
for (Enumeration<SnmpRequestTree.Handler> eh=handlers.getHandlers();eh.hasMoreElements();) {
h = eh.nextElement();
// Gets the Meta node. It can be either a Group Meta or a
// Table Meta.
//
meta = handlers.getMetaNode(h);
// Gets the depth of the Meta node in the OID tree
final int depth = handlers.getOidDepth(h);
for (Enumeration<SnmpMibSubRequest> rqs=handlers.getSubRequests(h);
rqs.hasMoreElements();) {
// Invoke the set() operation
meta.set(rqs.nextElement(),depth);
}
}
}
/**
* Checks if a <CODE>set</CODE> operation can be performed.
* If the operation cannot be performed, the method will raise a
* <CODE>SnmpStatusException</CODE>.
*
*/
// Implements the method defined in SnmpMibAgent. See SnmpMibAgent
// for java-doc
//
@Override
public void check(SnmpMibRequest req) throws SnmpStatusException {
final int reqType = SnmpDefinitions.pduWalkRequest;
// Builds the request tree: creation is allowed, operation
// is atomic.
SnmpRequestTree handlers = getHandlers(req,true,true,reqType);
SnmpRequestTree.Handler h;
SnmpMibNode meta;
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, SnmpMib.class.getName(),
"check", "Processing handlers for CHECK... ");
}
// For each sub-request stored in the request-tree, invoke the
// check() method.
for (Enumeration<SnmpRequestTree.Handler> eh=handlers.getHandlers();eh.hasMoreElements();) {
h = eh.nextElement();
// Gets the Meta node. It can be either a Group Meta or a
// Table Meta.
//
meta = handlers.getMetaNode(h);
// Gets the depth of the Meta node in the OID tree
final int depth = handlers.getOidDepth(h);
for (Enumeration<SnmpMibSubRequest> rqs=handlers.getSubRequests(h);
rqs.hasMoreElements();) {
// Invoke the check() operation
meta.check(rqs.nextElement(),depth);
}
}
// Optimization: we're going to pass the whole SnmpRequestTree
// to the "set" method, so that we don't have to rebuild it there.
//
if (req instanceof SnmpMibRequestImpl) {
((SnmpMibRequestImpl)req).setRequestTree(handlers);
}
}
/**
* Processes a <CODE>getNext</CODE> operation.
*
*/
// Implements the method defined in SnmpMibAgent. See SnmpMibAgent
// for java-doc
//
@Override
public void getNext(SnmpMibRequest req) throws SnmpStatusException {
// Build the request tree for the operation
// The subrequest stored in the request tree are valid GET requests
SnmpRequestTree handlers = getGetNextHandlers(req);
SnmpRequestTree.Handler h;
SnmpMibNode meta;
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, SnmpMib.class.getName(),
"getNext", "Processing handlers for GET-NEXT... ");
}
// Now invoke get() for each subrequest of the request tree.
for (Enumeration<SnmpRequestTree.Handler> eh=handlers.getHandlers();eh.hasMoreElements();) {
h = eh.nextElement();
// Gets the Meta node. It can be either a Group Meta or a
// Table Meta.
//
meta = handlers.getMetaNode(h);
// Gets the depth of the Meta node in the OID tree
int depth = handlers.getOidDepth(h);
for (Enumeration<SnmpMibSubRequest> rqs=handlers.getSubRequests(h);
rqs.hasMoreElements();) {
// Invoke the get() operation
meta.get(rqs.nextElement(),depth);
}
}
}
/**
* Processes a <CODE>getBulk</CODE> operation.
* The method implements the <CODE>getBulk</CODE> operation by calling
* appropriately the <CODE>getNext</CODE> method.
*
*/
// Implements the method defined in SnmpMibAgent. See SnmpMibAgent
// for java-doc
//
@Override
public void getBulk(SnmpMibRequest req, int nonRepeat, int maxRepeat)
throws SnmpStatusException {
getBulkWithGetNext(req, nonRepeat, maxRepeat);
}
/**
* Gets the root object identifier of the MIB.
* <P>In order to be accurate, the method should be called once the
* MIB is fully initialized (that is, after a call to <CODE>init</CODE>
* or <CODE>preRegister</CODE>).
*
* @return The root object identifier.
*/
@Override
public long[] getRootOid() {
if( rootOid == null) {
Vector<Integer> list= new Vector<>(10);
// Ask the tree to do the job !
//
root.getRootOid(list);
// Now format the result
//
rootOid= new long[list.size()];
int i=0;
for(Enumeration<Integer> e= list.elements(); e.hasMoreElements(); ) {
Integer val= e.nextElement();
rootOid[i++]= val.longValue();
}
}
return rootOid.clone();
}
// --------------------------------------------------------------------
// PRIVATE METHODS
//---------------------------------------------------------------------
/**
* This method builds the temporary request-tree that will be used to
* perform the SNMP request associated with the given vector of varbinds
* `list'.
*
* @param req The SnmpMibRequest object holding the varbind list
* concerning this MIB.
* @param createflag Indicates whether the operation allow for creation
* of new instances (ie: it is a SET).
* @param atomic Indicates whether the operation is atomic or not.
* @param type Request type (from SnmpDefinitions).
*
* @return The request-tree where the original varbind list has been
* dispatched to the appropriate nodes.
*/
private SnmpRequestTree getHandlers(SnmpMibRequest req,
boolean createflag, boolean atomic,
int type)
throws SnmpStatusException {
// Build an empty request tree
SnmpRequestTree handlers =
new SnmpRequestTree(req,createflag,type);
int index=0;
SnmpVarBind var;
final int ver= req.getVersion();
// For each varbind in the list finds its handling node.
for (Enumeration<SnmpVarBind> e= req.getElements(); e.hasMoreElements(); index++) {
var= e.nextElement();
try {
// Find the handling node for this varbind.
root.findHandlingNode(var,var.oid.longValue(false),
0,handlers);
} catch(SnmpStatusException x) {
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"getHandlers",
"Couldn't find a handling node for " +
var.oid.toString());
}
// If the operation is atomic (Check/Set) or the version
// is V1 we must generate an exception.
//
if (ver == SnmpDefinitions.snmpVersionOne) {
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"getHandlers", "\tV1: Throwing exception");
}
// The index in the exception must correspond to the
// SNMP index ...
//
final SnmpStatusException sse =
new SnmpStatusException(x, index + 1);
sse.initCause(x);
throw sse;
} else if ((type == SnmpDefinitions.pduWalkRequest) ||
(type == SnmpDefinitions.pduSetRequestPdu)) {
final int status =
SnmpRequestTree.mapSetException(x.getStatus(),ver);
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"getHandlers", "\tSET: Throwing exception");
}
final SnmpStatusException sse =
new SnmpStatusException(status, index + 1);
sse.initCause(x);
throw sse;
} else if (atomic) {
// Should never come here...
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"getHandlers", "\tATOMIC: Throwing exception");
}
final SnmpStatusException sse =
new SnmpStatusException(x, index + 1);
sse.initCause(x);
throw sse;
}
final int status =
SnmpRequestTree.mapGetException(x.getStatus(),ver);
if (status == SnmpStatusException.noSuchInstance) {
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"getHandlers",
"\tGET: Registering noSuchInstance");
}
var.value= SnmpVarBind.noSuchInstance;
} else if (status == SnmpStatusException.noSuchObject) {
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"getHandlers",
"\tGET: Registering noSuchObject");
}
var.value= SnmpVarBind.noSuchObject;
} else {
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"getHandlers",
"\tGET: Registering global error: " + status);
}
final SnmpStatusException sse =
new SnmpStatusException(status, index + 1);
sse.initCause(x);
throw sse;
}
}
}
return handlers;
}
/**
* This method builds the temporary request-tree that will be used to
* perform the SNMP GET-NEXT request associated with the given vector
* of varbinds `list'.
*
* @param req The SnmpMibRequest object holding the varbind list
* concerning this MIB.
*
* @return The request-tree where the original varbind list has been
* dispatched to the appropriate nodes, and where the original
* OIDs have been replaced with the correct "next" OID.
*/
private SnmpRequestTree getGetNextHandlers(SnmpMibRequest req)
throws SnmpStatusException {
// Creates an empty request tree, no entry creation is allowed (false)
SnmpRequestTree handlers = new
SnmpRequestTree(req,false,SnmpDefinitions.pduGetNextRequestPdu);
// Sets the getNext flag: if version=V2, status exception are
// transformed in endOfMibView
handlers.setGetNextFlag();
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, SnmpMib.class.getName(),
"getGetNextHandlers", "Received MIB request : " + req);
}
AcmChecker checker = new AcmChecker(req);
int index=0;
SnmpVarBind var = null;
final int ver= req.getVersion();
SnmpOid original = null;
// For each varbind, finds the handling node.
// This function has the side effect of transforming a GET-NEXT
// request into a valid GET request, replacing the OIDs in the
// original GET-NEXT request with the OID of the first leaf that
// follows.
for (Enumeration<SnmpVarBind> e= req.getElements(); e.hasMoreElements(); index++) {
var = e.nextElement();
SnmpOid result;
try {
// Find the node handling the OID that follows the varbind
// OID. `result' contains this next leaf OID.
//ACM loop.
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"getGetNextHandlers", " Next OID of : " + var.oid);
}
result = new SnmpOid(root.findNextHandlingNode
(var,var.oid.longValue(false),0,
0,handlers, checker));
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"getGetNextHandlers", " is : " + result);
}
// We replace the varbind original OID with the OID of the
// leaf object we have to return.
var.oid = result;
} catch(SnmpStatusException x) {
// if (isDebugOn())
// debug("getGetNextHandlers",
// "Couldn't find a handling node for "
// + var.oid.toString());
if (ver == SnmpDefinitions.snmpVersionOne) {
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"getGetNextHandlers",
"\tThrowing exception " + x.toString());
}
// The index in the exception must correspond to the
// SNMP index ...
//
throw new SnmpStatusException(x, index + 1);
}
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpMib.class.getName(),
"getGetNextHandlers",
"Exception : " + x.getStatus());
}
var.setSnmpValue(SnmpVarBind.endOfMibView);
}
}
return handlers;
}
// --------------------------------------------------------------------
// PROTECTED VARIABLES
// --------------------------------------------------------------------
/**
* The top element in the Mib tree.
* @serial
*/
protected SnmpMibOid root;
// --------------------------------------------------------------------
// PRIVATE VARIABLES
// --------------------------------------------------------------------
/**
* The root object identifier of the MIB.
*/
private transient long[] rootOid= null;
}

View File

@@ -0,0 +1,768 @@
/*
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// java imports
//
import java.io.Serializable;
import java.util.Vector;
import java.util.Enumeration;
// jmx imports
//
import javax.management.MBeanServer;
import javax.management.MBeanRegistration;
import javax.management.ObjectName;
import javax.management.InstanceNotFoundException;
import javax.management.ServiceNotFoundException;
import javax.management.ReflectionException;
import javax.management.MBeanException;
import com.sun.jmx.snmp.SnmpVarBind;
import com.sun.jmx.snmp.SnmpDefinitions;
import com.sun.jmx.snmp.SnmpStatusException;
import com.sun.jmx.snmp.SnmpPdu;
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.SnmpEngine;
/**
* Abstract class for representing an SNMP agent.
*
* The class is used by the SNMP protocol adaptor as the entry point in
* the SNMP agent to query.
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
*/
public abstract class SnmpMibAgent
implements SnmpMibAgentMBean, MBeanRegistration, Serializable {
/**
* Default constructor.
*/
public SnmpMibAgent() {
}
// ---------------------------------------------------------------------
// PUBLIC METHODS
//----------------------------------------------------------------------
/**
* Initializes the MIB (with no registration of the MBeans into the
* MBean server).
*
* @exception IllegalAccessException The MIB can not be initialized.
*/
public abstract void init() throws IllegalAccessException;
/**
* Initializes the MIB but each single MBean representing the MIB
* is inserted into the MBean server.
*
* @param server The MBean server to register the service with.
* @param name The object name.
*
* @return The name of the SNMP MIB registered.
*
* @exception java.lang.Exception
*/
@Override
public abstract ObjectName preRegister(MBeanServer server,
ObjectName name)
throws java.lang.Exception;
/**
* Not used in this context.
*/
@Override
public void postRegister (Boolean registrationDone) {
}
/**
* Not used in this context.
*/
@Override
public void preDeregister() throws java.lang.Exception {
}
/**
* Not used in this context.
*/
@Override
public void postDeregister() {
}
/**
* Processes a <CODE>get</CODE> operation.
* This method must update the SnmpVarBinds contained in the
* <var>{@link SnmpMibRequest} req</var> parameter.
*
* @param req The SnmpMibRequest object holding the list of variable to
* be retrieved. This list is composed of
* <CODE>SnmpVarBind</CODE> objects.
*
* @exception SnmpStatusException An error occurred during the operation.
*/
@Override
public abstract void get(SnmpMibRequest req)
throws SnmpStatusException;
/**
* Processes a <CODE>getNext</CODE> operation.
* This method must update the SnmpVarBinds contained in the
* <var>{@link SnmpMibRequest} req</var> parameter.
*
* @param req The SnmpMibRequest object holding the list of
* OIDs from which the next variables should be retrieved.
* This list is composed of <CODE>SnmpVarBind</CODE> objects.
*
* @exception SnmpStatusException An error occurred during the operation.
*/
@Override
public abstract void getNext(SnmpMibRequest req)
throws SnmpStatusException;
/**
* Processes a <CODE>getBulk</CODE> operation.
* This method must update the SnmpVarBinds contained in the
* <var>{@link SnmpMibRequest} req</var> parameter.
*
* @param req The SnmpMibRequest object holding the list of variable to
* be retrieved. This list is composed of
* <CODE>SnmpVarBind</CODE> objects.
*
* @param nonRepeat The number of variables, starting with the first
* variable in the variable-bindings, for which a single
* lexicographic successor is requested.
*
* @param maxRepeat The number of lexicographic successors requested
* for each of the last R variables. R is the number of variables
* following the first <CODE>nonRepeat</CODE> variables for which
* multiple lexicographic successors are requested.
*
* @exception SnmpStatusException An error occurred during the operation.
*/
@Override
public abstract void getBulk(SnmpMibRequest req, int nonRepeat,
int maxRepeat)
throws SnmpStatusException;
/**
* Processes a <CODE>set</CODE> operation.
* This method must update the SnmpVarBinds contained in the
* <var>{@link SnmpMibRequest} req</var> parameter.
* This method is called during the second phase of the SET two-phase
* commit.
*
* @param req The SnmpMibRequest object holding the list of variable to
* be set. This list is composed of
* <CODE>SnmpVarBind</CODE> objects.
*
* @exception SnmpStatusException An error occurred during the operation.
* Throwing an exception in this method will break the
* atomicity of the SET operation. Care must be taken so that
* the exception is thrown in the {@link #check(SnmpMibRequest)}
* method instead.
*/
@Override
public abstract void set(SnmpMibRequest req)
throws SnmpStatusException;
/**
* Checks if a <CODE>set</CODE> operation can be performed.
* If the operation can not be performed, the method should throw an
* <CODE>SnmpStatusException</CODE>.
* This method is called during the first phase of the SET two-phase
* commit.
*
* @param req The SnmpMibRequest object holding the list of variable to
* be set. This list is composed of
* <CODE>SnmpVarBind</CODE> objects.
*
* @exception SnmpStatusException The <CODE>set</CODE> operation
* cannot be performed.
*/
@Override
public abstract void check(SnmpMibRequest req)
throws SnmpStatusException;
/**
* Gets the root object identifier of the MIB.
* <P>The root object identifier is the object identifier uniquely
* identifying the MIB.
*
* @return The root object identifier.
*/
public abstract long[] getRootOid();
// ---------------------------------------------------------------------
// GETTERS AND SETTERS
// ---------------------------------------------------------------------
/**
* Gets the reference to the MBean server in which the SNMP MIB is
* registered.
*
* @return The MBean server or null if the MIB is not registered in any
* MBean server.
*/
@Override
public MBeanServer getMBeanServer() {
return server;
}
/**
* Gets the reference to the SNMP protocol adaptor to which the MIB is
* bound.
*
* @return The SNMP MIB handler.
*/
@Override
public SnmpMibHandler getSnmpAdaptor() {
return adaptor;
}
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and add this new MIB in the SNMP MIB handler.
*
* @param stack The SNMP MIB handler.
*/
@Override
public void setSnmpAdaptor(SnmpMibHandler stack) {
if (adaptor != null) {
adaptor.removeMib(this);
}
adaptor = stack;
if (adaptor != null) {
adaptor.addMib(this);
}
}
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and add this new MIB in the SNMP MIB handler.
* This method is to be called to set a specific agent to a specific OID. This can be useful when dealing with MIB overlapping.
* Some OID can be implemented in more than one MIB. In this case, the OID nearest the agent will be used on SNMP operations.
* @param stack The SNMP MIB handler.
* @param oids The set of OIDs this agent implements.
*
* @since 1.5
*/
@Override
public void setSnmpAdaptor(SnmpMibHandler stack, SnmpOid[] oids) {
if (adaptor != null) {
adaptor.removeMib(this);
}
adaptor = stack;
if (adaptor != null) {
adaptor.addMib(this, oids);
}
}
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and adds this new MIB in the SNMP MIB handler.
* Adds a new contextualized MIB in the SNMP MIB handler.
*
* @param stack The SNMP MIB handler.
* @param contextName The MIB context name. If null is passed, will be registered in the default context.
*
* @exception IllegalArgumentException If the parameter is null.
*
* @since 1.5
*/
@Override
public void setSnmpAdaptor(SnmpMibHandler stack, String contextName) {
if (adaptor != null) {
adaptor.removeMib(this, contextName);
}
adaptor = stack;
if (adaptor != null) {
adaptor.addMib(this, contextName);
}
}
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and adds this new MIB in the SNMP MIB handler.
* Adds a new contextualized MIB in the SNMP MIB handler.
*
* @param stack The SNMP MIB handler.
* @param contextName The MIB context name. If null is passed, will be registered in the default context.
* @param oids The set of OIDs this agent implements.
* @exception IllegalArgumentException If the parameter is null.
*
* @since 1.5
*/
@Override
public void setSnmpAdaptor(SnmpMibHandler stack,
String contextName,
SnmpOid[] oids) {
if (adaptor != null) {
adaptor.removeMib(this, contextName);
}
adaptor = stack;
if (adaptor != null) {
adaptor.addMib(this, contextName, oids);
}
}
/**
* Gets the object name of the SNMP protocol adaptor to which the MIB
* is bound.
*
* @return The name of the SNMP protocol adaptor.
*/
@Override
public ObjectName getSnmpAdaptorName() {
return adaptorName;
}
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and add this new MIB in the SNMP MIB handler
* associated to the specified <CODE>name</CODE>.
*
* @param name The name of the SNMP protocol adaptor.
*
* @exception InstanceNotFoundException The SNMP protocol adaptor does
* not exist in the MBean server.
*
* @exception ServiceNotFoundException This SNMP MIB is not registered
* in the MBean server or the requested service is not supported.
*/
@Override
public void setSnmpAdaptorName(ObjectName name)
throws InstanceNotFoundException, ServiceNotFoundException {
if (server == null) {
throw new ServiceNotFoundException(mibName + " is not registered in the MBean server");
}
// First remove the reference on the old adaptor server.
//
if (adaptor != null) {
adaptor.removeMib(this);
}
// Then update the reference to the new adaptor server.
//
Object[] params = {this};
String[] signature = {"com.sun.jmx.snmp.agent.SnmpMibAgent"};
try {
adaptor = (SnmpMibHandler)(server.invoke(name, "addMib", params,
signature));
} catch (InstanceNotFoundException e) {
throw new InstanceNotFoundException(name.toString());
} catch (ReflectionException e) {
throw new ServiceNotFoundException(name.toString());
} catch (MBeanException e) {
// Should never occur...
}
adaptorName = name;
}
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and add this new MIB in the SNMP MIB handler
* associated to the specified <CODE>name</CODE>.
* This method is to be called to set a specific agent to a specific OID. This can be useful when dealing with MIB overlapping.
* Some OID can be implemented in more than one MIB. In this case, the OID nearer agent will be used on SNMP operations.
* @param name The name of the SNMP protocol adaptor.
* @param oids The set of OIDs this agent implements.
* @exception InstanceNotFoundException The SNMP protocol adaptor does
* not exist in the MBean server.
*
* @exception ServiceNotFoundException This SNMP MIB is not registered
* in the MBean server or the requested service is not supported.
*
* @since 1.5
*/
@Override
public void setSnmpAdaptorName(ObjectName name, SnmpOid[] oids)
throws InstanceNotFoundException, ServiceNotFoundException {
if (server == null) {
throw new ServiceNotFoundException(mibName + " is not registered in the MBean server");
}
// First remove the reference on the old adaptor server.
//
if (adaptor != null) {
adaptor.removeMib(this);
}
// Then update the reference to the new adaptor server.
//
Object[] params = {this, oids};
String[] signature = {"com.sun.jmx.snmp.agent.SnmpMibAgent",
oids.getClass().getName()};
try {
adaptor = (SnmpMibHandler)(server.invoke(name, "addMib", params,
signature));
} catch (InstanceNotFoundException e) {
throw new InstanceNotFoundException(name.toString());
} catch (ReflectionException e) {
throw new ServiceNotFoundException(name.toString());
} catch (MBeanException e) {
// Should never occur...
}
adaptorName = name;
}
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and add this new MIB in the SNMP MIB handler
* associated to the specified <CODE>name</CODE>.
*
* @param name The name of the SNMP protocol adaptor.
* @param contextName The MIB context name. If null is passed, will be registered in the default context.
* @exception InstanceNotFoundException The SNMP protocol adaptor does
* not exist in the MBean server.
*
* @exception ServiceNotFoundException This SNMP MIB is not registered
* in the MBean server or the requested service is not supported.
*
* @since 1.5
*/
@Override
public void setSnmpAdaptorName(ObjectName name, String contextName)
throws InstanceNotFoundException, ServiceNotFoundException {
if (server == null) {
throw new ServiceNotFoundException(mibName + " is not registered in the MBean server");
}
// First remove the reference on the old adaptor server.
//
if (adaptor != null) {
adaptor.removeMib(this, contextName);
}
// Then update the reference to the new adaptor server.
//
Object[] params = {this, contextName};
String[] signature = {"com.sun.jmx.snmp.agent.SnmpMibAgent", "java.lang.String"};
try {
adaptor = (SnmpMibHandler)(server.invoke(name, "addMib", params,
signature));
} catch (InstanceNotFoundException e) {
throw new InstanceNotFoundException(name.toString());
} catch (ReflectionException e) {
throw new ServiceNotFoundException(name.toString());
} catch (MBeanException e) {
// Should never occur...
}
adaptorName = name;
}
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and add this new MIB in the SNMP MIB handler
* associated to the specified <CODE>name</CODE>.
*
* @param name The name of the SNMP protocol adaptor.
* @param contextName The MIB context name. If null is passed, will be registered in the default context.
* @param oids The set of OIDs this agent implements.
* @exception InstanceNotFoundException The SNMP protocol adaptor does
* not exist in the MBean server.
*
* @exception ServiceNotFoundException This SNMP MIB is not registered
* in the MBean server or the requested service is not supported.
*
* @since 1.5
*/
@Override
public void setSnmpAdaptorName(ObjectName name,
String contextName, SnmpOid[] oids)
throws InstanceNotFoundException, ServiceNotFoundException {
if (server == null) {
throw new ServiceNotFoundException(mibName + " is not registered in the MBean server");
}
// First remove the reference on the old adaptor server.
//
if (adaptor != null) {
adaptor.removeMib(this, contextName);
}
// Then update the reference to the new adaptor server.
//
Object[] params = {this, contextName, oids};
String[] signature = {"com.sun.jmx.snmp.agent.SnmpMibAgent", "java.lang.String", oids.getClass().getName()};
try {
adaptor = (SnmpMibHandler)(server.invoke(name, "addMib", params,
signature));
} catch (InstanceNotFoundException e) {
throw new InstanceNotFoundException(name.toString());
} catch (ReflectionException e) {
throw new ServiceNotFoundException(name.toString());
} catch (MBeanException e) {
// Should never occur...
}
adaptorName = name;
}
/**
* Indicates whether or not the MIB module is bound to a SNMP protocol
* adaptor.
* As a reminder, only bound MIBs can be accessed through SNMP protocol
* adaptor.
*
* @return <CODE>true</CODE> if the MIB module is bound,
* <CODE>false</CODE> otherwise.
*/
@Override
public boolean getBindingState() {
if (adaptor == null)
return false;
else
return true;
}
/**
* Gets the MIB name.
*
* @return The MIB name.
*/
@Override
public String getMibName() {
return mibName;
}
/**
* This is a factory method for creating new SnmpMibRequest objects.
* @param reqPdu The received PDU.
* @param vblist The vector of SnmpVarBind objects in which the
* MIB concerned by this request is involved.
* @param version The protocol version of the SNMP request.
* @param userData User allocated contextual data.
*
* @return A new SnmpMibRequest object.
*
* @since 1.5
**/
public static SnmpMibRequest newMibRequest(SnmpPdu reqPdu,
Vector<SnmpVarBind> vblist,
int version,
Object userData)
{
return new SnmpMibRequestImpl(null,
reqPdu,
vblist,
version,
userData,
null,
SnmpDefinitions.noAuthNoPriv,
getSecurityModel(version),
null,null);
}
/**
* This is a factory method for creating new SnmpMibRequest objects.
* @param engine The local engine.
* @param reqPdu The received pdu.
* @param vblist The vector of SnmpVarBind objects in which the
* MIB concerned by this request is involved.
* @param version The protocol version of the SNMP request.
* @param userData User allocated contextual data.
*
* @return A new SnmpMibRequest object.
*
* @since 1.5
**/
public static SnmpMibRequest newMibRequest(SnmpEngine engine,
SnmpPdu reqPdu,
Vector<SnmpVarBind> vblist,
int version,
Object userData,
String principal,
int securityLevel,
int securityModel,
byte[] contextName,
byte[] accessContextName) {
return new SnmpMibRequestImpl(engine,
reqPdu,
vblist,
version,
userData,
principal,
securityLevel,
securityModel,
contextName,
accessContextName);
}
// ---------------------------------------------------------------------
// PACKAGE METHODS
// ---------------------------------------------------------------------
/**
* Processes a <CODE>getBulk</CODE> operation using call to
* <CODE>getNext</CODE>.
* The method implements the <CODE>getBulk</CODE> operation by calling
* appropriately the <CODE>getNext</CODE> method.
*
* @param req The SnmpMibRequest containing the variable list to be
* retrieved.
*
* @param nonRepeat The number of variables, starting with the first
* variable in the variable-bindings, for which a single lexicographic
* successor is requested.
*
* @param maxRepeat The number of lexicographic successors
* requested for each of the last R variables. R is the number of
* variables following the first nonRepeat variables for which
* multiple lexicographic successors are requested.
*
* @return The variable list containing returned values.
*
* @exception SnmpStatusException An error occurred during the operation.
*/
void getBulkWithGetNext(SnmpMibRequest req, int nonRepeat, int maxRepeat)
throws SnmpStatusException {
final Vector<SnmpVarBind> list = req.getSubList();
// RFC 1905, Section 4.2.3, p14
final int L = list.size() ;
final int N = Math.max(Math.min(nonRepeat, L), 0) ;
final int M = Math.max(maxRepeat, 0) ;
final int R = L - N ;
// Let's build the varBindList for the response pdu
//
// int errorStatus = SnmpDefinitions.snmpRspNoError ;
// int errorIndex = 0 ;
if (L != 0) {
// Non-repeaters and first row of repeaters
//
getNext(req);
// Now the remaining repeaters
//
Vector<SnmpVarBind> repeaters= splitFrom(list, N);
SnmpMibRequestImpl repeatedReq =
new SnmpMibRequestImpl(req.getEngine(),
req.getPdu(),
repeaters,
SnmpDefinitions.snmpVersionTwo,
req.getUserData(),
req.getPrincipal(),
req.getSecurityLevel(),
req.getSecurityModel(),
req.getContextName(),
req.getAccessContextName());
for (int i = 2 ; i <= M ; i++) {
getNext(repeatedReq);
concatVector(req, repeaters);
}
}
}
// ---------------------------------------------------------------------
// PRIVATE METHODS
// ---------------------------------------------------------------------
/**
* This method creates a new Vector which does not contain the first
* element up to the specified limit.
*
* @param original The original vector.
* @param limit The limit.
*/
private Vector<SnmpVarBind> splitFrom(Vector<SnmpVarBind> original, int limit) {
int max= original.size();
Vector<SnmpVarBind> result= new Vector<>(max - limit);
int i= limit;
// Ok the loop looks a bit strange. But in order to improve the
// perf, we try to avoid reference to the limit variable from
// within the loop ...
//
for(Enumeration<SnmpVarBind> e= original.elements(); e.hasMoreElements(); --i) {
SnmpVarBind var= e.nextElement();
if (i >0)
continue;
result.addElement(new SnmpVarBind(var.oid, var.value));
}
return result;
}
private void concatVector(SnmpMibRequest req, Vector<SnmpVarBind> source) {
for(Enumeration<SnmpVarBind> e= source.elements(); e.hasMoreElements(); ) {
SnmpVarBind var= e.nextElement();
// We need to duplicate the SnmpVarBind otherwise it is going
// to be overloaded by the next get Next ...
req.addVarBind(new SnmpVarBind(var.oid, var.value));
}
}
private static int getSecurityModel(int version) {
switch(version) {
case SnmpDefinitions.snmpVersionOne:
return SnmpDefinitions.snmpV1SecurityModel;
default:
return SnmpDefinitions.snmpV2SecurityModel;
}
}
// ---------------------------------------------------------------------
// PROTECTED VARIABLES
// ---------------------------------------------------------------------
/**
* The object name of the MIB.
* @serial
*/
protected String mibName;
/**
* The reference to the MBean server.
* @serial
*/
protected MBeanServer server;
// ---------------------------------------------------------------------
// PRIVATE VARIABLES
// ---------------------------------------------------------------------
/**
* The object name of the SNMP protocol adaptor.
* @serial
*/
private ObjectName adaptorName;
/**
* The reference to the SNMP stack.
*/
private transient SnmpMibHandler adaptor;
}

View File

@@ -0,0 +1,316 @@
/*
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// java imports
//
import java.util.Vector;
// jmx imports
//
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.MalformedObjectNameException;
import javax.management.InstanceNotFoundException;
import javax.management.ServiceNotFoundException;
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.SnmpStatusException;
/**
* Exposes the remote management interface of the <CODE>SnmpMibAgent</CODE> MBean.
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
*/
public interface SnmpMibAgentMBean {
// PUBLIC METHODS
//---------------
/**
* Processes a <CODE>get</CODE> operation.
* This method must not be called from remote.
*
* @param req The SnmpMibRequest object holding the list of variables to
* be retrieved. This list is composed of
* <CODE>SnmpVarBind</CODE> objects.
*
* @exception SnmpStatusException An error occurred during the operation.
* @see SnmpMibAgent#get(SnmpMibRequest)
*/
public void get(SnmpMibRequest req) throws SnmpStatusException;
/**
* Processes a <CODE>getNext</CODE> operation.
* This method must not be called from remote.
*
* @param req The SnmpMibRequest object holding the list of variables to
* be retrieved. This list is composed of
* <CODE>SnmpVarBind</CODE> objects.
*
* @exception SnmpStatusException An error occurred during the operation.
* @see SnmpMibAgent#getNext(SnmpMibRequest)
*/
public void getNext(SnmpMibRequest req) throws SnmpStatusException;
/**
* Processes a <CODE>getBulk</CODE> operation.
* This method must not be called from remote.
*
* @param req The SnmpMibRequest object holding the list of variables to
* be retrieved. This list is composed of
* <CODE>SnmpVarBind</CODE> objects.
*
* @param nonRepeat The number of variables, starting with the first
* variable in the variable-bindings, for which a single
* lexicographic successor is requested.
*
* @param maxRepeat The number of lexicographic successors requested
* for each of the last R variables. R is the number of variables
* following the first <CODE>nonRepeat</CODE> variables for which
* multiple lexicographic successors are requested.
*
* @exception SnmpStatusException An error occurred during the operation.
* @see SnmpMibAgent#getBulk(SnmpMibRequest,int,int)
*/
public void getBulk(SnmpMibRequest req, int nonRepeat, int maxRepeat)
throws SnmpStatusException;
/**
* Processes a <CODE>set</CODE> operation.
* This method must not be called from remote.
*
* @param req The SnmpMibRequest object holding the list of variables to
* be set. This list is composed of
* <CODE>SnmpVarBind</CODE> objects.
*
* @exception SnmpStatusException An error occurred during the operation.
* @see SnmpMibAgent#set(SnmpMibRequest)
*/
public void set(SnmpMibRequest req) throws SnmpStatusException;
/**
* Checks if a <CODE>set</CODE> operation can be performed.
* If the operation cannot be performed, the method should emit a
* <CODE>SnmpStatusException</CODE>.
*
* @param req The SnmpMibRequest object holding the list of variables to
* be set. This list is composed of
* <CODE>SnmpVarBind</CODE> objects.
*
* @exception SnmpStatusException The <CODE>set</CODE> operation
* cannot be performed.
* @see SnmpMibAgent#check(SnmpMibRequest)
*/
public void check(SnmpMibRequest req) throws SnmpStatusException;
// GETTERS AND SETTERS
//--------------------
/**
* Gets the reference to the MBean server in which the SNMP MIB is
* registered.
*
* @return The MBean server or null if the MIB is not registered in any
* MBean server.
*/
public MBeanServer getMBeanServer();
/**
* Gets the reference to the SNMP protocol adaptor to which the MIB is
* bound.
* <BR>This method is used for accessing the SNMP MIB handler property
* of the SNMP MIB agent in case of a standalone agent.
*
* @return The SNMP MIB handler.
*/
public SnmpMibHandler getSnmpAdaptor();
/**
* Sets the reference to the SNMP protocol adaptor through which the
* MIB will be SNMP accessible and add this new MIB in the SNMP MIB
* handler.
* <BR>This method is used for setting the SNMP MIB handler property of
* the SNMP MIB agent in case of a standalone agent.
*
* @param stack The SNMP MIB handler.
*/
public void setSnmpAdaptor(SnmpMibHandler stack);
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and add this new MIB in the SNMP MIB handler.
* This method is to be called to set a specific agent to a specific OID.
* This can be useful when dealing with MIB overlapping.
* Some OID can be implemented in more than one MIB. In this case, the
* OID nearer agent will be used on SNMP operations.
* @param stack The SNMP MIB handler.
* @param oids The set of OIDs this agent implements.
*
* @since 1.5
*/
public void setSnmpAdaptor(SnmpMibHandler stack, SnmpOid[] oids);
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and add this new MIB in the SNMP MIB handler.
* Adds a new contextualized MIB in the SNMP MIB handler.
*
* @param stack The SNMP MIB handler.
* @param contextName The MIB context name. If null is passed, will be
* registered in the default context.
*
* @exception IllegalArgumentException If the parameter is null.
*
* @since 1.5
*/
public void setSnmpAdaptor(SnmpMibHandler stack, String contextName);
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and adds this new MIB in the SNMP MIB handler.
* Adds a new contextualized MIB in the SNMP MIB handler.
*
* @param stack The SNMP MIB handler.
* @param contextName The MIB context name. If null is passed, will be
* registered in the default context.
* @param oids The set of OIDs this agent implements.
* @exception IllegalArgumentException If the parameter is null.
*
* @since 1.5
*/
public void setSnmpAdaptor(SnmpMibHandler stack,
String contextName,
SnmpOid[] oids);
/**
* Gets the object name of the SNMP protocol adaptor to which the MIB is
* bound.
*
* @return The name of the SNMP protocol adaptor.
*/
public ObjectName getSnmpAdaptorName();
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and add this new MIB in the SNMP MIB handler
* associated to the specified <CODE>name</CODE>.
*
* @param name The object name of the SNMP MIB handler.
*
* @exception InstanceNotFoundException The MBean does not exist in the
* MBean server.
* @exception ServiceNotFoundException This SNMP MIB is not registered
* in the MBean server or the requested service is not supported.
*/
public void setSnmpAdaptorName(ObjectName name)
throws InstanceNotFoundException, ServiceNotFoundException;
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and add this new MIB in the SNMP MIB handler
* associated to the specified <CODE>name</CODE>.
* This method is to be called to set a specific agent to a specific OID.
* This can be useful when dealing with MIB overlapping.
* Some OID can be implemented in more than one MIB. In this case, the
* OID nearer agent will be used on SNMP operations.
* @param name The name of the SNMP protocol adaptor.
* @param oids The set of OIDs this agent implements.
* @exception InstanceNotFoundException The SNMP protocol adaptor does
* not exist in the MBean server.
*
* @exception ServiceNotFoundException This SNMP MIB is not registered
* in the MBean server or the requested service is not supported.
*
* @since 1.5
*/
public void setSnmpAdaptorName(ObjectName name, SnmpOid[] oids)
throws InstanceNotFoundException, ServiceNotFoundException;
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and add this new MIB in the SNMP MIB handler
* associated to the specified <CODE>name</CODE>.
*
* @param name The name of the SNMP protocol adaptor.
* @param contextName The MIB context name. If null is passed, will be
* registered in the default context.
* @exception InstanceNotFoundException The SNMP protocol adaptor does
* not exist in the MBean server.
*
* @exception ServiceNotFoundException This SNMP MIB is not registered
* in the MBean server or the requested service is not supported.
*
* @since 1.5
*/
public void setSnmpAdaptorName(ObjectName name, String contextName)
throws InstanceNotFoundException, ServiceNotFoundException;
/**
* Sets the reference to the SNMP protocol adaptor through which the MIB
* will be SNMP accessible and add this new MIB in the SNMP MIB handler
* associated to the specified <CODE>name</CODE>.
*
* @param name The name of the SNMP protocol adaptor.
* @param contextName The MIB context name. If null is passed, will be
* registered in the default context.
* @param oids The set of OIDs this agent implements.
* @exception InstanceNotFoundException The SNMP protocol adaptor does
* not exist in the MBean server.
*
* @exception ServiceNotFoundException This SNMP MIB is not registered
* in the MBean server or the requested service is not supported.
*
* @since 1.5
*/
public void setSnmpAdaptorName(ObjectName name,
String contextName,
SnmpOid[] oids)
throws InstanceNotFoundException, ServiceNotFoundException;
/**
* Indicates whether or not the MIB module is bound to a SNMP protocol
* adaptor.
* As a reminder, only bound MIBs can be accessed through SNMP protocol
* adaptor.
*
* @return <CODE>true</CODE> if the MIB module is bound,
* <CODE>false</CODE> otherwise.
*/
public boolean getBindingState();
/**
* Gets the MIB name.
*
* @return The MIB name.
*/
public String getMibName();
}

View File

@@ -0,0 +1,188 @@
/*
* Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// java imports
//
import com.sun.jmx.snmp.SnmpDefinitions;
import java.io.Serializable;
import com.sun.jmx.snmp.SnmpStatusException;
/**
* Represents a node in an SNMP MIB which corresponds to a table entry
* meta node.
* <P>
* This class is used by the class generated by <CODE>mibgen</CODE>.
* You should not need to use this class directly.
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
*/
public abstract class SnmpMibEntry extends SnmpMibNode
implements Serializable {
/**
* Tells whether the given arc identifies a variable (scalar object) in
* this entry.
*
* @param arc An OID arc.
*
* @return <CODE>true</CODE> if `arc' leads to a variable.
*/
public abstract boolean isVariable(long arc);
/**
* Tells whether the given arc identifies a readable scalar object in
* this entry.
*
* @param arc An OID arc.
*
* @return <CODE>true</CODE> if `arc' leads to a readable variable.
*/
public abstract boolean isReadable(long arc);
/**
* Get the next OID arc corresponding to a readable scalar variable.
*
*/
public long getNextVarId(long id, Object userData)
throws SnmpStatusException {
long nextvar = super.getNextVarId(id,userData);
while (!isReadable(nextvar))
nextvar = super.getNextVarId(nextvar,userData);
return nextvar;
}
/**
* Checks whether the given OID arc identifies a variable (columnar
* object).
*
* @param userData A contextual object containing user-data.
* This object is allocated through the <code>
* {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}</code>
* for each incoming SNMP request.
*
* @exception If the given `arc' does not identify any variable in this
* group, throws an SnmpStatusException.
*/
public void validateVarId(long arc, Object userData)
throws SnmpStatusException {
if (isVariable(arc) == false) {
throw new SnmpStatusException(SnmpDefinitions.snmpRspNoSuchName);
}
}
/**
* Generic handling of the <CODE>get</CODE> operation.
* <p>The actual implementation of this method will be generated
* by mibgen. Usually, this implementation only delegates the
* job to some other provided runtime class, which knows how to
* access the MBean. The current toolkit thus provides two
* implementations:
* <ul><li>The standard implementation will directly access the
* MBean through a java reference,</li>
* <li>The generic implementation will access the MBean through
* the MBean server.</li>
* </ul>
* <p>Both implementations rely upon specific - and distinct, set of
* mibgen generated methods.
* <p> You can override this method if you need to implement some
* specific policies for minimizing the accesses made to some remote
* underlying resources.
* <p>
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException An error occurred while accessing
* the MIB node.
*/
abstract public void get(SnmpMibSubRequest req, int depth)
throws SnmpStatusException;
/**
* Generic handling of the <CODE>set</CODE> operation.
* <p>The actual implementation of this method will be generated
* by mibgen. Usually, this implementation only delegates the
* job to some other provided runtime class, which knows how to
* access the MBean. The current toolkit thus provides two
* implementations:
* <ul><li>The standard implementation will directly access the
* MBean through a java reference,</li>
* <li>The generic implementation will access the MBean through
* the MBean server.</li>
* </ul>
* <p>Both implementations rely upon specific - and distinct, set of
* mibgen generated methods.
* <p> You can override this method if you need to implement some
* specific policies for minimizing the accesses made to some remote
* underlying resources.
* <p>
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException An error occurred while accessing
* the MIB node.
*/
abstract public void set(SnmpMibSubRequest req, int depth)
throws SnmpStatusException;
/**
* Generic handling of the <CODE>check</CODE> operation.
*
* <p>The actual implementation of this method will be generated
* by mibgen. Usually, this implementation only delegates the
* job to some other provided runtime class, which knows how to
* access the MBean. The current toolkit thus provides two
* implementations:
* <ul><li>The standard implementation will directly access the
* MBean through a java reference,</li>
* <li>The generic implementation will access the MBean through
* the MBean server.</li>
* </ul>
* <p>Both implementations rely upon specific - and distinct, set of
* mibgen generated methods.
* <p> You can override this method if you need to implement some
* specific policies for minimizing the accesses made to some remote
* underlying resources, or if you need to implement some consistency
* checks between the different values provided in the varbind list.
* <p>
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException An error occurred while accessing
* the MIB node.
*/
abstract public void check(SnmpMibSubRequest req, int depth)
throws SnmpStatusException;
}

View File

@@ -0,0 +1,521 @@
/*
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// java imports
//
import java.io.Serializable;
import java.util.Hashtable;
import java.util.Vector;
// jmx imports
//
import com.sun.jmx.snmp.SnmpVarBind;
import com.sun.jmx.snmp.SnmpStatusException;
/**
* Represents a node in an SNMP MIB which corresponds to a group.
* This class allows subnodes to be registered below a group, providing
* support for nested groups. The subnodes are registered at run time
* when registering the nested groups in the global MIB OID tree.
* <P>
* This class is used by the class generated by <CODE>mibgen</CODE>.
* You should not need to use this class directly.
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
*/
public abstract class SnmpMibGroup extends SnmpMibOid
implements Serializable {
// We will register the OID arcs leading to subgroups in this hashtable.
// So for each arc in varList, if the arc is also in subgroups, it leads
// to a subgroup, if it is not in subgroup, it leads either to a table
// or to a variable.
protected Hashtable<Long, Long> subgroups = null;
/**
* Tells whether the given arc identifies a table in this group.
*
* @param arc An OID arc.
*
* @return <CODE>true</CODE> if `arc' leads to a table.
*/
public abstract boolean isTable(long arc);
/**
* Tells whether the given arc identifies a variable (scalar object) in
* this group.
*
* @param arc An OID arc.
*
* @return <CODE>true</CODE> if `arc' leads to a variable.
*/
public abstract boolean isVariable(long arc);
/**
* Tells whether the given arc identifies a readable scalar object in
* this group.
*
* @param arc An OID arc.
*
* @return <CODE>true</CODE> if `arc' leads to a readable variable.
*/
public abstract boolean isReadable(long arc);
/**
* Gets the table identified by the given `arc'.
*
* @param arc An OID arc.
*
* @return The <CODE>SnmpMibTable</CODE> identified by `arc', or
* <CODE>null</CODE> if `arc' does not identify any table.
*/
public abstract SnmpMibTable getTable(long arc);
/**
* Checks whether the given OID arc identifies a variable (scalar
* object).
*
* @exception If the given `arc' does not identify any variable in this
* group, throws an SnmpStatusException.
*/
public void validateVarId(long arc, Object userData)
throws SnmpStatusException {
if (isVariable(arc) == false) {
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
}
}
// -------------------------------------------------------------------
// We use a hashtable (subgroup) in order to determine whether an
// OID arc leads to a subgroup. This implementation can be changed if
// needed...
// For instance, the subclass could provide a generated isNestedArc()
// method in which the subgroup OID arcs would be hardcoded.
// However, the generic approach was preferred because at this time
// groups and subgroups are dynamically registered in the MIB.
//
/**
* Tell whether the given OID arc identifies a sub-tree
* leading to a nested SNMP sub-group. This method is used internally.
* You shouldn't need to call it directly.
*
* @param arc An OID arc.
*
* @return <CODE>true</CODE> if the given OID arc identifies a subtree
* leading to a nested SNMP sub-group.
*
*/
public boolean isNestedArc(long arc) {
if (subgroups == null) return false;
Object obj = subgroups.get(new Long(arc));
// if the arc is registered in the hashtable,
// it leads to a subgroup.
return (obj != null);
}
/**
* Generic handling of the <CODE>get</CODE> operation.
* <p>The actual implementation of this method will be generated
* by mibgen. Usually, this implementation only delegates the
* job to some other provided runtime class, which knows how to
* access the MBean. The current toolkit thus provides two
* implementations:
* <ul><li>The standard implementation will directly access the
* MBean through a java reference,</li>
* <li>The generic implementation will access the MBean through
* the MBean server.</li>
* </ul>
* <p>Both implementations rely upon specific - and distinct, set of
* mibgen generated methods.
* <p> You can override this method if you need to implement some
* specific policies for minimizing the accesses made to some remote
* underlying resources.
* <p>
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException An error occurred while accessing
* the MIB node.
*/
@Override
abstract public void get(SnmpMibSubRequest req, int depth)
throws SnmpStatusException;
/**
* Generic handling of the <CODE>set</CODE> operation.
* <p>The actual implementation of this method will be generated
* by mibgen. Usually, this implementation only delegates the
* job to some other provided runtime class, which knows how to
* access the MBean. The current toolkit thus provides two
* implementations:
* <ul><li>The standard implementation will directly access the
* MBean through a java reference,</li>
* <li>The generic implementation will access the MBean through
* the MBean server.</li>
* </ul>
* <p>Both implementations rely upon specific - and distinct, set of
* mibgen generated methods.
* <p> You can override this method if you need to implement some
* specific policies for minimizing the accesses made to some remote
* underlying resources.
* <p>
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException An error occurred while accessing
* the MIB node.
*/
@Override
abstract public void set(SnmpMibSubRequest req, int depth)
throws SnmpStatusException;
/**
* Generic handling of the <CODE>check</CODE> operation.
*
* <p>The actual implementation of this method will be generated
* by mibgen. Usually, this implementation only delegates the
* job to some other provided runtime class, which knows how to
* access the MBean. The current toolkit thus provides two
* implementations:
* <ul><li>The standard implementation will directly access the
* MBean through a java reference,</li>
* <li>The generic implementation will access the MBean through
* the MBean server.</li>
* </ul>
* <p>Both implementations rely upon specific - and distinct, set of
* mibgen generated methods.
* <p> You can override this method if you need to implement some
* specific policies for minimizing the accesses made to some remote
* underlying resources, or if you need to implement some consistency
* checks between the different values provided in the varbind list.
* <p>
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException An error occurred while accessing
* the MIB node.
*/
@Override
abstract public void check(SnmpMibSubRequest req, int depth)
throws SnmpStatusException;
// --------------------------------------------------------------------
// If we reach this node, we are below the root OID, so we just
// return.
// --------------------------------------------------------------------
@Override
public void getRootOid(Vector<Integer> result) {
}
// -------------------------------------------------------------------
// PACKAGE METHODS
// -------------------------------------------------------------------
// -------------------------------------------------------------------
// This method can also be overriden in a subclass to provide a
// different implementation of the isNestedArc() method.
// => if isNestedArc() is hardcoded, then registerSubArc() becomes
// useless and can become empty.
/**
* Register an OID arc that identifies a sub-tree
* leading to a nested SNMP sub-group. This method is used internally.
* You shouldn't ever call it directly.
*
* @param arc An OID arc.
*
*/
void registerNestedArc(long arc) {
Long obj = new Long(arc);
if (subgroups == null) subgroups = new Hashtable<>();
// registers the arc in the hashtable.
subgroups.put(obj,obj);
}
// -------------------------------------------------------------------
// The SnmpMibOid algorithm relies on the fact that for every arc
// registered in varList, there is a corresponding node at the same
// position in children.
// So the trick is to register a null node in children for each variable
// in varList, so that the real subgroup nodes can be inserted at the
// correct location.
// registerObject() should be called for each scalar object and each
// table arc by the generated subclass.
/**
* Register an OID arc that identifies a scalar object or a table.
* This method is used internally. You shouldn't ever call it directly.
*
* @param arc An OID arc.
*
*/
protected void registerObject(long arc)
throws IllegalAccessException {
// this will register the variable in both varList and children
// The node registered in children will be null, so that the parent
// algorithm will behave as if no node were registered. This is a
// trick that makes the parent algorithm behave as if only subgroups
// were registered in varList and children.
long[] oid = new long[1];
oid[0] = arc;
super.registerNode(oid,0,null);
}
// -------------------------------------------------------------------
// registerNode() will be called at runtime when nested groups are
// registered in the MIB. So we do know that this method will only
// be called to register nested-groups.
// We trap registerNode() in order to call registerSubArc()
/**
* Register a child node of this node in the OID tree.
* This method is used internally. You shouldn't ever call it directly.
*
* @param oid The oid of the node being registered.
* @param cursor The position reached in the oid.
* @param node The node being registered.
*
*/
@Override
void registerNode(long[] oid, int cursor ,SnmpMibNode node)
throws IllegalAccessException {
super.registerNode(oid,cursor,node);
if (cursor < 0) return;
if (cursor >= oid.length) return;
// if we get here, then it means we are registering a subgroup.
// We will thus register the sub arc in the subgroups hashtable.
registerNestedArc(oid[cursor]);
}
// -------------------------------------------------------------------
// see comments in SnmpMibNode
// -------------------------------------------------------------------
@Override
void findHandlingNode(SnmpVarBind varbind,
long[] oid, int depth,
SnmpRequestTree handlers)
throws SnmpStatusException {
int length = oid.length;
if (handlers == null)
throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr);
final Object data = handlers.getUserData();
if (depth >= length) {
// Nothing is left... the oid is not valid
throw new SnmpStatusException(SnmpStatusException.noAccess);
}
long arc = oid[depth];
if (isNestedArc(arc)) {
// This arc leads to a subgroup: delegates the search to the
// method defined in SnmpMibOid
super.findHandlingNode(varbind,oid,depth,handlers);
} else if (isTable(arc)) {
// This arc leads to a table: forward the search to the table.
// Gets the table
SnmpMibTable table = getTable(arc);
// Forward the search to the table
table.findHandlingNode(varbind,oid,depth+1,handlers);
} else {
// If it's not a variable, throws an exception
validateVarId(arc, data);
// The trailing .0 is missing in the OID
if (depth+2 > length) {
throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
}
// There are too many arcs left in the OID (there should remain
// a single trailing .0)
if (depth+2 < length) {
throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
}
// The last trailing arc is not .0
if (oid[depth+1] != 0L) {
throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
}
// It's one of our variable, register this node.
handlers.add(this,depth,varbind);
}
}
// -------------------------------------------------------------------
// See comments in SnmpMibNode.
// -------------------------------------------------------------------
@Override
long[] findNextHandlingNode(SnmpVarBind varbind,
long[] oid, int pos, int depth,
SnmpRequestTree handlers, AcmChecker checker)
throws SnmpStatusException {
int length = oid.length;
SnmpMibNode node = null;
if (handlers == null) {
// This should be considered as a genErr, but we do not want to
// abort the whole request, so we're going to throw
// a noSuchObject...
//
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
}
final Object data = handlers.getUserData();
final int pduVersion = handlers.getRequestPduVersion();
// The generic case where the end of the OID has been reached is
// handled in the superclass
// XXX Revisit: this works but it is somewhat convoluted. Just setting
// arc to -1 would work too.
if (pos >= length)
return super.findNextHandlingNode(varbind,oid,pos,depth,
handlers, checker);
// Ok, we've got the arc.
long arc = oid[pos];
long[] result = null;
// We have a recursive logic. Should we have a loop instead?
try {
if (isTable(arc)) {
// If the arc identifies a table, then we need to forward
// the search to the table.
// Gets the table identified by `arc'
SnmpMibTable table = getTable(arc);
// Forward to the table
checker.add(depth, arc);
try {
result = table.findNextHandlingNode(varbind,oid,pos+1,
depth+1,handlers,
checker);
}catch(SnmpStatusException ex) {
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
} finally {
checker.remove(depth);
}
// Build up the leaf OID
result[depth] = arc;
return result;
} else if (isReadable(arc)) {
// If the arc identifies a readable variable, then two cases:
if (pos == (length - 1)) {
// The end of the OID is reached, so we return the leaf
// corresponding to the variable identified by `arc'
// Build up the OID
// result = new SnmpOid(0);
// result.insert((int)arc);
result = new long[depth+2];
result[depth+1] = 0L;
result[depth] = arc;
checker.add(depth, result, depth, 2);
try {
checker.checkCurrentOid();
} catch(SnmpStatusException e) {
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
} finally {
checker.remove(depth,2);
}
// Registers this node
handlers.add(this,depth,varbind);
return result;
}
// The end of the OID is not yet reached, so we must return
// the next leaf following the variable identified by `arc'.
// We cannot return the variable because whatever follows in
// the OID will be greater or equals to 0, and 0 identifies
// the variable itself - so we have indeed to return the
// next object.
// So we do nothing, because this case is handled at the
// end of the if ... else if ... else ... block.
} else if (isNestedArc(arc)) {
// Now if the arc leads to a subgroup, we delegate the
// search to the child, just as done in SnmpMibNode.
//
// get the child ( = nested arc node).
//
final SnmpMibNode child = getChild(arc);
if (child != null) {
checker.add(depth, arc);
try {
result = child.findNextHandlingNode(varbind,oid,pos+1,
depth+1,handlers,
checker);
result[depth] = arc;
return result;
} finally {
checker.remove(depth);
}
}
}
// The oid is not valid, we will throw an exception in order
// to try with the next valid identifier...
//
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
} catch (SnmpStatusException e) {
// We didn't find anything at the given arc, so we're going
// to try with the next valid arc
//
long[] newOid = new long[1];
newOid[0] = getNextVarId(arc,data,pduVersion);
return findNextHandlingNode(varbind,newOid,0,depth,
handlers,checker);
}
}
}

View File

@@ -0,0 +1,155 @@
/*
* Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// java imports
//
import java.util.Vector;
import java.io.IOException;
// jmx imports
//
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.SnmpStatusException;
/**
* The logical link between an SNMP MIB and the SNMP communication stack.
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
*/
public interface SnmpMibHandler {
/**
* Adds a new MIB in the SNMP MIB handler.
* This method is called automatically by {@link com.sun.jmx.snmp.agent.SnmpMibAgent#setSnmpAdaptor(SnmpMibHandler)} and
* {@link com.sun.jmx.snmp.agent.SnmpMibAgent#setSnmpAdaptorName(ObjectName)} and should not be called directly.
*
* @param mib The MIB to add.
*
* @return A reference on the SNMP MIB handler.
*
* @exception IllegalArgumentException If the parameter is null.
*/
public SnmpMibHandler addMib(SnmpMibAgent mib) throws IllegalArgumentException;
/**
* Adds a new MIB in the SNMP MIB handler.
*
* @param mib The MIB to add.
* @param oids The array of oid used to add the mib. Each oid is a root oid for the mib.
* @return A reference on the SNMP MIB handler.
*
* @exception IllegalArgumentException If the parameter is null.
*
* @since 1.5
*/
public SnmpMibHandler addMib(SnmpMibAgent mib, SnmpOid[] oids) throws IllegalArgumentException;
/**
* Adds a new contextualized MIB in the SNMP MIB handler.
*
* @param mib The MIB to add.
* @param contextName The MIB context name. If null is passed, will be registered in the default context.
*
* @return A reference to the SNMP MIB handler.
*
* @exception IllegalArgumentException If the parameter is null.
*
* @since 1.5
*/
public SnmpMibHandler addMib(SnmpMibAgent mib, String contextName)
throws IllegalArgumentException;
/**
* Adds a new contextualized MIB in the SNMP MIB handler.
*
* @param mib The MIB to add.
* @param contextName The MIB context name. If null is passed, will be registered in the default context.
* @param oids The array of oid used to add the mib. Each oid is a root oid for the mib.
*
* @return A reference to the SNMP MIB handler.
*
* @exception IllegalArgumentException If the parameter is null.
*
* @since 1.5
*/
public SnmpMibHandler addMib(SnmpMibAgent mib, String contextName, SnmpOid[] oids)
throws IllegalArgumentException;
/**
* Removes the specified MIB from the SNMP protocol adaptor.
* This method is called automatically by {@link com.sun.jmx.snmp.agent.SnmpMibAgent#setSnmpAdaptor(SnmpMibHandler)} and
* {@link com.sun.jmx.snmp.agent.SnmpMibAgent#setSnmpAdaptorName(ObjectName)} and should not be called directly.
*
* @param mib The MIB to be removed.
*
* @return <CODE>true</CODE> if the specified <CODE>mib</CODE> was a MIB included in the SNMP MIB handler,
* <CODE>false</CODE> otherwise.
*/
public boolean removeMib(SnmpMibAgent mib);
/**
* Removes the specified MIB from the SNMP protocol adaptor.
* This method is called automatically by {@link com.sun.jmx.snmp.agent.SnmpMibAgent#setSnmpAdaptor(SnmpMibHandler)} and
* {@link com.sun.jmx.snmp.agent.SnmpMibAgent#setSnmpAdaptorName(ObjectName)} and should not be called directly.
*
* @param mib The MIB to be removed.
* @param oids The oid the MIB was previously registered for.
* @return <CODE>true</CODE> if the specified <CODE>mib</CODE> was a MIB included in the SNMP MIB handler,
* <CODE>false</CODE> otherwise.
*
* @since 1.5
*/
public boolean removeMib(SnmpMibAgent mib, SnmpOid[] oids);
/**
* Removes the specified MIB from the SNMP protocol adaptor.
*
* @param mib The MIB to be removed.
* @param contextName The context name used at registration time.
*
* @return <CODE>true</CODE> if the specified <CODE>mib</CODE> was a MIB included in the SNMP MIB handler,
* <CODE>false</CODE> otherwise.
*
* @since 1.5
*/
public boolean removeMib(SnmpMibAgent mib, String contextName);
/**
* Removes the specified MIB from the SNMP protocol adaptor.
*
* @param mib The MIB to be removed.
* @param contextName The context name used at registration time.
* @param oids The oid the MIB was previously registered for.
* @return <CODE>true</CODE> if the specified <CODE>mib</CODE> was a MIB included in the SNMP MIB handler,
* <CODE>false</CODE> otherwise.
*
* @since 1.5
*/
public boolean removeMib(SnmpMibAgent mib, String contextName, SnmpOid[] oids);
}

View File

@@ -0,0 +1,406 @@
/*
* Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// java imports
//
import java.io.Serializable;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
// jmx imports
//
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.SnmpValue;
import com.sun.jmx.snmp.SnmpVarBind;
import com.sun.jmx.snmp.SnmpDefinitions;
import com.sun.jmx.snmp.SnmpStatusException;
/**
* The <CODE>SnmpMibNode</CODE> class represents a node in an SNMP MIB.
* <P>
* This class is used internally and by the class generated by
* <CODE>mibgen</CODE>.
* You should not need to use this class directly.
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
*/
public abstract class SnmpMibNode implements Serializable {
// ---------------------------------------------------------------------
// PUBLIC METHODS
//----------------------------------------------------------------------
/**
* Get the next OID arc corresponding to a readable scalar variable,
* a branch leading to a subgroub, or a table.
*
* @param id Id we start from looking for the next.
* @param userData A contextual object containing user-data.
* This object is allocated through the <code>
* {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}</code>
* for each incoming SNMP request.
*
* @return The next id in this group.
*
* @exception SnmpStatusException If no id is found after the given id.
*/
public long getNextVarId(long id, Object userData)
throws SnmpStatusException {
return getNextIdentifier(varList,id);
}
/**
* Get the next OID arc corresponding to a readable scalar variable,
* a branch leading to a subgroub, or a table, possibly skipping over
* those arcs that must not or cannot be returned.
*
* Calls {@link #getNextVarId(long,java.lang.Object)} until
* {@link #skipVariable(long,java.lang.Object,int)} returns false.
*
* @param id Id we start from looking for the next.
* @param userData A contextual object containing user-data.
* This object is allocated through the <code>
* {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}</code>
* for each incoming SNMP request.
* @param pduVersion Protocol version of the original request PDU.
*
* @return The next id in this group which can be returned using
* the given PDU's protocol version.
*
* @exception SnmpStatusException If no id is found after the given id.
*/
public long getNextVarId(long id, Object userData, int pduVersion)
throws SnmpStatusException {
long varid=id;
do {
varid = getNextVarId(varid,userData);
} while (skipVariable(varid,userData,pduVersion));
return varid;
}
/**
* Hook for subclasses.
* The default implementation of this method is to always return
* false. Subclasses should redefine this method so that it returns
* true when:
* <ul><li>the variable is a leaf that is not instantiated,</li>
* <li>or the variable is a leaf whose type cannot be returned by that
* version of the protocol (e.g. an Counter64 with SNMPv1).</li>
* </ul>
*
* @param id Id we start from looking for the next.
* @param userData A contextual object containing user-data.
* This object is allocated through the <code>
* {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}</code>
* for each incoming SNMP request.
* @param pduVersion Protocol version of the original request PDU.
*
* @return true if the variable must be skipped by the get-next
* algorithm.
*/
protected boolean skipVariable(long id, Object userData, int pduVersion) {
return false;
}
/**
* Find the node which handles a varbind, and register it in the
* SnmpRequestTree. This method is a pure internal method. You should
* never try to call it directly.
*
* @param varbind The varbind to be handled
*
* @param oid The OID array extracted from the varbind
*
* @param depth The depth reached in the OID at this step of the
* processing.
*
* @param handlers The Hashtable in which the varbind will be registered
* with its handling node. This hashtable contains
* <CODE>SnmpRequestTree.Handler</CODE> items.
*
* @exception SnmpStatusException No handling node was found.
**/
void findHandlingNode(SnmpVarBind varbind,
long[] oid, int depth,
SnmpRequestTree handlers)
throws SnmpStatusException {
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
}
/**
* Find the node which handles the leaf that immediately follows the
* given varbind OID, and register the it in the SnmpRequestTree.
* This method is a pure internal method. You should never try to call
* it directly.
*
* @param varbind The varbind to be handled
*
* @param oid The OID array extracted from the varbind
*
* @param depth The depth reached in the OID at this step of the
* processing.
*
* @param handlers The Hashtable in which the varbind will be registered
* with its handling node. This hashtable contains
* SnmpRequestTree.Handler items.
*
* @return The SnmpOid of the next leaf.
*
* @exception SnmpStatusException No handling node was found.
**/
long[] findNextHandlingNode(SnmpVarBind varbind,
long[] oid, int pos, int depth,
SnmpRequestTree handlers, AcmChecker checker)
throws SnmpStatusException {
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
}
/**
* Generic handling of the <CODE>get</CODE> operation.
*
* <p> You can override this method if you need to implement some
* specific policies for minimizing the accesses made to some remote
* underlying resources.
* <p>
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException An error occurred while accessing
* the MIB node.
*/
public abstract void get(SnmpMibSubRequest req, int depth)
throws SnmpStatusException;
/**
* Generic handling of the <CODE>set</CODE> operation.
* <p> You can override this method if you need to implement some
* specific policies for minimizing the accesses made to some remote
* underlying resources.
* <p>
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException An error occurred while accessing
* the MIB node.
*/
public abstract void set(SnmpMibSubRequest req, int depth)
throws SnmpStatusException;
/**
* Generic handling of the <CODE>check</CODE> operation.
* <p> You can override this method if you need to implement some
* specific policies for minimizing the accesses made to some remote
* underlying resources, or if you need to implement some consistency
* checks between the different values provided in the varbind list.
* <p>
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException An error occurred while accessing
* the MIB node.
*/
public abstract void check(SnmpMibSubRequest req, int depth)
throws SnmpStatusException;
/**
* Sorts the specified integer array.
*
* @param array An integer array.
*/
static public void sort(int array[]) {
QuickSort(array, 0, array.length - 1);
}
/**
* Computes the root OID of the MIB.
*/
public void getRootOid(Vector<Integer> result) {
return;
}
//----------------------------------------------------------------------
// PACKAGE METHODS
//----------------------------------------------------------------------
/**
* This is a generic version of C.A.R Hoare's Quick Sort
* algorithm. This will handle arrays that are already
* sorted, and arrays with duplicate keys.
*
* If you think of a one dimensional array as going from
* the lowest index on the left to the highest index on the right
* then the parameters to this function are lowest index or
* left and highest index or right. The first time you call
* this function it will be with the parameters 0, a.length - 1.
*
* @param a An integer array.
* @param lo0 Left boundary of array partition.
* @param hi0 Right boundary of array partition.
*/
static void QuickSort(int a[], int lo0, int hi0) {
int lo = lo0;
int hi = hi0;
int mid;
if ( hi0 > lo0) {
/* Arbitrarily establishing partition element as the midpoint of
* the array.
*/
mid = a[ ( lo0 + hi0 ) / 2 ];
// loop through the array until indices cross
while( lo <= hi ) {
/* find the first element that is greater than or equal to
* the partition element starting from the left Index.
*/
while( ( lo < hi0 ) && ( a[lo] < mid ))
++lo;
/* find an element that is smaller than or equal to
* the partition element starting from the right Index.
*/
while( ( hi > lo0 ) && ( a[hi] > mid ))
--hi;
// if the indexes have not crossed, swap
if( lo <= hi ) {
swap(a, lo, hi);
++lo;
--hi;
}
}
/* If the right index has not reached the left side of array
* must now sort the left partition.
*/
if( lo0 < hi )
QuickSort( a, lo0, hi );
/* If the left index has not reached the right side of array
* must now sort the right partition.
*/
if( lo < hi0 )
QuickSort( a, lo, hi0 );
}
}
//----------------------------------------------------------------------
// PROTECTED METHODS
//----------------------------------------------------------------------
/**
* This will give the first element greater than <CODE>value</CODE>
* in a sorted array.
* If there is no element of the array greater than <CODE>value</CODE>,
* the method will throw a <CODE>SnmpStatusException</CODE>.
*
* @param table A sorted integer array.
*
* @param value The greatest value.
*
* @exception SnmpStatusException If there is no element greater than
* <CODE>value</CODE>.
*/
final static protected int getNextIdentifier(int table[], long value)
throws SnmpStatusException {
final int[] a = table;
final int val= (int) value;
if (a == null) {
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
}
int low= 0;
int max= a.length;
int curr= low + (max-low)/2;
int elmt= 0;
// Basic check
//
if (max < 1) {
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
}
if (a[max-1] <= val) {
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
}
while (low <= max) {
elmt= a[curr];
if (val == elmt) {
// We ned to get the next index ...
//
curr++;
return a[curr];
}
if (elmt < val) {
low= curr +1;
} else {
max= curr -1;
}
curr= low + (max-low)/2;
}
return a[curr];
}
//----------------------------------------------------------------------
// PRIVATE METHODS
//----------------------------------------------------------------------
final static private void swap(int a[], int i, int j) {
int T;
T = a[i];
a[i] = a[j];
a[j] = T;
}
//----------------------------------------------------------------------
// PROTECTED VARIABLES
//----------------------------------------------------------------------
/**
* Contains the list of variable identifiers.
*/
protected int[] varList;
}

View File

@@ -0,0 +1,566 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// java imports
//
import java.io.Serializable;
import java.util.Vector;
import java.util.Enumeration;
// jmx imports
//
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.SnmpVarBind;
import com.sun.jmx.snmp.SnmpStatusException;
/**
* Represents a node in an SNMP MIB which is neither a group nor a variable.
* This class defines a list of sub-nodes and the methods that allow to
* manipulate the sub-nodes.
* <P>
* This class is used internally and by the class generated by
* <CODE>mibgen</CODE>.
* You should not need to use this class directly.
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
*/
public class SnmpMibOid extends SnmpMibNode implements Serializable {
private static final long serialVersionUID = 5012254771107446812L;
/**
* Default constructor.
*/
public SnmpMibOid() {
}
// PUBLIC METHODS
//---------------
/**
* Generic handling of the <CODE>get</CODE> operation.
*
* <p> This method should be overridden in subclasses.
* <p>
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException The default implementation (if not
* overridden) is to generate a SnmpStatusException.
*/
@Override
public void get(SnmpMibSubRequest req, int depth)
throws SnmpStatusException {
for (Enumeration<SnmpVarBind> e= req.getElements(); e.hasMoreElements();) {
SnmpVarBind var= e.nextElement();
SnmpStatusException x =
new SnmpStatusException(SnmpStatusException.noSuchObject);
req.registerGetException(var,x);
}
}
/**
* Generic handling of the <CODE>set</CODE> operation.
*
* <p> This method should be overridden in subclasses.
* <p>
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException The default implementation (if not
* overridden) is to generate a SnmpStatusException.
*/
@Override
public void set(SnmpMibSubRequest req, int depth)
throws SnmpStatusException {
for (Enumeration<SnmpVarBind> e= req.getElements(); e.hasMoreElements();) {
SnmpVarBind var= e.nextElement();
SnmpStatusException x =
new SnmpStatusException(SnmpStatusException.noAccess);
req.registerSetException(var,x);
}
}
/**
* Generic handling of the <CODE>check</CODE> operation.
*
* <p> This method should be overridden in subclasses.
* <p>
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException The default implementation (if not
* overridden) is to generate a SnmpStatusException.
*/
@Override
public void check(SnmpMibSubRequest req, int depth)
throws SnmpStatusException {
for (Enumeration<SnmpVarBind> e= req.getElements(); e.hasMoreElements();) {
SnmpVarBind var= e.nextElement();
SnmpStatusException x =
new SnmpStatusException(SnmpStatusException.noAccess);
req.registerCheckException(var,x);
}
}
// ---------------------------------------------------------------------
//
// Implements the method defined in SnmpMibNode.
//
// ---------------------------------------------------------------------
//
@Override
void findHandlingNode(SnmpVarBind varbind,
long[] oid, int depth,
SnmpRequestTree handlers)
throws SnmpStatusException {
final int length = oid.length;
SnmpMibNode node = null;
if (handlers == null)
throw new SnmpStatusException(SnmpStatusException.snmpRspGenErr);
if (depth > length) {
// Nothing is left... the oid is not valid
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
} else if (depth == length) {
// The oid is not complete...
throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
} else {
// Some children variable or subobject is being querried
// getChild() will raise an exception if no child is found.
//
final SnmpMibNode child= getChild(oid[depth]);
// XXXX zzzz : what about null children?
// (variables for nested groups)
// if child==null, then we're dealing with a variable or
// a table: we register this node.
// This behaviour should be overriden in subclasses,
// in particular in group meta classes: the group
// meta classes that hold tables should take care
// of forwarding this call to all the tables involved.
//
if (child == null)
handlers.add(this,depth,varbind);
else
child.findHandlingNode(varbind,oid,depth+1,handlers);
}
}
// ---------------------------------------------------------------------
//
// Implements the method defined in SnmpMibNode.
//
// ---------------------------------------------------------------------
//
@Override
long[] findNextHandlingNode(SnmpVarBind varbind,
long[] oid, int pos, int depth,
SnmpRequestTree handlers,
AcmChecker checker)
throws SnmpStatusException {
final int length = oid.length;
SnmpMibNode node = null;
long[] result = null;
if (handlers == null) {
// This should be considered as a genErr, but we do not want to
// abort the whole request, so we're going to throw
// a noSuchObject...
//
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
}
final Object data = handlers.getUserData();
final int pduVersion = handlers.getRequestPduVersion();
if (pos >= length) {
long[] newOid= new long[1];
newOid[0]= getNextVarId(-1,data,pduVersion);
result = findNextHandlingNode(varbind,newOid,0,depth,handlers,
checker);
return result;
}
// search the element specified in the oid
//
long[] newOid= new long[1];
long index= oid[pos];
while (true) {
try {
final SnmpMibNode child = getChild(index);
// SnmpOid result = null;
if (child == null) {
// shouldn't happen
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
// validateVarId(index);
// handlers.add(this,varbind,depth);
// result = new SnmpOid(0);
} else {
checker.add(depth, index);
try {
result = child.findNextHandlingNode(varbind,oid,pos+1,
depth+1,handlers,
checker);
} finally {
checker.remove(depth);
}
}
// Build up the leaf OID
result[depth] = index;
return result;
} catch(SnmpStatusException e) {
// If there is no such element go one level up ...
//
index= getNextVarId(index,data,pduVersion);
// There is no need to carry the original oid ...
newOid[0]=index;
pos= 1;
oid=newOid;
}
}
}
/**
* Computes the root OID of the MIB.
*/
@Override
public void getRootOid(Vector<Integer> result) {
// If a node has several children, let assume that we are one step to
// far in order to get the MIB root.
//
if (nbChildren != 1)
return;
result.addElement(varList[0]);
// Now query our child.
//
children.firstElement().getRootOid(result);
}
/**
* Registers a specific node in the tree.
*/
public void registerNode(String oidString ,SnmpMibNode node)
throws IllegalAccessException {
SnmpOid oid= new SnmpOid(oidString);
registerNode(oid.longValue(), 0, node);
}
// PROTECTED METHODS
//------------------
/**
* Registers a specific node in the tree.
*/
void registerNode(long[] oid, int cursor ,SnmpMibNode node)
throws IllegalAccessException {
if (cursor >= oid.length)
throw new IllegalAccessException();
// Check if the node is already defined
//
long var= oid[cursor];
//System.out.println("entering registration for val="
// + String.valueOf(var) + " position= " + cursor);
int pos = retrieveIndex(var);
if (pos == nbChildren) {
nbChildren++;
varList= new int[nbChildren];
varList[0]= (int) var;
pos =0;
if ( (cursor + 1) == oid.length) {
// That 's the end of the trip.
// Do not forward the registration
//System.out.println("End of trip for val="
// + String.valueOf(var) + " position= " + cursor);
children.insertElementAt(node,pos);
return;
}
//System.out.println("Create node for val="
// + String.valueOf(var) + " position= " + cursor);
SnmpMibOid child= new SnmpMibOid();
children.insertElementAt(child, pos);
child.registerNode(oid, cursor + 1, node);
return;
}
if (pos == -1) {
// The node is not yet registered
//
int[] tmp= new int[nbChildren + 1];
tmp[nbChildren]= (int) var;
System.arraycopy(varList, 0, tmp, 0, nbChildren);
varList= tmp;
nbChildren++;
SnmpMibNode.sort(varList);
int newPos = retrieveIndex(var);
varList[newPos]= (int) var;
if ( (cursor + 1) == oid.length) {
// That 's the end of the trip.
// Do not forward the registration
//System.out.println("End of trip for val="
// + String.valueOf(var) + " position= " + cursor);
children.insertElementAt(node, newPos);
return;
}
SnmpMibOid child= new SnmpMibOid();
// System.out.println("Create node for val=" +
// String.valueOf(var) + " position= " + cursor);
children.insertElementAt(child, newPos);
child.registerNode(oid, cursor + 1, node);
}
else {
// The node is already registered
//
SnmpMibNode child= children.elementAt(pos);
if ( (cursor + 1) == oid.length ) {
//System.out.println("Node already registered val=" +
// String.valueOf(var) + " position= " + cursor);
if (child == node) return;
if (child != null && node != null) {
// Now we're going to patch the tree the following way:
// if a subgroup has been registered before its father,
// we're going to replace the father OID node with
// the actual group-node and export the children from
// the temporary OID node to the actual group node.
//
if (node instanceof SnmpMibGroup) {
// `node' is a group => replace `child' with `node'
// export the child's subtree to `node'.
//
((SnmpMibOid)child).exportChildren((SnmpMibOid)node);
children.setElementAt(node,pos);
return;
} else if ((node instanceof SnmpMibOid) &&
(child instanceof SnmpMibGroup)) {
// `node' is a temporary node, and `child' is a
// group => keep child and export the node's
// subtree to `child'.
//
((SnmpMibOid)node).exportChildren((SnmpMibOid)child);
return;
} else if (node instanceof SnmpMibOid) {
// `node' and `child' are both temporary OID nodes
// => replace `child' with `node' and export child's
// subtree to `node'.
//
((SnmpMibOid)child).exportChildren((SnmpMibOid)node);
children.setElementAt(node,pos);
return;
}
}
children.setElementAt(node,pos);
} else {
if (child == null)
throw new IllegalAccessException();
((SnmpMibOid)child).registerNode(oid, cursor + 1, node);
}
}
}
/**
* Export this node's children to a brother node that will replace
* this node in the OID tree.
* This method is a patch that fixes the problem of registering
* a subnode before its father node.
*
**/
void exportChildren(SnmpMibOid brother)
throws IllegalAccessException {
if (brother == null) return;
final long[] oid = new long[1];
for (int i=0; i<nbChildren; i++) {
final SnmpMibNode child = children.elementAt(i);
if (child == null) continue;
oid[0] = varList[i];
brother.registerNode(oid,0,child);
}
}
// PRIVATE METHODS
//----------------
SnmpMibNode getChild(long id) throws SnmpStatusException {
// first we need to retrieve the identifier in the list of children
//
final int pos= getInsertAt(id);
if (pos >= nbChildren) {
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
}
if (varList[pos] != (int) id) {
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
}
// Access the node
//
SnmpMibNode child = null;
try {
child = children.elementAtNonSync(pos);
} catch(ArrayIndexOutOfBoundsException e) {
throw new SnmpStatusException(SnmpStatusException.noSuchObject);
}
if (child == null) {
throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
}
return child;
}
private int retrieveIndex(long val) {
int low= 0;
int cursor= (int) val;
if (varList == null || varList.length < 1)
return nbChildren;
int max= varList.length -1 ;
int curr= low + (max-low)/2;
int elmt;
while (low <= max) {
elmt= varList[curr];
if (cursor == elmt) {
// We need to get the next index ...
//
return curr;
}
if (elmt < cursor) {
low= curr +1;
} else {
max= curr -1;
}
curr= low + (max-low)/2;
}
return -1;
}
private int getInsertAt(long val) {
int low= 0;
final int index= (int) val;
if (varList == null)
return -1;
int max= varList.length -1 ;
int elmt;
//final int[] v = varList;
//if (index > a[max])
//return max +1;
int curr= low + (max-low)/2;
while (low <= max) {
elmt= varList[curr];
// never know ...we might find something ...
//
if (index == elmt)
return curr;
if (elmt < index) {
low= curr +1;
} else {
max= curr -1;
}
curr= low + (max-low)/2;
}
return curr;
}
// PRIVATE VARIABLES
//------------------
/**
* Contains the list of sub nodes.
*/
private NonSyncVector<SnmpMibNode> children = new NonSyncVector<>(1);
/**
* The number of sub nodes.
*/
private int nbChildren= 0;
// All the methods of the Vector class are synchronized.
// Synchronization is a very expensive operation. In our case it is
// not always required...
//
@SuppressWarnings("serial") // We will never serialize this
class NonSyncVector<E> extends Vector<E> {
public NonSyncVector(int size) {
super(size);
}
final void addNonSyncElement(E obj) {
ensureCapacity(elementCount + 1);
elementData[elementCount++] = obj;
}
@SuppressWarnings("unchecked") // cast to E
final E elementAtNonSync(int index) {
return (E) elementData[index];
}
}
}

View File

@@ -0,0 +1,175 @@
/*
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
import java.util.Enumeration;
import java.util.Vector;
import com.sun.jmx.snmp.SnmpVarBind;
import com.sun.jmx.snmp.SnmpPdu;
import com.sun.jmx.snmp.SnmpEngine;
/**
* This interface models the part of a SNMP request that involves
* a specific MIB. One object implementing this interface will be created
* for every MIB involved in a SNMP request, and that object will be passed
* to the SnmpMibAgent in charge of handling that MIB.
*
* Objects implementing this interface will be allocated by the SNMP engine.
* You will never need to implement this interface. You will only use it.
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
*/
public interface SnmpMibRequest {
/**
* Returns the list of varbind to be handled by the SNMP mib node.
*
* @return The element of the enumeration are instances of
* {@link com.sun.jmx.snmp.SnmpVarBind}
*/
public Enumeration<SnmpVarBind> getElements();
/**
* Returns the vector of varbind to be handled by the SNMP mib node.
* The caller shall not modify this vector.
*
* @return The element of the vector are instances of
* {@link com.sun.jmx.snmp.SnmpVarBind}
*/
public Vector<SnmpVarBind> getSubList();
/**
* Returns the SNMP protocol version of the original request. If SNMP V1 request are received, the version is upgraded to SNMP V2.
*
* @return The SNMP protocol version of the original request.
*/
public int getVersion();
/**
* Returns the SNMP protocol version of the original request. No translation is done on the version. The actual received request SNMP version is returned.
*
* @return The SNMP protocol version of the original request.
*
* @since 1.5
*/
public int getRequestPduVersion();
/**
* Returns the local engine. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise null is returned.
* @return the local engine.
*
* @since 1.5
*/
public SnmpEngine getEngine();
/**
* Gets the incoming request principal. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise null is returned.
* @return The request principal.
*
* @since 1.5
**/
public String getPrincipal();
/**
* Gets the incoming request security level. This level is defined in {@link com.sun.jmx.snmp.SnmpEngine SnmpEngine}. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise -1 is returned.
* @return The security level.
*
* @since 1.5
*/
public int getSecurityLevel();
/**
* Gets the incoming request security model. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise -1 is returned.
* @return The security model.
*
* @since 1.5
*/
public int getSecurityModel();
/**
* Gets the incoming request context name. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise null is returned.
* @return The context name.
*
* @since 1.5
*/
public byte[] getContextName();
/**
* Gets the incoming request context name used by Access Control Model in order to allow or deny the access to OIDs. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise null is returned.
* @return The checked context name.
*
* @since 1.5
*/
public byte[] getAccessContextName();
/**
* Returns a handle on a user allocated contextual object.
* This contextual object is allocated through the SnmpUserDataFactory
* on a per SNMP request basis, and is handed back to the user via
* SnmpMibRequest (and derivative) objects. It is never accessed by
* the system, but might be handed back in multiple threads. It is thus
* the user responsibility to make sure he handles this object in a
* thread safe manner.
*/
public Object getUserData();
/**
* Returns the varbind index that should be embedded in an
* SnmpStatusException for this particular varbind.
* This does not necessarily correspond to the "real"
* index value that will be returned in the result PDU.
*
* @param varbind The varbind for which the index value is
* querried. Note that this varbind <b>must</b> have
* been obtained from the enumeration returned by
* <CODE>getElements()</CODE>, or from the vector
* returned by <CODE>getSublist()</CODE>.
*
* @return The varbind index that should be embedded in an
* SnmpStatusException for this particular varbind.
*/
public int getVarIndex(SnmpVarBind varbind);
/**
* Adds a varbind to this request sublist. This method is used for
* internal purposes and you should never need to call it directly.
*
* @param varbind The varbind to be added in the sublist.
*
*/
public void addVarBind(SnmpVarBind varbind);
/**
* Returns the number of elements (varbinds) in this request sublist.
*
* @return The number of elements in the sublist.
*
**/
public int getSize();
/**
* Returns the SNMP PDU attached to the request.
* @return The SNMP PDU.
*
* @since 1.5
**/
public SnmpPdu getPdu();
}

View File

@@ -0,0 +1,257 @@
/*
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
import java.util.Enumeration;
import java.util.Vector;
import com.sun.jmx.snmp.SnmpPdu;
import com.sun.jmx.snmp.SnmpVarBind;
import com.sun.jmx.snmp.SnmpEngine;
/**
* This class implements the SnmpMibRequest interface.
* It represents the part of a SNMP request that involves a specific
* MIB. One instance of this class will be created for every MIB
* involved in a SNMP request, and will be passed to the SnmpMibAgent
* in charge of handling that MIB.
*
* Instances of this class are allocated by the SNMP engine. You will
* never need to use this class directly. You will only access
* instances of this class through their SnmpMibRequest interface.
*
*/
final class SnmpMibRequestImpl implements SnmpMibRequest {
/**
* @param engine The local engine.
* @param reqPdu The received pdu.
* @param vblist The vector of SnmpVarBind objects in which the
* MIB concerned by this request is involved.
* @param protocolVersion The protocol version of the SNMP request.
* @param userData User allocated contextual data. This object must
* be allocated on a per SNMP request basis through the
* SnmpUserDataFactory registered with the SnmpAdaptorServer,
* and is handed back to the user through SnmpMibRequest objects.
*/
public SnmpMibRequestImpl(SnmpEngine engine,
SnmpPdu reqPdu,
Vector<SnmpVarBind> vblist,
int protocolVersion,
Object userData,
String principal,
int securityLevel,
int securityModel,
byte[] contextName,
byte[] accessContextName) {
varbinds = vblist;
version = protocolVersion;
data = userData;
this.reqPdu = reqPdu;
this.engine = engine;
this.principal = principal;
this.securityLevel = securityLevel;
this.securityModel = securityModel;
this.contextName = contextName;
this.accessContextName = accessContextName;
}
// -------------------------------------------------------------------
// PUBLIC METHODS from SnmpMibRequest
// -------------------------------------------------------------------
/**
* Returns the local engine. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise null is returned.
* @return the local engine.
*/
@Override
public SnmpEngine getEngine() {
return engine;
}
/**
* Gets the incoming request principal. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise null is returned.
* @return The request principal.
**/
@Override
public String getPrincipal() {
return principal;
}
/**
* Gets the incoming request security level. This level is defined in {@link com.sun.jmx.snmp.SnmpEngine SnmpEngine}. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise -1 is returned.
* @return The security level.
*/
@Override
public int getSecurityLevel() {
return securityLevel;
}
/**
* Gets the incoming request security model. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise -1 is returned.
* @return The security model.
*/
@Override
public int getSecurityModel() {
return securityModel;
}
/**
* Gets the incoming request context name. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise null is returned.
* @return The context name.
*/
@Override
public byte[] getContextName() {
return contextName;
}
/**
* Gets the incoming request context name used by Access Control Model in order to allow or deny the access to OIDs. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise null is returned.
* @return The checked context.
*/
@Override
public byte[] getAccessContextName() {
return accessContextName;
}
// -------------------------------------------------------------------
// Implements the method defined in SnmpMibRequest interface.
// See SnmpMibRequest for the java doc.
// -------------------------------------------------------------------
@Override
public final SnmpPdu getPdu() {
return reqPdu;
}
// -------------------------------------------------------------------
// Implements the method defined in SnmpMibRequest interface.
// See SnmpMibRequest for the java doc.
// -------------------------------------------------------------------
@Override
public final Enumeration<SnmpVarBind> getElements() {return varbinds.elements();}
// -------------------------------------------------------------------
// Implements the method defined in SnmpMibRequest interface.
// See SnmpMibRequest for the java doc.
// -------------------------------------------------------------------
@Override
public final Vector<SnmpVarBind> getSubList() {return varbinds;}
// -------------------------------------------------------------------
// Implements the method defined in SnmpMibRequest interface.
// See SnmpMibRequest for the java doc.
// -------------------------------------------------------------------
@Override
public final int getSize() {
if (varbinds == null) return 0;
return varbinds.size();
}
// -------------------------------------------------------------------
// Implements the method defined in SnmpMibRequest interface.
// See SnmpMibRequest for the java doc.
// -------------------------------------------------------------------
@Override
public final int getVersion() {return version;}
// -------------------------------------------------------------------
// Implements the method defined in SnmpMibRequest interface.
// See SnmpMibRequest for the java doc.
// -------------------------------------------------------------------
@Override
public final int getRequestPduVersion() {return reqPdu.version;}
// -------------------------------------------------------------------
// Implements the method defined in SnmpMibRequest interface.
// See SnmpMibRequest for the java doc.
// -------------------------------------------------------------------
@Override
public final Object getUserData() {return data;}
// -------------------------------------------------------------------
// Implements the method defined in SnmpMibRequest interface.
// See SnmpMibRequest for the java doc.
// -------------------------------------------------------------------
@Override
public final int getVarIndex(SnmpVarBind varbind) {
return varbinds.indexOf(varbind);
}
// -------------------------------------------------------------------
// Implements the method defined in SnmpMibRequest interface.
// See SnmpMibRequest for the java doc.
// -------------------------------------------------------------------
@Override
public void addVarBind(SnmpVarBind varbind) {
varbinds.addElement(varbind);
}
// -------------------------------------------------------------------
// PACKAGE METHODS
// -------------------------------------------------------------------
// -------------------------------------------------------------------
// Allow to pass the request tree built during the check() phase
// to the set() method. Note: the if the tree is `null', then the
// set() method will rebuild a new tree identical to the tree built
// in the check() method.
//
// Passing this tree in the SnmpMibRequestImpl object allows to
// optimize the SET requests.
//
// -------------------------------------------------------------------
final void setRequestTree(SnmpRequestTree tree) {this.tree = tree;}
// -------------------------------------------------------------------
// Returns the SnmpRequestTree object built in the first operation
// phase for two-phase SNMP requests (like SET).
// -------------------------------------------------------------------
final SnmpRequestTree getRequestTree() {return tree;}
// -------------------------------------------------------------------
// Returns the underlying vector of SNMP varbinds (used for algorithm
// optimization).
// -------------------------------------------------------------------
final Vector<SnmpVarBind> getVarbinds() {return varbinds;}
// -------------------------------------------------------------------
// Private variables
// -------------------------------------------------------------------
// Ideally these variables should be declared final but it makes
// the jdk1.1.x compiler complain (seems to be a compiler bug, jdk1.2
// is OK).
private Vector<SnmpVarBind> varbinds;
private int version;
private Object data;
private SnmpPdu reqPdu = null;
// Non final variable.
private SnmpRequestTree tree = null;
private SnmpEngine engine = null;
private String principal = null;
private int securityLevel = -1;
private int securityModel = -1;
private byte[] contextName = null;
private byte[] accessContextName = null;
}

View File

@@ -0,0 +1,211 @@
/*
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
import java.util.Enumeration;
import java.util.Vector;
import com.sun.jmx.snmp.SnmpVarBind;
import com.sun.jmx.snmp.SnmpStatusException;
import com.sun.jmx.snmp.SnmpOid;
// import com.sun.jmx.snmp.SnmpIndex;
/**
* This interface models an SNMP sub request to be performed on a specific
* SNMP MIB node. The node involved can be either an SNMP group, an SNMP table,
* or an SNMP table entry (conceptual row). The conceptual row may or may not
* already exist. If the row did not exist at the time when the request
* was received, the <CODE>isNewEntry()</CODE> method will return <CODE>
* true</CODE>.
* <p>
* Objects implementing this interface will be allocated by the SNMP engine.
* You will never need to implement this interface. You will only use it.
* </p>
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
*/
public interface SnmpMibSubRequest extends SnmpMibRequest {
/**
* Return the list of varbind to be handled by the SNMP MIB node.
* <p>
* <b>Note:</b> <ul>
* <i>In case of SET operation, if this node is a table row which
* contains a control variable (as identified by the table's
* isRowStatus() method) the control variable will not
* be included in this list: it will be obtained by calling
* getRowStatusVarBind(). This will allow you to handle the control
* variable specifically.</i><br>
* You will never need to worry about this unless you need to
* implement a non standard mechanism for handling row
* creation and deletion.
* </ul>
* <p>
* @return The elements of the enumeration are instances of
* {@link com.sun.jmx.snmp.SnmpVarBind}
*/
@Override
public Enumeration<SnmpVarBind> getElements();
/**
* Return the list of varbind to be handled by the SNMP MIB node.
* <p>
* <b>Note:</b> <ul>
* <i>In case of SET operation, if this node is a table row which
* contains a control variable (as identified by the table's
* isRowStatus() method) the control variable will not
* be included in this list: it will be obtained by calling
* getRowStatusVarBind(). This will allow you to handle the control
* variable specifically.</i><br>
* You will never need to worry about this unless you need to
* implement a non standard mechanism for handling row
* creation and deletion.
* </ul>
* <p>
* @return The elements of the vector are instances of
* {@link com.sun.jmx.snmp.SnmpVarBind}
*/
@Override
public Vector<SnmpVarBind> getSubList();
/**
* Return the part of the OID identifying the table entry involved.
* <p>
*
* @return {@link com.sun.jmx.snmp.SnmpOid} or <CODE>null</CODE>
* if the request is not directed to an entry.
*/
public SnmpOid getEntryOid();
/**
* Indicate whether the entry involved is a new entry.
* This method will return <CODE>true</CODE> if the entry was not
* found when the request was processed. As a consequence, <CODE>
* true</CODE> means that either the entry does not exist yet,
* or it has been created while processing this request.
* The result of this method is only significant when an entry
* is involved.
*
* <p>
* @return <CODE>true</CODE> If the entry did not exist,
* or <CODE>false</CODE> if the entry involved was found.
*/
public boolean isNewEntry();
/**
* Return the varbind that holds the RowStatus variable.
* It corresponds to the varbind that was identified by
* the <code>isRowStatus()</code> method generated by mibgen
* on {@link com.sun.jmx.snmp.agent.SnmpMibTable} derivatives.
* <ul><li>In SMIv2, it is the varbind which contains the columnar
* object implementing the RowStatus TEXTUAL-CONVENTION.</li>
* <li>In SMIv1 nothing special is generated</li>
* <ul>You may however subclass the generated table metadata
* class in order to provide your own implementation of
* isRowStatus(), getRowAction(), isRowReady() and
* setRowStatus()
* (see {@link com.sun.jmx.snmp.agent.SnmpMibTable}).</ul>
* </ul>
* <p>
* @return a varbind that serves to control the table modification.
* <code>null</code> means that no such varbind could be
* identified.<br>
* <b>Note:</b><i>The runtime will only try to identify
* the RowStatus varbind when processing an
* SNMP SET request. In this case, the identified
* varbind will not be included in the set of varbinds
* returned by getSubList() and getElements().
* </i>
*
**/
public SnmpVarBind getRowStatusVarBind();
/**
* This method should be called when a status exception needs to
* be raised for a given varbind of an SNMP GET request. This method
* performs all the necessary conversions (SNMPv1 <=> SNMPv2) and
* propagates the exception if needed:
* If the version is SNMP v1, the exception is propagated.
* If the version is SNMP v2, the exception is stored in the varbind.
* This method also takes care of setting the correct value of the
* index field.
* <p>
*
* @param varbind The varbind for which the exception is
* registered. Note that this varbind <b>must</b> have
* been obtained from the enumeration returned by
* <CODE>getElements()</CODE>, or from the vector
* returned by <CODE>getSubList()</CODE>
*
* @param exception The exception to be registered for the given varbind.
*
*/
public void registerGetException(SnmpVarBind varbind,
SnmpStatusException exception)
throws SnmpStatusException;
/**
* This method should be called when a status exception needs to
* be raised for a given varbind of an SNMP SET request. This method
* performs all the necessary conversions (SNMPv1 <=> SNMPv2) and
* propagates the exception if needed.
* This method also takes care of setting the correct value of the
* index field.
* <p>
*
* @param varbind The varbind for which the exception is
* registered. Note that this varbind <b>must</b> have
* been obtained from the enumeration returned by
* <CODE>getElements()</CODE>, or from the vector
* returned by <CODE>getSubList()</CODE>
*
* @param exception The exception to be registered for the given varbind.
*
*/
public void registerSetException(SnmpVarBind varbind,
SnmpStatusException exception)
throws SnmpStatusException;
/**
* This method should be called when a status exception needs to
* be raised when checking a given varbind for an SNMP SET request.
* This method performs all the necessary conversions (SNMPv1 <=>
* SNMPv2) and propagates the exception if needed.
* This method also takes care of setting the correct value of the
* index field.
* <p>
*
* @param varbind The varbind for which the exception is
* registered. Note that this varbind <b>must</b> have
* been obtained from the enumeration returned by
* <CODE>getElements()</CODE>, or from the vector
* returned by <CODE>getSubList()</CODE>
*
* @param exception The exception to be registered for the given varbind.
*
*/
public void registerCheckException(SnmpVarBind varbind,
SnmpStatusException exception)
throws SnmpStatusException;
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,119 @@
/*
* Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// jmx imports
//
import com.sun.jmx.snmp.SnmpValue;
import com.sun.jmx.snmp.SnmpStatusException;
/**
* <p>
* This interface defines the methods that must be implemented by an
* SNMP metadata object that needs to interact with an
* {@link com.sun.jmx.snmp.agent.SnmpStandardObjectServer} object.
* </p>
* <p>
* All these methods are usually generated by <code>mibgen</code> when
* run in standard-metadata mode (default).
* </p>
* <p><b><i>
* This interface is used internally between the generated Metadata and
* the SNMP runtime and you shouldn't need to worry about it, because
* you will never have to use it directly.
* </b></i></p>
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
**/
public interface SnmpStandardMetaServer {
/**
* Returns the value of the scalar object identified by the given
* OID arc.
*
* @param arc OID arc of the querried scalar object.
*
* @return The <CODE>SnmpValue</CODE> of the scalar object identified
* by <CODE>arc</CODE>.
*
* @param userData A contextual object containing user-data.
* This object is allocated through the <code>
* {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}</code>
* for each incoming SNMP request.
*
* @exception SnmpStatusException If the arc is not valid, or if
* access is denied.
*
**/
public SnmpValue get(long arc, Object userData)
throws SnmpStatusException ;
/**
* Sets the value of the scalar object identified by the given
* OID arc.
*
* @param x New value for the scalar object identified by
* <CODE>arc</CODE>
*
* @param arc OID arc of the scalar object whose value is set.
*
* @return The new <CODE>SnmpValue</CODE> of the scalar object
* identified by <CODE>arc</CODE>.
*
* @param userData A contextual object containing user-data.
* This object is allocated through the <code>
* {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}</code>
* for each incoming SNMP request.
*
* @exception SnmpStatusException If the arc is not valid, or if
* access is denied.
*
**/
public SnmpValue set(SnmpValue x, long arc, Object userData)
throws SnmpStatusException ;
/**
* Checks that the new desired value of the scalar object identified
* by the given OID arc is valid.
*
* @param x New value for the scalar object identified by
* <CODE>arc</CODE>
*
* @param arc OID arc of the scalar object whose value is set.
*
* @param userData A contextual object containing user-data.
* This object is allocated through the <code>
* {@link com.sun.jmx.snmp.agent.SnmpUserDataFactory}</code>
* for each incoming SNMP request.
*
* @exception SnmpStatusException If the arc is not valid, or if
* access is denied, or if the new desired value is not valid.
*
**/
public void check(SnmpValue x, long arc, Object userData)
throws SnmpStatusException ;
}

View File

@@ -0,0 +1,256 @@
/*
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// java imports
//
import java.io.Serializable;
import java.util.Enumeration;
import com.sun.jmx.snmp.SnmpVarBind;
import com.sun.jmx.snmp.SnmpStatusException;
// SNMP Runtime imports
//
/**
* <p>
* This class is a utility class that transform SNMP GET / SET requests
* into series of get<i>AttributeName</i>() set<i>AttributeName</i>()
* invoked on the MBean.
* </p>
*
* <p>
* The transformation relies on the metadata information provided by the
* {@link com.sun.jmx.snmp.agent.SnmpStandardMetaServer} object which is
* passed as first parameter to every method. This SnmpStandardMetaServer
* object is usually a Metadata object generated by <code>mibgen</code>.
* </p>
*
* <p>
* The MBean is not invoked directly by this class but through the
* metadata object which holds a reference on it.
* </p>
*
* <p><b><i>
* This class is used internally by mibgen generated metadata objects and
* you should never need to use it directly.
* </b></i></p>
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
**/
public class SnmpStandardObjectServer implements Serializable {
private static final long serialVersionUID = -4641068116505308488L;
/**
* Generic handling of the <CODE>get</CODE> operation.
* <p> The default implementation of this method is to loop over the
* varbind list associated with the sub-request and to call
* <CODE>get(var.oid.getOidArc(depth), data);</CODE>
* <pre>
* public void get(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
* int depth)
* throws SnmpStatusException {
*
* final Object data = req.getUserData();
*
* for (Enumeration e= req.getElements(); e.hasMoreElements();) {
*
* final SnmpVarBind var= (SnmpVarBind) e.nextElement();
*
* try {
* // This method will generate a SnmpStatusException
* // if `depth' is out of bounds.
* //
* final long id = var.oid.getOidArc(depth);
* var.value = meta.get(id, data);
* } catch(SnmpStatusException x) {
* req.registerGetException(var,x);
* }
* }
* }
* </pre>
* <p> You can override this method if you need to implement some
* specific policies for minimizing the accesses made to some remote
* underlying resources.
* <p>
*
* @param meta A pointer to the generated meta-data object which
* implements the <code>SnmpStandardMetaServer</code>
* interface.
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException An error occurred while accessing
* the MIB node.
*/
public void get(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
int depth)
throws SnmpStatusException {
final Object data = req.getUserData();
for (Enumeration<SnmpVarBind> e= req.getElements(); e.hasMoreElements();) {
final SnmpVarBind var= e.nextElement();
try {
final long id = var.oid.getOidArc(depth);
var.value = meta.get(id, data);
} catch(SnmpStatusException x) {
req.registerGetException(var,x);
}
}
}
/**
* Generic handling of the <CODE>set</CODE> operation.
* <p> The default implementation of this method is to loop over the
* varbind list associated with the sub-request and to call
* <CODE>set(var.value, var.oid.getOidArc(depth), data);</CODE>
* <pre>
* public void set(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
* int depth)
* throws SnmpStatusException {
*
* final Object data = req.getUserData();
*
* for (Enumeration e= req.getElements(); e.hasMoreElements();) {
*
* final SnmpVarBind var= (SnmpVarBind) e.nextElement();
*
* try {
* // This method will generate a SnmpStatusException
* // if `depth' is out of bounds.
* //
* final long id = var.oid.getOidArc(depth);
* var.value = meta.set(var.value, id, data);
* } catch(SnmpStatusException x) {
* req.registerSetException(var,x);
* }
* }
* }
* </pre>
* <p> You can override this method if you need to implement some
* specific policies for minimizing the accesses made to some remote
* underlying resources.
* <p>
*
* @param meta A pointer to the generated meta-data object which
* implements the <code>SnmpStandardMetaServer</code>
* interface.
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException An error occurred while accessing
* the MIB node.
*/
public void set(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
int depth)
throws SnmpStatusException {
final Object data = req.getUserData();
for (Enumeration<SnmpVarBind> e= req.getElements(); e.hasMoreElements();) {
SnmpVarBind var = e.nextElement();
try {
// This method will generate a SnmpStatusException
// if `depth' is out of bounds.
//
final long id = var.oid.getOidArc(depth);
var.value = meta.set(var.value, id, data);
} catch(SnmpStatusException x) {
req.registerSetException(var,x);
}
}
}
/**
* Generic handling of the <CODE>check</CODE> operation.
* <p> The default implementation of this method is to loop over the
* varbind list associated with the sub-request and to call
* <CODE>check(var.value, var.oid.getOidArc(depth), data);</CODE>
* <pre>
* public void check(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
* int depth)
* throws SnmpStatusException {
*
* final Object data = req.getUserData();
*
* for (Enumeration e= req.getElements(); e.hasMoreElements();) {
*
* final SnmpVarBind var= (SnmpVarBind) e.nextElement();
*
* try {
* // This method will generate a SnmpStatusException
* // if `depth' is out of bounds.
* //
* final long id = var.oid.getOidArc(depth);
* meta.check(var.value, id, data);
* } catch(SnmpStatusException x) {
* req.registerCheckException(var,x);
* }
* }
* }
* </pre>
* <p> You can override this method if you need to implement some
* specific policies for minimizing the accesses made to some remote
* underlying resources, or if you need to implement some consistency
* checks between the different values provided in the varbind list.
* <p>
*
* @param meta A pointer to the generated meta-data object which
* implements the <code>SnmpStandardMetaServer</code>
* interface.
*
* @param req The sub-request that must be handled by this node.
*
* @param depth The depth reached in the OID tree.
*
* @exception SnmpStatusException An error occurred while accessing
* the MIB node.
*/
public void check(SnmpStandardMetaServer meta, SnmpMibSubRequest req,
int depth)
throws SnmpStatusException {
final Object data = req.getUserData();
for (Enumeration<SnmpVarBind> e= req.getElements(); e.hasMoreElements();) {
final SnmpVarBind var = e.nextElement();
try {
// This method will generate a SnmpStatusException
// if `depth' is out of bounds.
//
final long id = var.oid.getOidArc(depth);
meta.check(var.value,id,data);
} catch(SnmpStatusException x) {
req.registerCheckException(var,x);
}
}
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
import javax.management.ObjectName;
import com.sun.jmx.snmp.SnmpStatusException;
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.agent.SnmpMibTable;
/**
* This interface ensures the synchronization between Metadata table objects
* and bean-like table objects.
*
* It is used between mibgen generated table meta and table classes.
* <p><b><i>
* You should never need to use this interface directly.
* </p></b></i>
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
**/
public interface SnmpTableCallbackHandler {
/**
* This method is called by the SNMP runtime after a new entry
* has been added to the table.
*
* If an SnmpStatusException is raised, the entry will be removed
* and the operation will be aborted. In this case, the removeEntryCb()
* callback will not be called.
*
* <p><b><i>
* You should never need to use this method directly.
* </p></b></i>
*
**/
public void addEntryCb(int pos, SnmpOid row, ObjectName name,
Object entry, SnmpMibTable meta)
throws SnmpStatusException;
/**
* This method is called by the SNMP runtime after a new entry
* has been removed from the table.
*
* If raised, SnmpStatusException will be ignored.
*
* <p><b><i>
* You should never need to use this method directly.
* </p></b></i>
*
**/
public void removeEntryCb(int pos, SnmpOid row, ObjectName name,
Object entry, SnmpMibTable meta)
throws SnmpStatusException;
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
import com.sun.jmx.snmp.SnmpStatusException;
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.agent.SnmpMibTable;
import com.sun.jmx.snmp.agent.SnmpMibSubRequest;
/**
* This interface is implemented by mibgen generated table objects
* inheriting from {@link com.sun.jmx.snmp.agent.SnmpTableSupport}.
* <p>
* It is used internally by the metadata whenever a remote SNMP manager
* requests the creation of a new entry through an SNMP SET.
* </p>
* <p>
* At creation, the mibgen generated table object retrieves its
* corresponding metadata from the MIB and registers with
* this metadata as a SnmpTableEntryFactory.
* </p>
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
**/
public interface SnmpTableEntryFactory extends SnmpTableCallbackHandler {
/**
* This method is called by the SNMP runtime whenever a new entry
* creation is requested by a remote manager.
*
* The factory is responsible for instantiating the appropriate MBean
* and for registering it with the appropriate metadata object.
*
* Usually this method will:
* <ul>
* <li>Check whether the creation can be accepted
* <li>Instantiate a new entry
* <li>Possibly register this entry with the MBeanServer, if needed.
* <li>Call <code>addEntry()</code> on the given <code>meta</code> object.
* </ul>
* This method is usually generated by <code>mibgen</code> on table
* objects (inheriting from
* {@link com.sun.jmx.snmp.agent.SnmpTableSupport}). <br>
*
* <p><b><i>
* This method is called internally by the SNMP runtime whenever a
* new entry creation is requested by a remote SNMP manager.
* You should never need to call this method directlty.
* </i></b></p>
*
* @param request The SNMP subrequest containing the sublist of varbinds
* for the new entry.
* @param rowOid The OID indexing the conceptual row (entry) for which
* the creation was requested.
* @param depth The depth reached in the OID tree (the position at
* which the columnar object ids start in the OIDs
* included in the varbind).
* @param meta The metadata object impacted by the subrequest
*
* @exception SnmpStatusException The new entry cannot be created.
*
**/
public void createNewEntry(SnmpMibSubRequest request, SnmpOid rowOid,
int depth, SnmpMibTable meta)
throws SnmpStatusException;
}

View File

@@ -0,0 +1,141 @@
/*
* Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// jmx imports
//
import javax.management.Notification;
import javax.management.ObjectName;
/**
* Represents a notification emitted when an
* entry is added or deleted from an SNMP table.
* <P>
* The <CODE>SnmpTableEntryNotification</CODE> object contains
* the reference to the entry added or removed from the table.
* <P>
* The list of notifications fired by the <CODE>SnmpMibTable</CODE> is
* the following:
* <UL>
* <LI>A new entry has been added to the SNMP table.
* <LI>An existing entry has been removed from the SNMP table.
</UL>
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
*/
public class SnmpTableEntryNotification extends Notification {
/**
* Creates and initializes a table entry notification object.
*
* @param type The notification type.
* @param source The notification producer.
* @param sequenceNumber The notification sequence number within the
* source object.
* @param timeStamp The notification emission date.
* @param entry The entry object (may be null if the entry is
* registered in the MBeanServer).
* @param entryName The ObjectName entry object (may be null if the
* entry is not registered in the MBeanServer).
* @since 1.5
*/
SnmpTableEntryNotification(String type, Object source,
long sequenceNumber, long timeStamp,
Object entry, ObjectName entryName) {
super(type, source, sequenceNumber, timeStamp);
this.entry = entry;
this.name = entryName;
}
/**
* Gets the entry object.
* May be null if the entry is registered in the MBeanServer, and the
* MIB is using the generic MetaData (see mibgen).
*
* @return The entry.
*/
public Object getEntry() {
return entry;
}
/**
* Gets the ObjectName of the entry.
* May be null if the entry is not registered in the MBeanServer.
*
* @return The ObjectName of the entry.
* @since 1.5
*/
public ObjectName getEntryName() {
return name;
}
// PUBLIC VARIABLES
//-----------------
/**
* Notification type denoting that a new entry has been added to the
* SNMP table.
* <BR>The value of this notification type is
* <CODE>jmx.snmp.table.entry.added</CODE>.
*/
public static final String SNMP_ENTRY_ADDED =
"jmx.snmp.table.entry.added";
/**
* Notification type denoting that an entry has been removed from the
* SNMP table.
* <BR>The value of this notification type is
* <CODE>jmx.snmp.table.entry.removed</CODE>.
*/
public static final String SNMP_ENTRY_REMOVED =
"jmx.snmp.table.entry.removed";
// PRIVATE VARIABLES
//------------------
/**
* The entry object.
* @serial
*/
private final Object entry;
/**
* The entry name.
* @serial
* @since 1.5
*/
private final ObjectName name;
// Ensure compatibility
//
private static final long serialVersionUID = 5832592016227890252L;
}

View File

@@ -0,0 +1,571 @@
/*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
// java imports
//
import java.io.Serializable;
import java.util.Date;
import java.util.Vector;
import java.util.Enumeration;
import java.util.List;
import java.util.ArrayList;
// jmx imports
//
import javax.management.Notification;
import javax.management.ObjectName;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.NotificationBroadcaster;
import javax.management.MBeanNotificationInfo;
import javax.management.ListenerNotFoundException;
import com.sun.jmx.snmp.SnmpOid;
import com.sun.jmx.snmp.SnmpValue;
import com.sun.jmx.snmp.SnmpVarBind;
import com.sun.jmx.snmp.SnmpStatusException;
/**
* This class is an abstraction for an SNMP table.
* It is the base class for implementing SNMP tables in the
* MBean world.
*
* <p>
* Its responsibility is to synchronize the MBean view of the table
* (Table of entries) with the MIB view (array of OID indexes). Each
* object of this class will be bound to the Metadata object which
* manages the same SNMP Table within the MIB.
* </p>
*
* <p>
* For each table defined in a MIB, mibgen will generate a specific
* class called Table<i>TableName</i> that will subclass this class, and
* a corresponding <i>TableName</i>Meta class extending SnmpMibTable
* and corresponding to the MIB view of the same table.
* </p>
*
* <p>
* Objects of this class are instantiated by MBeans representing
* the SNMP Group to which the table belong.
* </p>
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
* @see com.sun.jmx.snmp.agent.SnmpTableEntryFactory
* @see com.sun.jmx.snmp.agent.SnmpMibTable
*
*/
public abstract class SnmpTableSupport implements SnmpTableEntryFactory,
// NPCTE fix for bugId 4499265, esc 0, MR 04 sept 2001
// SnmpTableCallbackHandler {
SnmpTableCallbackHandler, Serializable {
// end of NPCTE fix for bugId 4499265
//-----------------------------------------------------------------
//
// Protected Variables
//
//-----------------------------------------------------------------
/**
* The list of entries
**/
protected List<Object> entries;
/**
* The associated metadata object
**/
protected SnmpMibTable meta;
/**
* The MIB to which this table belongs
**/
protected SnmpMib theMib;
//-----------------------------------------------------------------
//
// Private Variables
//
//-----------------------------------------------------------------
/**
* This variable is initialized while binding this object to its
* corresponding meta object.
**/
private boolean registrationRequired = false;
//-----------------------------------------------------------------
//
// Constructor
//
//-----------------------------------------------------------------
/**
* Initializes the table.
* The steps are these:
* <ul><li> allocate an array for storing entry object,</li>
* <li> retrieve the corresponding metadata object
* from the MIB,
* <li> bind this object to the corresponding metadata object
* from the MIB.</li>
* </ul>
*
* @param mib The MIB to which this table belong.
*
**/
protected SnmpTableSupport(SnmpMib mib) {
theMib = mib;
meta = getRegisteredTableMeta(mib);
bindWithTableMeta();
entries = allocateTable();
}
//-----------------------------------------------------------------
//
// Implementation of the SnmpTableEntryFactory interface
//
//-----------------------------------------------------------------
/**
* Creates a new entry in the table.
*
* This factory method is generated by mibgen and used internally.
* It is part of the
* {@link com.sun.jmx.snmp.agent.SnmpTableEntryFactory} interface.
* You may subclass this method to implement any specific behaviour
* your application requires.
*
* @exception SnmpStatusException if the entry cannot be created.
**/
public abstract void createNewEntry(SnmpMibSubRequest request,
SnmpOid rowOid, int depth,
SnmpMibTable meta)
throws SnmpStatusException;
//-----------------------------------------------------------------
//
// Public methods
//
//-----------------------------------------------------------------
/**
* Returns the entry located at the given position in the table.
*
* @return The entry located at the given position, <code>null</code>
* if no entry can be found at this position.
**/
// XXXX xxxx zzz ZZZZ => public? or protected?
public Object getEntry(int pos) {
if (entries == null) return null;
return entries.get(pos);
}
/**
* Returns the number of entries registered in the table.
*
* @return The number of entries registered in the table.
**/
public int getSize() {
return meta.getSize();
}
/**
* This method lets you dynamically switch the creation policy.
*
* <CODE>setCreationEnabled()</CODE> will switch the policy of
* remote entry creation via SET operations, by calling
* <code>setCreationEnabled()</code> on the metadata object
* associated with this table.
* <BR> By default remote entry creation via SET operation is disabled.
*
* @param remoteCreationFlag Tells whether remote entry creation must
* be enabled or disabled.
* <li>
* <CODE>setCreationEnabled(true)</CODE> will enable remote entry
* creation via SET operations.</li>
* <li>
* <CODE>setCreationEnabled(false)</CODE> will disable remote entry
* creation via SET operations.</li>
* <p> By default remote entry creation via SET operation is disabled.
* </p>
*
* @see com.sun.jmx.snmp.agent.SnmpMibTable
*
**/
public void setCreationEnabled(boolean remoteCreationFlag) {
meta.setCreationEnabled(remoteCreationFlag);
}
/**
* Tells whether a new entry should be created when a SET operation
* is received for an entry that does not exist yet.
* This method calls <code>isCreationEnabled()</code> on the metadata
* object associated with this table.
*
* @return true if a new entry must be created, false otherwise.<br>
* [default: returns <CODE>false</CODE>]
*
* @see com.sun.jmx.snmp.agent.SnmpMibTable
**/
public boolean isCreationEnabled() {
return meta.isCreationEnabled();
}
/**
* Tells whether the metadata object to which this table is linked
* requires entries to be registered. In this case passing an
* ObjectName when registering entries will be mandatory.
*
* @return <code>true</code> if the associated metadata requires entries
* to be registered (mibgen generated generic metadata).
**/
public boolean isRegistrationRequired() {
return registrationRequired;
}
/**
* Builds an entry SnmpIndex from its row OID.
*
* This method is generated by mibgen and used internally.
*
* @param rowOid The SnmpOid object identifying a table entry.
*
* @return The SnmpIndex of the entry identified by <code>rowOid</code>.
*
* @exception SnmpStatusException if the index cannot be built from the
* given OID.
**/
public SnmpIndex buildSnmpIndex(SnmpOid rowOid)
throws SnmpStatusException {
return buildSnmpIndex(rowOid.longValue(false), 0);
}
/**
* Builds an SnmpOid from an SnmpIndex object.
*
* This method is generated by mibgen and used internally.
*
* @param index An SnmpIndex object identifying a table entry.
*
* @return The SnmpOid form of the given entry index.
*
* @exception SnmpStatusException if the given index is not valid.
**/
public abstract SnmpOid buildOidFromIndex(SnmpIndex index)
throws SnmpStatusException;
/**
* Builds the default ObjectName of an entry from the SnmpIndex
* identifying this entry. No access is made on the entry itself.
*
* This method is generated by mibgen and used internally.
* You can subclass this method if you want to change the default
* ObjectName policy. This is only meaningfull when entries
* are registered MBeans.
*
* @param index The SnmpIndex identifying the entry from which we
* want to build the default ObjectName.
*
* @return The default ObjectName for the entry identified by
* the given index.
*
* @exception SnmpStatusException if the given index is not valid.
**/
public abstract ObjectName buildNameFromIndex(SnmpIndex index)
throws SnmpStatusException;
//-----------------------------------------------------------------
//
// Implementation of the SnmpTableEntryFactory interface
//
//-----------------------------------------------------------------
/**
* This callback is called by the associated metadata object
* when a new table entry has been registered in the
* table metadata.
*
* This method will update the <code>entries</code> list.
*
* @param pos The position at which the new entry was inserted
* in the table.
* @param row The row OID of the new entry
* @param name The ObjectName of the new entry (as specified by the
* factory)
* @param entry The new entry (as returned by the factory)
* @param meta The table metadata object.
*
**/
public void addEntryCb(int pos, SnmpOid row, ObjectName name,
Object entry, SnmpMibTable meta)
throws SnmpStatusException {
try {
if (entries != null) entries.add(pos,entry);
} catch (Exception e) {
throw new SnmpStatusException(SnmpStatusException.noSuchName);
}
}
/**
* This callback is called by the associated metadata object
* when a new table entry has been removed from the
* table metadata.
*
* This method will update the <code>entries</code> list.
*
* @param pos The position from which the entry was deleted
* @param row The row OID of the deleted entry
* @param name The ObjectName of the deleted entry (may be null if
* ObjectName's were not required)
* @param entry The deleted entry (may be null if only ObjectName's
* were required)
* @param meta The table metadata object.
*
**/
public void removeEntryCb(int pos, SnmpOid row, ObjectName name,
Object entry, SnmpMibTable meta)
throws SnmpStatusException {
try {
if (entries != null) entries.remove(pos);
} catch (Exception e) {
}
}
/**
* Enables to add an SNMP entry listener to this
* <CODE>SnmpMibTable</CODE>.
*
* @param listener The listener object which will handle the
* notifications emitted by the registered MBean.
*
* @param filter The filter object. If filter is null, no filtering
* will be performed before handling notifications.
*
* @param handback The context to be sent to the listener when a
* notification is emitted.
*
* @exception IllegalArgumentException Listener parameter is null.
*/
public void
addNotificationListener(NotificationListener listener,
NotificationFilter filter, Object handback) {
meta.addNotificationListener(listener,filter,handback);
}
/**
* Enables to remove an SNMP entry listener from this
* <CODE>SnmpMibTable</CODE>.
*
* @param listener The listener object which will handle the
* notifications emitted by the registered MBean.
* This method will remove all the information related to this
* listener.
*
* @exception ListenerNotFoundException The listener is not registered
* in the MBean.
*/
public synchronized void
removeNotificationListener(NotificationListener listener)
throws ListenerNotFoundException {
meta.removeNotificationListener(listener);
}
/**
* Returns a <CODE>NotificationInfo</CODE> object containing the
* notification class and the notification type sent by the
* <CODE>SnmpMibTable</CODE>.
*/
public MBeanNotificationInfo[] getNotificationInfo() {
return meta.getNotificationInfo();
}
//-----------------------------------------------------------------
//
// Protected Abstract methods
//
//-----------------------------------------------------------------
/**
* Builds an SnmpIndex object from the index part of an OID.
*
* This method is generated by mibgen and used internally.
*
* @param oid The OID from which to build the index, represented
* as an array of long.
* @param start The position where to start from in the OID array.
*
* @return The SnmpOid form of the given entry index.
*
* @exception SnmpStatusException if the given index is not valid.
**/
protected abstract SnmpIndex buildSnmpIndex(long oid[], int start )
throws SnmpStatusException;
/**
* Returns the metadata object associated with this table.
*
* This method is generated by mibgen and used internally.
*
* @param mib The SnmpMib object holding the Metadata corresponding
* to this table.
*
* @return The metadata object associated with this table.
* Returns <code>null</code> if this implementation of the
* MIB doesn't support this table.
**/
protected abstract SnmpMibTable getRegisteredTableMeta(SnmpMib mib);
//-----------------------------------------------------------------
//
// Protected methods
//
//-----------------------------------------------------------------
/**
* Allocates an ArrayList for storing table entries.
*
* This method is called within the constructor at object creation.
* Any object implementing the {@link java.util.List} interface can
* be used.
*
* @return A new list in which to store entries. If <code>null</code>
* is returned then no entry will be stored in the list
* and getEntry() will always return null.
**/
protected List<Object> allocateTable() {
return new ArrayList<Object>();
}
/**
* Add an entry in this table.
*
* This method registers an entry in the table and perform
* synchronization with the associated table metadata object.
*
* This method assumes that the given entry will not be registered,
* or will be registered with its default ObjectName built from the
* associated SnmpIndex.
* <p>
* If the entry is going to be registered, then
* {@link com.sun.jmx.snmp.agent.SnmpTableSupport#addEntry(SnmpIndex, ObjectName, Object)} should be preferred.
* <br> This function is mainly provided for backward compatibility.
*
* @param index The SnmpIndex built from the given entry.
* @param entry The entry that should be added in the table.
*
* @exception SnmpStatusException if the entry cannot be registered with
* the given index.
**/
protected void addEntry(SnmpIndex index, Object entry)
throws SnmpStatusException {
SnmpOid oid = buildOidFromIndex(index);
ObjectName name = null;
if (isRegistrationRequired()) {
name = buildNameFromIndex(index);
}
meta.addEntry(oid,name,entry);
}
/**
* Add an entry in this table.
*
* This method registers an entry in the table and performs
* synchronization with the associated table metadata object.
*
* @param index The SnmpIndex built from the given entry.
* @param name The ObjectName with which this entry will be registered.
* @param entry The entry that should be added in the table.
*
* @exception SnmpStatusException if the entry cannot be registered with
* the given index.
**/
protected void addEntry(SnmpIndex index, ObjectName name, Object entry)
throws SnmpStatusException {
SnmpOid oid = buildOidFromIndex(index);
meta.addEntry(oid,name,entry);
}
/**
* Remove an entry from this table.
*
* This method unregisters an entry from the table and performs
* synchronization with the associated table metadata object.
*
* @param index The SnmpIndex identifying the entry.
* @param entry The entry that should be removed in the table. This
* parameter is optional and can be omitted if it doesn't
* need to be passed along to the
* <code>removeEntryCb()</code> callback defined in the
* {@link com.sun.jmx.snmp.agent.SnmpTableCallbackHandler}
* interface.
*
* @exception SnmpStatusException if the entry cannot be unregistered.
**/
protected void removeEntry(SnmpIndex index, Object entry)
throws SnmpStatusException {
SnmpOid oid = buildOidFromIndex(index);
meta.removeEntry(oid,entry);
}
// protected void removeEntry(ObjectName name, Object entry)
// throws SnmpStatusException {
// meta.removeEntry(name,entry);
// }
/**
* Returns the entries in the table.
*
* @return An Object[] array containing the entries registered in the
* table.
**/
protected Object[] getBasicEntries() {
if (entries == null) return null;
Object[] array= new Object[entries.size()];
entries.toArray(array);
return array;
}
/**
* Binds this table with its associated metadata, registering itself
* as an SnmpTableEntryFactory.
**/
protected void bindWithTableMeta() {
if (meta == null) return;
registrationRequired = meta.isRegistrationRequired();
meta.registerEntryFactory(this);
}
}

View File

@@ -0,0 +1,124 @@
/*
* Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp.agent;
import com.sun.jmx.snmp.SnmpPduPacket;
import com.sun.jmx.snmp.SnmpPdu;
import com.sun.jmx.snmp.SnmpStatusException;
/**
* This interface is provided to enable fine customization of the SNMP
* agent behaviour.
*
* <p>You will not need to implement this interface except if your agent
* needs extra customization requiring some contextual information.</p>
*
* <p>If an SnmpUserDataFactory is set on the SnmpAdaptorServer, then a new
* object containing user-data will be allocated through this factory
* for each incoming request. This object will be passed along to
* the SnmpMibAgent within SnmpMibRequest objects. By default, no
* SnmpUserDataFactory is set on the SnmpAdaptorServer, and the contextual
* object passed to SnmpMibAgent is null.</p>
*
* <p>You can use this feature to obtain on contextual information
* (such as community string etc...) or to implement a caching
* mechanism, or for whatever purpose might be required by your specific
* agent implementation.</p>
*
* <p>The sequence <code>allocateUserData() / releaseUserData()</code> can
* also be used to implement a caching mechanism:
* <ul>
* <li><code>allocateUserData()</code> could be used to allocate
* some cache space,</li>
* <li>and <code>releaseUserData()</code> could be used to flush it.</li>
* </ul></p>
*
* <p><b>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b></p>
* @see com.sun.jmx.snmp.agent.SnmpMibRequest
* @see com.sun.jmx.snmp.agent.SnmpMibAgent
* @see com.sun.jmx.snmp.daemon.SnmpAdaptorServer
*
**/
public interface SnmpUserDataFactory {
/**
* Called by the <CODE>SnmpAdaptorServer</CODE> adaptor.
* Allocate a contextual object containing some user data. This method
* is called once for each incoming SNMP request. The scope
* of this object will be the whole request. Since the request can be
* handled in several threads, the user should make sure that this
* object can be accessed in a thread-safe manner. The SNMP framework
* will never access this object directly - it will simply pass
* it to the <code>SnmpMibAgent</code> within
* <code>SnmpMibRequest</code> objects - from where it can be retrieved
* through the {@link com.sun.jmx.snmp.agent.SnmpMibRequest#getUserData() getUserData()} accessor.
* <code>null</code> is considered to be a valid return value.
*
* This method is called just after the SnmpPduPacket has been
* decoded.
*
* @param requestPdu The SnmpPduPacket received from the SNMP manager.
* <b>This parameter is owned by the SNMP framework and must be
* considered as transient.</b> If you wish to keep some of its
* content after this method returns (by storing it in the
* returned object for instance) you should clone that
* information.
*
* @return A newly allocated user-data contextual object, or
* <code>null</code>
* @exception SnmpStatusException If an SnmpStatusException is thrown,
* the request will be aborted.
*
* @since 1.5
**/
public Object allocateUserData(SnmpPdu requestPdu)
throws SnmpStatusException;
/**
* Called by the <CODE>SnmpAdaptorServer</CODE> adaptor.
* Release a previously allocated contextual object containing user-data.
* This method is called just before the responsePdu is sent back to the
* manager. It gives the user a chance to alter the responsePdu packet
* before it is encoded, and to free any resources that might have
* been allocated when creating the contextual object.
*
* @param userData The contextual object being released.
* @param responsePdu The SnmpPduPacket that will be sent back to the
* SNMP manager.
* <b>This parameter is owned by the SNMP framework and must be
* considered as transient.</b> If you wish to keep some of its
* content after this method returns you should clone that
* information.
*
* @exception SnmpStatusException If an SnmpStatusException is thrown,
* the responsePdu is dropped and nothing is returned to
* to the manager.
*
* @since 1.5
**/
public void releaseUserData(Object userData, SnmpPdu responsePdu)
throws SnmpStatusException;
}