1 package org.modeshape.rhq.plugin.util;
2
3 import java.lang.reflect.Method;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Map;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.jboss.managed.api.ManagedComponent;
14 import org.jboss.managed.api.ManagedOperation;
15 import org.jboss.metatype.api.types.MetaType;
16 import org.jboss.metatype.api.values.CollectionValueSupport;
17 import org.jboss.metatype.api.values.MetaValue;
18 import org.jboss.metatype.api.values.MetaValueFactory;
19 import org.modeshape.jboss.managed.ManagedRepository;
20 import org.modeshape.jboss.managed.ManagedSequencerConfig;
21 import org.modeshape.rhq.plugin.objects.ExecutedResult;
22 import org.modeshape.rhq.plugin.util.PluginConstants.ComponentType.Connector;
23 import org.rhq.plugins.jbossas5.connection.ProfileServiceConnection;
24
25 import com.sun.istack.Nullable;
26
27 public class ModeShapeManagementView implements PluginConstants {
28
29 private static final Log LOG = LogFactory
30 .getLog(PluginConstants.DEFAULT_LOGGER_CATEGORY);
31
32 private static final MetaValueFactory metaValueFactory = MetaValueFactory
33 .getInstance();
34
35 public ModeShapeManagementView() {
36
37 }
38
39
40
41
42 public Object getMetric(ProfileServiceConnection connection,
43 String componentType, String identifier, String metric,
44 Map<String, Object> valueMap) throws Exception {
45 Object resultObject = new Object();
46
47 if (componentType.equals(ComponentType.SequencingService.NAME)) {
48 resultObject = getSequencerServiceMetric(connection, componentType,
49 metric, valueMap);
50 } else if (componentType.equals(ComponentType.Connector.NAME)) {
51 resultObject = getConnectorMetric(connection, componentType,
52 metric, valueMap);
53 } else if (componentType.equals(ComponentType.Repository.NAME)) {
54 resultObject = getRepositoryMetric(connection, componentType,
55 metric, valueMap);
56 }
57
58 return resultObject;
59 }
60
61
62
63
64 private Object getSequencerServiceMetric(
65 ProfileServiceConnection connection, String componentType,
66 String metric, Map<String, Object> valueMap) throws Exception {
67
68 Object resultObject = new Object();
69 MetaValue value = null;
70
71 if (metric
72 .equals(ComponentType.SequencingService.Metrics.NUM_NODES_SEQUENCED)
73 || metric
74 .equals(ComponentType.SequencingService.Metrics.NUM_NODES_SKIPPED)) {
75 value = executeSequencingServiceOperation(connection, metric,
76 valueMap);
77 resultObject = ProfileServiceUtil.stringValue(value);
78 }
79 return resultObject;
80 }
81
82 private Object getRepositoryMetric(ProfileServiceConnection connection,
83 String componentType, String metric, Map<String, Object> valueMap)
84 throws Exception {
85
86 Object resultObject = new Object();
87 MetaValue value = null;
88
89 if (metric.equals(ComponentType.Repository.Metrics.ACTIVESESSIONS)) {
90 value = executeManagedOperation(
91 ProfileServiceUtil.getManagedEngine(connection),
92 metric,
93 new MetaValue[] { MetaValueFactory
94 .getInstance()
95 .create(
96 valueMap
97 .get(ComponentType.Repository.Operations.Parameters.REPOSITORY_NAME)) });
98 resultObject = ProfileServiceUtil.stringValue(value);
99 }
100 return resultObject;
101 }
102
103 private Object getConnectorMetric(ProfileServiceConnection connection,
104 String componentType, String metric, Map<String, Object> valueMap)
105 throws Exception {
106
107 Object resultObject = new Object();
108 MetaValue value = null;
109
110 if (metric.equals(ComponentType.Connector.Metrics.INUSECONNECTIONS)) {
111 value = executeManagedOperation(
112 ProfileServiceUtil.getManagedEngine(connection),
113 metric,
114 new MetaValue[] { MetaValueFactory
115 .getInstance()
116 .create(
117 valueMap
118 .get(ComponentType.Connector.Operations.Parameters.CONNECTOR_NAME)) });
119 resultObject = ProfileServiceUtil.stringValue(value);
120 } else if (metric.equals(ComponentType.Repository.Metrics.ACTIVESESSIONS)) {
121 value = executeManagedOperation(
122 ProfileServiceUtil.getManagedEngine(connection),
123 metric,
124 new MetaValue[] { MetaValueFactory
125 .getInstance()
126 .create(
127 valueMap
128 .get(ComponentType.Connector.Operations.Parameters.CONNECTOR_NAME)) });
129 resultObject = ProfileServiceUtil.stringValue(value);
130 }
131 return resultObject;
132 }
133
134
135
136
137
138 public void executeOperation(ProfileServiceConnection connection,
139 ExecutedResult operationResult, final Map<String, Object> valueMap) {
140
141 if (operationResult.getComponentType().equals(
142 ComponentType.Engine.MODESHAPE_ENGINE)) {
143 executeEngineOperation(connection, operationResult, operationResult
144 .getOperationName(), valueMap);
145 } else if (operationResult.getComponentType().equals(
146 ComponentType.Repository.NAME)) {
147
148 } else if (operationResult.getComponentType().equals(
149 ComponentType.Connector.NAME)) {
150 executeConnectorOperation(connection, operationResult,
151 operationResult.getOperationName(), valueMap);
152 }
153
154 }
155
156 private void executeEngineOperation(ProfileServiceConnection connection,
157 ExecutedResult operationResult, final String operationName,
158 final Map<String, Object> valueMap) {
159
160 try {
161 executeManagedOperation(ProfileServiceUtil
162 .getManagedEngine(connection), operationName,
163 new MetaValue[] { null });
164 } catch (Exception e) {
165 final String msg = "Exception executing operation: " + operationName;
166 LOG.error(msg, e);
167 }
168 }
169
170 private void executeConnectorOperation(ProfileServiceConnection connection,
171 ExecutedResult operationResult, final String operationName,
172 final Map<String, Object> valueMap) {
173
174 if (operationName.equals(Connector.Operations.PING)) {
175 try {
176 String connectorName = (String) valueMap
177 .get(Connector.Operations.Parameters.CONNECTOR_NAME);
178 MetaValue[] args = new MetaValue[] { metaValueFactory
179 .create(connectorName) };
180 MetaValue value = executeManagedOperation(ProfileServiceUtil
181 .getManagedEngine(connection), operationName,
182 operationResult, args);
183 operationResult.setContent(value);
184 } catch (Exception e) {
185 final String msg = "Exception executing operation: " + Connector.Operations.PING;
186 LOG.error(msg, e);
187 }
188 }
189 }
190
191 private MetaValue executeSequencingServiceOperation(
192 ProfileServiceConnection connection, final String operationName,
193 final Map<String, Object> valueMap) {
194 MetaValue value = null;
195 try {
196 MetaValue[] args = new MetaValue[] {};
197 value = executeManagedOperation(ProfileServiceUtil
198 .getManagedSequencingService(connection), operationName,
199 args);
200 } catch (Exception e) {
201 final String msg = "Exception executing operation: " + operationName;
202 LOG.error(msg, e);
203 }
204
205 return value;
206
207 }
208
209
210
211
212
213
214
215
216 public static MetaValue executeManagedOperation(ManagedComponent mc,
217 String operation, @Nullable MetaValue... args) throws Exception {
218
219 for (ManagedOperation mo : mc.getOperations()) {
220 String opName = mo.getName();
221 if (opName.equals(operation)) {
222 try {
223 if (args == null || (args.length == 1 && args[0] == null)) {
224 return mo.invoke();
225 }
226 return mo.invoke(args);
227 } catch (Exception e) {
228 final String msg = "Exception invoking " + operation;
229 LOG.error(msg, e);
230 throw e;
231 }
232 }
233 }
234 throw new Exception("No operation found with given name =" + operation);
235
236 }
237
238
239
240
241
242
243
244
245
246 public static MetaValue executeManagedOperation(ManagedComponent mc,
247 String operation, ExecutedResult operationResult,
248 @Nullable MetaValue... args) throws Exception {
249
250 for (ManagedOperation mo : mc.getOperations()) {
251 String opName = mo.getName();
252 if (opName.equals(operation)) {
253 operationResult.setManagedOperation(mo);
254 try {
255 if (args == null || (args.length == 1 && args[0] == null)) {
256 return mo.invoke();
257 }
258 return mo.invoke(args);
259 } catch (Exception e) {
260 final String msg = "Exception invoking " + operation;
261 LOG.error(msg, e);
262 throw e;
263 }
264 }
265 }
266 throw new Exception("No operation found with given name =" + operation);
267
268 }
269
270 public static Collection<ManagedRepository> getRepositoryCollectionValue(
271 MetaValue pValue) {
272 Collection<ManagedRepository> list = new ArrayList<ManagedRepository>();
273 MetaType metaType = pValue.getMetaType();
274 if (metaType.isCollection()) {
275 for (MetaValue value : ((CollectionValueSupport) pValue)
276 .getElements()) {
277 if (value.getMetaType().isComposite()) {
278 ManagedRepository repository = (ManagedRepository) MetaValueFactory
279 .getInstance().unwrap(value);
280 list.add(repository);
281 } else {
282 throw new IllegalStateException(pValue
283 + " is not a Composite type");
284 }
285 }
286 }
287 return list;
288 }
289
290 public static String getConnectorPingString(MetaValue pValue)
291 throws Exception {
292 MetaType metaType = pValue.getMetaType();
293 StringBuffer sb = new StringBuffer();
294 if (metaType.isCollection()) {
295 for (MetaValue value : ((CollectionValueSupport) pValue)
296 .getElements()) {
297 String resultValue = ProfileServiceUtil.stringValue(value);
298 sb.append(resultValue + " ");
299 }
300 }
301 return sb.toString();
302 }
303
304 public static Collection<ManagedSequencerConfig> getSequencerCollectionValue(
305 MetaValue pValue) {
306 Collection<ManagedSequencerConfig> list = new ArrayList<ManagedSequencerConfig>();
307 MetaType metaType = pValue.getMetaType();
308 if (metaType.isCollection()) {
309 for (MetaValue value : ((CollectionValueSupport) pValue)
310 .getElements()) {
311 if (value.getMetaType().isComposite()) {
312 ManagedSequencerConfig sequencer = (ManagedSequencerConfig) MetaValueFactory
313 .getInstance().unwrap(value);
314 list.add(sequencer);
315 } else {
316 throw new IllegalStateException(pValue
317 + " is not a Composite type");
318 }
319 }
320 }
321 return list;
322 }
323
324 private Collection createReportResultList(List fieldNameList,
325 Iterator objectIter) {
326 Collection reportResultList = new ArrayList();
327
328 while (objectIter.hasNext()) {
329 Object object = objectIter.next();
330
331 Class cls = null;
332 try {
333 cls = object.getClass();
334 Iterator methodIter = fieldNameList.iterator();
335 Map reportValueMap = new HashMap<String, String>();
336 while (methodIter.hasNext()) {
337 String fieldName = (String) methodIter.next();
338 String methodName = fieldName;
339 Method meth = cls.getMethod(methodName, (Class[]) null);
340 Object retObj = meth.invoke(object, (Object[]) null);
341 reportValueMap.put(fieldName, retObj);
342 }
343 reportResultList.add(reportValueMap);
344 } catch (Throwable e) {
345 System.err.println(e);
346 }
347 }
348 return reportResultList;
349 }
350
351 }