List of usage examples for java.net URLClassLoader findResource
public URL findResource(final String name)
From source file:org.deegree.workspace.standard.ModuleInfo.java
/** * Returns the {@link ModuleInfo} for the deegree module on the given classpath. * //from w w w . j ava 2 s .c om * @param classpathURL * classpath url, must not be <code>null</code> * @return module info or <code>null</code> (if the module does not have file META-INF/deegree/buildinfo.properties) * @throws IOException * if accessing <code>META-INF/deegree/buildinfo.properties</code> or * <code>META-INF/maven/[..]/pom.properties</code> fails */ public static ModuleInfo extractModuleInfo(URL classpathURL) throws IOException { ModuleInfo moduleInfo = null; ConfigurationBuilder builder = new ConfigurationBuilder(); builder = builder.setUrls(classpathURL); builder = builder.setScanners(new ResourcesScanner()); Reflections r = new Reflections(builder); Set<String> resources = r.getResources(Pattern.compile("(MANIFEST\\.MF|buildinfo\\.properties)")); if (!resources.isEmpty()) { URLClassLoader classLoader = new URLClassLoader(new URL[] { classpathURL }, null); String resourcePath = resources.iterator().next(); InputStream buildInfoStream = null; try { Properties props = new Properties(); buildInfoStream = classLoader.getResourceAsStream(resourcePath); props.load(buildInfoStream); String buildBy = props.getProperty("deegree-build-by", props.getProperty("build.by")); String buildArtifactId = props.getProperty("deegree-build-artifactId", props.getProperty("build.artifactId")); String buildDate = props.getProperty("deegree-build-date", props.getProperty("build.date")); String buildRev = props.getProperty("deegree-build-rev", props.getProperty("build.svnrev")); String pomVersion = null; if (buildArtifactId == null) { // skipping because this jar is not from deegree return null; } resources = r.getResources(Pattern.compile("pom\\.properties")); InputStream pomInputStream = null; if (!resources.isEmpty()) { resourcePath = resources.iterator().next(); try { props = new Properties(); pomInputStream = classLoader.findResource(resourcePath).openStream(); props.load(pomInputStream); String pomArtifactId = props.getProperty("artifactId"); if (!pomArtifactId.equals(buildArtifactId)) { LOG.warn( "ArtifactId mismatch for module on path: {} (MANIFEST.MF/buildinfo.properties vs. pom.properties).", classpathURL); } pomVersion = props.getProperty("version"); } finally { closeQuietly(pomInputStream); } } moduleInfo = new ModuleInfo(buildArtifactId, pomVersion, buildRev, buildDate, buildBy); } finally { closeQuietly(buildInfoStream); // * closeQuietly( classLoader ); */ // TODO enable for JDK 1.7 } } return moduleInfo; }
From source file:org.northrop.leanne.publisher.Main.java
public static void main(String[] args) { CommandLineParser parser = new GnuParser(); try {//from w w w .ja v a 2 s . com // parse the command line arguments CommandLine line = parser.parse(options, args); if (args.length == 0 || line.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("pub", options); } else if (line.hasOption("version")) { URLClassLoader cl = (URLClassLoader) Main.class.getClassLoader(); String title = ""; String version = ""; String date = ""; try { URL url = cl.findResource("META-INF/MANIFEST.MF"); Manifest manifest = new Manifest(url.openStream()); Attributes attr = manifest.getMainAttributes(); title = attr.getValue("Implementation-Title"); version = attr.getValue("Implementation-Version"); date = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM) .format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(attr.getValue("Built-Date"))); } catch (Exception e) { e.printStackTrace(); } System.out.println("------------------------------------------------------------"); System.out.println("Publisher Pipeline " + version); System.out.println("------------------------------------------------------------"); System.out.println(""); System.out.println(title + " build time " + date); System.out.println("Java: " + System.getProperty("java.version")); System.out.println("JVM: " + System.getProperty("java.vendor")); System.out.println("OS: " + System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch")); } else { Option[] options = line.getOptions(); Binding binding = new Binding(); binding.setVariable("home", System.getProperty("pub.home")); binding.setVariable("inputDir", System.getProperty("pub.home") + "/input"); binding.setVariable("outputDir", System.getProperty("pub.home") + "/output"); binding.setVariable("appName", System.getProperty("program.name")); binding.setVariable("isdebug", false); for (int i = 0; i < options.length; i++) { Option opt = options[i]; binding.setVariable("is" + opt.getOpt(), true); } String[] roots = new String[] { System.getProperty("pub.home") + "/resources/scripts", System.getProperty("pub.home") + "/resources/scripts/formats" }; ClassLoader parent = Main.class.getClassLoader(); GroovyScriptEngine gse = new GroovyScriptEngine(roots, parent); if (!line.hasOption("rerun")) { gse.run("prep.groovy", binding); } for (String name : getFormats()) { if (line.hasOption(name.toLowerCase())) { String file = ("" + name.charAt(0)).toLowerCase() + name.substring(1) + ".groovy"; gse.run(file, binding); } } } } catch (ParseException exp) { System.err.println("Command line parsing failed. Reason: " + exp.getMessage()); } catch (ResourceException resourceError) { System.err.println("Groovy script failed. Reason: " + resourceError.getMessage()); } catch (IOException ioError) { System.err.println("Groovy script failed. Reason: " + ioError.getMessage()); } catch (ScriptException error) { System.err.println("Groovy script failed. Reason: " + error.getMessage()); error.printStackTrace(); } }