001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.hadoop.tools.globalfeature;
031
032import java.io.IOException;
033import java.net.URI;
034import java.util.List;
035
036import org.apache.hadoop.conf.Configuration;
037import org.apache.hadoop.fs.FileSystem;
038import org.apache.hadoop.fs.LocalFileSystem;
039import org.apache.hadoop.fs.Path;
040import org.kohsuke.args4j.CmdLineException;
041import org.kohsuke.args4j.CmdLineParser;
042import org.kohsuke.args4j.Option;
043import org.kohsuke.args4j.ProxyOptionHandler;
044import org.openimaj.hadoop.sequencefile.SequenceFileUtility;
045import org.openimaj.tools.globalfeature.GlobalFeatureExtractor;
046import org.openimaj.tools.globalfeature.GlobalFeatureType;
047
048
049/**
050 * Options for the Hadoop version of the GlobalFeaturesTool
051 * 
052 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
053 */
054public class HadoopGlobalFeaturesOptions {
055        private String[] args;
056        
057        @Option(name="--feature-type", aliases="-f", handler=ProxyOptionHandler.class, usage="Feature type", required=true)
058        protected GlobalFeatureType feature;
059        protected GlobalFeatureExtractor featureOp;
060        
061        @Option(name = "-input", aliases="-i", required=true, usage="Set the input path(s) or uri(s)")
062        protected List<String> input;
063        
064        @Option(name = "-output", aliases="-o", required=true, usage="Set the output location")
065        protected String output;
066        
067        @Option(name = "--binary", aliases="-b", required=false, usage="Set output mode to binary")
068        protected boolean binary = false;
069        
070        @Option(name="--remove", aliases="-rm", required=false, usage="Remove the existing output location if it exists.", metaVar="BOOLEAN")
071        private boolean replace = false;
072
073        private boolean beforeMaps;
074        
075        /**
076         * Construct with the argument string.
077         * @param args the command line argumens
078         */
079        public HadoopGlobalFeaturesOptions(String[] args)
080        {
081                this(args, false);
082        }
083        
084        /**
085         * Construct with the argument string.
086         * @param args the command line argumens
087         * @param beforeMaps if true, then in the tool; false in a mapper.
088         */
089        public HadoopGlobalFeaturesOptions(String[] args, boolean beforeMaps) {
090                this.args = args;
091                this.beforeMaps = beforeMaps;
092                prepare();
093        }
094        
095        private void prepare() {
096                CmdLineParser parser = new CmdLineParser(this);
097
098                try {
099                        parser.parseArgument(args);
100                } catch (CmdLineException e) {
101                        System.err.println(e.getMessage());
102                        System.err.println("Usage: java -jar HadoopGlobalFeaturesTool.jar [options...]");
103                        parser.printUsage(System.err);
104                        
105                        if (feature == null) {
106                                for (GlobalFeatureType m : GlobalFeatureType.values()) {
107                                        System.err.println();
108                                        System.err.println(m + " options: ");
109                                        new CmdLineParser(m.getOptions()).printUsage(System.err);
110                                }
111                        }
112                        System.exit(1);
113                }
114        }
115
116        protected void validate() {
117                if(replace && beforeMaps) {
118                        try {
119                                URI outuri = SequenceFileUtility.convertToURI(output);
120                                FileSystem fs = getFileSystem(outuri);
121                                fs.delete(new Path(outuri.toString()), true);
122                        } catch (IOException e) {
123                                
124                        }
125                }
126        }
127        
128        static FileSystem getFileSystem(URI uri) throws IOException {
129                Configuration config = new Configuration();
130                FileSystem fs = FileSystem.get(uri, config);
131                if (fs instanceof LocalFileSystem) fs = ((LocalFileSystem)fs).getRaw();
132                return fs;
133        }
134}