View Javadoc

1   package net.sf.snmpadaptor4j.core.accessor;
2   
3   import javax.management.MBeanServer;
4   import javax.management.ObjectName;
5   import javax.management.openmbean.CompositeData;
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 for {@link CompositeData}.
12   * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a>
13   */
14  public final class CompositeDataAttributeAccessor
15  		implements AttributeAccessor {
16  
17  	/**
18  	 * <b>O</b>bject <b>ID</b>entifier (OID) to locate the attribute in the MIB.
19  	 */
20  	private final SnmpOid oid;
21  
22  	/**
23  	 * JMX agent where the MBean was registered.
24  	 */
25  	private final MBeanServer server;
26  
27  	/**
28  	 * MBean name.
29  	 */
30  	private final ObjectName mBeanName;
31  
32  	/**
33  	 * Attribute name of {@link CompositeData}.
34  	 */
35  	private final String attributeName;
36  
37  	/**
38  	 * Attribute key of value.
39  	 */
40  	private final String attributeKey;
41  
42  	/**
43  	 * SNMP data type of attribute.
44  	 */
45  	private final SnmpDataType snmpDataType;
46  
47  	/**
48  	 * Data type of JMX attribute.
49  	 */
50  	private final Class<?> jmxDataType;
51  
52  	/**
53  	 * Constructor.
54  	 * @param oid <b>O</b>bject <b>ID</b>entifier (OID) to locate the attribute in the MIB.
55  	 * @param server JMX agent where the MBean was registered.
56  	 * @param mBeanName MBean name.
57  	 * @param attributeName Attribute name of {@link CompositeData}.
58  	 * @param attributeKey Attribute key of value.
59  	 * @param snmpDataType SNMP data type of attribute.
60  	 * @param jmxDataType Data type of JMX attribute.
61  	 */
62  	public CompositeDataAttributeAccessor (final SnmpOid oid, final MBeanServer server, final ObjectName mBeanName, final String attributeName,
63  			final String attributeKey, final SnmpDataType snmpDataType, final Class<?> jmxDataType) {
64  		super();
65  		this.oid = oid;
66  		this.server = server;
67  		this.mBeanName = mBeanName;
68  		this.attributeName = attributeName;
69  		this.attributeKey = attributeKey;
70  		this.snmpDataType = snmpDataType;
71  		this.jmxDataType = jmxDataType;
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 		Object value = null;
104 		final CompositeData compositeData = (CompositeData) this.server.getAttribute(this.mBeanName, this.attributeName);
105 		if (compositeData != null) {
106 			value = compositeData.get(this.attributeKey);
107 		}
108 		return value;
109 	}
110 
111 	/*
112 	 * {@inheritDoc}
113 	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#setValue(java.lang.Object)
114 	 */
115 	public void setValue (final Object value) {
116 		// NOP
117 	}
118 
119 	/*
120 	 * {@inheritDoc}
121 	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#isReadable()
122 	 */
123 	public boolean isReadable () {
124 		return true;
125 	}
126 
127 	/*
128 	 * {@inheritDoc}
129 	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#isWritable()
130 	 */
131 	public boolean isWritable () {
132 		return false;
133 	}
134 
135 	/*
136 	 * {@inheritDoc}
137 	 * @see java.lang.Object#toString()
138 	 */
139 	@Override
140 	public String toString () {
141 		Object value;
142 		try {
143 			value = getValue();
144 		}
145 		catch (final Throwable e) {
146 			value = "ERROR: " + (e.getMessage() != null ? e.getMessage() : e.getClass().getName());
147 		}
148 		return this.oid + ": (" + getSnmpDataType() + ") " + value;
149 	}
150 
151 }