| 1 | /* |
| 2 | * Created on Jul 9, 2004 |
| 3 | * |
| 4 | * To change the template for this generated file go to |
| 5 | * Window - Preferences - Java - Code Generation - Code and Comments |
| 6 | */ |
| 7 | package biz.xsoftware.manifest; |
| 8 | |
| 9 | import java.io.File; |
| 10 | import java.io.IOException; |
| 11 | import java.lang.reflect.Method; |
| 12 | import java.net.URL; |
| 13 | import java.util.Iterator; |
| 14 | import java.util.Set; |
| 15 | import java.util.Map.Entry; |
| 16 | import java.util.jar.Attributes; |
| 17 | import java.util.jar.JarFile; |
| 18 | import java.util.jar.Manifest; |
| 19 | import java.util.logging.Level; |
| 20 | import java.util.logging.Logger; |
| 21 | |
| 22 | /** |
| 23 | * Miscellaneous class that just prints the version of the mock object jar |
| 24 | * getting it from the manifest file. |
| 25 | * |
| 26 | * @author Dean Hiller |
| 27 | */ |
| 28 | public class ManifestInfo { |
| 29 | |
| 30 | private static final Logger log = Logger.getLogger(ManifestInfo.class.getName()); |
| 31 | |
| 32 | private static ManifestUtil util = new ManifestUtilImpl(); |
| 33 | |
| 34 | //private Package thePackage; |
| 35 | private Manifest manifest; |
| 36 | |
| 37 | /** |
| 38 | * The main program for Version that prints the version info from |
| 39 | * the manifest file. |
| 40 | * |
| 41 | * java -jar xxx.jar |
| 42 | * 1. runs SubMain-Class in manifest file |
| 43 | * 2. If SubMain-Class is default value or "", prints usage info |
| 44 | * for -manifest and -version |
| 45 | * |
| 46 | * java -jar xxx.jar -version |
| 47 | * 1. prints version info |
| 48 | * |
| 49 | * java -jar xxx.jar -manifest |
| 50 | * 1. prints all manifest contents. |
| 51 | * |
| 52 | * @param args Ignores all arguments. |
| 53 | */ |
| 54 | public static void main(String[] args) throws Throwable { |
| 55 | try { |
| 56 | run(args); |
| 57 | } catch(Throwable e) { |
| 58 | log.log(Level.WARNING, "Exception occurred", e); |
| 59 | throw e; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public static void run(String[] args) throws IOException { |
| 64 | ManifestInfo manifestInfo = new ManifestInfo(); |
| 65 | |
| 66 | if(args.length > 0) { |
| 67 | if("-manifest".equals(args[0])) { |
| 68 | System.out.println(""+manifestInfo); |
| 69 | return; |
| 70 | } else if("-version".equals(args[0])) { |
| 71 | System.out.println(manifestInfo.getFullVersionInfo()); |
| 72 | return; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | String className = util.getMainClass(manifestInfo.getManifest()); |
| 77 | if(className == null) |
| 78 | className = ""; |
| 79 | if("TOOLS.JAVA.Main".equals(className.trim()) || "".equals(className)) { |
| 80 | System.err.println("Usage:"); |
| 81 | System.err.println("1. java -jar <jarfile> -version"); |
| 82 | System.err.println("2. java -jar <jarfile> -manifest"); |
| 83 | } else { |
| 84 | runProgram(className, args); |
| 85 | } |
| 86 | util.exit(1); |
| 87 | } |
| 88 | |
| 89 | static void runProgram(String className, String[] args) { |
| 90 | String msg = ""; |
| 91 | ClassLoader cl = ManifestInfo.class.getClassLoader(); |
| 92 | try { |
| 93 | |
| 94 | Class c = cl.loadClass(className); |
| 95 | log.finest("class="+c); |
| 96 | Method m = c.getMethod("main", new Class[] {String[].class}); |
| 97 | m.invoke(null, new Object[] { args }); |
| 98 | |
| 99 | } catch(ClassNotFoundException e) { |
| 100 | msg = "Class in manifest not found in classpath\n" |
| 101 | +"Fix the ant.properties file to refer to the\n" |
| 102 | +"main class or refer to nothing\n" |
| 103 | +"class="+className; |
| 104 | System.out.println(msg); |
| 105 | } catch(NoSuchMethodException e) { |
| 106 | msg = "You have specified a class that doesn't" |
| 107 | +"have a main method in ant.properties file." |
| 108 | +"class="+className; |
| 109 | System.out.println(msg); |
| 110 | } catch(Exception e) { |
| 111 | msg = "\n\n2. Unknown failure. Contact buildtemplate owner"; |
| 112 | log.log(Level.WARNING, "Exception occurred", e); |
| 113 | System.out.println(msg); |
| 114 | } |
| 115 | util.exit(1); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | public static void setJarLocator(ManifestUtil l) { |
| 120 | util = l; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Constructor that takes a class to get the version information |
| 125 | * from out of the manifest. Uses the class's package to retrieve |
| 126 | * the manifest version info. |
| 127 | * @param c The Class on whose package to use to get version info. |
| 128 | */ |
| 129 | public ManifestInfo() throws IOException { |
| 130 | URL url = ManifestInfo.class.getResource("ManifestInfo.class"); |
| 131 | |
| 132 | //set manifest from jar file |
| 133 | File f = util.getFile(url); |
| 134 | JarFile jarFile = new JarFile(f); |
| 135 | manifest = jarFile.getManifest(); |
| 136 | |
| 137 | //set the package of this guy(not really needed as we could get all this info |
| 138 | //directly from manifest) |
| 139 | //String name = ManifestInfo.class.getName(); |
| 140 | //int index = name.lastIndexOf("."); |
| 141 | //String packageName = name.substring(0, index); |
| 142 | //thePackage = Package.getPackage(packageName); |
| 143 | } |
| 144 | |
| 145 | private Manifest getManifest() { |
| 146 | return manifest; |
| 147 | } |
| 148 | |
| 149 | public String getVersion() { |
| 150 | Attributes attr = manifest.getMainAttributes(); |
| 151 | String version = attr.getValue("Implementation-Version"); |
| 152 | version = version.trim(); |
| 153 | int index = version.indexOf(" "); |
| 154 | version = version.substring(0, index); |
| 155 | return version; |
| 156 | } |
| 157 | |
| 158 | public String getFullVersionInfo() { |
| 159 | Attributes attr = manifest.getMainAttributes(); |
| 160 | String retVal = attr.getValue("Implementation-Title")+" information..."; |
| 161 | retVal += "\nwebsite= "+attr.getValue("Implementation-Vendor"); |
| 162 | retVal += "\nbuilt by= "+attr.getValue("Built-By"); |
| 163 | retVal += "\nclasspath= "+attr.getValue("Class-Path"); |
| 164 | retVal += "\nversion= "+attr.getValue("Implementation-Version")+"\n"; |
| 165 | |
| 166 | return retVal; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Prints the version info the Version represents. |
| 171 | * |
| 172 | * @see java.lang.Object#toString() |
| 173 | */ |
| 174 | public String toString() { |
| 175 | StringBuffer manifestInfo = new StringBuffer(); |
| 176 | |
| 177 | Attributes attr = manifest.getMainAttributes(); |
| 178 | Set entries = attr.entrySet(); |
| 179 | Iterator iter = entries.iterator(); |
| 180 | |
| 181 | while(iter.hasNext()) { |
| 182 | Entry entry = (Entry)iter.next(); |
| 183 | manifestInfo.append(entry.getKey()).append("=").append(entry.getValue()).append("\n"); |
| 184 | } |
| 185 | |
| 186 | return manifestInfo+""; |
| 187 | } |
| 188 | |
| 189 | public interface ManifestUtil { |
| 190 | public File getFile(URL url); |
| 191 | /** |
| 192 | * @param manifest |
| 193 | * @return |
| 194 | */ |
| 195 | public String getMainClass(Manifest manifest); |
| 196 | |
| 197 | public void exit(int code); |
| 198 | } |
| 199 | } |