All Downloads are FREE. Search and download functionalities are using the official Maven repository.

patterntesting.agent.ClasspathAgent Maven / Gradle / Ivy

Go to download

PatternTesting Agent (patterntesting-agent) is the agent for patterntesting-rt for classloaders which are not supported out of the box (like e.g. the classloader of the IBM JDK). With this agent patterntesting-rt is able to get the needed infos. If you use Sun's JDK you don't need it.

There is a newer version: 1.8.0
Show newest version
/*
 * $Id: ClasspathAgent.java,v 1.7 2012/03/22 18:11:52 oboehm Exp $
 *
 * Copyright (c) 2012 by Oliver Boehm
 *
 * 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 orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * (c)reated 24.02.2012 by oliver ([email protected])
 */

package patterntesting.agent;

import java.io.*;
import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.util.Arrays;
import java.util.logging.*;

import javax.management.*;

/**
 * This is a simple Java agent to be able to watch the classpath. We neede it
 * because some classloaders (e.g. the classloader of the IBM JDK) does not
 * provide the information which classes are loaded.
 * 
 * @author oboehm
 * @since 1.2.10-YEARS (24.02.2012)
 */
public class ClasspathAgent implements ClasspathAgentMBean {

    /** The Constant MBEAN_NAME for the registered name in JMX. */
    public static final String MBEAN_NAME = "patterntesting.agent:type=ClasspathAgent";
    
    private static final Logger log = Logger.getLogger(ClasspathAgent.class.getName());
    private static final long serialVersionUID = 20120302L;
    private static final ClasspathAgent instance = new ClasspathAgent();
    private static Instrumentation instrumentation;
    private static String args;
    
    private ClasspathAgent() {
        setUpLogging();
        try {
            this.registerAsMBean();
            log.info("ClasspathAgent is ready and registered as MBean \"" + MBEAN_NAME + "\".");
        } catch (MalformedObjectNameException e) {
            log.info("ClasspathAgent is ready but not registered as MBean \"" + MBEAN_NAME
                    + "\" because of " + e);
        } catch (MBeanRegistrationException e) {
            log.info("ClasspathAgent is ready but not registered as MBean \"" + MBEAN_NAME
                    + "\" because of " + e);
        } catch (NotCompliantMBeanException e) {
            log.info("ClasspathAgent is ready but not registered as MBean \"" + MBEAN_NAME
                    + "\" because of " + e);
        }        
    }
    
    /**
     * Gets the single instance of ClasspathAgent.
     *
     * @return single instance of ClasspathAgent
     */
    public static ClasspathAgent getInstance() {
        return instance;
    }
    
    /**
     * I guess this method will be called from the VM.
     *
     * @param agentArgs the agent args
     * @param inst the inst
     */
    public static void premain(final String agentArgs, final Instrumentation inst){
        instrumentation = inst;
        args = agentArgs;
    }
 
    /**
     * Gets the instrumentation.
     *
     * @return the instrumentation
     */
    public static Instrumentation getInstrumentation() {
        return instrumentation;
    }
    
    /**
     * Get the arguments from the call as agent
     *
     * @return the args
     */
    public String getArgs() {
        return args;
    }

    /**
     * Gets the loaded classes.
     *
     * @return the loaded classes
     * @see ClasspathAgentMBean#getLoadedClasses()
     */
    public Class[] getLoadedClasses() {
        if (instrumentation == null) {
            throw new IllegalStateException("I must be started as javaagent");
        }
        return instrumentation.getAllLoadedClasses();
    }
    
    /**
     * Gets the loaded classes.
     *
     * @return the loaded classes
     * @see ClasspathAgentMBean#getLoadedClasses()
     */
    public Class[] getLoadedClasses(final ClassLoader classloader) {
        if (instrumentation == null) {
            throw new IllegalStateException("I must be started as javaagent");
        }
        return instrumentation.getInitiatedClasses(classloader);
    }
    
    
    
    /**
     * Returns the classes which were loaded by the classloader.
     * The loaded packages are returned as string array so that it can
     * be displayed by the 'jconsole'.
     *
     * @return the classnames as string array
     */
    public String[] getLoadedClassnames() {
        Class[] classes = this.getLoadedClasses();
        String[] classnames = new String[classes.length];
        for (int i = 0; i < classnames.length; i++) {
            classnames[i] = (classes[i] == null) ? "-" : classes[i].toString();
        }
        Arrays.sort(classnames);
        return classnames;
    }
    
    /**
     * Checks if is active. This is true if this class here was started as
     * Java agent.
     *
     * @return true, if started as Java agent
     */
    public boolean isActive() {
        return (instrumentation != null);
    }
    
    /**
     * We use JDK logging because we don't want a dependency to other JAR
     * files.
     */
    private static void setUpLogging() {
        if (System.getProperty("java.util.logging.config.file") == null) {
            InputStream istream = ClasspathAgent.class.getResourceAsStream("logging.properties");
            if (istream == null) {
                log.warning("Using default logging because \"logging.properties\" not found.");
                return;
            }
            try {
                LogManager.getLogManager().readConfiguration(istream);
                istream.close();
            } catch (IOException ioe) {
                log.warning("Using default logging because can't \"logging.properties\": " + ioe);
            }
        }
    }
    
    private void registerAsMBean() throws MBeanRegistrationException, NotCompliantMBeanException,
            MalformedObjectNameException {
        try {
            ObjectName mbeanName = new ObjectName(MBEAN_NAME);
            ManagementFactory.getPlatformMBeanServer().registerMBean(this, mbeanName);
        } catch (InstanceAlreadyExistsException e) {
            log.info("Registration of \"" + MBEAN_NAME + "\" ignored because of " + e);
        }
    }
    
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy