1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.modeshape.rhq.plugin;
20
21
22 import java.io.File;
23 import java.io.InputStream;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.jboss.deployers.spi.management.ManagementView;
32 import org.jboss.managed.api.ComponentType;
33 import org.jboss.managed.api.ManagedComponent;
34 import org.jboss.managed.api.ManagedProperty;
35 import org.mc4j.ems.connection.EmsConnection;
36 import org.modeshape.rhq.plugin.util.PluginConstants;
37 import org.modeshape.rhq.plugin.util.ProfileServiceUtil;
38 import org.rhq.core.domain.configuration.Configuration;
39 import org.rhq.core.domain.configuration.ConfigurationUpdateStatus;
40 import org.rhq.core.domain.configuration.definition.ConfigurationTemplate;
41 import org.rhq.core.domain.content.PackageDetailsKey;
42 import org.rhq.core.domain.content.PackageType;
43 import org.rhq.core.domain.content.transfer.DeployPackageStep;
44 import org.rhq.core.domain.content.transfer.DeployPackagesResponse;
45 import org.rhq.core.domain.content.transfer.RemovePackagesResponse;
46 import org.rhq.core.domain.content.transfer.ResourcePackageDetails;
47 import org.rhq.core.domain.measurement.AvailabilityType;
48 import org.rhq.core.domain.measurement.MeasurementReport;
49 import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
50 import org.rhq.core.domain.resource.ResourceType;
51 import org.rhq.core.pluginapi.configuration.ConfigurationFacet;
52 import org.rhq.core.pluginapi.configuration.ConfigurationUpdateReport;
53 import org.rhq.core.pluginapi.content.ContentFacet;
54 import org.rhq.core.pluginapi.content.ContentServices;
55 import org.rhq.core.pluginapi.content.version.PackageVersions;
56 import org.rhq.core.pluginapi.inventory.CreateChildResourceFacet;
57 import org.rhq.core.pluginapi.inventory.DeleteResourceFacet;
58 import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
59 import org.rhq.core.pluginapi.inventory.ResourceComponent;
60 import org.rhq.core.pluginapi.inventory.ResourceContext;
61 import org.rhq.core.pluginapi.measurement.MeasurementFacet;
62 import org.rhq.core.pluginapi.operation.OperationFacet;
63 import org.rhq.core.pluginapi.operation.OperationResult;
64 import org.rhq.plugins.jbossas5.ApplicationServerComponent;
65 import org.rhq.plugins.jbossas5.ProfileServiceComponent;
66 import org.rhq.plugins.jbossas5.connection.ProfileServiceConnection;
67
68
69
70
71
72 @SuppressWarnings({ "unchecked" })
73 public abstract class Facet implements ProfileServiceComponent<ResourceComponent>, MeasurementFacet,
74 OperationFacet, ConfigurationFacet, ContentFacet, DeleteResourceFacet,
75 CreateChildResourceFacet {
76
77 protected final Log LOG = LogFactory.getLog(PluginConstants.DEFAULT_LOGGER_CATEGORY);
78
79
80
81
82
83 protected Configuration resourceConfiguration;
84
85
86
87
88
89 protected ResourceContext<?> resourceContext;
90
91 protected String name;
92
93 private String identifier;
94
95 protected String componentType;
96
97 protected boolean isAvailable = false;
98
99 private final Log log = LogFactory.getLog(this.getClass());
100
101
102
103
104
105 protected String deploymentName;
106
107 private PackageVersions versions = null;
108
109
110
111
112
113
114
115
116 private static final String PKG_TYPE_VDB = "vdb";
117
118
119
120
121 private static final String ARCHITECTURE = "noarch";
122
123 abstract String getComponentType();
124
125
126
127
128
129
130
131
132
133 public void start(ResourceContext context) {
134 resourceContext = context;
135 deploymentName = context.getResourceKey();
136 }
137
138
139
140
141
142
143
144
145 public void stop() {
146 this.isAvailable = false;
147 }
148
149
150
151
152 public Configuration getResourceConfiguration() {
153 return resourceConfiguration;
154 }
155
156
157
158
159
160 public void setResourceConfiguration(Configuration resourceConfiguration) {
161 this.resourceConfiguration = resourceConfiguration;
162 }
163
164 public String componentType() {
165 return name;
166 }
167
168 protected void setComponentName(String componentName) {
169 this.name = componentName;
170 }
171
172 public String getComponentIdentifier() {
173 return identifier;
174 }
175
176 protected void setComponentIdentifier(String identifier) {
177 this.identifier = identifier;
178 }
179
180 protected void setOperationArguments(String name,
181 Configuration configuration, Map<String, Object> argumentMap) {
182
183 throw new InvalidPluginConfigurationException(
184 "Not implemented on component type " + this.getComponentType()
185 + " named " + this.name);
186
187 }
188
189 protected void setMetricArguments(String name, Configuration configuration,
190 Map<String, Object> argumentMap) {
191
192 throw new InvalidPluginConfigurationException(
193 "Not implemented on component type " + this.getComponentType()
194 + " named " + this.name);
195
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public AvailabilityType getAvailability() {
212
213 LOG.debug("Checking availability of " + identifier);
214
215 return AvailabilityType.UP;
216 }
217
218
219
220
221
222
223
224 protected boolean isAvailable() {
225 return true;
226 }
227
228
229
230
231
232
233
234
235
236 public abstract void getValues(MeasurementReport arg0,
237 Set<MeasurementScheduleRequest> arg1) throws Exception;
238
239
240
241
242
243
244
245
246
247 public OperationResult invokeOperation(String name,
248 Configuration configuration) {
249
250
251
252
253
254
255
256
257
258
259
260
261 return null;
262
263 }
264
265
266
267
268
269
270
271
272
273
274 public Configuration loadResourceConfiguration() {
275
276
277 if (resourceConfiguration == null) {
278
279
280
281
282 resourceConfiguration = this.resourceContext
283 .getPluginConfiguration();
284 }
285
286 Configuration config = resourceConfiguration;
287
288 return config;
289 }
290
291
292
293
294
295
296
297
298
299 public void updateResourceConfiguration(ConfigurationUpdateReport report) {
300
301 resourceConfiguration = report.getConfiguration().deepCopy();
302
303 Configuration resourceConfig = report.getConfiguration();
304
305 ManagementView managementView = null;
306 ComponentType componentType = null;
307
308
309
310
311
312
313
314
315
316 ManagedComponent managedComponent = null;
317 report.setStatus(ConfigurationUpdateStatus.SUCCESS);
318 try {
319
320 managementView = ProfileServiceUtil.getManagementView(
321 ProfileServiceUtil.getProfileService(), true);
322 managedComponent = managementView.getComponent(this.name, componentType);
323 Map<String, ManagedProperty> managedProperties = managedComponent
324 .getProperties();
325
326 ProfileServiceUtil.convertConfigurationToManagedProperties(
327 managedProperties, resourceConfig, resourceContext
328 .getResourceType());
329
330 try {
331 managementView.updateComponent(managedComponent);
332 } catch (Exception e) {
333 LOG.error("Unable to update component ["
334 + managedComponent.getName() + "] of type "
335 + componentType + ".", e);
336 report.setStatus(ConfigurationUpdateStatus.FAILURE);
337 report.setErrorMessageFromThrowable(e);
338 }
339 } catch (Exception e) {
340 LOG.error("Unable to process update request", e);
341 report.setStatus(ConfigurationUpdateStatus.FAILURE);
342 report.setErrorMessageFromThrowable(e);
343 }
344 }
345
346
347
348
349
350 protected Map<String, ManagedProperty> getManagedProperties()
351 throws Exception {
352 return null;
353 }
354
355
356
357
358
359 protected void updateComponent(ManagedComponent managedComponent)
360 throws Exception {
361 log.trace("Updating " + this.name + " with component "
362 + managedComponent.toString() + "...");
363 ManagementView managementView = ProfileServiceUtil.getManagementView(
364 ProfileServiceUtil.getProfileService(), false);
365 managementView.updateComponent(managedComponent);
366
367 }
368
369 @Override
370 public void deleteResource() throws Exception {
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 @Override
400 public DeployPackagesResponse deployPackages(
401 Set<ResourcePackageDetails> packages,
402 ContentServices contentServices) {
403 return null;
404 }
405
406 @Override
407 public Set<ResourcePackageDetails> discoverDeployedPackages(PackageType arg0) {
408
409 File deploymentFile = null;
410
411 if (this.deploymentName != null) {
412 deploymentFile = new File(deploymentName.substring(7));
413 }
414
415 if (deploymentFile == null || !deploymentFile.exists())
416 throw new IllegalStateException("Deployment file '"
417 + deploymentFile + "' for " + this.getComponentType()
418 + " does not exist.");
419
420 String fileName = deploymentFile.getName();
421 org.rhq.core.pluginapi.content.version.PackageVersions packageVersions = loadPackageVersions();
422 String version = packageVersions.getVersion(fileName);
423 if (version == null) {
424
425
426 version = "1.0";
427 packageVersions.putVersion(fileName, version);
428 packageVersions.saveToDisk();
429 }
430
431
432 PackageDetailsKey key = new PackageDetailsKey(fileName, version,
433 PKG_TYPE_VDB, ARCHITECTURE);
434 ResourcePackageDetails packageDetails = new ResourcePackageDetails(key);
435 packageDetails.setFileName(fileName);
436 packageDetails.setLocation(deploymentFile.getPath());
437 if (!deploymentFile.isDirectory())
438 packageDetails.setFileSize(deploymentFile.length());
439 packageDetails.setFileCreatedDate(null);
440 Set<ResourcePackageDetails> packages = new HashSet<ResourcePackageDetails>();
441 packages.add(packageDetails);
442
443 return packages;
444 }
445
446 @Override
447 public List<DeployPackageStep> generateInstallationSteps(
448 ResourcePackageDetails arg0) {
449 return null;
450 }
451
452 @Override
453 public RemovePackagesResponse removePackages(
454 Set<ResourcePackageDetails> arg0) {
455 return null;
456 }
457
458 @Override
459 public InputStream retrievePackageBits(ResourcePackageDetails packageDetails) {
460 return null;
461 }
462
463 protected static Configuration getDefaultPluginConfiguration(
464 ResourceType resourceType) {
465 ConfigurationTemplate pluginConfigDefaultTemplate = resourceType
466 .getPluginConfigurationDefinition().getDefaultTemplate();
467 return (pluginConfigDefaultTemplate != null) ? pluginConfigDefaultTemplate
468 .createConfiguration()
469 : new Configuration();
470 }
471
472
473
474
475
476
477 private org.rhq.core.pluginapi.content.version.PackageVersions loadPackageVersions() {
478 if (this.versions == null) {
479 ResourceType resourceType = resourceContext.getResourceType();
480 String pluginName = resourceType.getPlugin();
481 File dataDirectoryFile = resourceContext.getDataDirectory();
482 dataDirectoryFile.mkdirs();
483 String dataDirectory = dataDirectoryFile.getAbsolutePath();
484 log.trace("Creating application versions store with plugin name ["
485 + pluginName + "] and data directory [" + dataDirectory
486 + "]");
487 this.versions = new PackageVersions(pluginName, dataDirectory);
488 this.versions.loadFromDisk();
489 }
490
491 return this.versions;
492 }
493
494 public ProfileServiceConnection getConnection() {
495 return ((ApplicationServerComponent) this.resourceContext
496 .getParentResourceComponent()).getConnection();
497 }
498
499 public EmsConnection getEmsConnection() {
500
501 return null;
502 }
503
504 }