summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/fedoraproject/maven/artifact/repository/MavenJPackageDepmap.java
blob: ef09d1248883e5af6d232fadb530a915147053a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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);
    }
}