[infinispan-commits] Infinispan SVN: r919 - in trunk/jopr-plugin/src/main/java/org/infinispan/jopr: infinispan and 1 other directory.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Wed Oct 7 09:01:12 EDT 2009
Author: galder.zamarreno at jboss.com
Date: 2009-10-07 09:01:11 -0400 (Wed, 07 Oct 2009)
New Revision: 919
Added:
trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheComponent.java
trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheDiscovery.java
trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheManagerComponent.java
trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheManagerDiscovery.java
trunk/jopr-plugin/src/main/java/org/infinispan/jopr/ConnectionHelper.java
Removed:
trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheComponent.java
trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheDiscovery.java
trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheManagerComponent.java
trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheManagerDiscovery.java
trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/ConnectionHelper.java
Log:
[ISPN-135] (Automatically generate the JOPR XML descriptor for the JOPR plugin) Second infinispan package name is redundant.
Copied: trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheComponent.java (from rev 918, trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheComponent.java)
===================================================================
--- trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheComponent.java (rev 0)
+++ trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheComponent.java 2009-10-07 13:01:11 UTC (rev 919)
@@ -0,0 +1,182 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.infinispan.jopr;
+
+import org.infinispan.util.logging.Log;
+import org.infinispan.util.logging.LogFactory;
+import org.mc4j.ems.connection.EmsConnection;
+import org.mc4j.ems.connection.bean.EmsBean;
+import org.mc4j.ems.connection.bean.attribute.EmsAttribute;
+import org.mc4j.ems.connection.bean.operation.EmsOperation;
+import org.rhq.core.domain.configuration.Configuration;
+import org.rhq.core.domain.measurement.AvailabilityType;
+import org.rhq.core.domain.measurement.MeasurementDataNumeric;
+import org.rhq.core.domain.measurement.MeasurementReport;
+import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
+import org.rhq.core.pluginapi.inventory.ResourceComponent;
+import org.rhq.core.pluginapi.inventory.ResourceContext;
+import org.rhq.core.pluginapi.measurement.MeasurementFacet;
+import org.rhq.core.pluginapi.operation.OperationFacet;
+import org.rhq.core.pluginapi.operation.OperationResult;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Component class for Caches within Infinispan
+ *
+ * @author Heiko W. Rupp
+ * @author Galder Zamarreño
+ */
+public class CacheComponent implements ResourceComponent<CacheManagerComponent>, MeasurementFacet, OperationFacet {
+ private final Log log = LogFactory.getLog(this.getClass());
+
+ /**
+ * Map to match an abbreviation for the MBean to the full MBean name
+ */
+ private static final Map<String, String> abbrevToMBean = new HashMap<String, String>();
+
+ static {
+ abbrevToMBean.put("Statistics", "Statistics");
+ abbrevToMBean.put("LockManager", "LockManager");
+ abbrevToMBean.put("Transactions", "Transactions");
+ }
+
+
+ private ResourceContext<CacheManagerComponent> context;
+ /**
+ * The naming pattern of the current bean without the actual bean name
+ */
+ private String myNamePattern;
+
+ /**
+ * Return availability of this resource
+ *
+ * @see org.rhq.core.pluginapi.inventory.ResourceComponent#getAvailability()
+ */
+ public AvailabilityType getAvailability() {
+ // TODO does a cache have a lifecycle of its own?
+ return context.getParentResourceComponent().getAvailability();
+ }
+
+
+ /**
+ * Start the resource connection
+ *
+ * @see org.rhq.core.pluginapi.inventory.ResourceComponent#start(org.rhq.core.pluginapi.inventory.ResourceContext)
+ */
+ public void start(ResourceContext<CacheManagerComponent> context) throws Exception {
+
+ this.context = context;
+ //
+ myNamePattern = context.getResourceKey();
+ myNamePattern = myNamePattern.substring(0, myNamePattern.indexOf("jmx-resource=") + 13);
+
+ }
+
+
+ /**
+ * Tear down the rescource connection
+ *
+ * @see org.rhq.core.pluginapi.inventory.ResourceComponent#stop()
+ */
+ public void stop() {
+
+
+ }
+
+
+ /**
+ * Gather measurement data
+ *
+ * @see org.rhq.core.pluginapi.measurement.MeasurementFacet#getValues(org.rhq.core.domain.measurement.MeasurementReport,
+ * java.util.Set)
+ */
+ public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception {
+
+ EmsConnection conn = getConnection();
+ for (MeasurementScheduleRequest req : metrics) {
+ String metric = req.getName();
+ try {
+ String abbrev = metric.substring(0, metric.indexOf("."));
+ String mbean = abbrevToMBean.get(abbrev);
+ mbean = myNamePattern + mbean;
+ EmsBean bean = conn.getBean(mbean);
+ bean.refreshAttributes();
+ String attName = metric.substring(metric.indexOf(".") + 1);
+ EmsAttribute att = bean.getAttribute(attName);
+
+ // Attribute values are of various data types ...
+ Object o = att.getValue();
+ Class type = att.getTypeClass();
+ if (type.equals(Long.class) || type.equals(long.class)) {
+ Long tmp = (Long) o;
+ MeasurementDataNumeric res = new MeasurementDataNumeric(req, Double.valueOf(tmp));
+ report.addData(res);
+ } else if (type.equals(Double.class) || type.equals(double.class)) {
+ Double tmp = (Double) o;
+ MeasurementDataNumeric res = new MeasurementDataNumeric(req, tmp);
+ report.addData(res);
+ } else if (type.equals(Integer.class) || type.equals(int.class)) {
+ Integer tmp = (Integer) o;
+ MeasurementDataNumeric res = new MeasurementDataNumeric(req, Double.valueOf(tmp));
+ report.addData(res);
+ }
+ }
+ catch (Exception e) {
+ log.warn("getValues failed for " + metric + " : ", e);
+ }
+ }
+ }
+
+ /**
+ * Invoke operations on the Cache MBean instance
+ *
+ * @param name Name of the operation
+ * @param parameters Parameters of the Operation
+ * @return OperationResult object if successful
+ * @throws Exception If operation was not successful
+ */
+ public OperationResult invokeOperation(String name,
+ Configuration parameters) throws Exception {
+ EmsConnection conn = getConnection();
+ String abbrev = name.substring(0, name.indexOf("."));
+ String mbean = abbrevToMBean.get(abbrev);
+ mbean = myNamePattern + mbean;
+ EmsBean bean = conn.getBean(mbean);
+ String opName = name.substring(name.indexOf(".") + 1);
+ EmsOperation ops = bean.getOperation(opName);
+ if (ops != null)
+ ops.invoke(new Object[]{});
+ else
+ throw new Exception("Operation " + name + " can't be found");
+
+
+ return new OperationResult();
+ }
+
+ private EmsConnection getConnection() {
+ return context.getParentResourceComponent().getConnection();
+ }
+
+}
\ No newline at end of file
Copied: trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheDiscovery.java (from rev 918, trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheDiscovery.java)
===================================================================
--- trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheDiscovery.java (rev 0)
+++ trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheDiscovery.java 2009-10-07 13:01:11 UTC (rev 919)
@@ -0,0 +1,89 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.infinispan.jopr;
+
+import org.infinispan.util.logging.Log;
+import org.infinispan.util.logging.LogFactory;
+import org.mc4j.ems.connection.EmsConnection;
+import org.mc4j.ems.connection.bean.EmsBean;
+import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
+import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent;
+import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;
+import org.rhq.plugins.jmx.ObjectNameQueryUtility;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Discovery class for individual cache instances
+ *
+ * @author Heiko W. Rupp
+ * @author Galder Zamarreño
+ */
+public class CacheDiscovery implements ResourceDiscoveryComponent<CacheManagerComponent> {
+ private static final Log log = LogFactory.getLog(CacheDiscovery.class);
+
+ /**
+ * Naming pattern of the cache MgmtInterceptor
+ */
+ private static final String CACHE_QUERY = "*:cache-name=%name%,jmx-resource=Cache";
+
+ /**
+ * Run the discovery
+ */
+ public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext<CacheManagerComponent> discoveryContext) throws Exception {
+ if (log.isTraceEnabled()) log.trace("Discover resources with context: " + discoveryContext);
+ Set<DiscoveredResourceDetails> discoveredResources = new HashSet<DiscoveredResourceDetails>();
+
+ EmsConnection conn = discoveryContext.getParentResourceComponent().getConnection();
+ if (log.isTraceEnabled()) log.trace("Connection to ems server stablished: " + conn);
+
+ ObjectNameQueryUtility queryUtility = new ObjectNameQueryUtility(CACHE_QUERY);
+ List<EmsBean> beans = conn.queryBeans(queryUtility.getTranslatedQuery());
+ if (log.isTraceEnabled()) log.trace("Querying [" + queryUtility.getTranslatedQuery() + "] returned beans: " + beans);
+
+ for (EmsBean bean : beans) {
+ /**
+ * A discovered resource must have a unique key, that must
+ * stay the same when the resource is discovered the next
+ * time
+ */
+ String name = bean.getBeanName().getCanonicalName();
+ name = name.substring(name.indexOf("jmx-resource=") + 13);
+ DiscoveredResourceDetails detail = new DiscoveredResourceDetails(
+ discoveryContext.getResourceType(), // Resource Type
+ bean.getBeanName().getCanonicalName(), // Resource Key
+ name, // Resource name
+ null, // Version
+ "One cache within Infinispan", // ResourceDescription
+ discoveryContext.getDefaultPluginConfiguration(), // Plugin Config
+ null // ProcessInfo
+ );
+
+ // Add to return values
+ discoveredResources.add(detail);
+ log.info("Discovered new ... " + bean.getBeanName().getCanonicalName());
+ }
+ return discoveredResources;
+ }
+}
\ No newline at end of file
Copied: trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheManagerComponent.java (from rev 918, trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheManagerComponent.java)
===================================================================
--- trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheManagerComponent.java (rev 0)
+++ trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheManagerComponent.java 2009-10-07 13:01:11 UTC (rev 919)
@@ -0,0 +1,120 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.infinispan.jopr;
+
+import org.mc4j.ems.connection.EmsConnection;
+import org.mc4j.ems.connection.bean.EmsBean;
+import org.rhq.core.domain.measurement.AvailabilityType;
+import org.rhq.core.domain.measurement.MeasurementDataNumeric;
+import org.rhq.core.domain.measurement.MeasurementReport;
+import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
+import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
+import org.rhq.core.pluginapi.inventory.ResourceComponent;
+import org.rhq.core.pluginapi.inventory.ResourceContext;
+import org.rhq.core.pluginapi.measurement.MeasurementFacet;
+
+import java.util.Set;
+
+/**
+ * The component class for the Infinispan manager
+ *
+ * @author Heiko W. Rupp
+ * @author Galder Zamarreño
+ */
+public class CacheManagerComponent implements ResourceComponent, MeasurementFacet {
+ private ResourceContext context;
+ private ConnectionHelper helper;
+
+
+ /**
+ * Return availability of this resource. We do this by checking the connection to it. If the Manager would expose
+ * some "run state" we could check for that too.
+ *
+ * @see org.rhq.core.pluginapi.inventory.ResourceComponent#getAvailability()
+ */
+ public AvailabilityType getAvailability() {
+ EmsConnection conn = getConnection();
+ try {
+ conn.refresh();
+ EmsBean bean = conn.getBean(context.getResourceKey());
+ if (bean != null)
+ bean.refreshAttributes();
+ return AvailabilityType.UP;
+ } catch (Exception e) {
+ return AvailabilityType.DOWN;
+ }
+ }
+
+ /**
+ * Start the resource connection
+ *
+ * @see org.rhq.core.pluginapi.inventory.ResourceComponent#start(org.rhq.core.pluginapi.inventory.ResourceContext)
+ */
+ public void start(ResourceContext context) throws InvalidPluginConfigurationException, Exception {
+
+ this.context = context;
+ helper = new ConnectionHelper();
+ getConnection();
+ }
+
+ /**
+ * Tear down the rescource connection
+ *
+ * @see org.rhq.core.pluginapi.inventory.ResourceComponent#stop()
+ */
+ public void stop() {
+ helper.closeConnection();
+
+ }
+
+ /**
+ * Gather measurement data
+ *
+ * @see org.rhq.core.pluginapi.measurement.MeasurementFacet#getValues(org.rhq.core.domain.measurement.MeasurementReport,
+ * java.util.Set)
+ */
+ public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception {
+
+ EmsConnection conn = getConnection();
+ EmsBean bean = conn.getBean(context.getPluginConfiguration().getSimpleValue("objectName", null));
+ bean.refreshAttributes();
+
+ for (MeasurementScheduleRequest req : metrics) {
+ // TODO check with Traits in the future - also why are the values Strings?
+ String tmp = (String) bean.getAttribute(req.getName()).getValue();
+ Double val = Double.valueOf(tmp);
+ MeasurementDataNumeric res = new MeasurementDataNumeric(req, val);
+ report.addData(res);
+ }
+ }
+
+ /**
+ * Helper to obtain a connection
+ *
+ * @return EmsConnection object
+ */
+ protected EmsConnection getConnection() {
+ EmsConnection conn = helper.getEmsConnection(context.getPluginConfiguration());
+ return conn;
+ }
+
+}
\ No newline at end of file
Copied: trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheManagerDiscovery.java (from rev 918, trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheManagerDiscovery.java)
===================================================================
--- trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheManagerDiscovery.java (rev 0)
+++ trunk/jopr-plugin/src/main/java/org/infinispan/jopr/CacheManagerDiscovery.java 2009-10-07 13:01:11 UTC (rev 919)
@@ -0,0 +1,115 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.infinispan.jopr;
+
+import static org.infinispan.jmx.CacheManagerJmxRegistration.*;
+import static org.infinispan.jmx.ComponentsJmxRegistration.*;
+import static org.infinispan.manager.DefaultCacheManager.*;
+import org.infinispan.util.logging.Log;
+import org.infinispan.util.logging.LogFactory;
+import org.mc4j.ems.connection.EmsConnection;
+import org.mc4j.ems.connection.bean.EmsBean;
+import org.rhq.core.domain.configuration.Configuration;
+import org.rhq.core.domain.configuration.PropertySimple;
+import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
+import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent;
+import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;
+import org.rhq.plugins.jmx.JMXDiscoveryComponent;
+import org.rhq.plugins.jmx.ObjectNameQueryUtility;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Discovery class for Infinispan engines
+ *
+ * @author Heiko W. Rupp
+ * @author Galder Zamarreño
+ */
+public class CacheManagerDiscovery implements ResourceDiscoveryComponent<CacheManagerComponent> {
+ private static final Log log = LogFactory.getLog(CacheManagerDiscovery.class);
+
+// // Assume a java5+ jmx-remote connector on port 6996
+// public static String REMOTE = "service:jmx:rmi://127.0.0.1/jndi/rmi://127.0.0.1:6996/jmxrmi";
+
+// public static String MANAGER_OBJECT = "*:cache-name=[global],jmx-resource=CacheManager";
+
+ private static final String MANAGER_OBJECT = "*:" + CACHE_NAME_KEY + '=' + GLOBAL_JMX_GROUP + "," + JMX_RESOURCE_KEY + "=" + OBJECT_NAME;
+ private static final String CONNECTOR = "org.mc4j.ems.connection.support.metadata.J2SE5ConnectionTypeDescriptor";
+
+ /**
+ * Run the discovery
+ */
+ public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext<CacheManagerComponent> discoveryContext) throws Exception {
+ boolean trace = log.isTraceEnabled();
+ if (trace) log.trace("Discover resources with context: " + discoveryContext);
+
+ Set<DiscoveredResourceDetails> discoveredResources = new HashSet<DiscoveredResourceDetails>();
+ // TODO check if we e.g. run inside a JBossAS to which we have a connection already that we can reuse.
+ Configuration c = discoveryContext.getDefaultPluginConfiguration();
+ c.put(c.getSimple(JMXDiscoveryComponent.CONNECTOR_ADDRESS_CONFIG_PROPERTY));
+ c.put(new PropertySimple(JMXDiscoveryComponent.CONNECTION_TYPE, CONNECTOR));
+ c.put(new PropertySimple("objectName", MANAGER_OBJECT));
+
+// c.put(new PropertySimple(JMXDiscoveryComponent.CONNECTOR_ADDRESS_CONFIG_PROPERTY, c.getSimple(JMXDiscoveryComponent.CONNECTOR_ADDRESS_CONFIG_PROPERTY)));
+// c.put(new PropertySimple(JMXDiscoveryComponent.CONNECTION_TYPE, connector));
+// c.put(new PropertySimple("objectName", MANAGER_OBJECT));
+
+ ConnectionHelper helper = new ConnectionHelper();
+ EmsConnection conn = helper.getEmsConnection(c);
+
+ if (trace) log.trace("Connection to ems server stablished: " + conn);
+
+ // Run query for manager_object
+ ObjectNameQueryUtility queryUtility = new ObjectNameQueryUtility(MANAGER_OBJECT);
+ List<EmsBean> beans = conn.queryBeans(queryUtility.getTranslatedQuery());
+ if (trace) log.trace("Querying [" + queryUtility.getTranslatedQuery() + "] returned beans: " + beans);
+
+ for (EmsBean bean : beans) {
+
+ String managerName = bean.getBeanName().getCanonicalName();
+ c.put(new PropertySimple("objectName", managerName));
+ /**
+ *
+ * A discovered resource must have a unique key, that must
+ * stay the same when the resource is discovered the next
+ * time
+ */
+ DiscoveredResourceDetails detail = new DiscoveredResourceDetails(
+ discoveryContext.getResourceType(), // ResourceType
+ managerName, // Resource Key
+ "Infinispan Cache Manager", // Resource Name
+ null, // Version TODO can we get that from discovery ?
+ "The Infinispan Manager", // Description
+ c, // Plugin Config
+ null // Process info from a process scan
+ );
+
+ // Add to return values
+ discoveredResources.add(detail);
+ log.info("Discovered Infinispan instance: " + managerName);
+ }
+ return discoveredResources;
+
+ }
+}
\ No newline at end of file
Copied: trunk/jopr-plugin/src/main/java/org/infinispan/jopr/ConnectionHelper.java (from rev 915, trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/ConnectionHelper.java)
===================================================================
--- trunk/jopr-plugin/src/main/java/org/infinispan/jopr/ConnectionHelper.java (rev 0)
+++ trunk/jopr-plugin/src/main/java/org/infinispan/jopr/ConnectionHelper.java 2009-10-07 13:01:11 UTC (rev 919)
@@ -0,0 +1,167 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2009 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.infinispan.jopr;
+
+import org.infinispan.util.Util;
+import org.infinispan.util.logging.Log;
+import org.infinispan.util.logging.LogFactory;
+import org.mc4j.ems.connection.ConnectionFactory;
+import org.mc4j.ems.connection.EmsConnection;
+import org.mc4j.ems.connection.settings.ConnectionSettings;
+import org.mc4j.ems.connection.support.ConnectionProvider;
+import org.mc4j.ems.connection.support.metadata.ConnectionTypeDescriptor;
+import org.rhq.core.domain.configuration.Configuration;
+import org.rhq.core.domain.configuration.PropertySimple;
+import org.rhq.plugins.jmx.JMXDiscoveryComponent;
+
+import java.util.Properties;
+
+/**
+ * Shared helper class to connect to a remote server
+ *
+ * @author Jay Shaughnessy
+ * @author Heiko W. Rupp
+ */
+public class ConnectionHelper {
+
+ private final Log log = LogFactory.getLog(ConnectionHelper.class);
+
+ private EmsConnection connection;
+ /**
+ * Controls the dampening of connection error stack traces in an attempt to control spam to the log file. Each time a
+ * connection error is encountered, this will be incremented. When the connection is finally established, this will
+ * be reset to zero.
+ */
+ private int consecutiveConnectionErrors;
+
+
+ public EmsConnection getEmsConnection(Configuration config) {
+ EmsConnection emsConnection = null;
+
+ try {
+ emsConnection = loadConnection(config);
+ } catch (Exception e) {
+ log.error("Component attempting to access a connection that could not be loaded");
+ }
+
+ return emsConnection;
+ }
+
+ /**
+ * This is the preferred way to use a connection from within this class; methods should not access the connection
+ * property directly as it may not have been instantiated if the connection could not be made. <p/> <p>If the
+ * connection has already been established, return the object reference to it. If not, attempt to make a live
+ * connection to the JMX server.</p> <p/> <p>If the connection could not be made in the
+ * start(org.rhq.core.pluginapi.inventory.ResourceContext) method, this method will effectively try to load the
+ * connection on each attempt to use it. As such, multiple threads may attempt to access the connection through this
+ * means at a time. Therefore, the method has been made synchronized on instances of the class.</p> <p/> <p>If any
+ * errors are encountered, this method will log the error, taking into account logic to prevent spamming the log
+ * file. Calling methods should take care to not redundantly log the exception thrown by this method.</p>
+ *
+ * @param pluginConfig
+ * @return live connection to the JMX server; this will not be <code>null</code>
+ * @throws Exception if there are any issues at all connecting to the server
+ */
+ private synchronized EmsConnection loadConnection(Configuration pluginConfig) throws Exception {
+ if (this.connection == null) {
+ try {
+ //Configuration pluginConfig = this.resourceContext.getPluginConfiguration();
+
+ ConnectionSettings connectionSettings = new ConnectionSettings();
+
+ String connectionTypeDescriptorClass = pluginConfig.getSimple(JMXDiscoveryComponent.CONNECTION_TYPE)
+ .getStringValue();
+ PropertySimple serverUrl = pluginConfig
+ .getSimple(JMXDiscoveryComponent.CONNECTOR_ADDRESS_CONFIG_PROPERTY);
+
+ connectionSettings.initializeConnectionType((ConnectionTypeDescriptor) Util.getInstance(
+ connectionTypeDescriptorClass));
+ // if not provided use the default serverUrl
+ if (null != serverUrl) {
+ connectionSettings.setServerUrl(serverUrl.getStringValue());
+ }
+
+// connectionSettings.setPrincipal(pluginConfig.getSimpleValue(PRINCIPAL_CONFIG_PROP, null));
+// connectionSettings.setCredentials(pluginConfig.getSimpleValue(CREDENTIALS_CONFIG_PROP, null));
+
+ if (connectionSettings.getAdvancedProperties() == null) {
+ connectionSettings.setAdvancedProperties(new Properties());
+ }
+
+ ConnectionFactory connectionFactory = new ConnectionFactory();
+
+
+ ConnectionProvider connectionProvider = connectionFactory.getConnectionProvider(connectionSettings);
+ this.connection = connectionProvider.connect();
+
+ this.connection.loadSynchronous(false); // this loads all the MBeans
+
+ this.consecutiveConnectionErrors = 0;
+
+ if (log.isDebugEnabled())
+ log.debug("Successfully made connection to the remote server instance");
+ } catch (Exception e) {
+
+ // The connection will be established even in the case that the principal cannot be authenticated,
+ // but the connection will not work. That failure seems to come from the call to loadSynchronous after
+ // the connection is established. If we get to this point that an exception was thrown, close any
+ // connection that was made and null it out so we can try to establish it again.
+ if (connection != null) {
+ if (log.isDebugEnabled())
+ log.debug("Connection created but an exception was thrown. Closing the connection.", e);
+ connection.close();
+ connection = null;
+ }
+
+ // Since the connection is attempted each time it's used, failure to connect could result in log
+ // file spamming. Log it once for every 10 consecutive times it's encountered.
+ if (consecutiveConnectionErrors % 10 == 0) {
+ log.warn("Could not establish connection to the Infinispan instance ["
+ + (consecutiveConnectionErrors + 1) + "] times.", e);
+ }
+
+ if (log.isDebugEnabled())
+ log.debug("Could not connect to the Infinispan instance for resource ", e);
+
+ consecutiveConnectionErrors++;
+
+ throw e;
+ }
+ }
+
+ return connection;
+ }
+
+
+ /**
+ * If necessary attempt to close the EMS connection, then set this.connection null. Synchronized ensure we play well
+ * with loadConnection.
+ */
+ public synchronized void closeConnection() {
+ if (this.connection != null) {
+ try {
+ this.connection.close();
+ } catch (Exception e) {
+ log.error("Error closing Infinispan EMS connection: " + e);
+ }
+ this.connection = null;
+ }
+ }
+
+}
Deleted: trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheComponent.java
===================================================================
--- trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheComponent.java 2009-10-07 12:59:24 UTC (rev 918)
+++ trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheComponent.java 2009-10-07 13:01:11 UTC (rev 919)
@@ -1,182 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.infinispan.jopr.infinispan;
-
-import org.infinispan.util.logging.Log;
-import org.infinispan.util.logging.LogFactory;
-import org.mc4j.ems.connection.EmsConnection;
-import org.mc4j.ems.connection.bean.EmsBean;
-import org.mc4j.ems.connection.bean.attribute.EmsAttribute;
-import org.mc4j.ems.connection.bean.operation.EmsOperation;
-import org.rhq.core.domain.configuration.Configuration;
-import org.rhq.core.domain.measurement.AvailabilityType;
-import org.rhq.core.domain.measurement.MeasurementDataNumeric;
-import org.rhq.core.domain.measurement.MeasurementReport;
-import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
-import org.rhq.core.pluginapi.inventory.ResourceComponent;
-import org.rhq.core.pluginapi.inventory.ResourceContext;
-import org.rhq.core.pluginapi.measurement.MeasurementFacet;
-import org.rhq.core.pluginapi.operation.OperationFacet;
-import org.rhq.core.pluginapi.operation.OperationResult;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * Component class for Caches within Infinispan
- *
- * @author Heiko W. Rupp
- * @author Galder Zamarreño
- */
-public class CacheComponent implements ResourceComponent<CacheManagerComponent>, MeasurementFacet, OperationFacet {
- private final Log log = LogFactory.getLog(this.getClass());
-
- /**
- * Map to match an abbreviation for the MBean to the full MBean name
- */
- private static final Map<String, String> abbrevToMBean = new HashMap<String, String>();
-
- static {
- abbrevToMBean.put("Statistics", "Statistics");
- abbrevToMBean.put("LockManager", "LockManager");
- abbrevToMBean.put("Transactions", "Transactions");
- }
-
-
- private ResourceContext<CacheManagerComponent> context;
- /**
- * The naming pattern of the current bean without the actual bean name
- */
- private String myNamePattern;
-
- /**
- * Return availability of this resource
- *
- * @see org.rhq.core.pluginapi.inventory.ResourceComponent#getAvailability()
- */
- public AvailabilityType getAvailability() {
- // TODO does a cache have a lifecycle of its own?
- return context.getParentResourceComponent().getAvailability();
- }
-
-
- /**
- * Start the resource connection
- *
- * @see org.rhq.core.pluginapi.inventory.ResourceComponent#start(org.rhq.core.pluginapi.inventory.ResourceContext)
- */
- public void start(ResourceContext<CacheManagerComponent> context) throws Exception {
-
- this.context = context;
- //
- myNamePattern = context.getResourceKey();
- myNamePattern = myNamePattern.substring(0, myNamePattern.indexOf("jmx-resource=") + 13);
-
- }
-
-
- /**
- * Tear down the rescource connection
- *
- * @see org.rhq.core.pluginapi.inventory.ResourceComponent#stop()
- */
- public void stop() {
-
-
- }
-
-
- /**
- * Gather measurement data
- *
- * @see org.rhq.core.pluginapi.measurement.MeasurementFacet#getValues(org.rhq.core.domain.measurement.MeasurementReport,
- * java.util.Set)
- */
- public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception {
-
- EmsConnection conn = getConnection();
- for (MeasurementScheduleRequest req : metrics) {
- String metric = req.getName();
- try {
- String abbrev = metric.substring(0, metric.indexOf("."));
- String mbean = abbrevToMBean.get(abbrev);
- mbean = myNamePattern + mbean;
- EmsBean bean = conn.getBean(mbean);
- bean.refreshAttributes();
- String attName = metric.substring(metric.indexOf(".") + 1);
- EmsAttribute att = bean.getAttribute(attName);
-
- // Attribute values are of various data types ...
- Object o = att.getValue();
- Class type = att.getTypeClass();
- if (type.equals(Long.class) || type.equals(long.class)) {
- Long tmp = (Long) o;
- MeasurementDataNumeric res = new MeasurementDataNumeric(req, Double.valueOf(tmp));
- report.addData(res);
- } else if (type.equals(Double.class) || type.equals(double.class)) {
- Double tmp = (Double) o;
- MeasurementDataNumeric res = new MeasurementDataNumeric(req, tmp);
- report.addData(res);
- } else if (type.equals(Integer.class) || type.equals(int.class)) {
- Integer tmp = (Integer) o;
- MeasurementDataNumeric res = new MeasurementDataNumeric(req, Double.valueOf(tmp));
- report.addData(res);
- }
- }
- catch (Exception e) {
- log.warn("getValues failed for " + metric + " : ", e);
- }
- }
- }
-
- /**
- * Invoke operations on the Cache MBean instance
- *
- * @param name Name of the operation
- * @param parameters Parameters of the Operation
- * @return OperationResult object if successful
- * @throws Exception If operation was not successful
- */
- public OperationResult invokeOperation(String name,
- Configuration parameters) throws Exception {
- EmsConnection conn = getConnection();
- String abbrev = name.substring(0, name.indexOf("."));
- String mbean = abbrevToMBean.get(abbrev);
- mbean = myNamePattern + mbean;
- EmsBean bean = conn.getBean(mbean);
- String opName = name.substring(name.indexOf(".") + 1);
- EmsOperation ops = bean.getOperation(opName);
- if (ops != null)
- ops.invoke(new Object[]{});
- else
- throw new Exception("Operation " + name + " can't be found");
-
-
- return new OperationResult();
- }
-
- private EmsConnection getConnection() {
- return context.getParentResourceComponent().getConnection();
- }
-
-}
\ No newline at end of file
Deleted: trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheDiscovery.java
===================================================================
--- trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheDiscovery.java 2009-10-07 12:59:24 UTC (rev 918)
+++ trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheDiscovery.java 2009-10-07 13:01:11 UTC (rev 919)
@@ -1,89 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.infinispan.jopr.infinispan;
-
-import org.infinispan.util.logging.Log;
-import org.infinispan.util.logging.LogFactory;
-import org.mc4j.ems.connection.EmsConnection;
-import org.mc4j.ems.connection.bean.EmsBean;
-import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
-import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent;
-import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;
-import org.rhq.plugins.jmx.ObjectNameQueryUtility;
-
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-/**
- * Discovery class for individual cache instances
- *
- * @author Heiko W. Rupp
- * @author Galder Zamarreño
- */
-public class CacheDiscovery implements ResourceDiscoveryComponent<CacheManagerComponent> {
- private static final Log log = LogFactory.getLog(CacheDiscovery.class);
-
- /**
- * Naming pattern of the cache MgmtInterceptor
- */
- private static final String CACHE_QUERY = "*:cache-name=%name%,jmx-resource=Cache";
-
- /**
- * Run the discovery
- */
- public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext<CacheManagerComponent> discoveryContext) throws Exception {
- if (log.isTraceEnabled()) log.trace("Discover resources with context: " + discoveryContext);
- Set<DiscoveredResourceDetails> discoveredResources = new HashSet<DiscoveredResourceDetails>();
-
- EmsConnection conn = discoveryContext.getParentResourceComponent().getConnection();
- if (log.isTraceEnabled()) log.trace("Connection to ems server stablished: " + conn);
-
- ObjectNameQueryUtility queryUtility = new ObjectNameQueryUtility(CACHE_QUERY);
- List<EmsBean> beans = conn.queryBeans(queryUtility.getTranslatedQuery());
- if (log.isTraceEnabled()) log.trace("Querying [" + queryUtility.getTranslatedQuery() + "] returned beans: " + beans);
-
- for (EmsBean bean : beans) {
- /**
- * A discovered resource must have a unique key, that must
- * stay the same when the resource is discovered the next
- * time
- */
- String name = bean.getBeanName().getCanonicalName();
- name = name.substring(name.indexOf("jmx-resource=") + 13);
- DiscoveredResourceDetails detail = new DiscoveredResourceDetails(
- discoveryContext.getResourceType(), // Resource Type
- bean.getBeanName().getCanonicalName(), // Resource Key
- name, // Resource name
- null, // Version
- "One cache within Infinispan", // ResourceDescription
- discoveryContext.getDefaultPluginConfiguration(), // Plugin Config
- null // ProcessInfo
- );
-
- // Add to return values
- discoveredResources.add(detail);
- log.info("Discovered new ... " + bean.getBeanName().getCanonicalName());
- }
- return discoveredResources;
- }
-}
\ No newline at end of file
Deleted: trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheManagerComponent.java
===================================================================
--- trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheManagerComponent.java 2009-10-07 12:59:24 UTC (rev 918)
+++ trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheManagerComponent.java 2009-10-07 13:01:11 UTC (rev 919)
@@ -1,120 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.infinispan.jopr.infinispan;
-
-import org.mc4j.ems.connection.EmsConnection;
-import org.mc4j.ems.connection.bean.EmsBean;
-import org.rhq.core.domain.measurement.AvailabilityType;
-import org.rhq.core.domain.measurement.MeasurementDataNumeric;
-import org.rhq.core.domain.measurement.MeasurementReport;
-import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
-import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
-import org.rhq.core.pluginapi.inventory.ResourceComponent;
-import org.rhq.core.pluginapi.inventory.ResourceContext;
-import org.rhq.core.pluginapi.measurement.MeasurementFacet;
-
-import java.util.Set;
-
-/**
- * The component class for the Infinispan manager
- *
- * @author Heiko W. Rupp
- * @author Galder Zamarreño
- */
-public class CacheManagerComponent implements ResourceComponent, MeasurementFacet {
- private ResourceContext context;
- private ConnectionHelper helper;
-
-
- /**
- * Return availability of this resource. We do this by checking the connection to it. If the Manager would expose
- * some "run state" we could check for that too.
- *
- * @see org.rhq.core.pluginapi.inventory.ResourceComponent#getAvailability()
- */
- public AvailabilityType getAvailability() {
- EmsConnection conn = getConnection();
- try {
- conn.refresh();
- EmsBean bean = conn.getBean(context.getResourceKey());
- if (bean != null)
- bean.refreshAttributes();
- return AvailabilityType.UP;
- } catch (Exception e) {
- return AvailabilityType.DOWN;
- }
- }
-
- /**
- * Start the resource connection
- *
- * @see org.rhq.core.pluginapi.inventory.ResourceComponent#start(org.rhq.core.pluginapi.inventory.ResourceContext)
- */
- public void start(ResourceContext context) throws InvalidPluginConfigurationException, Exception {
-
- this.context = context;
- helper = new ConnectionHelper();
- getConnection();
- }
-
- /**
- * Tear down the rescource connection
- *
- * @see org.rhq.core.pluginapi.inventory.ResourceComponent#stop()
- */
- public void stop() {
- helper.closeConnection();
-
- }
-
- /**
- * Gather measurement data
- *
- * @see org.rhq.core.pluginapi.measurement.MeasurementFacet#getValues(org.rhq.core.domain.measurement.MeasurementReport,
- * java.util.Set)
- */
- public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception {
-
- EmsConnection conn = getConnection();
- EmsBean bean = conn.getBean(context.getPluginConfiguration().getSimpleValue("objectName", null));
- bean.refreshAttributes();
-
- for (MeasurementScheduleRequest req : metrics) {
- // TODO check with Traits in the future - also why are the values Strings?
- String tmp = (String) bean.getAttribute(req.getName()).getValue();
- Double val = Double.valueOf(tmp);
- MeasurementDataNumeric res = new MeasurementDataNumeric(req, val);
- report.addData(res);
- }
- }
-
- /**
- * Helper to obtain a connection
- *
- * @return EmsConnection object
- */
- protected EmsConnection getConnection() {
- EmsConnection conn = helper.getEmsConnection(context.getPluginConfiguration());
- return conn;
- }
-
-}
\ No newline at end of file
Deleted: trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheManagerDiscovery.java
===================================================================
--- trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheManagerDiscovery.java 2009-10-07 12:59:24 UTC (rev 918)
+++ trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/CacheManagerDiscovery.java 2009-10-07 13:01:11 UTC (rev 919)
@@ -1,115 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.infinispan.jopr.infinispan;
-
-import static org.infinispan.jmx.CacheManagerJmxRegistration.*;
-import static org.infinispan.jmx.ComponentsJmxRegistration.*;
-import static org.infinispan.manager.DefaultCacheManager.*;
-import org.infinispan.util.logging.Log;
-import org.infinispan.util.logging.LogFactory;
-import org.mc4j.ems.connection.EmsConnection;
-import org.mc4j.ems.connection.bean.EmsBean;
-import org.rhq.core.domain.configuration.Configuration;
-import org.rhq.core.domain.configuration.PropertySimple;
-import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
-import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent;
-import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;
-import org.rhq.plugins.jmx.JMXDiscoveryComponent;
-import org.rhq.plugins.jmx.ObjectNameQueryUtility;
-
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-/**
- * Discovery class for Infinispan engines
- *
- * @author Heiko W. Rupp
- * @author Galder Zamarreño
- */
-public class CacheManagerDiscovery implements ResourceDiscoveryComponent<CacheManagerComponent> {
- private static final Log log = LogFactory.getLog(CacheManagerDiscovery.class);
-
-// // Assume a java5+ jmx-remote connector on port 6996
-// public static String REMOTE = "service:jmx:rmi://127.0.0.1/jndi/rmi://127.0.0.1:6996/jmxrmi";
-
-// public static String MANAGER_OBJECT = "*:cache-name=[global],jmx-resource=CacheManager";
-
- private static final String MANAGER_OBJECT = "*:" + CACHE_NAME_KEY + '=' + GLOBAL_JMX_GROUP + "," + JMX_RESOURCE_KEY + "=" + OBJECT_NAME;
- private static final String CONNECTOR = "org.mc4j.ems.connection.support.metadata.J2SE5ConnectionTypeDescriptor";
-
- /**
- * Run the discovery
- */
- public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext<CacheManagerComponent> discoveryContext) throws Exception {
- boolean trace = log.isTraceEnabled();
- if (trace) log.trace("Discover resources with context: " + discoveryContext);
-
- Set<DiscoveredResourceDetails> discoveredResources = new HashSet<DiscoveredResourceDetails>();
- // TODO check if we e.g. run inside a JBossAS to which we have a connection already that we can reuse.
- Configuration c = discoveryContext.getDefaultPluginConfiguration();
- c.put(c.getSimple(JMXDiscoveryComponent.CONNECTOR_ADDRESS_CONFIG_PROPERTY));
- c.put(new PropertySimple(JMXDiscoveryComponent.CONNECTION_TYPE, CONNECTOR));
- c.put(new PropertySimple("objectName", MANAGER_OBJECT));
-
-// c.put(new PropertySimple(JMXDiscoveryComponent.CONNECTOR_ADDRESS_CONFIG_PROPERTY, c.getSimple(JMXDiscoveryComponent.CONNECTOR_ADDRESS_CONFIG_PROPERTY)));
-// c.put(new PropertySimple(JMXDiscoveryComponent.CONNECTION_TYPE, connector));
-// c.put(new PropertySimple("objectName", MANAGER_OBJECT));
-
- ConnectionHelper helper = new ConnectionHelper();
- EmsConnection conn = helper.getEmsConnection(c);
-
- if (trace) log.trace("Connection to ems server stablished: " + conn);
-
- // Run query for manager_object
- ObjectNameQueryUtility queryUtility = new ObjectNameQueryUtility(MANAGER_OBJECT);
- List<EmsBean> beans = conn.queryBeans(queryUtility.getTranslatedQuery());
- if (trace) log.trace("Querying [" + queryUtility.getTranslatedQuery() + "] returned beans: " + beans);
-
- for (EmsBean bean : beans) {
-
- String managerName = bean.getBeanName().getCanonicalName();
- c.put(new PropertySimple("objectName", managerName));
- /**
- *
- * A discovered resource must have a unique key, that must
- * stay the same when the resource is discovered the next
- * time
- */
- DiscoveredResourceDetails detail = new DiscoveredResourceDetails(
- discoveryContext.getResourceType(), // ResourceType
- managerName, // Resource Key
- "Infinispan Cache Manager", // Resource Name
- null, // Version TODO can we get that from discovery ?
- "The Infinispan Manager", // Description
- c, // Plugin Config
- null // Process info from a process scan
- );
-
- // Add to return values
- discoveredResources.add(detail);
- log.info("Discovered Infinispan instance: " + managerName);
- }
- return discoveredResources;
-
- }
-}
\ No newline at end of file
Deleted: trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/ConnectionHelper.java
===================================================================
--- trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/ConnectionHelper.java 2009-10-07 12:59:24 UTC (rev 918)
+++ trunk/jopr-plugin/src/main/java/org/infinispan/jopr/infinispan/ConnectionHelper.java 2009-10-07 13:01:11 UTC (rev 919)
@@ -1,167 +0,0 @@
-/*
- * RHQ Management Platform
- * Copyright (C) 2005-2009 Red Hat, Inc.
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-package org.infinispan.jopr.infinispan;
-
-import org.infinispan.util.Util;
-import org.infinispan.util.logging.Log;
-import org.infinispan.util.logging.LogFactory;
-import org.mc4j.ems.connection.ConnectionFactory;
-import org.mc4j.ems.connection.EmsConnection;
-import org.mc4j.ems.connection.settings.ConnectionSettings;
-import org.mc4j.ems.connection.support.ConnectionProvider;
-import org.mc4j.ems.connection.support.metadata.ConnectionTypeDescriptor;
-import org.rhq.core.domain.configuration.Configuration;
-import org.rhq.core.domain.configuration.PropertySimple;
-import org.rhq.plugins.jmx.JMXDiscoveryComponent;
-
-import java.util.Properties;
-
-/**
- * Shared helper class to connect to a remote server
- *
- * @author Jay Shaughnessy
- * @author Heiko W. Rupp
- */
-public class ConnectionHelper {
-
- private final Log log = LogFactory.getLog(ConnectionHelper.class);
-
- private EmsConnection connection;
- /**
- * Controls the dampening of connection error stack traces in an attempt to control spam to the log file. Each time a
- * connection error is encountered, this will be incremented. When the connection is finally established, this will
- * be reset to zero.
- */
- private int consecutiveConnectionErrors;
-
-
- public EmsConnection getEmsConnection(Configuration config) {
- EmsConnection emsConnection = null;
-
- try {
- emsConnection = loadConnection(config);
- } catch (Exception e) {
- log.error("Component attempting to access a connection that could not be loaded");
- }
-
- return emsConnection;
- }
-
- /**
- * This is the preferred way to use a connection from within this class; methods should not access the connection
- * property directly as it may not have been instantiated if the connection could not be made. <p/> <p>If the
- * connection has already been established, return the object reference to it. If not, attempt to make a live
- * connection to the JMX server.</p> <p/> <p>If the connection could not be made in the
- * start(org.rhq.core.pluginapi.inventory.ResourceContext) method, this method will effectively try to load the
- * connection on each attempt to use it. As such, multiple threads may attempt to access the connection through this
- * means at a time. Therefore, the method has been made synchronized on instances of the class.</p> <p/> <p>If any
- * errors are encountered, this method will log the error, taking into account logic to prevent spamming the log
- * file. Calling methods should take care to not redundantly log the exception thrown by this method.</p>
- *
- * @param pluginConfig
- * @return live connection to the JMX server; this will not be <code>null</code>
- * @throws Exception if there are any issues at all connecting to the server
- */
- private synchronized EmsConnection loadConnection(Configuration pluginConfig) throws Exception {
- if (this.connection == null) {
- try {
- //Configuration pluginConfig = this.resourceContext.getPluginConfiguration();
-
- ConnectionSettings connectionSettings = new ConnectionSettings();
-
- String connectionTypeDescriptorClass = pluginConfig.getSimple(JMXDiscoveryComponent.CONNECTION_TYPE)
- .getStringValue();
- PropertySimple serverUrl = pluginConfig
- .getSimple(JMXDiscoveryComponent.CONNECTOR_ADDRESS_CONFIG_PROPERTY);
-
- connectionSettings.initializeConnectionType((ConnectionTypeDescriptor) Util.getInstance(
- connectionTypeDescriptorClass));
- // if not provided use the default serverUrl
- if (null != serverUrl) {
- connectionSettings.setServerUrl(serverUrl.getStringValue());
- }
-
-// connectionSettings.setPrincipal(pluginConfig.getSimpleValue(PRINCIPAL_CONFIG_PROP, null));
-// connectionSettings.setCredentials(pluginConfig.getSimpleValue(CREDENTIALS_CONFIG_PROP, null));
-
- if (connectionSettings.getAdvancedProperties() == null) {
- connectionSettings.setAdvancedProperties(new Properties());
- }
-
- ConnectionFactory connectionFactory = new ConnectionFactory();
-
-
- ConnectionProvider connectionProvider = connectionFactory.getConnectionProvider(connectionSettings);
- this.connection = connectionProvider.connect();
-
- this.connection.loadSynchronous(false); // this loads all the MBeans
-
- this.consecutiveConnectionErrors = 0;
-
- if (log.isDebugEnabled())
- log.debug("Successfully made connection to the remote server instance");
- } catch (Exception e) {
-
- // The connection will be established even in the case that the principal cannot be authenticated,
- // but the connection will not work. That failure seems to come from the call to loadSynchronous after
- // the connection is established. If we get to this point that an exception was thrown, close any
- // connection that was made and null it out so we can try to establish it again.
- if (connection != null) {
- if (log.isDebugEnabled())
- log.debug("Connection created but an exception was thrown. Closing the connection.", e);
- connection.close();
- connection = null;
- }
-
- // Since the connection is attempted each time it's used, failure to connect could result in log
- // file spamming. Log it once for every 10 consecutive times it's encountered.
- if (consecutiveConnectionErrors % 10 == 0) {
- log.warn("Could not establish connection to the Infinispan instance ["
- + (consecutiveConnectionErrors + 1) + "] times.", e);
- }
-
- if (log.isDebugEnabled())
- log.debug("Could not connect to the Infinispan instance for resource ", e);
-
- consecutiveConnectionErrors++;
-
- throw e;
- }
- }
-
- return connection;
- }
-
-
- /**
- * If necessary attempt to close the EMS connection, then set this.connection null. Synchronized ensure we play well
- * with loadConnection.
- */
- public synchronized void closeConnection() {
- if (this.connection != null) {
- try {
- this.connection.close();
- } catch (Exception e) {
- log.error("Error closing Infinispan EMS connection: " + e);
- }
- this.connection = null;
- }
- }
-
-}
More information about the infinispan-commits
mailing list