View Javadoc

1   package net.sf.snmpadaptor4j.mbean;
2   
3   import java.io.Serializable;
4   import javax.management.MBeanAttributeInfo;
5   import javax.management.MBeanConstructorInfo;
6   import javax.management.MBeanInfo;
7   import javax.management.MBeanNotificationInfo;
8   import javax.management.MBeanOperationInfo;
9   import javax.management.MBeanParameterInfo;
10  import javax.management.NotCompliantMBeanException;
11  import javax.management.StandardMBean;
12  
13  /**
14   * MBean containing all informations on the system.
15   * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a>
16   */
17  public final class SystemInfo
18  		extends StandardMBean
19  		implements SystemInfoMBean, Serializable {
20  
21  	/**
22  	 * Serial number.
23  	 */
24  	private static final long serialVersionUID = -6965962577731511748L;
25  
26  	/**
27  	 * Name given to the system (<code>system.sysName.0</code>).
28  	 */
29  	private String sysName;
30  
31  	/**
32  	 * Description on the system (<code>system.sysDescr.0</code>).
33  	 */
34  	private String sysDescr;
35  
36  	/**
37  	 * Location of the system (<code>system.sysLocation.0</code>).
38  	 */
39  	private String sysLocation;
40  
41  	/**
42  	 * Administrator contact of system (<code>system.sysContact.0</code>).
43  	 */
44  	private String sysContact;
45  
46  	/**
47  	 * Time in milliseconds at the last boot.
48  	 */
49  	private final long lastBootTime;
50  
51  	/**
52  	 * Constructor.
53  	 * @throws NotCompliantMBeanException if the <var>mbeanInterface</var> does not follow JMX design patterns for Management Interfaces, or if <var>this</var> does
54  	 *             not implement the specified interface.
55  	 */
56  	public SystemInfo () throws NotCompliantMBeanException {
57  		this(null, null, null, null);
58  	}
59  
60  	/**
61  	 * Constructor.
62  	 * @param sysName Name given to the system (<code>system.sysName.0</code>).
63  	 * @param sysDescr Description on the system (<code>system.sysDescr.0</code>).
64  	 * @throws NotCompliantMBeanException if the <var>mbeanInterface</var> does not follow JMX design patterns for Management Interfaces, or if <var>this</var> does
65  	 *             not implement the specified interface.
66  	 */
67  	public SystemInfo (final String sysName, final String sysDescr) throws NotCompliantMBeanException {
68  		this(sysName, sysDescr, null, null);
69  	}
70  
71  	/**
72  	 * Constructor.
73  	 * @param sysName Name given to the system (<code>system.sysName.0</code>).
74  	 * @param sysDescr Description on the system (<code>system.sysDescr.0</code>).
75  	 * @param sysLocation Location of the system (<code>system.sysLocation.0</code>).
76  	 * @param sysContact Administrator contact of system (<code>system.sysContact.0</code>).
77  	 * @throws NotCompliantMBeanException if the <var>mbeanInterface</var> does not follow JMX design patterns for Management Interfaces, or if <var>this</var> does
78  	 *             not implement the specified interface.
79  	 */
80  	public SystemInfo (final String sysName, final String sysDescr, final String sysLocation, final String sysContact) throws NotCompliantMBeanException {
81  		super(SystemInfoMBean.class);
82  		final String sysNameDescription = "Name given to the Java application (corresponds to SNMP attribute system.sysName.0)";
83  		final String sysDescrDescription = "Description on the Java application (corresponds to SNMP attribute system.sysDescr.0)";
84  		final String sysLocationDescription = "Location of the computer hosting the Java application (corresponds to SNMP attribute system.sysLocation.0)";
85  		final String sysContactDescription = "Administrator contact of the Java application (corresponds to SNMP attribute system.sysContact.0)";
86  		final String sysUpTimeDescription = "Elapsed time in milliseconds since the last boot of the Java application (corresponds to SNMP attribute system.sysUpTime.0)";
87  		final MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[] {
88  				new MBeanAttributeInfo("SysName", String.class.getName(), sysNameDescription, true, true, false),
89  				new MBeanAttributeInfo("SysDescr", String.class.getName(), sysDescrDescription, true, true, false),
90  				new MBeanAttributeInfo("SysLocation", String.class.getName(), sysLocationDescription, true, true, false),
91  				new MBeanAttributeInfo("SysContact", String.class.getName(), sysContactDescription, true, true, false),
92  				new MBeanAttributeInfo("SysUpTime", long.class.getName(), sysUpTimeDescription, true, false, false) };
93  		final MBeanConstructorInfo constructor1 = new MBeanConstructorInfo(getClass().getName(), "Constructor without parameter", new MBeanParameterInfo[] {});
94  		final MBeanConstructorInfo constructor2 = new MBeanConstructorInfo(getClass().getName(), "Constructor with only sysName and sysDescr parameters",
95  				new MBeanParameterInfo[] { new MBeanParameterInfo("sysName", String.class.getName(), sysNameDescription),
96  						new MBeanParameterInfo("sysDescr", String.class.getName(), sysDescrDescription) });
97  		final MBeanConstructorInfo constructor3 = new MBeanConstructorInfo(getClass().getName(), "Constructor with all parameters", new MBeanParameterInfo[] {
98  				new MBeanParameterInfo("sysName", String.class.getName(), sysNameDescription),
99  				new MBeanParameterInfo("sysDescr", String.class.getName(), sysDescrDescription),
100 				new MBeanParameterInfo("sysLocation", String.class.getName(), sysLocationDescription),
101 				new MBeanParameterInfo("sysContact", String.class.getName(), sysContactDescription) });
102 		final MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[] { constructor1, constructor2, constructor3 };
103 		cacheMBeanInfo(new MBeanInfo(getClass().getName(), "Informations on the Java application (used by SNMP)", attributes, constructors,
104 				new MBeanOperationInfo[] {}, new MBeanNotificationInfo[] {}));
105 		this.sysName = (sysName != null ? (sysName.trim().length() > 0 ? sysName : "javaApp") : "javaApp");
106 		this.sysDescr = (sysDescr != null ? (sysDescr.trim().length() > 0 ? sysDescr : "Java application") : "Java application");
107 		this.sysLocation = (sysLocation != null ? (sysLocation.trim().length() > 0 ? sysLocation : null) : null);
108 		this.sysContact = (sysContact != null ? (sysContact.trim().length() > 0 ? sysContact : null) : null);
109 		this.lastBootTime = System.currentTimeMillis();
110 	}
111 
112 	/*
113 	 * {@inheritDoc}
114 	 * @see net.sf.snmpadaptor4j.mbean.SystemInfoMBean#getSysName()
115 	 */
116 	public String getSysName () {
117 		return this.sysName;
118 	}
119 
120 	/*
121 	 * {@inheritDoc}
122 	 * @see net.sf.snmpadaptor4j.mbean.SystemInfoMBean#setSysName(java.lang.String)
123 	 */
124 	public void setSysName (final String sysName) {
125 		this.sysName = (sysName != null ? (sysName.trim().length() > 0 ? sysName : null) : null);
126 	}
127 
128 	/*
129 	 * {@inheritDoc}
130 	 * @see net.sf.snmpadaptor4j.mbean.SystemInfoMBean#getSysDescr()
131 	 */
132 	public String getSysDescr () {
133 		return this.sysDescr;
134 	}
135 
136 	/*
137 	 * {@inheritDoc}
138 	 * @see net.sf.snmpadaptor4j.mbean.SystemInfoMBean#setSysDescr(java.lang.String)
139 	 */
140 	public void setSysDescr (final String sysDescr) {
141 		this.sysDescr = (sysDescr != null ? (sysDescr.trim().length() > 0 ? sysDescr : null) : null);
142 	}
143 
144 	/*
145 	 * {@inheritDoc}
146 	 * @see net.sf.snmpadaptor4j.mbean.SystemInfoMBean#getSysLocation()
147 	 */
148 	public String getSysLocation () {
149 		return this.sysLocation;
150 	}
151 
152 	/*
153 	 * {@inheritDoc}
154 	 * @see net.sf.snmpadaptor4j.mbean.SystemInfoMBean#setSysLocation(java.lang.String)
155 	 */
156 	public void setSysLocation (final String sysLocation) {
157 		this.sysLocation = (sysLocation != null ? (sysLocation.trim().length() > 0 ? sysLocation : null) : null);
158 	}
159 
160 	/*
161 	 * {@inheritDoc}
162 	 * @see net.sf.snmpadaptor4j.mbean.SystemInfoMBean#getSysContact()
163 	 */
164 	public String getSysContact () {
165 		return this.sysContact;
166 	}
167 
168 	/*
169 	 * {@inheritDoc}
170 	 * @see net.sf.snmpadaptor4j.mbean.SystemInfoMBean#setSysContact(java.lang.String)
171 	 */
172 	public void setSysContact (final String sysContact) {
173 		this.sysContact = (sysContact != null ? (sysContact.trim().length() > 0 ? sysContact : null) : null);
174 	}
175 
176 	/*
177 	 * {@inheritDoc}
178 	 * @see net.sf.snmpadaptor4j.mbean.SystemInfoMBean#getSysUpTime()
179 	 */
180 	public long getSysUpTime () {
181 		return System.currentTimeMillis() - this.lastBootTime;
182 	}
183 
184 	/*
185 	 * {@inheritDoc}
186 	 * @see java.lang.Object#toString()
187 	 */
188 	@Override
189 	public String toString () {
190 		return this.sysName + " (" + this.sysDescr + ") - location: " + this.sysLocation + " - contact: " + this.sysContact;
191 	}
192 
193 }