001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.cli;
016    
017    import org.apache.commons.cli.CommandLine;
018    import org.apache.oozie.client.AuthOozieClient;
019    import org.apache.oozie.client.OozieClient;
020    import org.apache.oozie.client.XOozieClient;
021    import java.util.Map;
022    
023    /**
024     * Oozie command line utility.
025     */
026    public class AuthOozieCLI extends OozieCLI {
027        /**
028         * Entry point for the Oozie CLI when invoked from the command line.
029         * <p/>
030         * Upon completion this method exits the JVM with '0' (success) or '-1'
031         * (failure).
032         *
033         * @param args options and arguments for the Oozie CLI.
034         */
035        public static void main(String[] args) {
036            if (!System.getProperties().contains(AuthOozieClient.USE_AUTH_TOKEN_CACHE_SYS_PROP)) {
037                System.setProperty(AuthOozieClient.USE_AUTH_TOKEN_CACHE_SYS_PROP, "true");
038            }
039            System.exit(new AuthOozieCLI().run(args));
040        }
041    
042        /**
043         * Create a OozieClient. <p/> It injects any '-Dheader:' as header to the the {@link
044         * OozieClient}.
045         *
046         * @param commandLine the parsed command line options.
047         * @return a pre configured eXtended workflow client.
048         * @throws OozieCLIException thrown if the OozieClient could not be
049         *         configured.
050         */
051        @Override
052        protected OozieClient createOozieClient(CommandLine commandLine) throws OozieCLIException {
053            return createXOozieClient(commandLine);
054        }
055    
056        //TODO: This method should be made protected in OozieCLI so we don't have to reimplement it here
057        private void addHeader(OozieClient wc) {
058            for (Map.Entry entry : System.getProperties().entrySet()) {
059                String key = (String) entry.getKey();
060                if (key.startsWith(WS_HEADER_PREFIX)) {
061                    String header = key.substring(WS_HEADER_PREFIX.length());
062                    wc.setHeader(header, (String) entry.getValue());
063                }
064            }
065        }
066    
067        /**
068         * Create a XOozieClient. <p/> It injects any '-Dheader:' as header to the the {@link
069         * OozieClient}.
070         *
071         * @param commandLine the parsed command line options.
072         * @return a pre configured eXtended workflow client.
073         * @throws OozieCLIException thrown if the XOozieClient could not be
074         *         configured.
075         */
076        @Override
077        protected XOozieClient createXOozieClient(CommandLine commandLine) throws OozieCLIException {
078            XOozieClient wc = new AuthOozieClient(getOozieUrl(commandLine));
079            addHeader(wc);
080            return wc;
081        }
082    
083    }