org.broadleafcommerce.common.extensibility.InstrumentationRuntimeFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of broadleaf-common Show documentation
Show all versions of broadleaf-common Show documentation
A collection of classes shared by broadleaf profile, cms, admin, and core.
/*
* Copyright 2008-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.broadleafcommerce.common.extensibility;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessController;
import java.security.CodeSource;
import java.security.PrivilegedAction;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* This class is based on the OpenJPA's org.apache.openjpa.enhance.InstrumentationFactory. It essentially does
* its best to install an instrumentation agent. The preferred or prescribed way to install an instrumentation agent
* is to add the agent as an argument on the command line when starting the JVM. This class attempts to do the
* same thing after the JVM has already started. Unfortunately, this is the only way we know of to attach an agent to
* the JVM except by adding a "javaagent:..." flag on the command line.
*
* @author Kelly Tisdell
* @deprecated Because of classloader differences, this approach is not reliable for some containers. Use the javaagent jvm argument instead to set instrumentation.
*/
@Deprecated
public class InstrumentationRuntimeFactory {
private static final Log LOG = LogFactory.getLog(InstrumentationRuntimeFactory.class);
private static final String IBM_VM_CLASS = "com.ibm.tools.attach.VirtualMachine";
private static final String SUN_VM_CLASS = "com.sun.tools.attach.VirtualMachine";
private static boolean isIBM = false;
private static Instrumentation inst;
/**
* This method is called by the JVM to set the instrumentation. We can't synchronize this because it will cause
* a deadlock with the thread calling the getInstrumentation() method when the instrumentation is installed.
*
* @param agentArgs
* @param instrumentation
*/
public static void agentmain(String agentArgs, Instrumentation instrumentation) {
inst = instrumentation;
}
/**
* This method returns the Instrumentation object provided by the JVM. If the Instrumentation object is null,
* it does its best to add an instrumentation agent to the JVM and then the instrumentation object.
* @return Instrumentation
*/
public static synchronized Instrumentation getInstrumentation() {
if (inst != null) {
return inst;
}
if (System.getProperty("java.vendor").toUpperCase().contains("IBM")) {
isIBM = true;
}
AccessController.doPrivileged(new PrivilegedAction
© 2015 - 2024 Weber Informatics LLC | Privacy Policy