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.BufferedReader; 021 import java.io.File; 022 import java.io.FileReader; 023 import java.io.IOException; 024 import java.io.StringWriter; 025 import java.util.Collection; 026 import java.util.Map; 027 import java.util.Properties; 028 import java.util.regex.Matcher; 029 import java.util.regex.Pattern; 030 031 public abstract class LauncherMain { 032 033 public static final String HADOOP_JOBS = "hadoopJobs"; 034 035 protected static void run(Class<? extends LauncherMain> klass, String[] args) throws Exception { 036 LauncherMain main = klass.newInstance(); 037 main.run(args); 038 } 039 040 public static Properties getHadoopJobIds(String logFile, Pattern[] patterns) throws IOException { 041 Properties props = new Properties(); 042 StringBuffer sb = new StringBuffer(100); 043 if (!new File(logFile).exists()) { 044 System.err.println("Log file: " + logFile + " not present. Therefore no Hadoop jobids found"); 045 props.setProperty(HADOOP_JOBS, ""); 046 } 047 else { 048 BufferedReader br = new BufferedReader(new FileReader(logFile)); 049 String line = br.readLine(); 050 String separator = ""; 051 while (line != null) { 052 for (Pattern pattern : patterns) { 053 Matcher matcher = pattern.matcher(line); 054 if (matcher.find()) { 055 String jobId = matcher.group(1); 056 sb.append(separator).append(jobId); 057 separator = ","; 058 } 059 } 060 line = br.readLine(); 061 } 062 br.close(); 063 props.setProperty(HADOOP_JOBS, sb.toString()); 064 } 065 return props; 066 } 067 068 protected abstract void run(String[] args) throws Exception; 069 070 /** 071 * Write to STDOUT (the task log) the Configuration/Properties values. All properties that contain 072 * any of the strings in the maskSet will be masked when writting it to STDOUT. 073 * 074 * @param header message for the beginning of the Configuration/Properties dump. 075 * @param maskSet set with substrings of property names to mask. 076 * @param conf Configuration/Properties object to dump to STDOUT 077 * @throws IOException thrown if an IO error ocurred. 078 */ 079 @SuppressWarnings("unchecked") 080 protected static void logMasking(String header, Collection<String> maskSet, Iterable conf) throws IOException { 081 StringWriter writer = new StringWriter(); 082 writer.write(header + "\n"); 083 writer.write("--------------------\n"); 084 for (Map.Entry entry : (Iterable<Map.Entry>) conf){ 085 String name = (String) entry.getKey(); 086 String value = (String) entry.getValue(); 087 for (String mask : maskSet) { 088 if (name.contains(mask)) { 089 value = "*MASKED*"; 090 } 091 } 092 writer.write(" " + name + " : " + value + "\n"); 093 } 094 writer.write("--------------------\n"); 095 writer.close(); 096 System.out.println(writer.toString()); 097 System.out.flush(); 098 } 099 100 } 101 102 class LauncherMainException extends Exception { 103 private int errorCode; 104 105 public LauncherMainException(int code) { 106 errorCode = code; 107 } 108 109 int getErrorCode() { 110 return errorCode; 111 } 112 }