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