-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathArtifactManager.java
More file actions
156 lines (127 loc) · 5.94 KB
/
ArtifactManager.java
File metadata and controls
156 lines (127 loc) · 5.94 KB
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
package me.neatmonster.spacemodule.management;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedHashMap;
import me.neatmonster.spacemodule.SpaceModule;
import me.neatmonster.spacemodule.utilities.Console;
import me.neatmonster.spacemodule.utilities.Utilities;
import org.bukkit.configuration.file.YamlConfiguration;
import com.drdanick.rtoolkit.util.config.ConfigurationFile;
import com.drdanick.rtoolkit.util.config.Node;
/**
* Manages dependency artifacts.
*/
public class ArtifactManager {
private String name;
private String version;
private String jenkinsURLBase;
private int recommendedBuild = -1;
private int developmentBuild = -1;
private String buildAPIString;
private String recommendedAPIString;
private String artifactName;
private boolean recommended;
private final LinkedHashMap<Integer, String> builds = new LinkedHashMap<Integer, String>();
public ArtifactManager(String name, String version, String jenkinsURLBase, boolean recommended) {
this.name = name;
this.version = version;
this.recommended = recommended;
this.jenkinsURLBase = jenkinsURLBase;
buildAPIString = "/api/xml?tree=jobs[builds[number,artifacts[fileName],actions[levelValue]]]&wrapper=jenkins&xpath=//job/build[starts-with(artifact/fileName/text(),'"+name.toLowerCase()+"-"+version+"')]&exclude=//job/build/action[not(node())]|//job/build[not(artifact)]";
recommendedAPIString = buildAPIString + "|//job/build[not(action/levelValue=4)]";
}
/**
* Matches an MD5 with a build number
* @param md5 MD5 to match
* @return Build number, -1 if none
*/
public int match(final String md5) {
if (builds.containsValue(md5))
for (final int buildNumber : builds.keySet()) {
final String bMD5 = builds.get(buildNumber);
if (bMD5 != null && bMD5.equalsIgnoreCase(md5))
return buildNumber;
}
return -1;
}
//XXX: Update engine is far too messy and needs to be rethought.
public void setup(boolean printProgress, int progressMin, int progressMax) {
double progress = progressMin;
updateProgress(printProgress, progress);
YamlConfiguration database = YamlConfiguration.loadConfiguration(SpaceModule.DATABASE);
try {
URL url = new URL(jenkinsURLBase);
URLConnection conn = url.openConnection();
conn.setConnectTimeout(2000);
conn.connect();
} catch (IOException e) {
System.err.println("[SpaceBukkit] Unable to connect to the update server!");
return;
}
Object artifactAPIResponse = SpaceModule.getXStream().fromXML(Utilities.getContent(jenkinsURLBase + buildAPIString));
Object recommendedArtifactAPIResponse = SpaceModule.getXStream().fromXML(Utilities.getContent(jenkinsURLBase + recommendedAPIString));
ConfigurationFile allBuilds = new ConfigurationFile(new Node(artifactAPIResponse));
ConfigurationFile recommendedBuilds = new ConfigurationFile(new Node(recommendedArtifactAPIResponse));
int lastCheckedBuild = database.getInt(name + ".lastChecked", 0);
database.set(name + ".version", version);
String lastBuild = allBuilds.getString("jenkins[0].number");
String lastRecommendedBuild = recommendedBuilds.getString("jenkins[0].number");
developmentBuild = Integer.parseInt(lastBuild.trim());
try {
if(lastRecommendedBuild != null)
recommendedBuild = Integer.parseInt(lastRecommendedBuild.trim());
else
recommendedBuild = developmentBuild;
} catch(NumberFormatException e) {
recommendedBuild = developmentBuild;
}
if (lastCheckedBuild == developmentBuild) {//No need to update the artifact name
artifactName = database.getString(name+"-"+version+".artifactName");
} else {
artifactName = (SpaceModule.getInstance().recommended ? recommendedBuilds : allBuilds).getString("jenkins[0].artifact.fileName");
database.set(name+".artifactName", artifactName);
}
//Map build artifacts to hashes taken from jenkins
double progressDiv = (double)(progressMax - progressMin)/(double)allBuilds.getList("jenkins").size();
for(Object o : (recommended ? recommendedBuilds : allBuilds).getList("jenkins")) {
ConfigurationFile c = new ConfigurationFile(new Node(o));
int number = Integer.parseInt(c.getString("number"));
final String buildPage = Utilities.getContent(jenkinsURLBase + "/job/" + name + "/"
+ number + "/artifact/target/" + artifactName + "/*fingerprint*/");
if (buildPage != null) {
final int beginIndex = buildPage.indexOf("<div class=\"md5sum\">MD5: ") + 25;
final String md5 = buildPage.substring(beginIndex, beginIndex + 32);
progress += progressDiv;
updateProgress(printProgress, progress);
builds.put(number, md5);
database.set(name + ".build" + number, md5);
}
}
database.set(name + ".LastChecked", developmentBuild);
try {
database.save(SpaceModule.DATABASE);
} catch (IOException e) {
e.printStackTrace();
}
database.set(name+".build" + ".lastChecked", developmentBuild);
updateProgress(printProgress, progressMax);
}
public String getArtifactFileName() {
return artifactName;
}
public String getJobName() {
return name;
}
public int getRecommendedBuild() {
return recommendedBuild;
}
public int getDevelopmentBuild() {
return developmentBuild;
}
private void updateProgress(boolean printProgress, double p) {
if(printProgress)
Console.progress("Checking for updates", (int)p);
}
}