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