1 /*** 2 * Copyright (C) 2008 Jeremy Thomerson (jthomerson@users.sourceforge.net) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package net.sf.vcaperture.util; 18 19 import java.io.FileInputStream; 20 import java.io.InputStream; 21 import java.util.Properties; 22 import java.util.jar.Manifest; 23 import java.util.regex.Matcher; 24 import java.util.regex.Pattern; 25 26 import net.sf.vcaperture.web.ApertureWebApp; 27 28 public class Version { 29 30 private static String VERSION = null; 31 32 public static String getVersion() { 33 if (VERSION == null) { 34 try { 35 InputStream in = ((ApertureWebApp) ApertureWebApp.get()).getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF"); 36 Manifest man = new Manifest(in); 37 VERSION = man.getMainAttributes().getValue("Implementation-Version"); 38 System.out.println("LOADED APP VERSION FROM /META-INF/MANIFEST.MF"); 39 } catch (Exception ex1) { 40 System.err.println("Error loading version from manifest: " + ex1.getMessage()); 41 try { 42 InputStream in = ((ApertureWebApp) ApertureWebApp.get()).getServletContext().getResourceAsStream("/META-INF/maven/net.sf.vcaperture/vcaperture/pom.properties"); 43 Properties props = new Properties(); 44 props.load(in); 45 VERSION = props.getProperty("version"); 46 System.out.println("LOADED APP VERSION FROM /META-INF/maven/net.sf.vcaperture/vcaperture/pom.properties"); 47 } catch(Exception ex2) { 48 System.err.println("Error loading version from pom.properties: " + ex2.getMessage()); 49 try { 50 String dir = System.getProperty("user.dir"); 51 InputStream in = new FileInputStream(dir + "/pom.xml"); 52 StringBuffer out = new StringBuffer(); 53 byte[] b = new byte[4096]; 54 for (int n; (n = in.read(b)) != -1;) { 55 out.append(new String(b, 0, n)); 56 } 57 Pattern patt = Pattern.compile("<version>(.*)</version>"); 58 Matcher matcher = patt.matcher(out.toString()); 59 matcher.find(); 60 VERSION = matcher.group(1); 61 System.out.println("LOADED APP VERSION FROM pom.xml"); 62 } catch(Exception ex3) { 63 System.err.println("Error loading version from pom.xml: " + ex3.getMessage()); 64 ex3.printStackTrace(); 65 VERSION = "UNKNOWN"; 66 } 67 } 68 } 69 System.out.println("LOADED APPLICATION VERSION: " + VERSION); 70 } 71 return VERSION; 72 } 73 }