OperationsServiceImpl.java
3.64 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.settings.SlingSettingsService
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.operations.impl;
import com.adobe.granite.operations.OperationsService;
import java.lang.management.ManagementFactory;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.QueryExp;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.settings.SlingSettingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service(value={OperationsService.class})
public class OperationsServiceImpl
implements OperationsService {
private final Logger logger;
@Reference
SlingSettingsService settings;
private MBeanServer mbeanServer;
public OperationsServiceImpl() {
this.logger = LoggerFactory.getLogger((String)this.getClass().getName());
}
@Activate
protected void activate() {
this.mbeanServer = ManagementFactory.getPlatformMBeanServer();
}
@Deactivate
protected void deactivate() {
this.mbeanServer = null;
}
private boolean checkRunMode(ValueMap properties) {
boolean enabled = true;
String[] runModes = (String[])properties.get("granite.operations.condition.runmode", String[].class);
if (runModes != null) {
Set activeModes = this.settings.getRunModes();
for (String r : runModes) {
String mode;
enabled = r.startsWith("!") ? !activeModes.contains(mode = r.substring(1)) : activeModes.contains(r);
if (!enabled) break;
}
}
return enabled;
}
private boolean checkMBean(String instr) {
String mbeanName = instr;
try {
ObjectName oName = new ObjectName(mbeanName);
return !this.mbeanServer.queryNames(oName, null).isEmpty();
}
catch (MalformedObjectNameException e) {
this.logger.warn("Unable to check condition, malformed object name " + mbeanName, (Throwable)e);
return false;
}
}
private boolean checkMBean(ValueMap properties) {
boolean enabled = true;
String[] instr = (String[])properties.get("granite.operations.condition.mbean", String[].class);
if (instr != null) {
for (String r : instr) {
if (this.checkMBean(r)) continue;
enabled = false;
break;
}
}
return enabled;
}
@Override
public boolean isEnabled(ValueMap properties) {
boolean enabled = true;
enabled = this.checkRunMode(properties) && this.checkMBean(properties);
return enabled;
}
protected void bindSettings(SlingSettingsService slingSettingsService) {
this.settings = slingSettingsService;
}
protected void unbindSettings(SlingSettingsService slingSettingsService) {
if (this.settings == slingSettingsService) {
this.settings = null;
}
}
}