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