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 | |
|
15 | |
|
16 | |
|
17 | |
public final class SystemInfo |
18 | |
extends StandardMBean |
19 | |
implements SystemInfoMBean, Serializable { |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
private static final long serialVersionUID = -6965962577731511748L; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
private String sysName; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
private String sysDescr; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
private String sysLocation; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
private String sysContact; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private final long lastBootTime; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public SystemInfo () throws NotCompliantMBeanException { |
57 | 61 | this(null, null, null, null); |
58 | 61 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public SystemInfo (final String sysName, final String sysDescr) throws NotCompliantMBeanException { |
68 | 1 | this(sysName, sysDescr, null, null); |
69 | 1 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public SystemInfo (final String sysName, final String sysDescr, final String sysLocation, final String sysContact) throws NotCompliantMBeanException { |
81 | 70 | 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 | 70 | 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 | 70 | final MBeanConstructorInfo constructor1 = new MBeanConstructorInfo(getClass().getName(), "Constructor without parameter", new MBeanParameterInfo[] {}); |
94 | 70 | 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 | 70 | 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 | 70 | final MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[] { constructor1, constructor2, constructor3 }; |
103 | 70 | cacheMBeanInfo(new MBeanInfo(getClass().getName(), "Informations on the Java application (used by SNMP)", attributes, constructors, |
104 | |
new MBeanOperationInfo[] {}, new MBeanNotificationInfo[] {})); |
105 | 70 | this.sysName = (sysName != null ? (sysName.trim().length() > 0 ? sysName : "javaApp") : "javaApp"); |
106 | 70 | this.sysDescr = (sysDescr != null ? (sysDescr.trim().length() > 0 ? sysDescr : "Java application") : "Java application"); |
107 | 70 | this.sysLocation = (sysLocation != null ? (sysLocation.trim().length() > 0 ? sysLocation : null) : null); |
108 | 70 | this.sysContact = (sysContact != null ? (sysContact.trim().length() > 0 ? sysContact : null) : null); |
109 | 70 | this.lastBootTime = System.currentTimeMillis(); |
110 | 70 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public String getSysName () { |
117 | 36 | return this.sysName; |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public void setSysName (final String sysName) { |
125 | 18 | this.sysName = (sysName != null ? (sysName.trim().length() > 0 ? sysName : null) : null); |
126 | 18 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public String getSysDescr () { |
133 | 36 | return this.sysDescr; |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public void setSysDescr (final String sysDescr) { |
141 | 18 | this.sysDescr = (sysDescr != null ? (sysDescr.trim().length() > 0 ? sysDescr : null) : null); |
142 | 18 | } |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public String getSysLocation () { |
149 | 36 | return this.sysLocation; |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public void setSysLocation (final String sysLocation) { |
157 | 18 | this.sysLocation = (sysLocation != null ? (sysLocation.trim().length() > 0 ? sysLocation : null) : null); |
158 | 18 | } |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
public String getSysContact () { |
165 | 36 | return this.sysContact; |
166 | |
} |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public void setSysContact (final String sysContact) { |
173 | 18 | this.sysContact = (sysContact != null ? (sysContact.trim().length() > 0 ? sysContact : null) : null); |
174 | 18 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public long getSysUpTime () { |
181 | 5 | return System.currentTimeMillis() - this.lastBootTime; |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
@Override |
189 | |
public String toString () { |
190 | 1 | return this.sysName + " (" + this.sysDescr + ") - location: " + this.sysLocation + " - contact: " + this.sysContact; |
191 | |
} |
192 | |
|
193 | |
} |