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.HashMap;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import javax.naming.NamingException;
31  
32  import org.jboss.metatype.api.values.MetaValue;
33  import org.jboss.metatype.api.values.SimpleValueSupport;
34  import org.mc4j.ems.connection.EmsConnection;
35  import org.modeshape.rhq.plugin.util.ModeShapeManagementView;
36  import org.modeshape.rhq.plugin.util.PluginConstants;
37  import org.modeshape.rhq.plugin.util.ProfileServiceUtil;
38  import org.modeshape.rhq.plugin.util.PluginConstants.ComponentType;
39  import org.modeshape.rhq.plugin.util.PluginConstants.ComponentType.Connector;
40  import org.rhq.core.domain.configuration.Configuration;
41  import org.rhq.core.domain.measurement.AvailabilityType;
42  import org.rhq.core.domain.measurement.MeasurementDataTrait;
43  import org.rhq.core.domain.measurement.MeasurementReport;
44  import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
45  import org.rhq.core.pluginapi.inventory.CreateResourceReport;
46  import org.rhq.plugins.jbossas5.connection.ProfileServiceConnection;
47  
48  public class ConnectorComponent extends Facet {
49  
50  	/**
51  	 * {@inheritDoc}
52  	 * 
53  	 * @see org.modeshape.rhq.plugin.Facet#getComponentType()
54  	 */
55  	@Override
56  	String getComponentType() {
57  		return ComponentType.Connector.NAME;
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
62  	 * 
63  	 * @see org.modeshape.rhq.plugin.Facet#getAvailability()
64  	 */
65  	@Override
66  	public AvailabilityType getAvailability() {
67  
68  		MetaValue value;
69  		Boolean pingResultSuccess = new Boolean(false);
70  		try {
71  			String connectorName = this.resourceContext.getResourceKey();
72  			MetaValue[] args = new MetaValue[] { SimpleValueSupport.wrap(connectorName) };
73  			value = ModeShapeManagementView.executeManagedOperation(
74  					ProfileServiceUtil.getManagedEngine(getConnection()),
75  					"pingConnector", args);
76  			pingResultSuccess = ProfileServiceUtil.booleanValue(value);
77  		} catch (NamingException e) {
78  			LOG.error("Naming exception getting: "
79  					+ PluginConstants.ComponentType.Engine.MODESHAPE_ENGINE);
80  			return AvailabilityType.DOWN;
81  		} catch (Exception e) {
82  			LOG.error("Naming exception getting: "
83  					+ PluginConstants.ComponentType.Engine.MODESHAPE_ENGINE);
84  			return AvailabilityType.DOWN;
85  		}
86  
87  		return (pingResultSuccess) ? AvailabilityType.UP
88  				: AvailabilityType.DOWN;
89  	}
90  
91  	/**
92  	 * {@inheritDoc}
93  	 * 
94  	 * @see org.modeshape.rhq.plugin.Facet#setOperationArguments(java.lang.String,
95  	 *      org.rhq.core.domain.configuration.Configuration, java.util.Map)
96  	 */
97  	@Override
98  	protected void setOperationArguments(String name,
99  			Configuration configuration, Map<String, Object> valueMap) {
100 		valueMap.put(Connector.Operations.Parameters.CONNECTOR_NAME,
101 				this.resourceContext.getResourceKey());
102 		// Parameter logic for engine Operations
103 		if (name.equals(Connector.Operations.PING)) {
104 			// only parameter is name which is already set
105 		}
106 	}
107 
108 	/**
109 	 * {@inheritDoc}
110 	 * 
111 	 * @see org.modeshape.rhq.plugin.Facet#getValues(org.rhq.core.domain.measurement.MeasurementReport,
112 	 *      java.util.Set)
113 	 */
114 	@Override
115 	public void getValues(MeasurementReport report,
116 			Set<MeasurementScheduleRequest> requests) throws Exception {
117 
118 		ModeShapeManagementView view = new ModeShapeManagementView();
119 
120 		Map<String, Object> valueMap = new HashMap<String, Object>();
121 		valueMap.put(Connector.Operations.Parameters.CONNECTOR_NAME,
122 				this.resourceContext.getResourceKey());
123 
124 		for (MeasurementScheduleRequest request : requests) {
125 			String name = request.getName();
126 			LOG.debug("Measurement name = " + name); //$NON-NLS-1$
127 
128 			Object metricReturnObject = view.getMetric(getConnection(),
129 					getComponentType(), this.getComponentIdentifier(), name,
130 					valueMap);
131 
132 			try {
133 				if (request
134 						.getName()
135 						.equals(
136 								PluginConstants.ComponentType.Connector.Metrics.INUSECONNECTIONS)) {
137 					report.addData(new MeasurementDataTrait(request,
138 							(String) metricReturnObject));
139 				}
140 			} catch (Exception e) {
141 				LOG.error("Failed to obtain measurement [" + name //$NON-NLS-1$
142 						+ "]. Cause: " + e); //$NON-NLS-1$
143 				// throw(e);
144 			}
145 		}
146 
147 	}
148 
149 	/**
150 	 * {@inheritDoc}
151 	 * 
152 	 * @see org.rhq.core.pluginapi.inventory.CreateChildResourceFacet#createResource(org.rhq.core.pluginapi.inventory.CreateResourceReport)
153 	 */
154 	@Override
155 	public CreateResourceReport createResource(CreateResourceReport arg0) {
156 		return null;
157 	}
158 
159 	/**
160 	 * {@inheritDoc}
161 	 * 
162 	 * @see org.rhq.plugins.jbossas5.ProfileServiceComponent#getConnection()
163 	 */
164 	@Override
165 	public ProfileServiceConnection getConnection() {
166 		return ((EngineComponent) this.resourceContext
167 				.getParentResourceComponent()).getConnection();
168 	}
169 
170 	/**
171 	 * {@inheritDoc}
172 	 * 
173 	 * @see org.rhq.plugins.jmx.JMXComponent#getEmsConnection()
174 	 */
175 	@Override
176 	public EmsConnection getEmsConnection() {
177 		return null;
178 	}
179 
180 }