1/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2 *
3 * This program and the accompanying materials are made available under
4 * the terms of the Common Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/cpl-v10.html
6 *
7 * $Id: mergeCommand.java,v 1.1.1.1.2.1 2004/07/16 23:32:29 vlad_r Exp $
8 */
9package com.vladium.emma.data;
10
11import java.io.IOException;
12
13import com.vladium.util.ClassLoaderResolver;
14import com.vladium.util.args.IOptsParser;
15import com.vladium.emma.Command;
16import com.vladium.emma.IAppConstants;
17import com.vladium.emma.IAppErrorCodes;
18import com.vladium.emma.EMMARuntimeException;
19
20// ----------------------------------------------------------------------------
21/**
22 * @author Vlad Roubtsov, (C) 2003
23 */
24public
25final class mergeCommand extends Command
26{
27    // public: ................................................................
28
29    public mergeCommand (final String usageToolName, final String [] args)
30    {
31        super (usageToolName, args);
32    }
33
34    public synchronized void run ()
35    {
36        ClassLoader loader;
37        try
38        {
39            loader = ClassLoaderResolver.getClassLoader ();
40        }
41        catch (Throwable t)
42        {
43            loader = getClass ().getClassLoader ();
44        }
45
46        try
47        {
48            // process 'args':
49            {
50                final IOptsParser parser = getOptParser (loader);
51                final IOptsParser.IOpts parsedopts = parser.parse (m_args);
52
53                // check if usage is requested before checking args parse errors etc:
54                {
55                    final int usageRequestLevel = parsedopts.usageRequestLevel ();
56
57                    if (usageRequestLevel > 0)
58                    {
59                        usageexit (parser, usageRequestLevel, null);
60                        return;
61                    }
62                }
63
64                final IOptsParser.IOpt [] opts = parsedopts.getOpts ();
65
66                if (opts == null) // this means there were args parsing errors
67                {
68                    parsedopts.error (m_out, STDOUT_WIDTH);
69                    usageexit (parser, IOptsParser.SHORT_USAGE, null);
70                    return;
71                }
72
73                // process parsed args:
74
75                try
76                {
77                    for (int o = 0; o < opts.length; ++ o)
78                    {
79                        final IOptsParser.IOpt opt = opts [o];
80                        final String on = opt.getCanonicalName ();
81
82                        if (! processOpt (opt))
83                        {
84                            if ("in".equals (on))
85                            {
86                                m_datapath = getListOptValue (opt, PATH_DELIMITERS, true);
87                            }
88                            else if ("out".equals (on))
89                            {
90                                m_outFileName = opt.getFirstValue ();
91                            }
92                        }
93                    }
94
95                    // user '-props' file property overrides:
96
97                    if (! processFilePropertyOverrides ()) return;
98
99                    // process prefixed opts:
100
101                    processCmdPropertyOverrides (parsedopts);
102                }
103                catch (IOException ioe)
104                {
105                    throw new EMMARuntimeException (IAppErrorCodes.ARGS_IO_FAILURE, ioe);
106                }
107
108                // handle cmd line-level defaults:
109                {
110                }
111            }
112
113            // run the reporter:
114            {
115                final MergeProcessor processor = MergeProcessor.create ();
116                processor.setAppName (IAppConstants.APP_NAME); // for log prefixing
117
118                processor.setDataPath (m_datapath);
119                processor.setSessionOutFile (m_outFileName);
120                processor.setPropertyOverrides (m_propertyOverrides);
121
122                processor.run ();
123            }
124        }
125        catch (EMMARuntimeException yre)
126        {
127            // TODO: see below
128
129            exit (true, yre.getMessage (), yre, RC_UNEXPECTED); // does not return
130            return;
131        }
132        catch (Throwable t)
133        {
134            // TODO: embed: OS/JVM fingerprint, build #, etc
135            // TODO: save stack trace in a file and prompt user to send it to ...
136
137            exit (true, "unexpected failure: ", t, RC_UNEXPECTED); // does not return
138            return;
139        }
140
141        exit (false, null, null, RC_OK);
142    }
143
144    // protected: .............................................................
145
146
147    protected void initialize ()
148    {
149        super.initialize ();
150    }
151
152    protected String usageArgsMsg ()
153    {
154        return "[options]";
155    }
156
157    // package: ...............................................................
158
159    // private: ...............................................................
160
161
162    private String [] m_datapath; // list of data files, not a real path
163    private String m_outFileName;
164
165} // end of class
166// ----------------------------------------------------------------------------