001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.util;
016    
017    import java.io.IOException;
018    
019    import org.apache.hadoop.conf.Configuration;
020    import org.apache.hadoop.fs.FileStatus;
021    import org.apache.hadoop.fs.FileSystem;
022    import org.apache.hadoop.fs.Path;
023    import org.apache.oozie.client.OozieClient;
024    import org.apache.oozie.client.XOozieClient;
025    import org.apache.oozie.service.HadoopAccessorException;
026    import org.apache.oozie.service.HadoopAccessorService;
027    import org.apache.oozie.service.Services;
028    
029    /**
030     * Job utilities.
031     */
032    public class JobUtils {
033        /**
034         * Normalize appPath in job conf with the provided user/group - If it's not
035         * jobs via proxy submission, after normalization appPath always points to
036         * job's Xml definition file.
037         * <p/>
038         * 
039         * @param user user
040         * @param group group
041         * @param conf job configuration.
042         * @throws IOException thrown if normalization can not be done properly.
043         */
044        public static void normalizeAppPath(String user, String group, Configuration conf) throws IOException {
045            if (user == null) {
046                throw new IllegalArgumentException("user cannot be null");
047            }
048            
049            if (group == null) {
050                throw new IllegalArgumentException("group cannot be null");
051            }
052            
053            if (conf.get(XOozieClient.IS_PROXY_SUBMISSION) != null) { // do nothing for proxy submission job;
054                return;
055            }
056    
057            String wfPathStr = conf.get(OozieClient.APP_PATH);
058            String coordPathStr = conf.get(OozieClient.COORDINATOR_APP_PATH);
059            String appPathStr = wfPathStr != null ? wfPathStr : coordPathStr;
060    
061            FileSystem fs = null;
062            try {
063                fs = Services.get().get(HadoopAccessorService.class).createFileSystem(user, group, new Path(appPathStr).toUri(), conf);
064            }
065            catch (HadoopAccessorException ex) {
066                throw new IOException(ex.getMessage());
067            }
068    
069            Path appPath = new Path(appPathStr);
070            String normalizedAppPathStr = appPathStr;
071            if (!fs.exists(appPath)) {
072                throw new IOException("Error: " + appPathStr + " does not exist");
073            }
074    
075            FileStatus fileStatus = fs.getFileStatus(appPath);
076            Path appXml = appPath;
077            // Normalize appPath here - it will always point to a workflow/coordinator xml definition file;
078            if (fileStatus.isDir()) {
079                appXml = new Path(appPath, (wfPathStr != null)? "workflow.xml" : "coordinator.xml");
080                normalizedAppPathStr = appXml.toString();
081            }
082    
083            if (wfPathStr != null) {
084                conf.set(OozieClient.APP_PATH, normalizedAppPathStr);
085            }
086            else if (coordPathStr != null) {
087                conf.set(OozieClient.COORDINATOR_APP_PATH, normalizedAppPathStr);
088            }
089        }
090    }