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 019package org.apache.oozie.action.hadoop; 020 021import org.apache.hadoop.conf.Configuration; 022import org.apache.hadoop.fs.FSDataOutputStream; 023import org.apache.hadoop.fs.FileSystem; 024import org.apache.hadoop.fs.Path; 025import org.apache.oozie.action.ActionExecutorException; 026import org.apache.oozie.util.XLog; 027import org.jdom.Element; 028import org.jdom.Namespace; 029 030import java.io.IOException; 031import java.nio.charset.StandardCharsets; 032import java.util.List; 033 034public abstract class ScriptLanguageActionExecutor extends JavaActionExecutor { 035 036 public ScriptLanguageActionExecutor(String type) { 037 super(type); 038 } 039 040 @SuppressWarnings("rawtypes") 041 @Override 042 public List<Class> getLauncherClasses() { 043 return null; 044 } 045 046 protected boolean shouldAddScriptToCache(){ 047 return true; 048 } 049 050 @Override 051 protected Configuration setupLauncherConf(Configuration conf, Element actionXml, Path appPath, Context context) 052 throws ActionExecutorException { 053 super.setupLauncherConf(conf, actionXml, appPath, context); 054 if(shouldAddScriptToCache()) { 055 addScriptToCache(conf, actionXml, appPath, context); 056 } 057 return conf; 058 059 } 060 061 protected void addScriptToCache(Configuration conf, Element actionXml, Path appPath, Context context) 062 throws ActionExecutorException { 063 Namespace ns = actionXml.getNamespace(); 064 String script = actionXml.getChild("script", ns).getTextTrim(); 065 String name = new Path(script).getName(); 066 String scriptContent = context.getProtoActionConf().get(this.getScriptName()); 067 068 Path scriptFile = null; 069 if (scriptContent != null) { // Create script on filesystem if this is 070 // an http submission job; 071 FSDataOutputStream dos = null; 072 try { 073 Path actionPath = context.getActionDir(); 074 scriptFile = new Path(actionPath, script); 075 FileSystem fs = context.getAppFileSystem(); 076 dos = fs.create(scriptFile); 077 dos.write(scriptContent.getBytes(StandardCharsets.UTF_8)); 078 079 addToCache(conf, actionPath, script + "#" + name, false); 080 } 081 catch (Exception ex) { 082 throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FAILED_OPERATION", XLog 083 .format("Not able to write script file {0} on hdfs", scriptFile), ex); 084 } 085 finally { 086 try { 087 if (dos != null) { 088 dos.close(); 089 } 090 } 091 catch (IOException ex) { 092 XLog.getLog(getClass()).error("Error: " + ex.getMessage()); 093 } 094 } 095 } 096 else { 097 addToCache(conf, appPath, script + "#" + name, false); 098 } 099 } 100 101 protected abstract String getScriptName(); 102}