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 }
54
55 return resultObject;
56 }
57
58
59
60
61 private Object getSequencerServiceMetric(
62 ProfileServiceConnection connection, String componentType,
63 String metric, Map<String, Object> valueMap) throws Exception {
64
65 Object resultObject = new Object();
66 MetaValue value = null;
67
68 if (metric
69 .equals(ComponentType.SequencingService.Metrics.NUM_NODES_SEQUENCED)
70 || metric
71 .equals(ComponentType.SequencingService.Metrics.NUM_NODES_SKIPPED)) {
72 value = executeSequencingServiceOperation(connection, metric,
73 valueMap);
74 resultObject = ProfileServiceUtil.stringValue(value);
75 }
76 return resultObject;
77 }
78
79 private Object getConnectorMetric(ProfileServiceConnection connection,
80 String componentType, String metric, Map<String, Object> valueMap)
81 throws Exception {
82
83 Object resultObject = new Object();
84 MetaValue value = null;
85
86 if (metric.equals(ComponentType.Connector.Metrics.INUSECONNECTIONS)) {
87 value = executeManagedOperation(
88 ProfileServiceUtil.getManagedEngine(connection),
89 metric,
90 new MetaValue[] { MetaValueFactory
91 .getInstance()
92 .create(
93 valueMap
94 .get(ComponentType.Connector.Operations.Parameters.CONNECTOR_NAME)) });
95 resultObject = ProfileServiceUtil.stringValue(value);
96 }
97 return resultObject;
98 }
99
100
101
102
103
104 public void executeOperation(ProfileServiceConnection connection,
105 ExecutedResult operationResult, final Map<String, Object> valueMap) {
106
107 if (operationResult.getComponentType().equals(
108 ComponentType.Engine.MODESHAPE_ENGINE)) {
109 executeEngineOperation(connection, operationResult, operationResult
110 .getOperationName(), valueMap);
111 } else if (operationResult.getComponentType().equals(
112 ComponentType.Repository.NAME)) {
113
114 } else if (operationResult.getComponentType().equals(
115 ComponentType.Connector.NAME)) {
116 executeConnectorOperation(connection, operationResult,
117 operationResult.getOperationName(), valueMap);
118 }
119
120 }
121
122 private void executeEngineOperation(ProfileServiceConnection connection,
123 ExecutedResult operationResult, final String operationName,
124 final Map<String, Object> valueMap) {
125
126 try {
127 executeManagedOperation(ProfileServiceUtil
128 .getManagedEngine(connection), operationName,
129 new MetaValue[] { null });
130 } catch (Exception e) {
131 final String msg = "Exception executing operation: " + operationName;
132 LOG.error(msg, e);
133 }
134 }
135
136 private void executeConnectorOperation(ProfileServiceConnection connection,
137 ExecutedResult operationResult, final String operationName,
138 final Map<String, Object> valueMap) {
139
140 if (operationName.equals(Connector.Operations.PING)) {
141 try {
142 String connectorName = (String) valueMap
143 .get(Connector.Operations.Parameters.CONNECTOR_NAME);
144 MetaValue[] args = new MetaValue[] { metaValueFactory
145 .create(connectorName) };
146 MetaValue value = executeManagedOperation(ProfileServiceUtil
147 .getManagedEngine(connection), operationName,
148 operationResult, args);
149 operationResult.setContent(value);
150 } catch (Exception e) {
151 final String msg = "Exception executing operation: " + Connector.Operations.PING;
152 LOG.error(msg, e);
153 }
154 }
155 }
156
157 private MetaValue executeSequencingServiceOperation(
158 ProfileServiceConnection connection, final String operationName,
159 final Map<String, Object> valueMap) {
160 MetaValue value = null;
161 try {
162 MetaValue[] args = new MetaValue[] {};
163 value = executeManagedOperation(ProfileServiceUtil
164 .getManagedSequencingService(connection), operationName,
165 args);
166 } catch (Exception e) {
167 final String msg = "Exception executing operation: " + operationName;
168 LOG.error(msg, e);
169 }
170
171 return value;
172
173 }
174
175
176
177
178
179
180
181
182 public static MetaValue executeManagedOperation(ManagedComponent mc,
183 String operation, @Nullable MetaValue... args) throws Exception {
184
185 for (ManagedOperation mo : mc.getOperations()) {
186 String opName = mo.getName();
187 if (opName.equals(operation)) {
188 try {
189 if (args == null || (args.length == 1 && args[0] == null)) {
190 return mo.invoke();
191 }
192 return mo.invoke(args);
193 } catch (Exception e) {
194 final String msg = "Exception invoking " + operation;
195 LOG.error(msg, e);
196 throw e;
197 }
198 }
199 }
200 throw new Exception("No operation found with given name =" + operation);
201
202 }
203
204
205
206
207
208
209
210
211
212 public static MetaValue executeManagedOperation(ManagedComponent mc,
213 String operation, ExecutedResult operationResult,
214 @Nullable MetaValue... args) throws Exception {
215
216 for (ManagedOperation mo : mc.getOperations()) {
217 String opName = mo.getName();
218 if (opName.equals(operation)) {
219 operationResult.setManagedOperation(mo);
220 try {
221 if (args == null || (args.length == 1 && args[0] == null)) {
222 return mo.invoke();
223 }
224 return mo.invoke(args);
225 } catch (Exception e) {
226 final String msg = "Exception invoking " + operation;
227 LOG.error(msg, e);
228 throw e;
229 }
230 }
231 }
232 throw new Exception("No operation found with given name =" + operation);
233
234 }
235
236 public static Collection<ManagedRepository> getRepositoryCollectionValue(
237 MetaValue pValue) {
238 Collection<ManagedRepository> list = new ArrayList<ManagedRepository>();
239 MetaType metaType = pValue.getMetaType();
240 if (metaType.isCollection()) {
241 for (MetaValue value : ((CollectionValueSupport) pValue)
242 .getElements()) {
243 if (value.getMetaType().isComposite()) {
244 ManagedRepository repository = (ManagedRepository) MetaValueFactory
245 .getInstance().unwrap(value);
246 list.add(repository);
247 } else {
248 throw new IllegalStateException(pValue
249 + " is not a Composite type");
250 }
251 }
252 }
253 return list;
254 }
255
256 public static String getConnectorPingString(MetaValue pValue)
257 throws Exception {
258 MetaType metaType = pValue.getMetaType();
259 StringBuffer sb = new StringBuffer();
260 if (metaType.isCollection()) {
261 for (MetaValue value : ((CollectionValueSupport) pValue)
262 .getElements()) {
263 String resultValue = ProfileServiceUtil.stringValue(value);
264 sb.append(resultValue + " ");
265 }
266 }
267 return sb.toString();
268 }
269
270 public static Collection<ManagedSequencerConfig> getSequencerCollectionValue(
271 MetaValue pValue) {
272 Collection<ManagedSequencerConfig> list = new ArrayList<ManagedSequencerConfig>();
273 MetaType metaType = pValue.getMetaType();
274 if (metaType.isCollection()) {
275 for (MetaValue value : ((CollectionValueSupport) pValue)
276 .getElements()) {
277 if (value.getMetaType().isComposite()) {
278 ManagedSequencerConfig sequencer = (ManagedSequencerConfig) MetaValueFactory
279 .getInstance().unwrap(value);
280 list.add(sequencer);
281 } else {
282 throw new IllegalStateException(pValue
283 + " is not a Composite type");
284 }
285 }
286 }
287 return list;
288 }
289
290 private Collection createReportResultList(List fieldNameList,
291 Iterator objectIter) {
292 Collection reportResultList = new ArrayList();
293
294 while (objectIter.hasNext()) {
295 Object object = objectIter.next();
296
297 Class cls = null;
298 try {
299 cls = object.getClass();
300 Iterator methodIter = fieldNameList.iterator();
301 Map reportValueMap = new HashMap<String, String>();
302 while (methodIter.hasNext()) {
303 String fieldName = (String) methodIter.next();
304 String methodName = fieldName;
305 Method meth = cls.getMethod(methodName, (Class[]) null);
306 Object retObj = meth.invoke(object, (Object[]) null);
307 reportValueMap.put(fieldName, retObj);
308 }
309 reportResultList.add(reportValueMap);
310 } catch (Throwable e) {
311 System.err.println(e);
312 }
313 }
314 return reportResultList;
315 }
316
317 }