001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     * 
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.oozie.action.hadoop;
019    
020    import java.io.IOException;
021    import java.io.StringWriter;
022    import java.util.Collection;
023    import java.util.Map;
024    
025    public abstract class LauncherMain {
026    
027        protected static void run(Class<? extends LauncherMain> klass, String[] args) throws Exception {
028            LauncherMain main = klass.newInstance();
029            main.run(args);
030        }
031    
032        protected abstract void run(String[] args) throws Exception;
033    
034        /**
035         * Write to STDOUT (the task log) the Configuration/Properties values. All properties that contain
036         * any of the strings in the maskSet will be masked when writting it to STDOUT.
037         *
038         * @param header message for the beginning of the Configuration/Properties dump.
039         * @param maskSet set with substrings of property names to mask.
040         * @param conf Configuration/Properties object to dump to STDOUT
041         * @throws IOException thrown if an IO error ocurred.
042         */
043        @SuppressWarnings("unchecked")
044        protected static void logMasking(String header, Collection<String> maskSet, Iterable conf) throws IOException {
045            StringWriter writer = new StringWriter();
046            writer.write(header + "\n");
047            writer.write("--------------------\n");
048            for (Map.Entry entry : (Iterable<Map.Entry>) conf){
049                String name = (String) entry.getKey();
050                String value = (String) entry.getValue();
051                for (String mask : maskSet) {
052                    if (name.contains(mask)) {
053                        value = "*MASKED*";
054                    }
055                }
056                writer.write(" " + name + " : " + value + "\n");
057            }
058            writer.write("--------------------\n");
059            writer.close();
060            System.out.println(writer.toString());
061            System.out.flush();
062        }
063    
064    }
065    
066    class LauncherMainException extends Exception {
067        private int errorCode;
068        
069        public LauncherMainException(int code) {
070            errorCode = code;
071        }
072        
073        int getErrorCode() {
074            return errorCode;
075        }
076    }