org.codehaus.mojo.chronos.jmeter.DependencyUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.codehaus.mojo.chronos.jmeter.DependencyUtil.java

Source

/*
 * Chronos - Copyright (C) 2011 National Board of e-Health (NSI), Denmark (http://www.nsi.dk)
 *
 * All source code and information supplied as part of Chronos is
 * copyright to National Board of e-Health.
 *
 * The source code has been released under a dual license - meaning you can
 * use either licensed version of the library with your code.
 *
 * It is released under the LGPL (GNU Lesser General Public License), either
 * version 2.1 of the License, or (at your option) any later version. A copy
 * of which can be found at the link below.
 * http://www.gnu.org/copyleft/lesser.html
 *
 * $HeadURL$
 * $Id$
 */
package org.codehaus.mojo.chronos.jmeter;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.jfree.io.IOUtils;

/**
 * Manages dependencies for the current maven project (making them available for jmeter).
 * 
 * @author ksr@lakeside.dk
 */
public final class DependencyUtil {
    private String jmeterhome;

    private Log log;

    public DependencyUtil(String jmeterHome, Log log) {
        this.jmeterhome = jmeterHome;
        this.log = log;
    }

    public List getDependencies(MavenProject project) {
        List result = new ArrayList();

        Iterator it = project.getAttachedArtifacts().iterator();
        while (it.hasNext()) {
            Artifact artifact = (Artifact) it.next();
            File attachedArtifactFile = artifact.getFile();
            result.add(attachedArtifactFile);
        }
        File artifactFile = project.getArtifact().getFile();
        if (artifactFile == null) {
            log.warn("Artifact not found. Note that if Your JMeter test contains JUnittestcases, "
                    + "You can only invoke this goal through the default lifecycle.");
        } else {
            result.add(artifactFile);
        }
        Set dependencyArtifacts = project.getArtifacts();
        if (dependencyArtifacts != null) {
            Iterator deps = dependencyArtifacts.iterator();
            while (deps.hasNext()) {
                Artifact dependency = (Artifact) deps.next();
                result.add(dependency.getFile());
            }
        }
        return result;
    }

    List copyDependencies(MavenProject project) throws IOException {
        final List copied = new ArrayList();
        Iterator it = getDependencies(project).iterator();
        while (it.hasNext()) {
            File artifactFile = (File) it.next();
            copyFileToDir(artifactFile, copied);
        }
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                cleanUpDependencies(copied);
            }
        });
        return copied;
    }

    void cleanUpDependencies(List copied) {
        for (Iterator iterator = copied.iterator(); iterator.hasNext();) {
            File file = (File) iterator.next();
            if (file.exists()) {
                file.delete();
            }
        }
    }

    void copyFileToDir(File file, List copied) throws IOException {
        File lib = new File(jmeterhome, "lib");
        File junitdir = new File(lib, "junit");
        File target = new File(junitdir, file.getName());

        /* Merge from Atlassion */
        int i = 0;
        while (target.exists()) {
            target = new File(junitdir, String.valueOf(i) + "-" + file.getName());
            i++;
        }
        /* End */

        System.out.println(target.getAbsolutePath());
        target.createNewFile();
        InputStream input = new BufferedInputStream(new FileInputStream(file));
        OutputStream output = new BufferedOutputStream(new FileOutputStream(target));
        IOUtils.getInstance().copyStreams(input, output);
        output.close();
        input.close();
        log.debug("Dependency copied to jmeter distribution at: " + target);
        copied.add(target);
    }
}