File: runCommand.java

package info (click to toggle)
emma-coverage 2.0.5312%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,964 kB
  • ctags: 3,634
  • sloc: java: 23,109; xml: 414; makefile: 23
file content (332 lines) | stat: -rw-r--r-- 13,488 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
 * 
 * This program and the accompanying materials are made available under
 * the terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * $Id: runCommand.java,v 1.1.1.1.2.1 2004/07/16 23:32:03 vlad_r Exp $
 */
package com.vladium.emma;

import java.io.File;
import java.io.IOException;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import com.vladium.util.ClassLoaderResolver;
import com.vladium.util.Strings;
import com.vladium.util.args.IOptsParser;
import com.vladium.util.asserts.$assert;
import com.vladium.emma.rt.AppRunner;

// ----------------------------------------------------------------------------
/**
 * @author Vlad Roubtsov, (C) 2003
 */
public
final class runCommand extends Command
{
    // public: ................................................................


    public synchronized void run ()
    {
        ClassLoader loader;
        try
        {
            loader = ClassLoaderResolver.getClassLoader ();
        }
        catch (Throwable t)
        {
            loader = getClass ().getClassLoader ();
        }
        
        try
        {
            // process 'args':
            {
                final IOptsParser parser = getOptParser (loader);
                final IOptsParser.IOpts parsedopts = parser.parse (m_args);
                
                // check if usage is requested before checking args parse errors etc:
                {
                    final int usageRequestLevel = parsedopts.usageRequestLevel ();

                    if (usageRequestLevel > 0)
                    {
                        usageexit (parser, usageRequestLevel, null);
                        return;
                    }
                }
                
                final IOptsParser.IOpt [] opts = parsedopts.getOpts ();
                
                if (opts == null) // this means there were args parsing errors
                {
                    parsedopts.error (m_out, STDOUT_WIDTH);
                    usageexit (parser, IOptsParser.SHORT_USAGE, null);
                    return;
                }
                
                // process parsed args:
                try
                {
                    for (int o = 0; o < opts.length; ++ o)
                    {
                        final IOptsParser.IOpt opt = opts [o];
                        final String on = opt.getCanonicalName ();
                        
                        if (! processOpt (opt))
                        {
                            if ("cp".equals (on))
                            {
                                m_classpath = getListOptValue (opt, PATH_DELIMITERS, true);
                            }
                            else if ("jar".equals (on))
                            {
                                m_jarMode = true;
                            }
                            else if ("f".equals (on))
                            {
                                m_scanCoveragePath = getOptionalBooleanOptValue (opt);
                            }
                            else if ("sp".equals (on))
                            {
                                m_srcpath = getListOptValue (opt, PATH_DELIMITERS, true);
                            }
                            else if ("raw".equals (on))
                            {
                                m_dumpRawData = getOptionalBooleanOptValue (opt); 
                            }
                            else if ("out".equals (on))
                            {
                                m_outFileName = opt.getFirstValue ();
                            }
                            else if ("merge".equals (on))
                            {
                                m_outDataMerge = getOptionalBooleanOptValue (opt) ? Boolean.TRUE : Boolean.FALSE; 
                            }
                            else if ("r".equals (on))
                            {
                                m_reportTypes = Strings.merge (opt.getValues (), COMMA_DELIMITERS, true);
                            }
                            else if ("ix".equals (on))
                            {
                                m_ixpath = getListOptValue (opt, COMMA_DELIMITERS, true);
                            }
                        }
                    }
                    
                    // user '-props' file property overrides:
                    
                    if (! processFilePropertyOverrides ()) return;
                    
                    // process prefixed opts:
                    
                    processCmdPropertyOverrides (parsedopts);
                }
                catch (IOException ioe)
                {
                    throw new EMMARuntimeException (IAppErrorCodes.ARGS_IO_FAILURE, ioe);
                }
                
                
                // process free args:
                {
                    final String [] freeArgs = parsedopts.getFreeArgs ();
                    
                    if (m_jarMode)
                    {
                        if ((freeArgs == null) || (freeArgs.length == 0))
                        {
                            usageexit (parser, IOptsParser.SHORT_USAGE, "missing jar file name");
                            return;
                        }
                        
                        if ($assert.ENABLED) $assert.ASSERT (freeArgs != null && freeArgs.length > 0, "invalid freeArgs");
    
                        final File jarfile = new File (freeArgs [0]);
                        final String jarMainClass;
                        try
                        {
                            jarMainClass = openJarFile (jarfile); // the rest of free args are *not* ignored
                        }
                        catch (IOException ioe)
                        {
                            // TODO: is the right error code?
                            throw new EMMARuntimeException (IAppErrorCodes.ARGS_IO_FAILURE, ioe);
                        }                    
                        
                        if (jarMainClass == null)
                        {
                            exit (true, "failed to load Main-Class manifest attribute from [" + jarfile.getAbsolutePath () + "]", null, RC_UNEXPECTED);
                            return; 
                        }
                        
                        if ($assert.ENABLED) $assert.ASSERT (jarMainClass != null, "invalid jarMainClass");
                        
                        m_appArgs = new String [freeArgs.length];
                        System.arraycopy (freeArgs, 1, m_appArgs, 1, freeArgs.length - 1);
                        m_appArgs [0] = jarMainClass;
                        
                        m_classpath = new String [] { jarfile.getPath () };
                    }
                    else
                    {
                        if ((freeArgs == null) || (freeArgs.length == 0))
                        {
                            usageexit (parser, IOptsParser.SHORT_USAGE, "missing application class name");
                            return;
                        }
                        
                        m_appArgs = freeArgs;
                    }
                }
                // [m_appArgs: { mainclass, arg1, arg2, ... }]

                
                // handle cmd line-level defaults and option interaction
                {
                    if (DEFAULT_TO_SYSTEM_CLASSPATH)
                    {
                        if (m_classpath == null)
                        {
                            // TODO" this is not guaranteed to work (in WebStart etc), so double check if I should remove this
                            
                            final String systemClasspath = System.getProperty ("java.class.path", "");
                            if (systemClasspath.length () == 0)
                            {
                                // TODO: assume "." just like Sun JVMs in this case?
                                usageexit (parser, IOptsParser.SHORT_USAGE, "could not infer coverage classpath from 'java.class.path'; use an explicit -cp option");
                                return;
                            }
                            
                            m_classpath = new String [] {systemClasspath};
                        }
                    }
                    else
                    {
                        if (m_classpath == null)
                        {
                            usageexit (parser, IOptsParser.SHORT_USAGE, "either '-cp' or '-jar' option is required");
                            return;
                        }
                    }               

                    // TODO: who owns setting this 'txt' default? most likely AppRunner
                    if (m_reportTypes == null)
                    {
                        m_reportTypes = new String [] {"txt"};
                    }
                }
            }
            
            // run the app:
            {
                if ($assert.ENABLED) $assert.ASSERT (m_appArgs != null && m_appArgs.length > 0, "invalid m_appArgs");
                
                final String [] appargs = new String [m_appArgs.length - 1];
                System.arraycopy (m_appArgs, 1, appargs, 0, appargs.length);
                
                final AppRunner processor = AppRunner.create (loader);
                processor.setAppName (IAppConstants.APP_NAME); // for log prefixing
                
                processor.setAppClass (m_appArgs [0], appargs);
                processor.setCoveragePath (m_classpath, true); // TODO: an option to set 'canonical'?
                processor.setScanCoveragePath (m_scanCoveragePath);
                processor.setSourcePath (m_srcpath);
                processor.setInclExclFilter (m_ixpath);
                processor.setDumpSessionData (m_dumpRawData);
                processor.setSessionOutFile (m_outFileName);
                processor.setSessionOutMerge (m_outDataMerge);
                if ($assert.ENABLED) $assert.ASSERT (m_reportTypes != null, "m_reportTypes no set");
                processor.setReportTypes (m_reportTypes);
                processor.setPropertyOverrides (m_propertyOverrides);
                
                processor.run ();
            }
        }
        catch (EMMARuntimeException yre)
        {
            // TODO: see below
            
            exit (true, yre.getMessage (), yre, RC_UNEXPECTED); // does not return
            return;
        }
        catch (Throwable t)
        {
            // TODO: embed: OS/JVM fingerprint, build #, etc
            // TODO: save stack trace in a file and prompt user to send it to ...
            
            exit (true, "unexpected failure: ", t, RC_UNEXPECTED); // does not return
            return;
        }

        exit (false, null, null, RC_OK);
    }
    
    // protected: .............................................................


    protected runCommand (final String usageToolName, final String [] args)
    {
        super (usageToolName, args);
    }

    protected void initialize ()
    {
        // TODO: clean up instance state
        
        super.initialize ();
    }
    
    protected String usageArgsMsg ()
    {
        return "[options] class [args...] | -jar [options] jarfile [args...]";
    }
    
    // package: ...............................................................
    
    // private: ...............................................................
    
    
    private static String openJarFile (final File file)
        throws IOException
    {
        JarFile jarfile = null;
        try
        {
            jarfile = new JarFile (file, false);
            
            final Manifest manifest = jarfile.getManifest ();
            if (manifest == null) return null;
            
            final Attributes attributes = manifest.getMainAttributes ();
            if (attributes == null) return null;

            final String jarMainClass = attributes.getValue (Attributes.Name.MAIN_CLASS);
            
            return jarMainClass;
        }
        finally
        {
            if (jarfile != null) try { jarfile.close (); } catch (IOException ignore) {}
        }
    }
        
        
    private String [] m_classpath, m_srcpath;
    private boolean m_jarMode;
    private boolean m_scanCoveragePath; // defaults to false
    private String [] m_ixpath;
    private String [] m_appArgs;
    private boolean m_dumpRawData; // defaults to false
    private String m_outFileName;
    private Boolean m_outDataMerge; 
    private String [] m_reportTypes;
    
    private static final boolean DEFAULT_TO_SYSTEM_CLASSPATH = false;
        
} // end of class
// ----------------------------------------------------------------------------