summaryrefslogtreecommitdiff
path: root/core/java12/com/vladium/emma/instr/instrCommand.java
blob: eb253128080d863338e93744020cc93a214bc356 (plain)
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
/* 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: instrCommand.java,v 1.1.1.1.2.1 2004/07/16 23:32:04 vlad_r Exp $
 */
package com.vladium.emma.instr;

import java.io.IOException;

import com.vladium.util.ClassLoaderResolver;
import com.vladium.util.args.IOptsParser;
import com.vladium.util.asserts.$assert;
import com.vladium.emma.Command;
import com.vladium.emma.IAppConstants;
import com.vladium.emma.IAppErrorCodes;
import com.vladium.emma.EMMARuntimeException;

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

    public instrCommand (final String usageToolName, final String [] args)
    {
        super (usageToolName, args);
        
        m_outMode = InstrProcessor.OutMode.OUT_MODE_COPY; // default
    }

    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 ("ip".equals (on))
                            {
                                m_instrpath = getListOptValue (opt, PATH_DELIMITERS, true);
                            }
                            else if ("d".equals (on))
                            {
                                m_outDirName = opt.getFirstValue ();
                            }
                            else if ("out".equals (on))
                            {
                                m_outFileName = opt.getFirstValue ();
                            }
                            else if ("merge".equals (on))
                            {
                                m_outDataMerge = getOptionalBooleanOptValue (opt) ? Boolean.TRUE : Boolean.FALSE; 
                            }
                            else if ("ix".equals (on))
                            {
                                // note: this allows path delimiter in the pattern list as well
                                m_ixpath = getListOptValue (opt, COMMA_DELIMITERS, true);
                            }
                            else if ("m".equals (on))
                            {
                                final String ov = opt.getFirstValue ();
                                
                                final InstrProcessor.OutMode outMode = InstrProcessor.OutMode.nameToMode (ov);
                                if (outMode == null)
                                {
                                    usageexit (parser, IOptsParser.SHORT_USAGE,
                                        "invalid '" + opts [o].getName () + "' option value: " + ov);
                                    return;
                                }
                                m_outMode = outMode;
                            }
                        }
                    }

                    // user '-props' file property overrides:
                    
                    if (! processFilePropertyOverrides ()) return;
                    
                    // process prefixed opts:
                    
                    processCmdPropertyOverrides (parsedopts);
                }
                catch (IOException ioe)
                {
                    throw new EMMARuntimeException (IAppErrorCodes.ARGS_IO_FAILURE, ioe);
                }
                
                // handle cmd line-level defaults:
                {
                    if ($assert.ENABLED) $assert.ASSERT (m_outMode != null, "m_outMode not set");
                    
                    if ((m_outMode != InstrProcessor.OutMode.OUT_MODE_OVERWRITE) && (m_outDirName == null))
                    {
                        usageexit (parser, IOptsParser.SHORT_USAGE,
                            "output directory must be specified for '" + m_outMode + "' output mode");
                        return;
                    }
                }
            }
            
            // run the instrumentor:
            {
                final InstrProcessor processor = InstrProcessor.create ();
                processor.setAppName (IAppConstants.APP_NAME); // for log prefixing
                
                processor.setInstrPath (m_instrpath, true); // TODO: an option to set 'canonical'?
                processor.setInclExclFilter (m_ixpath);
                $assert.ASSERT (m_outMode != null, "m_outMode not set");
                processor.setOutMode (m_outMode);
                processor.setInstrOutDir (m_outDirName);
                processor.setMetaOutFile (m_outFileName);
                processor.setMetaOutMerge (m_outDataMerge);
                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 void initialize ()
    {
        super.initialize ();
    }
    
    protected String usageArgsMsg ()
    {
        return "[options]";
    }

    // package: ...............................................................
    
    // private: ...............................................................
    
        
    private String [] m_instrpath;
    private String [] m_ixpath;
    private String m_outDirName;
    private String m_outFileName;
    private Boolean m_outDataMerge;
    private InstrProcessor.OutMode m_outMode;
    
} // end of class
// ----------------------------------------------------------------------------