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.core.mapping.MBeanAttributeMapping;
8   import net.sf.snmpadaptor4j.object.SnmpDataType;
9   import net.sf.snmpadaptor4j.object.SnmpOid;
10  
11  /**
12   * Object representing an accessor to an attribute of MBean.
13   * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a>
14   */
15  public final class MBeanAttributeAccessor
16  		implements AttributeAccessor {
17  
18  	/**
19  	 * JMX agent where the MBean was registered.
20  	 */
21  	private final MBeanServer server;
22  
23  	/**
24  	 * MBean name.
25  	 */
26  	private final ObjectName mBeanName;
27  
28  	/**
29  	 * Mapping for access to a MBean attribute.
30  	 */
31  	private final MBeanAttributeMapping mapping;
32  
33  	/**
34  	 * Constructor.
35  	 * @param server JMX agent where the MBean was registered.
36  	 * @param mBeanName MBean name.
37  	 * @param mapping Mapping for access to a MBean attribute.
38  	 */
39  	public MBeanAttributeAccessor (final MBeanServer server, final ObjectName mBeanName, final MBeanAttributeMapping mapping) {
40  		super();
41  		this.server = server;
42  		this.mBeanName = mBeanName;
43  		this.mapping = mapping;
44  	}
45  
46  	/**
47  	 * Returns the MBean name.
48  	 * @return MBean name.
49  	 */
50  	public ObjectName getMBeanName () {
51  		return this.mBeanName;
52  	}
53  
54  	/*
55  	 * {@inheritDoc}
56  	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getOid()
57  	 */
58  	public SnmpOid getOid () {
59  		return this.mapping.getOid();
60  	}
61  
62  	/*
63  	 * {@inheritDoc}
64  	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getSnmpDataType()
65  	 */
66  	public SnmpDataType getSnmpDataType () {
67  		return this.mapping.getSnmpDataType();
68  	}
69  
70  	/*
71  	 * {@inheritDoc}
72  	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getJmxDataType()
73  	 */
74  	public Class<?> getJmxDataType () {
75  		return this.mapping.getJmxDataType();
76  	}
77  
78  	/*
79  	 * {@inheritDoc}
80  	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#getValue()
81  	 */
82  	public Object getValue () throws Exception {
83  		return this.server.getAttribute(this.mBeanName, this.mapping.getAttributeName());
84  	}
85  
86  	/*
87  	 * {@inheritDoc}
88  	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#setValue(java.lang.Object)
89  	 */
90  	public void setValue (final Object value) throws Exception {
91  		this.server.setAttribute(this.mBeanName, new Attribute(this.mapping.getAttributeName(), value));
92  	}
93  
94  	/*
95  	 * {@inheritDoc}
96  	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#isReadable()
97  	 */
98  	public boolean isReadable () {
99  		return this.mapping.isReadable();
100 	}
101 
102 	/*
103 	 * {@inheritDoc}
104 	 * @see net.sf.snmpadaptor4j.api.AttributeAccessor#isWritable()
105 	 */
106 	public boolean isWritable () {
107 		return this.mapping.isWritable();
108 	}
109 
110 	/*
111 	 * {@inheritDoc}
112 	 * @see java.lang.Object#toString()
113 	 */
114 	@Override
115 	public String toString () {
116 		Object value;
117 		try {
118 			value = getValue();
119 		}
120 		catch (final Throwable e) {
121 			value = "ERROR: " + (e.getMessage() != null ? e.getMessage() : e.getClass().getName());
122 		}
123 		return this.mapping.getOid() + ": (" + this.mapping.getSnmpDataType() + ") " + value;
124 	}
125 
126 }