Created
December 12, 2012 20:22
-
-
Save main--/4271233 to your computer and use it in GitHub Desktop.
MV autoupdates Appengine API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.onarandombox.mvapi; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Persistence; | |
public final class EMF { | |
private static final EntityManagerFactory emfInstance = | |
Persistence.createEntityManagerFactory("transactions-optional"); | |
private EMF() {} | |
public static EntityManagerFactory get() { | |
return emfInstance; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.onarandombox.mvapi; | |
import java.io.IOException; | |
import javax.persistence.EntityManager; | |
import javax.servlet.http.*; | |
@SuppressWarnings("serial") | |
public class LatestServlet extends HttpServlet { | |
@Override | |
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { | |
resp.setContentType("text/plain"); | |
String name = req.getParameter("name"); | |
if ((name == null) || name.equals("")) { | |
resp.getWriter().println("No name given!"); | |
resp.setStatus(400); | |
return; | |
} | |
System.out.println("Checking for name: " + name); | |
EntityManager em = EMF.get().createEntityManager(); | |
try { | |
PluginEntry entry = em.find(PluginEntry.class, name); | |
if (entry == null) { | |
// wtf | |
resp.getWriter().println(String.format("No entry for plugin '%s'!", name)); | |
System.out.println("Doesn't contain " + name); | |
resp.setStatus(400); | |
} else { | |
String response = entry.getLatestResponse(); | |
System.out.println("Responding:"); | |
System.out.println(response); | |
resp.getWriter().println(response); | |
} | |
} finally { | |
em.close(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.onarandombox.mvapi; | |
import javax.persistence.Basic; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
@Entity | |
public class PluginEntry { | |
private static final String VERSION_KEY = "version"; | |
private static final String URL_KEY = "updateUrl"; | |
@Id | |
private String pluginName; | |
@Basic(optional = false) | |
private String versionString; | |
@Basic(optional = false) | |
private String downloadUrl; | |
@Basic(optional = true) | |
private String latestResponse; | |
public PluginEntry(String pluginName, String versionString, String downloadUrl) { | |
this.pluginName = pluginName; | |
this.versionString = versionString; | |
this.downloadUrl = downloadUrl; | |
} | |
public String getPluginName() { | |
return pluginName; | |
} | |
public String getVersionString() { | |
return versionString; | |
} | |
public String getDownloadUrl() { | |
return downloadUrl; | |
} | |
public String getLatestResponse() { | |
// build lazily | |
if (latestResponse == null) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(VERSION_KEY).append(": ").append(versionString).append('\n'); | |
sb.append(URL_KEY).append(": ").append(downloadUrl).append('\n'); | |
latestResponse = sb.toString(); | |
} | |
return latestResponse; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.onarandombox.mvapi; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import javax.persistence.EntityManager; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
@SuppressWarnings("serial") | |
public class PushCoreUpdateServlet extends HttpServlet { | |
private static final String JENKINS = "http://ci.onarandombox.com/job/Multiverse-Core/lastSuccessfulBuild/"; | |
private static final Pattern BUILD_NUMBER = Pattern.compile(".*#(\\d+).*");//".*Multiverse-Core #(\\d*).*"); | |
private static final Pattern VERSION = Pattern.compile(".*Multiverse-Core-(.*)\\.jar.*"); | |
@Override | |
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { | |
resp.setContentType("text/plain"); | |
// just look it up from jenkins | |
URL url = new URL(JENKINS); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); | |
StringBuilder pageBuilder = new StringBuilder(); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
pageBuilder.append(line); //.append('\n'); | |
} | |
reader.close(); | |
String page = pageBuilder.toString(); | |
System.out.println("Page"); | |
System.out.println(page); | |
Matcher buildMatcher = BUILD_NUMBER.matcher(page); | |
buildMatcher.matches(); | |
int build = Integer.parseInt(buildMatcher.group(1)); | |
Matcher versionMatcher = VERSION.matcher(page); | |
versionMatcher.matches(); | |
String version = versionMatcher.group(1); | |
String versionString = String.format("%s-b%d", version, build); | |
String downloadUrl = String.format("http://ci.onarandombox.com/job/Multiverse-Core/" + | |
"lastSuccessfulBuild/artifact/target/Multiverse-Core-%s.jar", version); | |
EntityManager entityManager = EMF.get().createEntityManager(); | |
try { | |
entityManager.persist(new PluginEntry("Multiverse-Core", versionString, downloadUrl)); | |
} finally { | |
entityManager.close(); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment