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.Collection;
27  import java.util.HashSet;
28  import java.util.Set;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.jboss.managed.api.ComponentType;
33  import org.jboss.managed.api.ManagedComponent;
34  import org.jboss.metatype.api.types.EnumMetaType;
35  import org.jboss.metatype.api.values.CollectionValueSupport;
36  import org.jboss.metatype.api.values.CompositeValueSupport;
37  import org.jboss.metatype.api.values.EnumValueSupport;
38  import org.jboss.metatype.api.values.MetaValue;
39  import org.modeshape.jboss.managed.ManagedEngine;
40  import org.modeshape.rhq.plugin.util.ModeShapeManagementView;
41  import org.modeshape.rhq.plugin.util.PluginConstants;
42  import org.modeshape.rhq.plugin.util.ProfileServiceUtil;
43  import org.rhq.core.domain.configuration.Configuration;
44  import org.rhq.core.domain.configuration.PropertyList;
45  import org.rhq.core.domain.configuration.PropertyMap;
46  import org.rhq.core.domain.configuration.PropertySimple;
47  import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
48  import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
49  import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent;
50  import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;
51  
52  /**
53   * 
54   */
55  public class SequencerDiscoveryComponent implements
56  		ResourceDiscoveryComponent<SequencerComponent> {
57  
58  	private final Log log = LogFactory
59  			.getLog(PluginConstants.DEFAULT_LOGGER_CATEGORY);
60  
61  	/**
62  	 * {@inheritDoc}
63  	 * 
64  	 * @see org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent#discoverResources(org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext)
65  	 */
66  	public Set<DiscoveredResourceDetails> discoverResources(
67  			ResourceDiscoveryContext discoveryContext)
68  			throws InvalidPluginConfigurationException, Exception {
69  
70  		Set<DiscoveredResourceDetails> discoveredResources = new HashSet<DiscoveredResourceDetails>();
71  
72  		ManagedComponent mc = ProfileServiceUtil
73  				.getManagedComponent(
74  						((EngineComponent) discoveryContext
75  								.getParentResourceComponent()).getConnection(),
76  						new ComponentType(
77  								PluginConstants.ComponentType.SequencingService.MODESHAPE_TYPE,
78  								PluginConstants.ComponentType.SequencingService.MODESHAPE_SUB_TYPE),
79  						PluginConstants.ComponentType.SequencingService.NAME);
80  
81  		if (mc == null) {
82  			return discoveredResources;
83  		}
84  		
85  		String operation = "getSequencers";
86  
87  		MetaValue sequencers = ModeShapeManagementView.executeManagedOperation(
88  				mc, operation, new MetaValue[] { null });
89  
90  		if (sequencers == null) {
91  			return discoveredResources;
92  		}
93  
94  		Collection<MetaValue> sequencerCollection = ModeShapeManagementView
95  				.getSequencerCollectionValue(sequencers);
96  
97  		for (MetaValue managedSequencer : sequencerCollection) {
98  
99  			MetaValue name = ((CompositeValueSupport)managedSequencer).get("name");
100 			MetaValue description = ((CompositeValueSupport)managedSequencer).get("description");
101 
102 			/**
103 			 * 
104 			 * A discovered resource must have a unique key, that must stay the
105 			 * same when the resource is discovered the next time
106 			 */
107 			DiscoveredResourceDetails detail = new DiscoveredResourceDetails(
108 					discoveryContext.getResourceType(), // ResourceType
109 					ProfileServiceUtil.stringValue(name), // Resource Key
110 					ProfileServiceUtil.stringValue(name), // Resource name
111 					null, // version
112 					ProfileServiceUtil.stringValue(description), // Description
113 					discoveryContext.getDefaultPluginConfiguration(), // Plugin config
114 					null // Process info from a process scan
115 			);
116 			
117 			Configuration c = detail.getPluginConfiguration();
118 
119 			operation = "getProperties";
120 
121 			EnumValueSupport enumVs = new EnumValueSupport(new EnumMetaType(ManagedEngine.Component.values()), ManagedEngine.Component.SEQUENCER);
122 			
123 			MetaValue[] args = new MetaValue[] {
124 					name,
125 					enumVs };
126 			
127 			MetaValue properties = ModeShapeManagementView
128 					.executeManagedOperation(mc, operation, args);
129 
130 			MetaValue[] propertyArray = ((CollectionValueSupport) properties)
131 					.getElements();
132 
133 			PropertyList list = new PropertyList("propertyList");
134 			PropertyMap propMap = null;
135 			c.put(list);
136 			
137 			for (MetaValue property : propertyArray) {
138 
139 				CompositeValueSupport proCvs = (CompositeValueSupport) property;
140 				propMap = new PropertyMap("map");
141 				propMap.put(new PropertySimple("label", ProfileServiceUtil
142 						.stringValue(proCvs.get("label"))));
143 				propMap.put(new PropertySimple("value", ProfileServiceUtil
144 						.stringValue(proCvs.get("value"))));
145 				propMap.put(new PropertySimple("description", ProfileServiceUtil
146 						.stringValue(proCvs.get("description"))));
147 				list.add(propMap);
148 			}
149 
150 
151 			// Add to return values
152 			discoveredResources.add(detail);
153 			log.debug("Discovered ModeShape Sequencer: " + name);
154 		}
155 
156 		return discoveredResources;
157 
158 	}
159 }