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