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.values.MetaValue;
34  import org.modeshape.rhq.plugin.util.ModeShapeManagementView;
35  import org.modeshape.rhq.plugin.util.PluginConstants;
36  import org.modeshape.rhq.plugin.util.ProfileServiceUtil;
37  import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
38  import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
39  import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent;
40  import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;
41  import org.rhq.plugins.jbossas5.ApplicationServerComponent;
42  
43  /**
44   * 
45   */
46  public class EngineDiscoveryComponent implements
47  		ResourceDiscoveryComponent<EngineComponent> {
48  
49  	private final Log log = LogFactory
50  			.getLog(PluginConstants.DEFAULT_LOGGER_CATEGORY);
51  
52  	/**
53  	 * {@inheritDoc}
54  	 * 
55  	 * @see org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent#discoverResources(org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext)
56  	 */
57  	public Set<DiscoveredResourceDetails> discoverResources(
58  			ResourceDiscoveryContext discoveryContext)
59  			throws InvalidPluginConfigurationException, Exception {
60  
61  		Set<DiscoveredResourceDetails> discoveredResources = new HashSet<DiscoveredResourceDetails>();
62  
63  		ManagedComponent mc = ProfileServiceUtil
64  				.getManagedComponent(((ApplicationServerComponent) discoveryContext
65  						.getParentResourceComponent()).getConnection(),
66  						new ComponentType(
67  								PluginConstants.ComponentType.Engine.MODESHAPE_TYPE,
68  								PluginConstants.ComponentType.Engine.MODESHAPE_SUB_TYPE),
69  						PluginConstants.ComponentType.Engine.MODESHAPE_ENGINE);
70  		
71  		if (mc==null){
72  			log.debug("No ModeShape Engine discovered");
73  			return discoveredResources;
74  		}
75  		
76  		String version = ProfileServiceUtil.stringValue(ModeShapeManagementView.executeManagedOperation(mc, "getVersion", new MetaValue[]{null}));
77  
78  		/**
79  		 * 
80  		 * A discovered resource must have a unique key, that must stay the same
81  		 * when the resource is discovered the next time
82  		 */
83  		DiscoveredResourceDetails detail = new DiscoveredResourceDetails(
84  				discoveryContext.getResourceType(), // ResourceType
85  				mc.getName(), // Resource Key
86  				PluginConstants.ComponentType.Engine.MODESHAPE_DISPLAYNAME, // Resource
87  				// name
88  				version,
89  				PluginConstants.ComponentType.Engine.MODESHAPE_ENGINE, // Description
90  				discoveryContext.getDefaultPluginConfiguration(), // Plugin Config
91  				null // Process info from a process scan
92  		);
93  
94  		// Add to return values
95  		discoveredResources.add(detail);
96  		log.debug("Discovered ModeShape Engine");
97  		return discoveredResources;
98  
99  	}
100 }