package net.sourceforge.javautil.classloader.resolver.impl.maven;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.javautil.classloader.resolver.IClassArtifactReference;
import net.sourceforge.javautil.classloader.resolver.IClassPackage;
import net.sourceforge.javautil.classloader.resolver.IClassPackageDescriptor;
import net.sourceforge.javautil.classloader.resolver.IClassPackageReference;
import net.sourceforge.javautil.classloader.resolver.IClassPackage.IVersion;
import net.sourceforge.javautil.classloader.resolver.IClassPackageRepositoryLocal.RepositoryType;
import net.sourceforge.javautil.classloader.resolver.IClassPackageResolver;
import net.sourceforge.javautil.classloader.resolver.impl.ClassPackageImpl;
import net.sourceforge.javautil.classloader.resolver.impl.ClassPackageRepositoryLocalAbstract;
import net.sourceforge.javautil.classloader.source.ClassSource;
import net.sourceforge.javautil.classloader.source.ClassSourceUtil;
import net.sourceforge.javautil.common.VirtualArtifactUtil;
import net.sourceforge.javautil.common.classloader.ClassLoaderResource;
import net.sourceforge.javautil.common.classloader.ClassLoaderScanner;
import net.sourceforge.javautil.common.io.IVirtualFile;
import net.sourceforge.javautil.common.io.impl.ZippedDirectory;
import net.sourceforge.javautil.common.xml.XMLDocument;
/**
* This represents a Maven repository that resolves packages via the current class loader.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class MavenRepositoryClassLoader extends ClassPackageRepositoryLocalAbstract {
@Override protected boolean refreshRoot() { return false; }
@Override protected ClassSource resolve(String type, IClassPackageResolver resolver, IClassPackageReference reference) {
if (!"main".equalsIgnoreCase(type)) return null;
ClassLoaderResource rsc = this.getResource(reference);
return rsc == null ? null : ClassSourceUtil.getFor(rsc.getArchive().getURL());
}
@Override protected IClassPackageDescriptor resolveDescriptor(IClassPackageResolver resolver, IClassPackageReference reference) {
IClassPackageDescriptor descriptor = this.getDescriptor((IClassArtifactReference)reference);
return descriptor != null && descriptor.getVersion().compareTo(reference.getVersion()) == 0 ? descriptor : null;
}
public RepositoryType getRepositoryType() { return RepositoryType.Packaged; }
public boolean contains(IClassPackageResolver resolver, IClassArtifactReference reference) { return this.getResource(reference) != null; }
public boolean contains(IClassPackageResolver resolver, IClassPackageReference reference) {
return this.getVersionsAvailable(reference).contains(reference.getVersion());
}
public List<IVersion> getVersionsAvailable(IClassArtifactReference reference) {
List<IVersion> versions = new ArrayList<IVersion>();
IClassPackageDescriptor descriptor = this.getDescriptor(reference);
if (descriptor != null) versions.add(descriptor.getVersion());
return versions;
}
protected ClassLoaderResource getResource (IClassArtifactReference reference) {
return ClassLoaderScanner.getResource("META-INF/" + reference.getGroupId() + "/" + reference.getArtifactId() + "/pom.xml");
}
protected IClassPackageDescriptor getDescriptor (IClassArtifactReference reference) {
ClassLoaderResource rsc = this.getResource(reference);
return rsc == null ? null : XMLDocument.read(rsc.getFile(), ProjectObjectModel.class);
}
}