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

com.github.mrstampy.poisonivy.IvyLibraryRetriever Maven / Gradle / Ivy

There is a newer version: 1.4
Show newest version
/*
 * Poison Ivy - Java Library Dependency Resolver and Application Launcher 
 *
 * Copyright (C) 2014 Burton Alexander
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * 
 */
package com.github.mrstampy.poisonivy;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class uses  0) return true;
				}
				return false;
			}
		});

		for (File del : sourcesAndJavadoc) {
			deleteFile(del);
		}
	}

	private void logError(final InputStream in) {
		Thread thread = new Thread("Resolver error stream thread") {
			public void run() {
				try {
					while(resolving) {
						Thread.sleep(200);
						String error = getOutput(in);
						if (error != null) System.err.print(error);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
		
		thread.start();
	}

	private void logOutput(final InputStream in) {
		Thread thread = new Thread("Resolver error stream thread") {
			public void run() {
				try {
					while(resolving) {
						Thread.sleep(200);
						String out = getOutput(in);
						if (out != null) System.out.print(out);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
		
		thread.start();
	}

	private String getOutput(InputStream in) throws IOException {
		int available = in.available();
		if (available <= 0) return null;

		byte[] b = new byte[available];
		in.read(b);

		return new String(b);
	}

	private String[] createCommand(String ivyfile, String ivysettings) {
		List command = new ArrayList();

		command.add("java");
		command.add("-cp");
		command.add(getClasspath());
		command.add("org.apache.ivy.Main");
		command.add("-ivy");
		command.add(ivyfile);

		if (ivysettings != null) {
			command.add("-settings");
			command.add(ivysettings);
		}

		command.add("-retrieve");
		command.add(getResolvePattern());

		return command.toArray(new String[] {});
	}

	private File getFile(String fileName, String name) throws FileNotFoundException {
		File file = new File(fileName);

		if (file == null || !file.exists()) {
			log.error("No {} file found for {}", name, fileName);
			throw new FileNotFoundException(fileName);
		}

		return file;
	}

	/**
	 * Returns true if the {@link #getLibdir()} exists and is not empty.
	 * 
	 * @return true, if successful
	 */
	public boolean librariesRetrieved() {
		File lib = new File(getLibdir());
		if (!lib.exists()) return false;
		if (!lib.isDirectory()) return false;

		String[] libs = lib.list();

		log.debug("Libraries in {}", getLibdir());
		for (String s : libs) {
			log.debug("**** Library: {}", s);
		}

		return libs != null && libs.length > 0;
	}

	/**
	 * Gets the classpath used to start the currently running Java process.
	 * 
	 * @return the classpath
	 */
	public static String getClasspath() {
		return System.getProperty("java.class.path");
	}

	/**
	 * If true any source and javadoc jars are deleted after library retrieval.
	 * 
	 * @return true, if is clean sources and javadoc
	 */
	public boolean isCleanSourcesAndJavadoc() {
		return cleanSourcesAndJavadoc;
	}

	/**
	 * If true any source and javadoc jars are deleted after library retrieval.
	 * 
	 * @param cleanSourcesAndJavadoc
	 *          the new clean sources and javadoc
	 */
	public void setCleanSourcesAndJavadoc(boolean cleanSourcesAndJavadoc) {
		this.cleanSourcesAndJavadoc = cleanSourcesAndJavadoc;
	}

	/**
	 * Gets the libdir, default {@value #LIBRARIES_DIR}.
	 * 
	 * @return the libdir
	 */
	public String getLibdir() {
		return libdir == null ? LIBRARIES_DIR : libdir;
	}

	/**
	 * Sets the libdir, default {@value #LIBRARIES_DIR}.
	 * 
	 * @param libdir
	 *          the new libdir
	 */
	public void setLibdir(String libdir) {
		this.libdir = libdir;
	}

	/**
	 * Gets the resolve pattern including the {@link #getLibdir()}, default
	 * {@value #LIBRARIES_DIR}/{@value #RESOLVE_PATTERN}.
	 * 
	 * @return the resolve pattern
	 */
	public String getResolvePattern() {
		return resolvePattern == null ? getLibdir() + File.separator + RESOLVE_PATTERN : resolvePattern;
	}

	/**
	 * Sets the resolve pattern, default {@value #RESOLVE_PATTERN}.
	 * 
	 * @param resolvePattern
	 *          the new resolve pattern
	 */
	public void setResolvePattern(String resolvePattern) {
		this.resolvePattern = resolvePattern;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy