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.ManagedComponent;
32  import org.jboss.metatype.api.values.CollectionValueSupport;
33  import org.jboss.metatype.api.values.CompositeValueSupport;
34  import org.jboss.metatype.api.values.MetaValue;
35  import org.jboss.metatype.api.values.MetaValueFactory;
36  import org.modeshape.jboss.managed.ManagedEngine;
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 ConnectorDiscoveryComponent implements
53  		ResourceDiscoveryComponent<ConnectorComponent> {
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  				.getManagedEngine(
71  						((EngineComponent) discoveryContext
72  								.getParentResourceComponent()).getConnection());
73  
74  		String operation = "getConnectors";
75  
76  		MetaValue connectors = ModeShapeManagementView.executeManagedOperation(
77  				mc, operation, new MetaValue[] { null });
78  
79  		if (connectors == null) {
80  			return discoveredResources;
81  		}
82  
83  		MetaValue[] mvConnectorArray = ((CollectionValueSupport) connectors)
84  				.getElements();
85  
86  		for (MetaValue value : mvConnectorArray) {
87  
88  			CompositeValueSupport cvs = (CompositeValueSupport) value;
89  			String name = ProfileServiceUtil.stringValue(cvs.get("name"));
90  
91  			/**
92  			 * 
93  			 * A discovered resource must have a unique key, that must stay the
94  			 * same when the resource is discovered the next time
95  			 */
96  			DiscoveredResourceDetails detail = new DiscoveredResourceDetails(
97  					discoveryContext.getResourceType(), // ResourceType
98  					name, // Resource Key
99  					name, // Resource name
100 					null,
101 					PluginConstants.ComponentType.Connector.DESCRIPTION, // Description
102 					discoveryContext.getDefaultPluginConfiguration(), // Plugin
103 																		// config
104 					null // Process info from a process scan
105 			);
106 
107 			Configuration c = detail.getPluginConfiguration();
108 
109 
110 			//Load connector properties
111 			operation = "getProperties";
112 
113 			MetaValue[] args = new MetaValue[] {
114 					MetaValueFactory.getInstance().create(name),
115 					MetaValueFactory.getInstance().create(
116 							ManagedEngine.Component.CONNECTOR) };
117 
118 			MetaValue properties = ModeShapeManagementView
119 					.executeManagedOperation(mc, operation, args);
120 
121 			MetaValue[] propertyArray = ((CollectionValueSupport) properties)
122 					.getElements();
123 
124 			PropertyList connectorlist = new PropertyList("connectorPropertyList");
125 			c.put(connectorlist);
126 			loadProperties(propertyArray, connectorlist);
127 
128 			//Load connection pool properties
129 			args = new MetaValue[] {
130 					MetaValueFactory.getInstance().create(name),
131 					MetaValueFactory.getInstance().create(
132 							ManagedEngine.Component.CONNECTIONPOOL) };
133 
134 			properties = ModeShapeManagementView
135 					.executeManagedOperation(mc, operation, args);
136 
137 			propertyArray = ((CollectionValueSupport) properties)
138 					.getElements();
139 			PropertyList connectionPoollist = new PropertyList("connectionpoolPropertyList");
140 			c.put(connectionPoollist);
141 			loadProperties(propertyArray, connectionPoollist);
142 			
143 			detail.setPluginConfiguration(c);
144 
145 			// Add to return values
146 			discoveredResources.add(detail);
147 			log.info("Discovered ModeShape repositories: " + mc.getName());
148 		}
149 
150 		return discoveredResources;
151 
152 	}
153 
154 	/**
155 	 * @param propertyArray
156 	 * @param list
157 	 * @throws Exception
158 	 */
159 	private void loadProperties(MetaValue[] propertyArray, PropertyList list)
160 			throws Exception {
161 		PropertyMap propMap;
162 		for (MetaValue property : propertyArray) {
163 
164 			CompositeValueSupport proCvs = (CompositeValueSupport) property;
165 			propMap = new PropertyMap("map");
166 			propMap.put(new PropertySimple("label", ProfileServiceUtil
167 					.stringValue(proCvs.get("label"))));
168 			propMap.put(new PropertySimple("value", ProfileServiceUtil
169 					.stringValue(proCvs.get("value"))));
170 			propMap.put(new PropertySimple("description", ProfileServiceUtil
171 					.stringValue(proCvs.get("description"))));
172 			list.add(propMap);
173 		}
174 	}
175 }