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.storm.tool;
031
032import java.io.IOException;
033import java.util.ArrayList;
034import java.util.List;
035
036import org.kohsuke.args4j.CmdLineException;
037import org.kohsuke.args4j.CmdLineParser;
038import org.kohsuke.args4j.Option;
039import org.kohsuke.args4j.ProxyOptionHandler;
040import org.openimaj.kestrel.KestrelServerSpec;
041import org.openimaj.rdf.storm.tool.topology.TopologyMode;
042import org.openimaj.rdf.storm.tool.topology.TopologyModeOption;
043import org.openimaj.tools.InOutToolOptions;
044
045import backtype.storm.Config;
046import backtype.storm.generated.StormTopology;
047
048/**
049 * @author Jon Hare (jsh2@ecs.soton.ac.uk), Sina Samangooei (ss@ecs.soton.ac.uk)
050 * 
051 */
052public abstract class StormToolOptions extends InOutToolOptions {
053        private static final String KESTREL_FORMAT = "%s:%s";
054        /**
055         * The topology
056         */
057        @Option(
058                        name = "--topology-mode",
059                        aliases = "-tm",
060                        required = false,
061                        usage = "The kind of topology to submit to",
062                        handler = ProxyOptionHandler.class)
063        public TopologyModeOption tm = TopologyModeOption.STORM;
064        public TopologyMode tmOp = tm.getOptions();
065
066        /**
067         * the ketrel queues for input and output
068         */
069        @Option(
070                        name = "--kestrel-host",
071                        aliases = "-kh",
072                        required = false,
073                        usage = "The message queue host from which and to which triples will be written",
074                        metaVar = "STRING",
075                        multiValued = true)
076        public List<String> kestrelHosts = new ArrayList<String>();
077
078        /**
079         * number of topology workers selected
080         */
081        @Option(
082                        name = "--topology-workers",
083                        aliases = "-twork",
084                        required = false,
085                        usage = "The number of workers running the executors of this topology")
086        public int numberOfWorkers = 2;
087        /**
088         * parsed kestrel server specs
089         */
090        public List<KestrelServerSpec> kestrelSpecList = new ArrayList<KestrelServerSpec>();
091
092        private String[] args;
093
094        /**
095         * @param args
096         */
097        public StormToolOptions(String[] args) {
098                this.args = args;
099        }
100
101        /**
102         * @return provides a config based on the options
103         */
104        public abstract Config prepareConfig();
105
106        /**
107         * @return provides a {@link StormTopology} based on the options
108         */
109        public abstract StormTopology constructTopology();
110
111        /**
112         * @return the topology name
113         */
114        public abstract String topologyName();
115
116        /**
117         * called when the {@link TopologyModeOption} believes it is done
118         */
119        public abstract void topologyCleanup();
120
121        /**
122         * Parse arguments and validate
123         * 
124         * @throws IOException
125         */
126        public void prepare() throws IOException {
127                final CmdLineParser parser = new CmdLineParser(this);
128                try {
129                        parser.parseArgument(args);
130                        this.validateInternal(parser);
131                } catch (final CmdLineException e) {
132                        System.err.println(e.getMessage());
133                        System.err.println("Usage: java -jar ReteStormTool.jar [options...] ");
134                        parser.printUsage(System.err);
135                        System.err.println(this.getExtractUsageInfo());
136                        System.exit(1);
137                }
138        }
139
140        /**
141         * @return usage of the tool
142         */
143        public abstract String getExtractUsageInfo();
144
145        /**
146         * validate the parsed input, could be overwritten by sublcasses
147         * 
148         * @param parser
149         * @throws Exception
150         * @throws IOException
151         * @throws CmdLineException
152         */
153        private void validateInternal(CmdLineParser parser) throws CmdLineException, IOException {
154                if (this.kestrelHosts.size() == 0) {
155                        this.kestrelHosts.add(String.format(KESTREL_FORMAT, KestrelServerSpec.LOCALHOST, KestrelServerSpec.DEFAULT_KESTREL_THRIFT_PORT));
156                }
157                this.kestrelSpecList = KestrelServerSpec.parseKestrelAddressList(this.kestrelHosts);
158                this.validate(parser);
159        }
160
161        /**
162         * @param parser
163         *            initialise the tool
164         * @throws IOException
165         * @throws CmdLineException
166         *             when something goes wrong
167         */
168        public abstract void validate(CmdLineParser parser) throws CmdLineException, IOException;
169}