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