View Javadoc

1   package net.sf.snmpadaptor4j.core.accessor;
2   
3   import javax.management.Attribute;
4   import javax.management.MBeanServer;
5   import javax.management.ObjectName;
6   import net.sf.snmpadaptor4j.api.AttributeAccessor;
7   import net.sf.snmpadaptor4j.object.SnmpDataType;
8   import net.sf.snmpadaptor4j.object.SnmpOid;
9   
10  /**
11   * Object representing an accessor to an attribute of JVM MBean.
12   * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a>
13   */
14  public final class JvmAttributeAccessor
15  		implements AttributeAccessor {
16  
17  	/**
18  	 * JMX agent where the MBean was registered.
19  	 */
20  	private final MBeanServer server;
21  
22  	/**
23  	 * MBean name.
24  	 */
25  	private final ObjectName mBeanName;
26  
27  	/**
28  	 * <b>O</b>bject <b>ID</b>entifier (OID) to locate the attribute in the MIB.
29  	 */
30  	private final SnmpOid oid;
31  
32  	/**
33  	 * Attribute name.
34  	 */
35  	private final String attributeName;
36  
37  	/**
38  	 * SNMP data type of attribute.
39  	 */
40  	private final SnmpDataType snmpDataType;
41  
42  	/**
43  	 * Data type of JMX attribute.
44  	 */
45  	private final Class<?> jmxDataType;
46  
47  	/**
48  	 * <code>TRUE</code> if the attribute can be write (for SNMP write community), <code>FALSE</code> otherwise.
49  	 */
50  	private final boolean writable;
51  
52  	/**
53  	 * Constructor.
54  	 * @param server JMX agent where the MBean was registered.
55  	 * @param mBeanName MBean name.
56  	 * @param oid <b>O</b>bject <b>ID</b>entifier (OID) to locate the attribute in the MIB.
57  	 * @param attributeName Attribute name.
58  	 * @param snmpDataType SNMP data type of attribute.
59  	 * @param jmxDataType Data type of JMX attribute.
60  	 * @param writable <code>TRUE</code> if the attribute can be write (for SNMP write community), <code>FALSE</code> otherwise.
61  	 */
62  	public JvmAttributeAccessor (final MBeanServer server, final ObjectName mBeanName, final SnmpOid oid, final String attributeName,
63  			final SnmpDataType snmpDataType, final Class<?> jmxDataType, final boolean writable) {
64  		super();
65  		this.server = server;
66  		this.mBeanName = mBeanName;
67  		this.oid = oid;
68  		this.attributeName = attributeName;
69  		this.snmpDataType = snmpDataType;
70  		this.jmxDataType = jmxDataType;
71  		this.writable = writable;
72  	}
73  
74  	/*
75  	 * {@inheritDoc}
76  	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getOid()
77  	 */
78  	public SnmpOid getOid () {
79  		return this.oid;
80  	}
81  
82  	/*
83  	 * {@inheritDoc}
84  	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getSnmpDataType()
85  	 */
86  	public SnmpDataType getSnmpDataType () {
87  		return this.snmpDataType;
88  	}
89  
90  	/*
91  	 * {@inheritDoc}
92  	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getJmxDataType()
93  	 */
94  	public Class<?> getJmxDataType () {
95  		return this.jmxDataType;
96  	}
97  
98  	/*
99  	 * {@inheritDoc}
100 	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getValue()
101 	 */
102 	public Object getValue () throws Exception {
103 		return this.server.getAttribute(this.mBeanName, this.attributeName);
104 	}
105 
106 	/*
107 	 * {@inheritDoc}
108 	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#setValue(java.lang.Object)
109 	 */
110 	public void setValue (Object value) throws Exception {
111 		this.server.setAttribute(this.mBeanName, new Attribute(this.attributeName, value));
112 	}
113 
114 	/*
115 	 * {@inheritDoc}
116 	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#isReadable()
117 	 */
118 	public boolean isReadable () {
119 		return true;
120 	}
121 
122 	/*
123 	 * {@inheritDoc}
124 	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#isWritable()
125 	 */
126 	public boolean isWritable () {
127 		return this.writable;
128 	}
129 
130 	/*
131 	 * {@inheritDoc}
132 	 * @see java.lang.Object#toString()
133 	 */
134 	@Override
135 	public String toString () {
136 		return "JvmAttributeAccessor[oid=" + this.oid + "; attributeName=" + this.attributeName + "; snmpDataType=" + this.snmpDataType + "; jmxDataType="
137 				+ this.jmxDataType + "; readable=" + isReadable() + "; writable=" + this.writable + "]";
138 	}
139 
140 }