EMMA Coverage Report (generated Mon Oct 04 21:03:19 MDT 2004)
[all classes][biz.xsoftware.manifest]

COVERAGE SUMMARY FOR SOURCE FILE [ManifestInfo.java]

nameclass, %method, %block, %line, %
ManifestInfo.java100% (1/1)100% (10/10)90%  (344/382)91%  (72.2/79)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ManifestInfo100% (1/1)100% (10/10)90%  (344/382)91%  (72.2/79)
main (String []): void 100% (1/1)33%  (4/12)50%  (3/6)
ManifestInfo (): void 100% (1/1)80%  (40/50)97%  (9.7/10)
runProgram (String, String []): void 100% (1/1)80%  (78/97)82%  (15.5/19)
<static initializer> 100% (1/1)94%  (15/16)97%  (1.9/2)
getFullVersionInfo (): String 100% (1/1)100% (71/71)100% (7/7)
getManifest (): Manifest 100% (1/1)100% (3/3)100% (1/1)
getVersion (): String 100% (1/1)100% (22/22)100% (6/6)
run (String []): void 100% (1/1)100% (70/70)100% (18/18)
setJarLocator (ManifestInfo$ManifestUtil): void 100% (1/1)100% (3/3)100% (2/2)
toString (): String 100% (1/1)100% (38/38)100% (8/8)

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 */
7package biz.xsoftware.manifest;
8 
9import java.io.File;
10import java.io.IOException;
11import java.lang.reflect.Method;
12import java.net.URL;
13import java.util.Iterator;
14import java.util.Set;
15import java.util.jar.Attributes;
16import java.util.jar.JarFile;
17import java.util.jar.Manifest;
18import java.util.logging.Level;
19import 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 */
27public 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}

[all classes][biz.xsoftware.manifest]
EMMA 2.0.4217 (C) Vladimir Roubtsov