From 3067ff2cd4b0370551287ef186772490c9d9775a Mon Sep 17 00:00:00 2001 From: Stanislav Ochotnicky Date: Tue, 2 Nov 2010 13:22:55 +0100 Subject: Initial commit --- pom.xml | 27 ++++ .../artifact/repository/MavenJPackageDepmap.java | 166 +++++++++++++++++++++ .../artifact/resolver/JavadirWorkspaceReader.java | 93 ++++++++++++ 3 files changed, 286 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/org/fedoraproject/maven/artifact/repository/MavenJPackageDepmap.java create mode 100644 src/main/java/org/fedoraproject/maven/artifact/resolver/JavadirWorkspaceReader.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ce6115c --- /dev/null +++ b/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + org.fedoraproject.maven + maven-javadir-resolver + 0.0.1 + Maven Javadir Resolver + Custom Maven3 resolver for local artifacts in /usr/share/java + 2010 + + Fedora + http://fedoraproject.org/ + + + + org.jdom + jdom + 1.1 + runtime + + + org.sonatype.aether + aether-api + 1.7 + compile + + + \ No newline at end of file diff --git a/src/main/java/org/fedoraproject/maven/artifact/repository/MavenJPackageDepmap.java b/src/main/java/org/fedoraproject/maven/artifact/repository/MavenJPackageDepmap.java new file mode 100644 index 0000000..ef09d12 --- /dev/null +++ b/src/main/java/org/fedoraproject/maven/artifact/repository/MavenJPackageDepmap.java @@ -0,0 +1,166 @@ +package org.fedoraproject.maven.artifact.repository; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.List; +import java.util.StringTokenizer; + +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jdom.input.SAXBuilder; +import org.xml.sax.InputSource; + +public class MavenJPackageDepmap { + + private static MavenJPackageDepmap instance; + private static Hashtable jppArtifactMap; + + private MavenJPackageDepmap() { + jppArtifactMap = new Hashtable(); + buildJppArtifactMap(); + } + + public static MavenJPackageDepmap getInstance() { + if (instance == null) { + instance = new MavenJPackageDepmap(); + } + + return instance; + } + + public Hashtable getMappedInfo(Hashtable mavenDep) { + return getMappedInfo((String) mavenDep.get("group"), + (String) mavenDep.get("artifact"), + (String) mavenDep.get("version")); + } + + public Hashtable getMappedInfo(String groupId, String artifactId, String version) { + + Hashtable jppDep; + String idToCheck, jppCombination; + + if (System.getProperty("maven2.ignore.versions") == null && System.getProperty("maven2.jpp.mode") == null) { + idToCheck = groupId+","+artifactId+","+version; + } else { + idToCheck = groupId+","+artifactId; + } + + jppCombination = (String) jppArtifactMap.get(idToCheck); + + //System.err.println("*** " + groupId+","+artifactId+","+version + " => " + jppCombination); + + jppDep = new Hashtable(); + if (jppCombination != null && jppCombination != "") { + + StringTokenizer st = new StringTokenizer(jppCombination, ","); + + jppDep.put("group", st.nextToken()); + jppDep.put("artifact",st.nextToken()); + jppDep.put("version",st.nextToken()); + + } else { + jppDep.put("group", groupId); + jppDep.put("artifact", artifactId); + jppDep.put("version", version); + } + + return jppDep; + } + + + /** + * Returns whether or not the given dependency should be dropped. + */ + public boolean shouldEliminate(String groupId, String artifactId, String version) { + String idToCheck; + + if (System.getProperty("maven2.ignore.versions") == null && System.getProperty("maven2.jpp.mode") == null) { + idToCheck = groupId+","+artifactId+","+version; + } else { + idToCheck = groupId+","+artifactId; + } + + return jppArtifactMap.get(idToCheck) != null && jppArtifactMap.get(idToCheck).equals(""); + + } + + private static void buildJppArtifactMap() { + + if (System.getProperty("maven2.ignore.versions") != null || System.getProperty("maven2.jpp.mode") != null) { + debug("Processing file: /usr/share/java-utils/xml/maven2-versionless-depmap.xml"); + processDepmapFile("/etc/maven/maven2-versionless-depmap.xml"); + } + + debug("Processing file: /usr/share/java-utils/xml/maven2-depmap.xml"); + processDepmapFile("/etc/maven/maven2-depmap.xml"); + + String customFileName = System.getProperty("maven2.jpp.depmap.file", null); + if (customFileName != null) { + debug("Processing file: " + customFileName); + processDepmapFile(customFileName); + } + } + + private static void processDepmapFile(String fileName) { + + Document mapDocument; + + try { + mapDocument = (new SAXBuilder()).build(new InputSource(new FileInputStream(fileName))); + } catch (FileNotFoundException fnfe) { + System.err.println("ERROR: Unable to find map file: " + fileName); + fnfe.printStackTrace(); + return; + } catch (IOException ioe) { + System.err.println("ERROR: I/O exception occured when opening map file"); + ioe.printStackTrace(); + return; + } catch (JDOMException jde) { + System.err.println("ERROR: Unable to instantiate parser"); + jde.printStackTrace(); + return; + } + + List l = mapDocument.getRootElement().getChildren("dependency"); + + Iterator i = l.iterator(); + while (i.hasNext()) { + Element depElement = (Element) i.next(); + + Element mElem = depElement.getChild("maven"); + Element jppElem = depElement.getChild("jpp"); + + String mG = mElem.getChildText("groupId"); + String mA = mElem.getChildText("artifactId"); + String mV = mElem.getChildText("version"); + + // jppElem == null => drop this dependency + if (jppElem != null) { + + String jppG = jppElem.getChildText("groupId"); + String jppA = jppElem.getChildText("artifactId"); + String jppV = jppElem.getChildText("version"); + + if (System.getProperty("maven2.ignore.versions") == null && System.getProperty("maven2.jpp.mode") == null) { + debug("*** Adding: " + mG+","+mA+","+mV + " => " + jppG+","+jppA+","+jppV + " to map..."); + jppArtifactMap.put(mG+","+mA+","+mV, jppG+","+jppA+","+jppV); + } else { + debug("*** Adding: " + mG+","+mA + " => " + jppG+","+jppA+","+jppV + " to map..."); + jppArtifactMap.put(mG+","+mA, jppG+","+jppA+","+jppV); + } + } else { + debug("*** Adding: " + mG+","+mA+"," + " => " + "JPP/maven2,empty-dep,"+mV + " to map..."); + jppArtifactMap.put(mG+","+mA, "JPP/maven2,empty-dep,"+mV); + } + } + } + + private static void debug(String msg) { + if (System.getProperty("maven2.jpp.debug") != null) + System.err.println(msg); + } +} diff --git a/src/main/java/org/fedoraproject/maven/artifact/resolver/JavadirWorkspaceReader.java b/src/main/java/org/fedoraproject/maven/artifact/resolver/JavadirWorkspaceReader.java new file mode 100644 index 0000000..7ed0a2a --- /dev/null +++ b/src/main/java/org/fedoraproject/maven/artifact/resolver/JavadirWorkspaceReader.java @@ -0,0 +1,93 @@ +package org.fedoraproject.maven.artifact.resolver; + +import org.fedoraproject.maven.artifact.repository.MavenJPackageDepmap; +import java.io.File; +import java.util.List; +import java.util.LinkedList; +import java.util.Hashtable; + +import org.sonatype.aether.artifact.Artifact; +import org.sonatype.aether.repository.WorkspaceReader; +import org.sonatype.aether.repository.WorkspaceRepository; + +public class JavadirWorkspaceReader + implements WorkspaceReader +{ + private WorkspaceRepository workspaceRepository; + + private static final char GROUP_SEPARATOR = '.'; + private static final char PATH_SEPARATOR = '/'; + + + public JavadirWorkspaceReader() { + workspaceRepository = new WorkspaceRepository("javadir-workspace"); + } + + public WorkspaceRepository getRepository() { + return workspaceRepository; + } + + public File findArtifact( Artifact artifact ) { + System.err.println("=============JAVADIRREADER-FIND_ARTIFACT: " + artifact.getArtifactId()); + StringBuffer path = new StringBuffer(); + + String artifactId = artifact.getArtifactId(); + String groupId = artifact.getGroupId(); + String version = artifact.getVersion(); + + System.err.println("Wanted GROUPID=" + groupId); + System.err.println("Wanted ARTIFACTID=" + artifactId); + + if (!groupId.startsWith("JPP")) { + MavenJPackageDepmap map = MavenJPackageDepmap.getInstance(); + Hashtable newInfo = map.getMappedInfo(groupId, artifactId, version); + + groupId = (String) newInfo.get("group"); + artifactId = (String) newInfo.get("artifact"); + } + System.err.println("Resolved GROUPID=" + groupId); + System.err.println("Resolved ARTIFACTID=" + artifactId); + + if (artifact.getExtension().equals("pom")) { + path = getPOMPath(groupId, artifactId); + } else if (artifact.getExtension().equals("signature")) { + path.append("/usr/share/maven/repository/"); + path.append( groupId ).append( '/' ); + path.append( artifactId ).append( ".signature" ); + } else { + path.append("/usr/share/maven/repository/"); + path.append( groupId ).append( '/' ); + path.append( artifactId ).append( ".jar" ); + } + + System.err.println("Returning " + path.toString()); + return new File(path.toString()); + } + + public List findVersions( Artifact artifact ) { + List ret = new LinkedList(); + ret.add("DUMMY_VER"); + return ret; + } + + private StringBuffer getPOMPath(String groupId, String artifactId) { + + StringBuffer path = new StringBuffer(); + String fName = groupId.replace(PATH_SEPARATOR, GROUP_SEPARATOR) + "-" + artifactId + ".pom"; + path.append(System.getProperty("maven2.jpp.pom.path", "JPP/maven2/poms")).append("/").append(fName); + java.io.File f; + + // NOTE: We are returning default_poms/ as the path for this pom + // even though it may not exist there. This may cause an error, + // but that is fine because if the pom is not there, there is + // a serious problem anyways.. + f = new java.io.File(System.getProperty("maven2.jpp.default.repo", "/usr/share/maven2/repository") + "/" + path.toString()); + //System.err.println("Checking path " + f.getAbsolutePath() + " for the pom"); + if (!f.exists()) { + path = new StringBuffer(); + path.append(System.getProperty("maven2.jpp.default.pom.path", "JPP/maven2/default_poms")).append("/").append(fName); + } + path.insert(0, "/usr/share/maven2/repository/"); + return path; + } +} -- cgit