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 java.util.List;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.Path;
024import org.apache.oozie.action.ActionExecutorException;
025import org.apache.oozie.service.Services;
026import org.apache.oozie.util.XLog;
027import org.jdom.Element;
028
029
030public class DistcpActionExecutor extends JavaActionExecutor{
031    public static final String CONF_OOZIE_DISTCP_ACTION_MAIN_CLASS = "org.apache.hadoop.tools.DistCp";
032    public static final String CLASS_NAMES = "oozie.actions.main.classnames";
033    private static final XLog LOG = XLog.getLog(DistcpActionExecutor.class);
034    public static final String DISTCP_TYPE = "distcp";
035
036    public DistcpActionExecutor() {
037        super("distcp");
038    }
039
040    @Override
041    Configuration setupActionConf(Configuration actionConf, Context context, Element actionXml, Path appPath)
042            throws ActionExecutorException {
043        actionConf = super.setupActionConf(actionConf, context, actionXml, appPath);
044        String classNameDistcp = CONF_OOZIE_DISTCP_ACTION_MAIN_CLASS;
045        String name = getClassNamebyType(DISTCP_TYPE);
046        if(name != null){
047            classNameDistcp = name;
048        }
049        actionConf.set(JavaMain.JAVA_MAIN_CLASS, classNameDistcp);
050        return actionConf;
051    }
052
053    @Override
054    public List<Class> getLauncherClasses() {
055       return super.getLauncherClasses();
056    }
057
058    /**
059     * This function returns the Action classes names from the configuration
060     *
061     * @param type This is type of the action classes
062     * @return Name of the class from the configuration
063     */
064    public static String getClassNamebyType(String type){
065        Configuration conf = Services.get().getConf();
066        String classname = null;
067        if (conf.get(CLASS_NAMES, "").trim().length() > 0) {
068            for (String function : conf.getStrings(CLASS_NAMES)) {
069                function = DistcpActionExecutor.Trim(function);
070                LOG.debug("class for Distcp Action: " + function);
071                String[] str = function.split("=");
072                if (str.length > 0) {
073                    if(type.equalsIgnoreCase(str[0])){
074                        classname = new String(str[1]);
075                    }
076                }
077            }
078        }
079        return classname;
080    }
081
082    /**
083     * To trim string
084     *
085     * @param str
086     * @return trim string
087     */
088    public static String Trim(String str) {
089        if (str != null) {
090            str = str.replaceAll("\\n", "");
091            str = str.replaceAll("\\t", "");
092            str = str.trim();
093        }
094        return str;
095    }
096
097    /**
098     * Return the sharelib name for the action.
099     *
100     * @return returns <code>distcp</code>.
101     * @param actionXml
102     */
103    @Override
104    protected String getDefaultShareLibName(Element actionXml) {
105        return "distcp";
106    }
107
108}