Package net.sourceforge.javautil.classloader.resolver.impl.maven

Source Code of net.sourceforge.javautil.classloader.resolver.impl.maven.MavenRepositoryLocalProject

package net.sourceforge.javautil.classloader.resolver.impl.maven;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.ClassPackageResolverContext;
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.ClassPackageReferenceImpl;
import net.sourceforge.javautil.classloader.resolver.impl.ClassPackageRepositoryLocalAbstract;
import net.sourceforge.javautil.classloader.source.ClassSource;
import net.sourceforge.javautil.classloader.source.VirtualDirectoryClassSource;
import net.sourceforge.javautil.common.FileUtil;
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.logging.ILogger;
import net.sourceforge.javautil.common.logging.LoggingContext;

/**
* A local maven repository of projects that are not in normal maven layout
* and do not necessarily contain packaged jars.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: MavenRepositoryLocalProject.java 2745 2011-02-05 04:23:36Z ponderator $
*/
public class MavenRepositoryLocalProject extends ClassPackageRepositoryLocalAbstract {
 
  protected static final ILogger log = LoggingContext.getLogger(MavenRepositoryLocalProject.class);
 
  protected long modified = -1;
  protected Map<String, ProjectSetup> projectRoots = new HashMap<String, ProjectSetup>();

  protected final IVirtualDirectory directory;
  protected long lastModified;
 
  public MavenRepositoryLocalProject(IVirtualDirectory directory) {
    this.directory = directory;
  }

  public RepositoryType getRepositoryType() {
    return RepositoryType.Development;
  }

  @Override protected boolean refreshRoot() {
    if (this.directory.getLastModified() > this.lastModified) {
      this.lastModified = this.directory.getLastModified();
      return true;
    }
    return false;
  }

  public List<IVersion> getVersionsAvailable(IClassArtifactReference reference) {
    List<IVersion> versions = new ArrayList<IVersion>();
   
    String id = this.createUniqueId(reference);
    for (String key : projectRoots.keySet()) {
      if (key.startsWith(id)) {
        versions.add( projectRoots.get(key).model.getVersion() );
      }
    }
   
    return versions;
  }

  public boolean contains(IClassPackageResolver resolver, IClassArtifactReference reference) {
    this.scan(resolver); return this.projectRoots.containsKey(reference);
  }

  public boolean contains(IClassPackageResolver resolver, IClassPackageReference reference) {
    this.scan(resolver); return this.projectRoots.containsKey(reference);
  }

  @Override public IClassPackageDescriptor resolveDescriptor(IClassPackageResolver resolver, IClassPackageReference reference) {
    ProjectSetup setup = this.getProjectSetup(resolver, reference);
    return setup == null ? null : setup.model;
  }

  public ClassSource resolve (String type, IClassPackageResolver resolver, IClassPackageReference reference) {
    ProjectSetup setup = this.getProjectSetup(resolver, reference);
    if (setup == null) return null;
   
    String target = null;
    if ("main".equalsIgnoreCase(type)) {
      target = setup.model.getCompileDirectoryOrDefault();
    } else if ("test".equalsIgnoreCase(type)) {
      target = setup.model.getCompileTestsDirectoryOrDefault();
    } else {
      return null;
    }
   
    try {
      return this.getClassSource(reference, setup.root.getDirectory(new SimplePath(target)));
    } catch (VirtualArtifactNotFoundException e) {
      return null;
    } finally {
     
    }
  }

  /**
   * @param groupId The group id to get setup info for
   * @param artifactId The artifact id to get setup info for
   * @param version The version of the package
   * @return Information about the project setup, or null if it does not exist (was not detected on the last {@link #scan()})
   */
  public ProjectSetup getProjectSetup (IClassPackageResolver resolver, IClassPackageReference reference) {
    this.scan(resolver); return this.projectRoots.get(this.createUniqueId(reference));
  }
 
  /**
   * This will scan through the project directory attempting to find
   * projects (sub directories) that have a pom.xml which can be used
   * to detect which package the project is defining.
   */
  protected void scan (IClassPackageResolver resolver) {
    if (this.modified < directory.getLastModified()) {
      long start = System.nanoTime();
      log.info("Scanning local projects... (in " + directory + ") : begin");
     
      this.projectRoots.clear();
      this.modified = directory.getLastModified();
     
      for (IVirtualDirectory sub : directory.getDirectories()) {
       
        IVirtualFile pomXmlFile = sub.getFile("pom.xml");
        if (pomXmlFile != null) {
         
          ProjectObjectModel model = ProjectObjectModel.parse(resolver, pomXmlFile);
          this.projectRoots.put(this.createUniqueId(model), new ProjectSetup(sub, model));
         
        }
       
      }
     
      long total = System.nanoTime() - start;
      log.info("Scanning local projects... (in " + directory + ") : complete in " + total + "ns/" + (total / 1000000) + "ms");
    }
  }
 
  /**
   * Composite information about a maven project directory.
   *
   * @author elponderador
   * @author $Author: ponderator $
   * @version $Id: MavenRepositoryLocalProject.java 2745 2011-02-05 04:23:36Z ponderator $
   */
  public class ProjectSetup {
   
    private IVirtualDirectory root;
    private ProjectObjectModel model;
   
    public ProjectSetup(IVirtualDirectory root, ProjectObjectModel model) {
      this.root = root;
      this.model = model;
    }
   
    public IVirtualDirectory getRoot() { return root; }

    public ProjectObjectModel getModel() { return model; }
   
  }

}
TOP

Related Classes of net.sourceforge.javautil.classloader.resolver.impl.maven.MavenRepositoryLocalProject

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.