com.ritchey.sample.plugin.BrowseXMLMojo.java Source code

Java tutorial

Introduction

Here is the source code for com.ritchey.sample.plugin.BrowseXMLMojo.java

Source

package com.ritchey.sample.plugin;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * 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.
 */

import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.SAXParserFactory;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.classworlds.realm.ClassRealm;

import com.browsexml.core.XmlObject;
import com.browsexml.core.XmlParser;
import com.sun.tools.attach.VirtualMachine;

@Mojo(name = "run")
public class BrowseXMLMojo extends AbstractMojo {
    private XmlParser parser;
    private XmlObject root;

    @Parameter(property = "javaagentparameters", defaultValue = "trace")
    private String javaagentParameters;

    @Parameter(property = "file", required = true)
    private File file;

    @Parameter(property = "additionalPath", defaultValue = "src/main/resources")
    private File additionalPath;

    @Parameter(property = "javaagent")
    private File javaagent;

    @Parameter(property = "initialContext")
    private String initialContext;

    @Parameter(property = "cp", defaultValue = "${project.runtimeClasspathElements}")
    private List<String> classpathElements;

    // http://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/index.html

    public void execute() throws MojoExecutionException {
        getLog().info("file = " + file.toString());

        if (javaagent != null) {
            getLog().debug("load javaagent = " + javaagent);
            getLog().debug("javaagent parameters = " + javaagentParameters);

            loadAgent(getLog(), javaagent, javaagentParameters);
        }

        ClassRealm cr = (ClassRealm) Thread.currentThread().getContextClassLoader();

        for (String path : classpathElements) {
            getLog().info("PATH: " + path);
            try {
                cr.addURL(new File(path).toURI().toURL());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        if (additionalPath != null) {
            try {
                cr.addURL(additionalPath.toURI().toURL());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        Thread.currentThread().setContextClassLoader(cr);

        URLClassLoader urlLoader = (URLClassLoader) (Thread.currentThread().getContextClassLoader());

        for (URL url : urlLoader.getURLs()) {
            getLog().info("URL: " + url.toString());
        }

        if (initialContext != null)
            System.setProperty("java.naming.factory.initial", initialContext);
        getLog().info("initial 1 context set to " + System.getProperty("java.naming.factory.initial"));
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);

        parser = null;

        try {
            parser = new XmlParser(file.getAbsolutePath(), factory, (Map) null);
        } catch (java.net.ConnectException e) {
            e.printStackTrace();
        } catch (Exception p) {
            p.printStackTrace();
        }

        root = parser.getRoot();
        parser.execute();

    }

    public static void loadAgent(Log log, File javaagent, String parameters) {
        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
        String nameOfRunningVM = runtime.getName();
        log.info("dynamically loading javaagent   name: " + runtime.getName() + "  vm version = "
                + runtime.getVmVersion());

        int p = nameOfRunningVM.indexOf('@');
        String pid = nameOfRunningVM.substring(0, p);

        try {
            VirtualMachine vm = VirtualMachine.attach(pid);

            vm.loadAgent(javaagent.getAbsolutePath(), parameters);
            vm.detach();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}