001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.util.cli;
018:
019: import java.io.File;
020: import java.io.IOException;
021:
022: import org.apache.commons.cli.Options;
023: import org.apache.commons.cli.Option;
024: import org.apache.commons.cli.CommandLine;
025: import org.apache.commons.cli.CommandLineParser;
026: import org.apache.commons.cli.PosixParser;
027: import org.apache.commons.cli.ParseException;
028: import org.apache.commons.cli.HelpFormatter;
029: import org.apache.pluto.util.assemble.Assembler;
030: import org.apache.pluto.util.assemble.AssemblerFactory;
031: import org.apache.pluto.util.assemble.AssemblerConfig;
032: import org.apache.pluto.util.UtilityException;
033:
034: /**
035: * Command Line Interface to the Pluto Assembler.
036: *
037: * @version 1.0
038: * @since Oct 15, 2004
039: */
040: public class AssemblerCLI {
041:
042: private final Options options;
043: private final String[] args;
044:
045: public AssemblerCLI(String[] args) {
046: this .args = args;
047: options = new Options();
048: Option destination = new Option("d", "destination", true,
049: "specify where the resulting webapp should be written ");
050: destination.setArgName("file");
051:
052: Option debug = new Option("debug", false,
053: "print debug information.");
054: options.addOption(destination);
055: options.addOption(debug);
056: }
057:
058: public void run() throws ParseException, IOException,
059: UtilityException {
060: CommandLineParser parser = new PosixParser();
061: CommandLine line = parser.parse(options, args);
062:
063: String[] args = line.getArgs();
064: if (args.length != 1) {
065: abort();
066: return;
067: }
068:
069: String dest = line.getOptionValue("file");
070: if (dest == null) {
071: dest = args[0];
072: }
073:
074: File source = new File(args[0]);
075: File result = new File(dest);
076: result.getParentFile().mkdirs();
077:
078: if (!source.exists()) {
079: System.out.println("File does not exist: "
080: + source.getCanonicalPath());
081: }
082:
083: System.out
084: .println("-----------------------------------------------");
085: System.out.println("Assembling: " + source.getCanonicalPath());
086: System.out.println(" to: " + result.getCanonicalPath());
087:
088: File webXml = new File(args[0], Assembler.SERVLET_XML);
089: File portletXml = new File(args[0], Assembler.PORTLET_XML);
090:
091: AssemblerConfig config = new AssemblerConfig();
092: config.setWebappDescriptor(webXml);
093: config.setPortletDescriptor(portletXml);
094: config.setDestination(webXml);
095:
096: Assembler assembler = AssemblerFactory.getFactory()
097: .createAssembler(config);
098:
099: config.setPortletDescriptor(portletXml);
100: config.setWebappDescriptor(webXml);
101:
102: assembler.assemble(config);
103:
104: System.out.println("Complete!");
105: }
106:
107: public void abort() {
108: HelpFormatter help = new HelpFormatter();
109: help.defaultArgName = "webapp";
110: help.defaultWidth = 60;
111: help.printHelp("assemble", options);
112: }
113:
114: public static void main(String[] args) throws ParseException,
115: IOException, UtilityException {
116: new AssemblerCLI(args).run();
117: }
118: }
|