Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.rhq.plugins.byteman;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.byteman.agent.submit.Submit;
import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.domain.configuration.ConfigurationUpdateStatus;
import org.rhq.core.domain.configuration.Property;
import org.rhq.core.domain.configuration.PropertyList;
import org.rhq.core.domain.configuration.PropertyMap;
import org.rhq.core.domain.configuration.PropertySimple;
import org.rhq.core.domain.content.PackageDetailsKey;
import org.rhq.core.domain.content.PackageType;
import org.rhq.core.domain.content.transfer.ContentResponseResult;
import org.rhq.core.domain.content.transfer.DeployIndividualPackageResponse;
import org.rhq.core.domain.content.transfer.DeployPackageStep;
import org.rhq.core.domain.content.transfer.DeployPackagesResponse;
import org.rhq.core.domain.content.transfer.RemoveIndividualPackageResponse;
import org.rhq.core.domain.content.transfer.RemovePackagesResponse;
import org.rhq.core.domain.content.transfer.ResourcePackageDetails;
import org.rhq.core.domain.measurement.AvailabilityType;
import org.rhq.core.domain.measurement.MeasurementDataNumeric;
import org.rhq.core.domain.measurement.MeasurementDataTrait;
import org.rhq.core.domain.measurement.MeasurementReport;
import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
import org.rhq.core.domain.resource.CreateResourceStatus;
import org.rhq.core.domain.resource.ResourceType;
import org.rhq.core.pluginapi.configuration.ConfigurationFacet;
import org.rhq.core.pluginapi.configuration.ConfigurationUpdateReport;
import org.rhq.core.pluginapi.content.ContentContext;
import org.rhq.core.pluginapi.content.ContentFacet;
import org.rhq.core.pluginapi.content.ContentServices;
import org.rhq.core.pluginapi.inventory.CreateChildResourceFacet;
import org.rhq.core.pluginapi.inventory.CreateResourceReport;
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 org.rhq.core.util.MessageDigestGenerator;
import org.rhq.core.util.exception.ThrowableUtil;
/**
* Component that represents the remote Byteman agent listening for requests.
*
* A note about adding boot/system classpath jars and the content this component supports related to that feature.
* There are operations this component supports to add jars to the byteman classpath. Those operations tell the
* byteman agent to add jars, but those jars must already exist and be accessible for the byteman agent to do so.
* This component will not manage the jars added via the operations. If, however, a user pushes jar content
* from the RHQ server to this plugin via the content facet, those jars will be managed by this component as they
* are added to the byteman agent.
*
* @author John Mazzitelli
*/
public class BytemanAgentComponent implements ResourceComponent, MeasurementFacet,
OperationFacet, ContentFacet, CreateChildResourceFacet, ConfigurationFacet {
private static final String PKG_TYPE_NAME_BOOT_JAR = "bootJar";
private static final String PKG_TYPE_NAME_SYSTEM_JAR = "systemJar";
private final Log log = LogFactory.getLog(BytemanAgentComponent.class);
private ResourceContext resourceContext;
private Submit bytemanClient;
private File bootJarsDataDir; // where managed boot jars will be persisted
private File systemJarsDataDir; // where managed system jars will be persisted
private File scriptsDataDir; // where managed scripts will be persisted
private Map allKnownScripts; // cached copy of currently known scripts
/**
* Start the management component. This will immediately attempt to add previously
* deployed classpath jars, if it is found that the remote Byteman agent no longer
* has those jars in its classpath.
*
* @see ResourceComponent#start(ResourceContext)
*/
public void start(ResourceContext context) {
this.resourceContext = context;
this.bootJarsDataDir = getResourceDataDirectory("boot");
this.systemJarsDataDir = getResourceDataDirectory("system");
this.scriptsDataDir = getResourceDataDirectory("script");
getBytemanClient(); // creates its client now
// now that we are starting to manage the byteman agent, make sure the managed classpath jars are added
try {
addDeployedClasspathJars();
} catch (Throwable t) {
log.warn("Failed to add managed classpath jars to the byteman agent - is it up?", t);
}
getAvailability(); // forces the scripts cache to load
return;
}
/**
* Called when the resource component will no longer manage the remote Byteman agent.
* This method will clean up the resource component.
*
* @see ResourceComponent#stop()
*/
public void stop() {
this.resourceContext = null;
this.bytemanClient = null;
this.allKnownScripts = null;
}
/**
* Determines if the Byteman agent is up by asking it for the current list of all scripts and their rules.
*
* @see AvailabilityFacet#getAvailability()
*/
public AvailabilityType getAvailability() {
try {
this.allKnownScripts = getBytemanClient().getAllRules();
return AvailabilityType.UP;
} catch (Exception e) {
this.allKnownScripts = null;
return AvailabilityType.DOWN;
}
}
/**
* The plugin container will call this method when metrics are to be collected.
*
* @see MeasurementFacet#getValues(MeasurementReport, Set)
*/
public void getValues(MeasurementReport report, Set requests) {
Submit client = getBytemanClient();
// a cache so we don't ask the byteman agent more than once for this while we are in here
// we don't rely on this.allKnownScripts because we want the latest-n-greatest value for our metrics
Map allScripts = null;
for (MeasurementScheduleRequest request : requests) {
String name = request.getName();
try {
if (name.equals("TRAIT-clientVersion")) {
String clientVersion = client.getClientVersion();
report.addData(new MeasurementDataTrait(request, clientVersion));
} else if (name.equals("totalNumberOfScripts")) {
int total = 0;
if (allScripts == null) {
allScripts = client.getAllRules();
}
if (allScripts != null) {
total += allScripts.size();
}
report.addData(new MeasurementDataNumeric(request, Double.valueOf(total)));
} else if (name.equals("totalNumberOfRules")) {
int total = 0;
if (allScripts == null) {
allScripts = client.getAllRules();
}
if (allScripts != null) {
for (String script : allScripts.values()) {
total += client.splitAllRulesFromScript(script).size();
}
}
report.addData(new MeasurementDataNumeric(request, Double.valueOf(total)));
} else if (name.equals("totalNumberOfBootJars")) {
int total = 0;
List loadedJars = client.getLoadedBootClassloaderJars();
if (loadedJars != null) {
total = loadedJars.size();
}
report.addData(new MeasurementDataNumeric(request, Double.valueOf(total)));
} else if (name.equals("totalNumberOfSystemJars")) {
int total = 0;
List loadedJars = client.getLoadedSystemClassloaderJars();
if (loadedJars != null) {
total = loadedJars.size();
}
report.addData(new MeasurementDataNumeric(request, Double.valueOf(total)));
} else {
throw new Exception("cannot collect unknown metric");
}
} catch (Exception e) {
log.error("Failed to obtain measurement [" + name + "]. Cause: " + e);
}
}
return;
}
public Configuration loadResourceConfiguration() throws Exception {
Properties props = getBytemanClient().listSystemProperties();
Configuration config = new Configuration();
PropertyList list = new PropertyList("bytemanSystemProperties");
for (Map.Entry