View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.hadoop.tools.globalfeature;
31  
32  import java.io.IOException;
33  import java.net.URI;
34  import java.util.List;
35  
36  import org.apache.hadoop.conf.Configuration;
37  import org.apache.hadoop.fs.FileSystem;
38  import org.apache.hadoop.fs.LocalFileSystem;
39  import org.apache.hadoop.fs.Path;
40  import org.kohsuke.args4j.CmdLineException;
41  import org.kohsuke.args4j.CmdLineParser;
42  import org.kohsuke.args4j.Option;
43  import org.kohsuke.args4j.ProxyOptionHandler;
44  import org.openimaj.hadoop.sequencefile.SequenceFileUtility;
45  import org.openimaj.tools.globalfeature.GlobalFeatureExtractor;
46  import org.openimaj.tools.globalfeature.GlobalFeatureType;
47  
48  
49  /**
50   * Options for the Hadoop version of the GlobalFeaturesTool
51   * 
52   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
53   */
54  public class HadoopGlobalFeaturesOptions {
55  	private String[] args;
56  	
57  	@Option(name="--feature-type", aliases="-f", handler=ProxyOptionHandler.class, usage="Feature type", required=true)
58  	protected GlobalFeatureType feature;
59  	protected GlobalFeatureExtractor featureOp;
60  	
61  	@Option(name = "-input", aliases="-i", required=true, usage="Set the input path(s) or uri(s)")
62  	protected List<String> input;
63  	
64  	@Option(name = "-output", aliases="-o", required=true, usage="Set the output location")
65  	protected String output;
66  	
67  	@Option(name = "--binary", aliases="-b", required=false, usage="Set output mode to binary")
68  	protected boolean binary = false;
69  	
70  	@Option(name="--remove", aliases="-rm", required=false, usage="Remove the existing output location if it exists.", metaVar="BOOLEAN")
71  	private boolean replace = false;
72  
73  	private boolean beforeMaps;
74  	
75  	/**
76  	 * Construct with the argument string.
77  	 * @param args the command line argumens
78  	 */
79  	public HadoopGlobalFeaturesOptions(String[] args)
80  	{
81  		this(args, false);
82  	}
83  	
84  	/**
85  	 * Construct with the argument string.
86  	 * @param args the command line argumens
87  	 * @param beforeMaps if true, then in the tool; false in a mapper.
88  	 */
89  	public HadoopGlobalFeaturesOptions(String[] args, boolean beforeMaps) {
90  		this.args = args;
91  		this.beforeMaps = beforeMaps;
92  		prepare();
93  	}
94  	
95  	private void prepare() {
96  		CmdLineParser parser = new CmdLineParser(this);
97  
98  		try {
99  			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 }