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.action.hadoop;
016    
017    import java.io.IOException;
018    import java.io.StringWriter;
019    import java.util.Collection;
020    import java.util.Map;
021    
022    public abstract class LauncherMain {
023    
024        protected static void run(Class<? extends LauncherMain> klass, String[] args) throws Exception {
025            LauncherMain main = klass.newInstance();
026            main.run(args);
027        }
028    
029        protected abstract void run(String[] args) throws Exception;
030    
031        /**
032         * Write to STDOUT (the task log) the Configuration/Properties values. All properties that contain
033         * any of the strings in the maskSet will be masked when writting it to STDOUT.
034         *
035         * @param header message for the beginning of the Configuration/Properties dump.
036         * @param maskSet set with substrings of property names to mask.
037         * @param conf Configuration/Properties object to dump to STDOUT
038         * @throws IOException thrown if an IO error ocurred.
039         */
040        @SuppressWarnings("unchecked")
041        protected static void logMasking(String header, Collection<String> maskSet, Iterable conf) throws IOException {
042            StringWriter writer = new StringWriter();
043            writer.write(header + "\n");
044            writer.write("--------------------\n");
045            for (Map.Entry entry : (Iterable<Map.Entry>) conf){
046                String name = (String) entry.getKey();
047                String value = (String) entry.getValue();
048                for (String mask : maskSet) {
049                    if (name.contains(mask)) {
050                        value = "*MASKED*";
051                    }
052                }
053                writer.write(" " + name + " : " + value + "\n");
054            }
055            writer.write("--------------------\n");
056            writer.close();
057            System.out.println(writer.toString());
058            System.out.flush();
059        }
060    
061    }
062    
063    class LauncherMainException extends Exception {
064        private int errorCode;
065        
066        public LauncherMainException(int code) {
067            errorCode = code;
068        }
069        
070        int getErrorCode() {
071            return errorCode;
072        }
073    }