View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.rhq.plugin;
25  
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.jboss.managed.api.ComponentType;
32  import org.jboss.managed.api.ManagedComponent;
33  import org.jboss.metatype.api.types.EnumMetaType;
34  import org.jboss.metatype.api.values.CollectionValueSupport;
35  import org.jboss.metatype.api.values.CompositeValueSupport;
36  import org.jboss.metatype.api.values.EnumValueSupport;
37  import org.jboss.metatype.api.values.MetaValue;
38  import org.modeshape.jboss.managed.ManagedEngine;
39  import org.modeshape.rhq.plugin.util.ModeShapeManagementView;
40  import org.modeshape.rhq.plugin.util.PluginConstants;
41  import org.modeshape.rhq.plugin.util.ProfileServiceUtil;
42  import org.rhq.core.domain.configuration.Configuration;
43  import org.rhq.core.domain.configuration.PropertyList;
44  import org.rhq.core.domain.configuration.PropertyMap;
45  import org.rhq.core.domain.configuration.PropertySimple;
46  import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
47  import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
48  import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent;
49  import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;
50  
51  /**
52   * 
53   */
54  public class SequencingServiceDiscoveryComponent implements
55  		ResourceDiscoveryComponent<SequencingServiceComponent> {
56  
57  	private final Log log = LogFactory
58  			.getLog(PluginConstants.DEFAULT_LOGGER_CATEGORY);
59  
60  	/**
61  	 * {@inheritDoc}
62  	 * 
63  	 * @see org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent#discoverResources(org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext)
64  	 */
65  	public Set<DiscoveredResourceDetails> discoverResources(
66  			ResourceDiscoveryContext discoveryContext)
67  			throws InvalidPluginConfigurationException, Exception {
68  
69  		Set<DiscoveredResourceDetails> discoveredResources = new HashSet<DiscoveredResourceDetails>();
70  
71  		ManagedComponent mc = ProfileServiceUtil
72  				.getManagedComponent( ((EngineComponent) discoveryContext
73  						.getParentResourceComponent()).getConnection(),
74  						new ComponentType(
75  								PluginConstants.ComponentType.SequencingService.MODESHAPE_TYPE,
76  								PluginConstants.ComponentType.SequencingService.MODESHAPE_SUB_TYPE),
77  								PluginConstants.ComponentType.SequencingService.NAME);
78  
79  		if (mc == null) {
80  			return discoveredResources;
81  		}
82  
83  		/**
84  		 * 
85  		 * A discovered resource must have a unique key, that must stay the same
86  		 * when the resource is discovered the next time
87  		 */
88  		DiscoveredResourceDetails detail = new DiscoveredResourceDetails(
89  				discoveryContext.getResourceType(), // ResourceType
90  				PluginConstants.ComponentType.SequencingService.NAME, // Resource Key
91  				PluginConstants.ComponentType.SequencingService.DISPLAY_NAME, // Resource name
92  				null, //version 
93  				PluginConstants.ComponentType.SequencingService.DESC, // Description
94  				discoveryContext.getDefaultPluginConfiguration(), // Plugin config
95  				null // Process info from a process scan
96  		);
97  		Configuration c = detail.getPluginConfiguration();
98  		
99  		//Load properties
100 		String operation = "getProperties";
101 
102 		EnumValueSupport enumVs = new EnumValueSupport(new EnumMetaType(ManagedEngine.Component.values()), ManagedEngine.Component.SEQUENCINGSERVICE);
103 		MetaValue[] args = new MetaValue[] {
104 				null,
105 				enumVs };
106 
107 		MetaValue properties = ModeShapeManagementView
108 				.executeManagedOperation(mc, operation, args);
109 
110 		MetaValue[] propertyArray = ((CollectionValueSupport) properties)
111 				.getElements();
112 
113 		PropertyList list = new PropertyList("propertyList");
114 		c.put(list);
115 		loadProperties(propertyArray, list);
116 
117 		// Add to return values
118 		discoveredResources.add(detail);
119 		log.debug("Discovered ModeShape Sequencing Service: " + mc.getName());
120 
121 		return discoveredResources;
122 
123 	}
124 	
125 	/**
126 	 * @param propertyArray
127 	 * @param list
128 	 * @throws Exception
129 	 */
130 	private void loadProperties(MetaValue[] propertyArray, PropertyList list)
131 			throws Exception {
132 		PropertyMap propMap;
133 		for (MetaValue property : propertyArray) {
134 
135 			CompositeValueSupport proCvs = (CompositeValueSupport) property;
136 			propMap = new PropertyMap("map");
137 			propMap.put(new PropertySimple("label", ProfileServiceUtil
138 					.stringValue(proCvs.get("label"))));
139 			propMap.put(new PropertySimple("value", ProfileServiceUtil
140 					.stringValue(proCvs.get("value"))));
141 			propMap.put(new PropertySimple("description", ProfileServiceUtil
142 					.stringValue(proCvs.get("description"))));
143 			list.add(propMap);
144 		}
145 	}
146 }