Package alt.jiapi.jazzpect

Source Code of alt.jiapi.jazzpect.Initializer

package alt.jiapi.jazzpect;

import org.apache.log4j.Category;

import org.aopalliance.intercept.MethodInterceptor;

import alt.jiapi.InstrumentationContext;
import alt.jiapi.InstrumentationDescriptor;
import alt.jiapi.JiapiException;
import alt.jiapi.interceptor.InvocationInterceptor;
import alt.jiapi.util.Bootstrapper;
import alt.jiapi.util.InstrumentingClassLoader;

import alt.jiapi.jazzpect.interceptor.MInterceptor;

/**
* Class Initializer.
*
* @author Mika Riekkinen
*/
public class Initializer {
    private static Category log = Category.getInstance(Initializer.class);

    private InstrumentationContext ctx;
    private ClassLoader classLoader;

    public Initializer(MethodInterceptor mi) throws JiapiException {
        this(new String[] {"*"},
             new String[] {"java.*", "org.*", "javax.*"},
             "*", mi);
    }

    public Initializer(String inclusionRule, MethodInterceptor mi) throws JiapiException {
        this(new String[] {inclusionRule}, null, "*", mi);
    }

    public Initializer(String[] inclusionRules, MethodInterceptor mi) throws JiapiException {
        this(inclusionRules, null, "*", mi);
    }

    public Initializer(String[] inclusionRules, String[] exclusionRules,
                       String resolution, MethodInterceptor mi) throws JiapiException {
        this.ctx = new InstrumentationContext();
        InstrumentationDescriptor id = new InstrumentationDescriptor();
        ctx.addInstrumentationDescriptor(id);

        if (inclusionRules != null) {
            for (int i = 0; i < inclusionRules.length; i++) {
                if (inclusionRules[i] != null) {
                    id.addInclusionRule(inclusionRules[i]);
                }
                else {
                    log.warn("Skipping null inclusion rule");
                }
            }
        }

        if (exclusionRules != null) {
            for (int i = 0; i < exclusionRules.length; i++) {
                if (exclusionRules[i] != null) {
                    id.addExclusionRule(exclusionRules[i]);
                }
                else {
                    log.warn("Skipping null exclusion rule");
                }
            }
        }
       
        if (resolution == null) {
            log.warn("Null resolution, no invocation is trapped");
            resolution = ""; // Instrument nothing(but dummy "<clinit>")
        }

        // Associate interceptor with descriptor
        InvocationInterceptor ii =
            new InvocationInterceptor(id, resolution, new MInterceptor(mi));

        this.classLoader = InstrumentingClassLoader.createClassLoader(ctx);
    }


    /**
     * Gets the context, that specifies which classes are to be instrumented.
     */
    public InstrumentationContext getContext() {
        return ctx;
    }


    /**
     * Gets a ClassLoader, that instruments classes for interception
     * according to rules given.
     *
     * @return a ClassLoader;
     */
    public ClassLoader getClassLoader() {
        return classLoader;
    }


    /**
     * Runs a main method of given className.
     * Instrumentation is done according to rules given.
     */
    public void runMainMethod(String className, String[] args) {
        Bootstrapper.launch(className, args, getContext(), getClassLoader());
    }
}
TOP

Related Classes of alt.jiapi.jazzpect.Initializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.