ScriptMBean.java
6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.jmx.annotation.OpenTypeUtils
* org.apache.felix.scr.annotations.Property
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.monitoring.impl;
import com.adobe.granite.jmx.annotation.OpenTypeUtils;
import com.adobe.granite.monitoring.impl.ScriptConfig;
import com.adobe.granite.monitoring.impl.ShellScriptExecutor;
import java.util.HashMap;
import java.util.LinkedList;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.ReflectionException;
import javax.management.openmbean.OpenMBeanAttributeInfo;
import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
import javax.management.openmbean.OpenMBeanConstructorInfo;
import javax.management.openmbean.OpenMBeanInfoSupport;
import javax.management.openmbean.OpenMBeanOperationInfo;
import javax.management.openmbean.OpenMBeanOperationInfoSupport;
import javax.management.openmbean.OpenMBeanParameterInfo;
import javax.management.openmbean.OpenType;
import org.apache.felix.scr.annotations.Property;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ScriptMBean
implements DynamicMBean {
private static final Logger log = LoggerFactory.getLogger(ScriptMBean.class);
private static final String[] attribNames = new String[]{"script.filename", "script.statuscode", "script.stdout", "script.stderr"};
private static final String[] descriptions = new String[]{"Filename of the script", "Statuscode of the last execution of the script", "Unformatted output of the last execution of the script", "Unformatted error output the last execution of the script"};
@Property(value={"com.adobe.granite.monitoring:type=OS"}, propertyPrivate=1)
private final HashMap<String, Object> attribs = new HashMap();
private JSONObject lastResult;
private final ShellScriptExecutor executor;
private final ScriptConfig cfg;
protected ScriptMBean(ShellScriptExecutor executor, ScriptConfig cfg) {
this.executor = executor;
this.cfg = cfg;
log.info("Script MBean instantiated with script " + cfg.getScriptFilename());
this.attribs.put(attribNames[0], cfg.getScriptFilename());
this.attribs.put(attribNames[1], -1);
this.attribs.put(attribNames[2], "");
this.attribs.put(attribNames[3], "");
}
public ScriptConfig getCfg() {
return this.cfg;
}
public int invoke() {
ShellScriptExecutor.Result result = this.executor.execute(this.cfg);
if (result == null) {
this.attribs.put(attribNames[1], -1);
this.attribs.put(attribNames[2], "");
this.attribs.put(attribNames[3], "");
return -1;
}
this.attribs.put(attribNames[1], result.getExitCode());
this.attribs.put(attribNames[2], result.getStdout());
this.attribs.put(attribNames[3], result.getStderr());
try {
this.lastResult = new JSONObject(result.getStdout());
}
catch (Exception ignore) {
this.lastResult = null;
}
return result.getExitCode();
}
private Object internalGetAttribute(String name) {
Object value = this.attribs.get(name);
if (value == null) {
value = this.lastResult == null ? null : this.lastResult.opt(name);
}
return value;
}
public synchronized Object getAttribute(String name) throws AttributeNotFoundException {
Object value = this.internalGetAttribute(name);
if (value == null) {
throw new AttributeNotFoundException("No such attribute: " + name);
}
return value;
}
public synchronized void setAttribute(Attribute attribute) throws InvalidAttributeValueException, MBeanException, AttributeNotFoundException {
throw new InvalidAttributeValueException("attribute value not settable");
}
public synchronized AttributeList getAttributes(String[] names) {
AttributeList list = new AttributeList();
for (String name : names) {
Object value = this.internalGetAttribute(name);
if (value == null) continue;
list.add(new Attribute(name, value));
}
return list;
}
public synchronized AttributeList setAttributes(AttributeList list) {
return null;
}
public Object invoke(String name, Object[] args, String[] sig) throws MBeanException, ReflectionException {
return this.invoke();
}
public synchronized MBeanInfo getMBeanInfo() {
LinkedList<OpenMBeanAttributeInfoSupport> attrs = new LinkedList<OpenMBeanAttributeInfoSupport>();
for (int i = 0; i < attribNames.length; ++i) {
attrs.add(new OpenMBeanAttributeInfoSupport(attribNames[i], descriptions[i], OpenTypeUtils.getSimpleType(i == 1 ? Integer.class : String.class), true, false, false));
}
if (this.lastResult != null) {
JSONArray jNames = this.lastResult.names();
for (int i2 = 0; i2 < jNames.length(); ++i2) {
String name = jNames.optString(i2);
if (name == null) continue;
attrs.add(new OpenMBeanAttributeInfoSupport(name, "Probe value for " + name, OpenTypeUtils.getSimpleType(String.class), true, false, false));
}
}
OpenMBeanOperationInfo[] ops = new OpenMBeanOperationInfoSupport[]{new OpenMBeanOperationInfoSupport("execute", "execute the script " + this.cfg.getScriptFilename(), null, OpenTypeUtils.getSimpleType(Boolean.class), 2)};
return new OpenMBeanInfoSupport(this.getClass().getName(), "Script Result for " + this.cfg.getScriptFilename(), attrs.toArray(new OpenMBeanAttributeInfoSupport[attrs.size()]), null, ops, null);
}
}