Created
February 26, 2014 19:22
-
-
Save gordolio/9236487 to your computer and use it in GitHub Desktop.
Get library versions from loaded war file
This file contains hidden or 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.mycompany; | |
public class Main { | |
public static void main(String[] args){ | |
Map<String,Object> versions = ResourceGrabber.grabLibraryVersions(); | |
System.out.println("joda-time: " + versions.get("joda-time").toString()); | |
} | |
} |
This file contains hidden or 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.mycompany; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.MalformedURLException; | |
import java.net.URISyntaxException; | |
import java.net.URL; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Set; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import javax.servlet.ServletContext; | |
import com.mycompany.utils.SpringUtil; | |
public class ResourceGrabber { | |
private static ResourceGrabber singleton = new ResourceGrabber(); | |
private ServletContext servletContext; | |
public ResourceGrabber() { | |
servletContext = (ServletContext)SpringUtil.getBean("servletContext"); | |
} | |
public static String grabResourceJndi(String resource) { | |
URL r; | |
try { | |
r = singleton.servletContext.getResource(resource); | |
} catch(MalformedURLException ex) { | |
return "Malformed URL"; | |
} | |
try { | |
return r.toURI().toASCIIString(); | |
} catch (URISyntaxException e) { | |
return "Bad URI Syntax: " + r.toString(); | |
} catch(NullPointerException ex) { | |
return null; | |
} | |
} | |
public static Map<String,String> grabResource(String resource){ | |
Map<String,String> data = new HashMap<String,String>(); | |
data.put("jndi",grabResourceJndi(resource)); | |
data.put("data",grabResourceData(resource)); | |
return data; | |
} | |
public static String grabResourceData(String resource) { | |
InputStream is = singleton.servletContext.getResourceAsStream(resource); | |
if(is == null) { | |
return null; | |
} | |
StringBuilder sb = new StringBuilder(1024); | |
try { | |
byte[] readBytes = new byte[1024]; | |
while(is.available() > 0) { | |
sb.append(new String(readBytes,0,is.read(readBytes))); | |
} | |
}catch(IOException ex) {} | |
return sb.toString(); | |
} | |
public static Map<String,Object> grabManifest() { | |
String manifest = grabResourceData("/META-INF/MANIFEST.MF"); | |
if(manifest == null) { | |
return null; | |
} | |
manifest = manifest.replaceAll("[\\r\\n]{1,2} ",""); | |
Map<String,Object> ret = new HashMap<String,Object>(); | |
for(String s : manifest.split("[\\r\\n]+")) { | |
String[] spl = s.split(": "); | |
String left = spl[0]; | |
if(spl.length > 1) { | |
if(spl[1].contains(",")) { | |
ret.put(left,spl[1].split(",")); | |
} else { | |
ret.put(left,spl[1]); | |
} | |
} | |
} | |
return ret; | |
} | |
public static Map<String,String> grabLibraryVersions() { | |
Map<String,String> ret = new HashMap<String,String>(); | |
Map<String,Object> manifest = grabManifest(); | |
if(manifest == null) { | |
return null; | |
} | |
Pattern p = Pattern.compile("^WEB-INF/lib/([^\\.]+)-(.+?)\\.jar$"); | |
for(String s : (String[])manifest.get("Bundle-ClassPath")) { | |
Matcher m = p.matcher(s); | |
if(m.matches()) { | |
ret.put(m.group(1),m.group(2)); | |
} | |
} | |
return ret; | |
} | |
public static Set<String> grabResourcePaths(String resource) { | |
return singleton.servletContext.getResourcePaths(resource); | |
} | |
} |
This file contains hidden or 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.mycompany.utils; | |
import org.springframework.context.ApplicationContext; | |
import org.codehaus.groovy.grails.web.context.ServletContextHolder; | |
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes; | |
public class SpringUtil { | |
public static ApplicationContext getCtx() { | |
return getApplicationContext(); | |
} | |
public static ApplicationContext getApplicationContext() { | |
return (ApplicationContext) ServletContextHolder.getServletContext().getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT); | |
} | |
@SuppressWarnings("unchecked") | |
public static <T> T getBean(String beanName) { | |
return (T) getApplicationContext().getBean(beanName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment