/
SwitchYardStoreImpl.java
538 lines (456 loc) · 20.8 KB
/
SwitchYardStoreImpl.java
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @author tags. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.switchyard.console.client.model;
import static org.jboss.dmr.client.ModelDescriptionConstants.NAME;
import static org.jboss.dmr.client.ModelDescriptionConstants.OP;
import static org.jboss.dmr.client.ModelDescriptionConstants.OP_ADDR;
import static org.jboss.dmr.client.ModelDescriptionConstants.RESULT;
import static org.jboss.dmr.client.ModelDescriptionConstants.SUBSYSTEM;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import org.jboss.as.console.client.core.ApplicationProperties;
import org.jboss.as.console.client.shared.dispatch.DispatchAsync;
import org.jboss.as.console.client.shared.dispatch.impl.DMRAction;
import org.jboss.as.console.client.shared.dispatch.impl.DMRResponse;
import org.jboss.dmr.client.ModelNode;
import org.switchyard.console.client.BeanFactory;
import org.switchyard.console.components.client.model.Component;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.autobean.shared.AutoBeanCodex;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
* SwitchYardStoreImpl
*
* SwitchYardStore implementation which uses JBoss DMR.
*
* @author Rob Cernich
*/
public class SwitchYardStoreImpl implements SwitchYardStore {
// management api
private static final String APPLICATION_NAME = "application-name";
private static final String GET_VERSION = "get-version";
private static final String LIST_APPLICATIONS = "list-applications";
private static final String LIST_COMPONENTS = "list-components";
private static final String LIST_SERVICES = "list-services";
private static final String READ_APPLICATION = "read-application";
private static final String READ_COMPONENT = "read-component";
private static final String READ_SERVICE = "read-service";
private static final String SERVICE_NAME = "service-name";
private static final String SHOW_METRICS = "show-metrics";
private static final String SWITCHYARD = "switchyard";
private final DispatchAsync _dispatcher;
private final BeanFactory _factory;
private final ApplicationProperties _bootstrap;
private final boolean _isStandalone;
/**
* Create a new SwitchYardStoreImpl.
*
* @param dispatcher the injected dispatcher.
* @param factory the injected bean factory.
* @param bootstrap the injected bootstrap context.
*/
@Inject
public SwitchYardStoreImpl(DispatchAsync dispatcher, BeanFactory factory, ApplicationProperties bootstrap) {
this._dispatcher = dispatcher;
this._factory = factory;
this._bootstrap = bootstrap;
this._isStandalone = bootstrap.getProperty(ApplicationProperties.STANDALONE).equals("true");
}
@Override
public BeanFactory getBeanFactory() {
return _factory;
}
@Override
public void loadSystemDetails(final AsyncCallback<SystemDetails> callback) {
// /subsystem=switchyard:get-version()
final ModelNode operation = new ModelNode();
operation.get(OP_ADDR, SUBSYSTEM).set(SWITCHYARD);
operation.get(OP).set(GET_VERSION);
_dispatcher.execute(new DMRAction(operation), new AsyncCallback<DMRResponse>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(DMRResponse result) {
final ModelNode response = result.get().get(RESULT);
SystemDetails systemDetails = null;
if (response.isDefined()) {
systemDetails = createSystemDetails(response);
}
callback.onSuccess(systemDetails);
}
});
}
@Override
public void loadApplications(final AsyncCallback<List<Application>> callback) {
// /subsystem=switchyard:list-applications()
final List<Application> applications = new ArrayList<Application>();
final ModelNode operation = new ModelNode();
operation.get(OP_ADDR, SUBSYSTEM).set(SWITCHYARD);
operation.get(OP).set(LIST_APPLICATIONS);
_dispatcher.execute(new DMRAction(operation), new AsyncCallback<DMRResponse>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(DMRResponse result) {
final ModelNode response = result.get().get(RESULT);
if (response.isDefined()) {
for (final ModelNode applicationNode : response.asList()) {
try {
final Application application = _factory.application().as();
application.setName(applicationNode.asString());
applications.add(application);
} catch (IllegalArgumentException e) {
Log.error("Failed to parse data source representation", e);
}
}
}
callback.onSuccess(applications);
}
});
}
@Override
public void loadApplication(final String applicationName, final AsyncCallback<Application> callback) {
// /subsystem=switchyard:read-application(name=applicationName)
final ModelNode operation = new ModelNode();
operation.get(OP).set(READ_APPLICATION);
operation.get(OP_ADDR, SUBSYSTEM).set(SWITCHYARD);
operation.get(NAME).set(applicationName);
_dispatcher.execute(new DMRAction(operation), new AsyncCallback<DMRResponse>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(DMRResponse result) {
final ModelNode response = result.get();
if (response.hasDefined(RESULT)) {
final Application application = createApplication(response.get(RESULT).asList().get(0));
if (application != null) {
callback.onSuccess(application);
return;
}
}
callback.onFailure(new Exception("Could not load information for application: " + applicationName));
}
});
}
@Override
public void loadComponents(final AsyncCallback<List<Component>> callback) {
// /subsystem=switchyard:list-components
final List<Component> components = new ArrayList<Component>();
final ModelNode operation = new ModelNode();
operation.get(OP).set(LIST_COMPONENTS);
operation.get(OP_ADDR).add(SUBSYSTEM, "switchyard");
_dispatcher.execute(new DMRAction(operation), new AsyncCallback<DMRResponse>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(DMRResponse result) {
final ModelNode response = result.get().get(RESULT);
if (response.isDefined()) {
for (final ModelNode componentNode : response.asList()) {
try {
final Component component = _factory.component().as();
component.setName(componentNode.asString());
components.add(component);
} catch (IllegalArgumentException e) {
Log.error("Failed to parse data source representation", e);
}
}
}
callback.onSuccess(components);
}
});
}
@Override
public void loadComponent(final String componentName, final AsyncCallback<Component> callback) {
// /subsystem=switchyard:read-component(name=componentName)
final ModelNode operation = new ModelNode();
operation.get(OP).set(READ_COMPONENT);
operation.get(OP_ADDR, SUBSYSTEM).set(SWITCHYARD);
operation.get(NAME).set(componentName);
_dispatcher.execute(new DMRAction(operation), new AsyncCallback<DMRResponse>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(DMRResponse result) {
final ModelNode response = result.get();
if (response.hasDefined(RESULT)) {
final Component component = createComponent(response.get(RESULT).asList().get(0));
if (component != null) {
callback.onSuccess(component);
return;
}
}
callback.onFailure(new Exception("Could not load information for component: " + componentName));
}
});
}
@Override
public void loadServices(final AsyncCallback<List<Service>> callback) {
// /subsystem=switchyard:list-services()
final List<Service> services = new ArrayList<Service>();
final ModelNode operation = new ModelNode();
operation.get(OP_ADDR, SUBSYSTEM).set(SWITCHYARD);
operation.get(OP).set(LIST_SERVICES);
_dispatcher.execute(new DMRAction(operation), new AsyncCallback<DMRResponse>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(DMRResponse result) {
final ModelNode response = result.get().get(RESULT);
if (response.isDefined()) {
for (final ModelNode serviceNode : response.asList()) {
final Service service = createServiceStub(serviceNode);
if (service != null) {
services.add(service);
}
}
}
callback.onSuccess(services);
}
});
}
@Override
public void loadService(final String serviceName, final String applicationName,
final AsyncCallback<Service> callback) {
// /subsystem=switchyard:read-service(service-name=serviceName,
// application-name=applicationName)
final ModelNode operation = new ModelNode();
operation.get(OP).set(READ_SERVICE);
operation.get(OP_ADDR, SUBSYSTEM).set(SWITCHYARD);
operation.get(SERVICE_NAME).set(serviceName);
operation.get(APPLICATION_NAME).set(applicationName);
_dispatcher.execute(new DMRAction(operation), new AsyncCallback<DMRResponse>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(DMRResponse result) {
final ModelNode response = result.get();
if (response.hasDefined(RESULT)) {
final Service service = createService(response.get(RESULT).asList().get(0));
if (service != null) {
callback.onSuccess(service);
return;
}
}
callback.onFailure(new Exception("Could not load information for service: " + serviceName
+ " from application: " + applicationName));
}
});
}
@Override
public void loadServiceMetrics(final String serviceName, final AsyncCallback<ServiceMetrics> callback) {
// /subsystem=switchyard:show-metrics(service-name=serviceName)
final ModelNode operation = new ModelNode();
operation.get(OP).set(SHOW_METRICS);
operation.get(OP_ADDR, SUBSYSTEM).set(SWITCHYARD);
operation.get(SERVICE_NAME).set(serviceName);
_dispatcher.execute(new DMRAction(operation), new AsyncCallback<DMRResponse>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(DMRResponse result) {
final ModelNode response = result.get();
if (response.hasDefined(RESULT)) {
final List<ServiceMetrics> metrics = createServiceMetrics(response.get(RESULT));
if (metrics != null && metrics.size() > 0) {
callback.onSuccess(metrics.get(0));
return;
}
}
callback.onFailure(new Exception("Could not load metrics for service: " + serviceName));
}
});
}
@Override
public void loadAllServiceMetrics(final AsyncCallback<List<ServiceMetrics>> callback) {
// /subsystem=switchyard:show-metrics(service-name=*)
final ModelNode operation = new ModelNode();
operation.get(OP).set(SHOW_METRICS);
operation.get(OP_ADDR, SUBSYSTEM).set(SWITCHYARD);
operation.get(SERVICE_NAME).set("*");
_dispatcher.execute(new DMRAction(operation), new AsyncCallback<DMRResponse>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(DMRResponse result) {
final ModelNode response = result.get();
if (response.hasDefined(RESULT)) {
final List<ServiceMetrics> metrics = createServiceMetrics(response.get(RESULT));
if (metrics != null) {
callback.onSuccess(metrics);
return;
}
}
callback.onFailure(new Exception("Could not load all service metrics."));
}
});
}
@Override
public void loadSystemMetrics(final AsyncCallback<MessageMetrics> callback) {
// /subsystem=switchyard:show-metrics()
final ModelNode operation = new ModelNode();
operation.get(OP).set(SHOW_METRICS);
operation.get(OP_ADDR, SUBSYSTEM).set(SWITCHYARD);
_dispatcher.execute(new DMRAction(operation), new AsyncCallback<DMRResponse>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(DMRResponse result) {
final ModelNode response = result.get();
if (response.hasDefined(RESULT)) {
final MessageMetrics metrics = createMessageMetrics(response.get(RESULT).asList().get(0));
if (metrics != null) {
callback.onSuccess(metrics);
return;
}
}
callback.onFailure(new Exception("Could not load metrics for system"));
}
});
}
@Override
public void loadArtifactReferences(final AsyncCallback<List<ArtifactReference>> callback) {
// /subsystem=switchyard:read-application()
final ModelNode operation = new ModelNode();
operation.get(OP).set(READ_APPLICATION);
operation.get(OP_ADDR, SUBSYSTEM).set(SWITCHYARD);
_dispatcher.execute(new DMRAction(operation), new AsyncCallback<DMRResponse>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(DMRResponse result) {
final ModelNode response = result.get();
if (response.hasDefined(RESULT)) {
Map<String, ArtifactReference> references = new HashMap<String, ArtifactReference>();
for (ModelNode node : response.get(RESULT).asList()) {
Application application = createApplication(node);
if (application == null) {
continue;
}
List<ArtifactReference> artifacts = application.getArtifacts();
if (artifacts == null) {
continue;
}
for (ArtifactReference artifact : artifacts) {
if (references.containsKey(artifact.key())) {
artifact = references.get(artifact.key());
} else {
ArtifactReference copy = getBeanFactory().artifactReference().as();
copy.setName(artifact.getName());
copy.setUrl(artifact.getUrl());
copy.setApplications(new ArrayList<Application>());
artifact = copy;
references.put(artifact.key(), artifact);
}
artifact.getApplications().add(application);
}
}
callback.onSuccess(new ArrayList<ArtifactReference>(references.values()));
return;
}
callback.onFailure(new Exception("Could not load artifact references."));
}
});
}
private SystemDetails createSystemDetails(final ModelNode systemDetailsNode) {
try {
return AutoBeanCodex.decode(_factory, SystemDetails.class, systemDetailsNode.toJSONString(true)).as();
} catch (Exception e) {
Log.error("Failed to parse data source representation", e);
return null;
}
}
private Application createApplication(final ModelNode applicationNode) {
try {
return AutoBeanCodex.decode(_factory, Application.class, applicationNode.toJSONString(true)).as();
} catch (Exception e) {
Log.error("Failed to parse data source representation", e);
return null;
}
}
private Component createComponent(final ModelNode componentNode) {
try {
return AutoBeanCodex.decode(_factory, Component.class, componentNode.toJSONString(true)).as();
} catch (Exception e) {
Log.error("Failed to parse data source representation", e);
return null;
}
}
private Service createServiceStub(final ModelNode serviceNode) {
try {
return AutoBeanCodex.decode(_factory, Service.class, serviceNode.toJSONString(true)).as();
} catch (Exception e) {
Log.error("Failed to parse data source representation", e);
return null;
}
}
private Service createService(final ModelNode serviceNode) {
try {
return AutoBeanCodex.decode(_factory, Service.class, serviceNode.toJSONString(true)).as();
} catch (Exception e) {
Log.error("Failed to parse data source representation", e);
return null;
}
}
private List<ServiceMetrics> createServiceMetrics(final ModelNode metricsNode) {
final List<ModelNode> items = metricsNode.asList();
final List<ServiceMetrics> metrics = new ArrayList<ServiceMetrics>(items.size());
for (ModelNode item : items) {
try {
metrics.add(AutoBeanCodex.decode(_factory, ServiceMetrics.class, item.toJSONString(true)).as());
} catch (Exception e) {
Log.error("Failed to parse data source representation", e);
}
}
return metrics;
}
private MessageMetrics createMessageMetrics(final ModelNode metricsNode) {
try {
return AutoBeanCodex.decode(_factory, MessageMetrics.class, metricsNode.toJSONString(true)).as();
} catch (Exception e) {
Log.error("Failed to parse data source representation", e);
return null;
}
}
}