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