package net.sourceforge.javautil.classloader.resolver.impl.maven;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import net.sourceforge.javautil.classloader.boot.EntryPoint;
import net.sourceforge.javautil.classloader.boot.EntryPointClassPackage;
import net.sourceforge.javautil.classloader.boot.IEntryPointType;
import net.sourceforge.javautil.classloader.impl.ClassContext;
import net.sourceforge.javautil.classloader.impl.StandardClassLoaderHeiarchy;
import net.sourceforge.javautil.classloader.impl.maven.MavenEntryPoint;
import net.sourceforge.javautil.classloader.resolver.IClassPackage;
import net.sourceforge.javautil.classloader.resolver.ClassPackageContext;
import net.sourceforge.javautil.classloader.resolver.IClassPackageDependencyReference;
import net.sourceforge.javautil.classloader.resolver.IClassPackageReference;
import net.sourceforge.javautil.classloader.resolver.IClassPackageResolver;
import net.sourceforge.javautil.classloader.resolver.IClassDependencyPool;
import net.sourceforge.javautil.classloader.resolver.IClassDependencyPool.PoolScope;
import net.sourceforge.javautil.classloader.resolver.impl.ClassPackageReferenceImpl;
import net.sourceforge.javautil.classloader.resolver.impl.ClassPackageResolverImpl;
import net.sourceforge.javautil.classloader.resolver.impl.ClassDependencyPoolImpl;
import net.sourceforge.javautil.classloader.source.ClassSource;
import net.sourceforge.javautil.classloader.source.CompositeClassSource;
import net.sourceforge.javautil.classloader.source.VirtualDirectoryClassSource;
import net.sourceforge.javautil.classloader.util.ClassPackageUtil;
import net.sourceforge.javautil.common.ChecksumUtil;
import net.sourceforge.javautil.common.CollectionUtil;
import net.sourceforge.javautil.common.IOUtil;
import net.sourceforge.javautil.common.exception.ThrowableManagerRegistry;
import net.sourceforge.javautil.common.io.VirtualArtifactNotFoundException;
import net.sourceforge.javautil.common.io.IVirtualDirectory;
import net.sourceforge.javautil.common.io.IVirtualFile;
import net.sourceforge.javautil.common.io.impl.SimplePath;
import net.sourceforge.javautil.common.io.impl.SystemDirectory;
/**
* Utilities for getting and resolving maven packages.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: MavenRepositoryUtil.java 2727 2011-01-30 06:48:00Z ponderator $
*/
public class MavenRepositoryUtil {
/**
* @param usePackageContext True if any {@link MavenEntryPoint}'s should be detected and used for dependency resolution, otherwise false
* @return A standard resolver setup, possibly integrated with any {@link MavenEntryPoint} that may have been used
*/
public static IClassPackageResolver createStandardRepositorySetup (boolean usePackageContext) {
if (usePackageContext && ClassPackageContext.getPackageResolver() != null) {
return ClassPackageContext.getPackageResolver();
} else {
return createStandardResolver();
}
}
/**
* @return A standard group of resolvers
*/
public static IClassPackageResolver createStandardResolver () {
MavenRepositoryLocalPackage local = new MavenRepositoryLocalPackage();
ClassPackageResolverImpl resolver = new ClassPackageResolverImpl(local);
if (!MavenLocalSettings.getDefaultInstance().isOffline()) {
resolver.addRemoteRepository( MavenRepositoryRemote.createDefault() );
List<MavenRepositoryRemote> localsetup = MavenLocalSettings.getDefaultInstance().createReleaseList();
for (MavenRepositoryRemote remote : localsetup) {
resolver.addRemoteRepository( remote );
}
}
return resolver;
}
public static void addProjectMainTarget (CompositeClassSource ccs, ProjectObjectModel model, IVirtualDirectory root) {
try {
IVirtualDirectory dir = root.getDirectory( new SimplePath(model.getCompileDirectoryOrDefault()) );
ccs.add(new VirtualDirectoryClassSource(dir));
} catch (VirtualArtifactNotFoundException e) {
}
}
public static void addProjectTestTarget (CompositeClassSource ccs, ProjectObjectModel model, IVirtualDirectory root) {
try{
IVirtualDirectory dir = root.getDirectory( new SimplePath(model.getCompileTestsDirectoryOrDefault()) );
ccs.add(new VirtualDirectoryClassSource(dir));
} catch (VirtualArtifactNotFoundException e) {
}
}
public static IClassDependencyPool createFrom (IClassPackageResolver local, ProjectObjectModel model, Collection<? extends IClassPackageDependencyReference> refs) {
return createFrom(local, model, refs.toArray(new IClassPackageDependencyReference[refs.size()]));
}
/**
* @param local The resolver to use for dependency resolution
* @param model The pom.xml model
* @param dependencies The dependencies to add in addition to the model
*
* @return A dependency pool containing all the recursive dependencies that do not conflict with already
* loaded {@link IClassPackage}'s in the parent {@link #pool}.
*/
public static IClassDependencyPool createFrom (IClassPackageResolver local, ProjectObjectModel model, IClassPackageDependencyReference... dependencies) {
IClassDependencyPool pool = ClassPackageUtil.createStandardDependencyPool("Maven Pool: " + model, true, PoolScope.Root);
List<? extends IClassPackageReference> modeDependencies = model.getDependencies();
ClassPackageUtil.append(pool, local, true, model,
CollectionUtil.insert(dependencies, 0, (Object[]) modeDependencies.toArray(new IClassPackageReference[modeDependencies.size()]))
);
return pool;
}
}