summaryrefslogtreecommitdiffstats
path: root/tags/cpp-0.1.incubating-M1-RC3b/sca/tools/scagen/src/org/apache/tuscany/sca/cpp/tools/services/Scagen.java
blob: a6f6f58644d146cea70c2e20e0ef6b291a906f57 (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
/*
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.sca.cpp.tools.services;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.tuscany.sca.cpp.tools.common.CParsingTool;
import org.apache.tuscany.sca.cpp.tools.common.Options;
import org.apache.tuscany.sca.cpp.tools.common.Utils;

/**
 * This is the main top level class. Its purpose is to create a
 * Module/FragmentFile handler visitor and pass it to a DirectoryScanner for
 * processing.
 */
public class Scagen extends CParsingTool {

    public static Set MODULE_EXTENSIONS = new HashSet(Arrays
            .asList(new Object[] { "module", "fragment" }));

    /**
     * @throws Exception
     *  
     */
    public Scagen(String[] args) throws Exception {
        super(args);
    }

    /**
     * Take a directory scanning class and create a vistor that knows how to
     * handle any sca.module or .fragment that the scanner comes across.
     * 
     * @param args
     *            standard main args. THe values we expect in this class are
     *            scagen -dir input_dir -output output_dir
     *  
     */
    public static void main(String[] args) {
        boolean failed = false;
        try {
            Scagen env = new Scagen(args);
            ModuleOrFragmentFileHandler module_handler = new ModuleOrFragmentFileHandler();

            // Check and access the input SCA module directory
            String name = (String) Options.getOption("-dir");
            if (null == name) {
                Utils
                        .screenMessage("Please provide a SCA module directory name as a \"-dir\" option.");
                env.printUsage();
                System.exit(-1);
            }
            File source = new File(name);
            if (!source.isFile() && !source.isDirectory()) {
                Utils
                        .screenMessage("The SCA module directory provided as the \"-dir\" option cannot be accessed,");
                Utils.screenMessage("the option given was: " + source);
                env.printUsage();
                System.exit(-1);
            }

            String deployDir = null;
            try {
                deployDir = (String) Options.getOption("-deploy");
                if (null != deployDir || Options.deploy()) {
                    Utils.setReportArtefacts(true);
                }
            } catch (Exception e) {
                // let it default to null
            }

            Utils.postEvent(Utils.DEPLOYMENT_INPUT_DIRECTORY, source
                    .getAbsolutePath());

            // We check the -output option here as we wish to
            // reuse the env.maybeCreateDirectory method
            // unchanged from the original that went into axis and it will do a
            // System.exit if there is no matching Option
            String outputDirName = (String) Options.getOption("-output");
            if (null == outputDirName) {
                Utils
                        .screenMessage("Please provide an output directory name for the generated files as a \"-output\" option.");
                env.printUsage();
                System.exit(-1);
            }

            File outputDir = new File(outputDirName);

            // Check we can create the output directory
            if (outputDir == null || !outputDir.exists() && !outputDir.mkdir()) {
                Utils.screenMessage("Failed to create output directory: "
                        + outputDirName);
                env.printUsage();
                System.exit(-1);
            }

            Utils.postEvent(Utils.DEPLOYMENT_OUTPUT_DIRECTORY, outputDir
                    .getAbsolutePath());

            DirectoryScanner scanner = new DirectoryScanner(module_handler,
                    MODULE_EXTENSIONS);
            scanner.walkTree(source, outputDir, 1);

            if (0 == module_handler.getFilesActedOn()) {
                Utils
                        .screenMessage("No SCA module or fragment files were found in: "
                                + source);
            }

            failed = module_handler.failed;

        } catch (Exception exception) {
            Utils
                    .screenMessage("Unexpected error occurred while runnning the Scagen tool. The Java exception is below.");
            exception.printStackTrace();
            failed = true;
        }

        if (failed) {
            Utils
                    .outputDebugString("Finished! (but encountered problems parsing modules)");
            System.exit(-2);
        }

        Utils.outputDebugString("Finished!");
    }

    /**
     * Provide a hint to the user on how to call this class
     */
    protected void printUsage() {
        System.out
                .println("usage: Java Scagen -dir <input_directory> -output <output_directory> [-verbose] [-deploy <deploy_dir>] [-nogenerate] [-outputCommand] [-command <copy_cmd>]");
        System.out
                .println("       -dir <input_directory>: the SCA module root directory");
        System.out
                .println("       -output <output_directory>: a directory to put the generated output into");
        System.out.println("       [-verbose]: report on what scagen is doing");
        System.out
                .println("       [-deploy <deploy_dir>]: output text to help in deploying the module's artefacts");
        System.out
                .println("       [-command <copy_cmd>]: a string that is injected into the deploy text");
        System.out
                .println("       [-list]: change the deploy output text to a simple list of artefacts");
        System.out
                .println("       [-outputCommand]: change the deploy output text to command text format");
        System.out
                .println("                       output is of form \"copy_cmd <dir>file1 <deploy_dir>file1\"");
        System.out
                .println("       [-nogenerate]: do not generate proxies and wrappers");

    }

}