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.geronimo.cli.deployer;
018    
019    import org.apache.commons.cli.CommandLine;
020    import org.apache.commons.cli.CommandLineParser;
021    import org.apache.commons.cli.GnuParser;
022    import org.apache.commons.cli.Option;
023    import org.apache.commons.cli.OptionBuilder;
024    import org.apache.commons.cli.Options;
025    import org.apache.commons.cli.ParseException;
026    import org.apache.geronimo.cli.CLParserException;
027    
028    /**
029     * @version $Rev: 476049 $ $Date: 2006-11-17 15:35:17 +1100 (Fri, 17 Nov 2006) $
030     */
031    public class DistributeCommandArgs implements CommandArgs {
032        private final static String ARGUMENT_IN_PLACE_SHORTFORM = "i";
033        private final static String ARGUMENT_IN_PLACE = "inPlace";
034    
035        private final static String ARGUMENT_TARGETS_SHORTFORM = "t";
036        private final static String ARGUMENT_TARGETS = "targets";
037        
038        protected final Options options;
039        protected CommandLine commandLine;
040    
041        public DistributeCommandArgs(String[] args) throws CLParserException {
042            options = new Options();
043            addInPlace();
044            addTargets();
045            
046            CommandLineParser parser = new GnuParser();
047            try {
048                commandLine = parser.parse(options, args, true);
049            } catch (ParseException e) {
050                throw new CLParserException(e.getMessage(), e);
051            }
052            
053            if (0 == commandLine.getArgs().length) {
054                throw new CLParserException("Must specify a module or plan (or both)");
055            } else if (2 < commandLine.getArgs().length) {
056                throw new CLParserException("Too many arguments");
057            }
058        }
059    
060        protected void addTargets() {
061            OptionBuilder optionBuilder = OptionBuilder.hasArg().withArgName("targets");
062            optionBuilder = optionBuilder.withLongOpt(ARGUMENT_TARGETS);
063            optionBuilder = optionBuilder
064                    .withDescription("If no targets are provided, the module is distributed to all available "
065                            + "targets. Geronimo only provides one target (ever), so this is primarily "
066                            + "useful when using a different driver.\n");
067            Option option = optionBuilder.create(ARGUMENT_TARGETS_SHORTFORM);
068            options.addOption(option);
069        }
070    
071        protected void addInPlace() {
072            options.addOption(ARGUMENT_IN_PLACE_SHORTFORM,
073                    ARGUMENT_IN_PLACE,
074                    false,
075                    "If inPlace is provided, the module is not copied to the configuration "
076                            + "store of the selected targets. The targets directly use the module.");
077        }
078    
079        public String[] getTargets() {
080            String targets = commandLine.getOptionValue(ARGUMENT_TARGETS_SHORTFORM);
081            if (null == targets) {
082                return new String[0];
083            }
084            return targets.split(";");
085        }
086        
087        public boolean isInPlace() {
088            return commandLine.hasOption(ARGUMENT_IN_PLACE_SHORTFORM);
089        }
090        
091        public String[] getArgs() {
092            return commandLine.getArgs();
093        }
094        
095    }