rempl-maven-plugin-1.1.3.src.main.java.com.rempl.maven.MavenLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rempl-maven-plugin Show documentation
Show all versions of rempl-maven-plugin Show documentation
Reverse Engineering Meta Programming Library (REMPL) that
enables manipulations with source code meta constructs,
like classes, methods, files, packages, etc. in runtime.
/**
* Copyright (c) 2011, REMPL.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3) Neither the name of the REMPL.com nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.rempl.maven;
// Bridge to Aether from com.rempl:rempl-aether
import com.rempl.aether.AbstractAetherLoader;
import com.rempl.aether.Resolver;
// REMPL API from com.rempl:rempl-api
import com.rempl.api.LoadingException;
import com.rempl.api.ROM;
import com.ymock.util.Logger;
// JDK
import java.io.File;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
// REMPL API from com.rempl:rempl-api
// JDK
// for file and string manipulations
/**
* Loader of dependencies in Maven.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id: MavenLoader.java 1648 2011-07-14 22:18:06Z guard $
*/
public final class MavenLoader extends AbstractAetherLoader {
/**
* Used to look up Artifacts in the remote repository.
* @see MavenLoader(MavenResolver)
*/
private final MavenResolver resolver;
/**
* Custom class loader.
*/
private final MavenClassLoader loader;
/**
* Privileged creator of loader.
*/
private static final class Runner
implements PrivilegedAction {
/**
* {@inheritDoc}
*/
@Override
public MavenClassLoader run() {
return new MavenClassLoader(
Thread.currentThread().getContextClassLoader()
);
}
}
/**
* Public ctor.
* @param res The resolver
*/
public MavenLoader(final MavenResolver res) {
super();
this.resolver = res;
// @see http://download.oracle.com/javase/1.4.2/docs/guide/security/doprivileged.html
this.loader = AccessController.doPrivileged(new Runner());
Thread.currentThread().setContextClassLoader(this.loader);
}
/**
* {@inheritDoc}
*/
@Override
protected Resolver getResolver() {
return this.resolver;
}
/**
* {@inheritDoc}
*/
@Override
protected void addToLoader(final URL[] urls) {
this.loader.addURLs(urls);
}
/**
* {@inheritDoc}
*/
@Override
protected String rom(final Collection jars)
throws LoadingException {
String xml = null;
for (File jar : jars) {
if (jar.isDirectory()) {
xml = this.inDirectory(jar);
} else {
xml = this.readRomFromJar(jar);
}
if (xml != null) {
break;
}
}
if (xml == null) {
throw new LoadingException(
"ROM.xml is not found in "
+ StringUtils.join(jars, ";")
);
}
return xml;
}
/**
* Load ROM.xml content from the directory.
* @param dir The directory to look into
* @return Content of ROM.xml found, or NULL if nothing found in the dir
* @throws LoadingException If anything goes wrong
* @see #rom(Collection)
*/
private String inDirectory(final File dir) throws LoadingException {
String xml = null;
// This is a directory with files, already unpacked
// by Maven from JAR
final File romFile = new File(dir, ROM.PATH);
if (romFile.exists()) {
try {
xml = FileUtils.readFileToString(romFile);
} catch (java.io.IOException ex) {
throw new LoadingException(ex);
}
Logger.debug(
this,
"#inDirectory(%s): rom.xml found (%d bytes)",
dir,
xml.length()
);
}
return xml;
}
}