1 package cz.cuni.amis.pogamut.base.utils.jmx;
2
3 import javax.management.ObjectName;
4
5 import cz.cuni.amis.pogamut.base.agent.IAgentId;
6 import cz.cuni.amis.pogamut.base.agent.impl.AbstractAgent;
7 import cz.cuni.amis.pogamut.base.utils.Pogamut;
8 import cz.cuni.amis.pogamut.base.utils.PogamutProperty;
9 import cz.cuni.amis.pogamut.base.utils.exception.PogamutJMXNameException;
10 import cz.cuni.amis.utils.NullCheck;
11 import cz.cuni.amis.utils.token.IToken;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public class PogamutJMX {
28
29
30
31
32 public static final String AGENT_SUBTYPE = "agent";
33
34
35
36
37 public static final String ACT_NAME = "act";
38
39
40
41
42 public static final String LOGCATEGORIES_NAME = "logcat";
43
44
45
46
47 public static final String LOGCATEGORY_SUBTYPE = "logcategory";
48
49
50
51
52 public static final String FLAGS_SUBTYPE = "flags";
53
54
55
56
57 public static final String INTROSPECTION_NAME = "introspection";
58
59
60
61
62 public static String AGENT_LOGGER_SUBTYPE = "agentlogger";
63
64
65
66
67
68
69
70
71
72
73 public static ObjectName getAgentObjectName(IToken agentId) {
74 return getAgentObjectName(agentId.getToken());
75 }
76
77
78
79
80
81
82
83
84 public static ObjectName getAgentObjectName(String agentId) {
85 return PogamutJMX.getObjectName(getPogamutJMXDomain(), PogamutJMX.AGENT_SUBTYPE + "-" + Pogamut.getPlatform().getProperty(PogamutProperty.POGAMUT_JMX_SUBDOMAIN.getKey()), agentId);
86 }
87
88
89
90
91
92
93
94
95 public static String getPogamutJMXDomain() {
96 return Pogamut.getPlatform().getProperty(PogamutProperty.POGAMUT_JMX_DOMAIN.getKey());
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 public static ObjectName getObjectName(ObjectName parent, String childName, String childTypeExtra) throws PogamutJMXNameException {
114 NullCheck.check(parent, "parent");
115
116 String parentDomain = parent.getDomain();
117 String parentType = parent.getKeyProperty("type");
118 String parentName = parent.getKeyProperty("name");
119
120 if (childTypeExtra != null) {
121 String[] childTypeParts = childTypeExtra.split(".");
122 if (childTypeParts.length != 0) {
123 for (int i = 0; i < childTypeParts.length; ++i) {
124 childTypeParts[i] = getSafeObjectNamePart(childTypeParts[i]);
125 }
126 childTypeExtra = childTypeParts[0];
127 for (int i = 1; i < childTypeParts.length; ++i) {
128 childTypeExtra += "." + childTypeParts[i];
129 }
130 } else {
131 childTypeExtra = getSafeObjectNamePart(childTypeExtra);
132 }
133 }
134
135 String childType = (childTypeExtra == null ?
136 parentType + "." + parentName
137 : parentType + "." + parentName + "." + childTypeExtra
138 );
139
140 return getObjectName(parentDomain, childType, childName);
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155 public static ObjectName getObjectName(ObjectName parent, String childName) throws PogamutJMXNameException {
156 NullCheck.check(parent, "parent");
157 NullCheck.check(childName, "childName");
158
159 String parentDomain = parent.getKeyProperty("domain");
160 String parentType = parent.getKeyProperty("type");
161 String parentName = parent.getKeyProperty("name");
162
163 String childType = parentType + "." + parentName;
164
165 return getObjectName(parentDomain, childType, childName);
166 }
167
168
169
170
171
172
173 public static String getSafeObjectNamePart(String str) {
174 return
175 str.replace('[', '_').replace(']', '_').replace('(', '_').replace(')', '_')
176 .replace('.', '_').replace(',', '_').replace('/', '_').replace('*', '_')
177 .replace('?', '_').replace(':', '_');
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192 public static ObjectName getObjectName(String jmxDomain, String type, String name) throws PogamutJMXNameException {
193
194 if (name != null) {
195 name = getSafeObjectNamePart(name);
196 }
197
198 String jmxName = (name == null ?
199 jmxDomain+":type="+type
200 : jmxDomain+":type="+type+",name="+name
201 );
202 try {
203 return ObjectName.getInstance(jmxName);
204 } catch (Exception e) {
205 throw new PogamutJMXNameException(jmxName, e);
206 }
207 }
208
209 }