| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BuildInfo |
|
| 0.0;0 |
| 1 | // Copyright (c) 2001 Dustin Sallings <dustin@spy.net> | |
| 2 | ||
| 3 | package net.spy.digg; | |
| 4 | ||
| 5 | import java.net.URL; | |
| 6 | import java.util.Properties; | |
| 7 | import java.util.Date; | |
| 8 | import java.text.SimpleDateFormat; | |
| 9 | import java.text.ParseException; | |
| 10 | import java.io.InputStream; | |
| 11 | import java.io.IOException; | |
| 12 | import java.io.FileNotFoundException; | |
| 13 | ||
| 14 | /** | |
| 15 | * Information regarding this spy.jar build. | |
| 16 | * | |
| 17 | * The following properties will be set at build time: | |
| 18 | * | |
| 19 | * <ul> | |
| 20 | * <li>build.date - Date of this build (yyyy/MM/dd HH:mm)</li> | |
| 21 | * <li>java.vendor - Java vendor who made the VM that built this jar</li> | |
| 22 | * <li>java.version - Java version of the the VM that built this jar</li> | |
| 23 | * <li>os.name - Name of the OS on which this jar was built</li> | |
| 24 | * <li>os.version - Version of the OS on which this jar was built</li> | |
| 25 | * </ul> | |
| 26 | */ | |
| 27 | public class BuildInfo extends Properties { | |
| 28 | ||
| 29 | /** | |
| 30 | * Get an instance of BuildInfo that describes the spy.jar build. | |
| 31 | */ | |
| 32 | public BuildInfo() throws IOException { | |
| 33 | 0 | this("net/spy/digg/build.properties"); |
| 34 | 0 | } |
| 35 | ||
| 36 | /** | |
| 37 | * Get an instance of BuildInfo that describes the build info found in | |
| 38 | * the given resource. | |
| 39 | */ | |
| 40 | protected BuildInfo(String resource) throws IOException { | |
| 41 | 0 | super(); |
| 42 | // Grab the build properties | |
| 43 | 0 | ClassLoader cl=getClass().getClassLoader(); |
| 44 | 0 | InputStream is=cl.getResourceAsStream(resource); |
| 45 | 0 | if(is==null) { |
| 46 | 0 | throw new IOException("No resources found for " + resource); |
| 47 | } | |
| 48 | try { | |
| 49 | 0 | load(is); |
| 50 | } finally { | |
| 51 | 0 | is.close(); |
| 52 | 0 | } |
| 53 | 0 | } |
| 54 | ||
| 55 | /** | |
| 56 | * Get the date of this build. | |
| 57 | */ | |
| 58 | public Date getBuildDate() { | |
| 59 | 0 | SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm"); |
| 60 | 0 | Date rv=null; |
| 61 | ||
| 62 | try { | |
| 63 | 0 | rv=sdf.parse(getProperty("build.date")); |
| 64 | 0 | } catch(ParseException pe) { |
| 65 | 0 | throw new RuntimeException( |
| 66 | "Invalid date from build properties file", pe); | |
| 67 | 0 | } |
| 68 | 0 | return(rv); |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | * Get a URL to a file within this classloader. | |
| 73 | * | |
| 74 | * @param rel the relative name (i.e. net.spy.changelog) | |
| 75 | * @return the URL | |
| 76 | * @throws FileNotFoundException if the file cannot be found | |
| 77 | */ | |
| 78 | public URL getFile(String rel) throws FileNotFoundException { | |
| 79 | 0 | ClassLoader cl=getClass().getClassLoader(); |
| 80 | 0 | URL u=cl.getResource(rel); |
| 81 | 0 | if(u == null) { |
| 82 | 0 | throw new FileNotFoundException("Can't find " + rel); |
| 83 | } | |
| 84 | 0 | return(u); |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * String me. | |
| 89 | */ | |
| 90 | @Override | |
| 91 | public String toString() { | |
| 92 | 0 | StringBuilder sb=new StringBuilder(256); |
| 93 | ||
| 94 | 0 | if(getProperty("build.number") != null) { |
| 95 | 0 | sb.append("build "); |
| 96 | 0 | sb.append(getProperty("build.number")); |
| 97 | } | |
| 98 | 0 | sb.append(" on "); |
| 99 | 0 | sb.append(getBuildDate()); |
| 100 | 0 | sb.append("\nBuild platform: java "); |
| 101 | 0 | sb.append(getProperty("java.version")); |
| 102 | 0 | sb.append(" from "); |
| 103 | 0 | sb.append(getProperty("java.vendor")); |
| 104 | 0 | sb.append(" on "); |
| 105 | 0 | sb.append(getProperty("os.name")); |
| 106 | 0 | sb.append(" version "); |
| 107 | 0 | sb.append(getProperty("os.version")); |
| 108 | 0 | if(getProperty("tree.version") != null) { |
| 109 | 0 | sb.append("\nTree version: "); |
| 110 | 0 | sb.append(getProperty("tree.version")); |
| 111 | } | |
| 112 | ||
| 113 | 0 | return(sb.toString()); |
| 114 | } | |
| 115 | ||
| 116 | /** | |
| 117 | * Print out the build properties. | |
| 118 | */ | |
| 119 | public static void main(String args[]) throws Exception { | |
| 120 | 0 | BuildInfo bi=new BuildInfo(); |
| 121 | 0 | String cl="%" + "CHANGELOG" + "%"; |
| 122 | ||
| 123 | 0 | System.out.println("spy.jar " + bi); |
| 124 | ||
| 125 | // If there was a changelog, let it be shown. | |
| 126 | 0 | if(!cl.equals("net/spy/digg/changelog.txt")) { |
| 127 | 0 | if(args.length > 0 && args[0].equals("-c")) { |
| 128 | 0 | System.out.println(" -- Changelog:\n"); |
| 129 | ||
| 130 | 0 | URL u=bi.getFile("net/spy/digg/changelog.txt"); |
| 131 | 0 | InputStream is=u.openStream(); |
| 132 | try { | |
| 133 | 0 | byte data[]=new byte[8192]; |
| 134 | 0 | int bread=0; |
| 135 | do { | |
| 136 | 0 | bread=is.read(data); |
| 137 | 0 | if(bread > 0) { |
| 138 | 0 | System.out.write(data, 0, bread); |
| 139 | } | |
| 140 | 0 | } while(bread != -1); |
| 141 | } finally { | |
| 142 | 0 | is.close(); |
| 143 | 0 | } |
| 144 | 0 | } else { |
| 145 | 0 | System.out.println("(add -c to see the recent changelog)"); |
| 146 | } | |
| 147 | } | |
| 148 | 0 | } |
| 149 | ||
| 150 | } |