This project has retired. For details please refer to its Attic page.
Source code
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.dependency;
020
021import java.net.URI;
022import java.net.URISyntaxException;
023import java.util.ArrayList;
024import java.util.List;
025import org.apache.commons.lang.StringUtils;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.oozie.ErrorCode;
028import org.apache.oozie.client.OozieClient;
029import org.apache.oozie.command.CommandException;
030import org.apache.oozie.coord.CoordELFunctions;
031import org.apache.oozie.service.Services;
032import org.apache.oozie.service.URIHandlerService;
033import org.apache.oozie.util.ParamChecker;
034import org.apache.oozie.util.XLog;
035
036public class DependencyChecker {
037
038    /**
039     * Return a string of missing dependencies concatenated by CoordELFunctions.INSTANCE_SEPARATOR
040     *
041     * @param missingDependencies list of missing dependencies
042     * @return missing dependencies as a string
043     */
044    public static String dependenciesAsString(List<String> missingDependencies) {
045        return StringUtils.join(missingDependencies, CoordELFunctions.INSTANCE_SEPARATOR);
046    }
047
048    /**
049     * Return a array of missing dependencies
050     *
051     * @param missingDependencies missing dependencies concatenated by
052     *        CoordELFunctions.INSTANCE_SEPARATOR
053     * @return missing dependencies as a array
054     */
055    public static String[] dependenciesAsArray(String missingDependencies) {
056        return missingDependencies.split(CoordELFunctions.INSTANCE_SEPARATOR);
057    }
058
059    /**
060     * Get the currently missing and available dependencies after checking the list of known missing
061     * dependencies against the source.
062     *
063     * @param missingDependencies known missing dependencies
064     * @param actionConf Configuration for the action
065     * @param stopOnFirstMissing Does not continue check for the rest of list if there is a missing
066     *        dependency
067     * @return ActionDependency which has the list of missing and available dependencies
068     * @throws CommandException
069     */
070    public static ActionDependency checkForAvailability(String missingDependencies, Configuration actionConf,
071            boolean stopOnFirstMissing) throws CommandException {
072        return checkForAvailability(dependenciesAsArray(missingDependencies), actionConf, stopOnFirstMissing);
073    }
074
075    /**
076     * Get the currently missing and available dependencies after checking the list of known missing
077     * dependencies against the source.
078     *
079     * @param missingDependencies known missing dependencies
080     * @param actionConf Configuration for the action
081     * @param stopOnFirstMissing Does not continue check for the rest of list if there is a missing
082     *        dependency
083     * @return ActionDependency which has the list of missing and available dependencies
084     * @throws CommandException
085     */
086    public static ActionDependency checkForAvailability(String[] missingDependencies, Configuration actionConf,
087            boolean stopOnFirstMissing) throws CommandException {
088        final XLog LOG = XLog.getLog(DependencyChecker.class); //OOZIE-1251. Don't initialize as static variable.
089        String user = ParamChecker.notEmpty(actionConf.get(OozieClient.USER_NAME), OozieClient.USER_NAME);
090        List<String> missingDeps = new ArrayList<String>();
091        List<String> availableDeps = new ArrayList<String>();
092        URIHandlerService uriService = Services.get().get(URIHandlerService.class);
093        boolean continueChecking = true;
094        try {
095            for (int index = 0; index < missingDependencies.length; index++) {
096                if (continueChecking) {
097                    String dependency = missingDependencies[index];
098
099                    URI uri = new URI(dependency);
100                    URIHandler uriHandler = uriService.getURIHandler(uri);
101                    LOG.debug("Checking for the availability of dependency [{0}] ", dependency);
102                    if (uriHandler.exists(uri, actionConf, user)) {
103                        LOG.debug("Dependency [{0}] is available", dependency);
104                        availableDeps.add(dependency);
105                    }
106                    else {
107                        LOG.debug("Dependency [{0}] is missing", dependency);
108                        missingDeps.add(dependency);
109                        if (stopOnFirstMissing) {
110                            continueChecking = false;
111                        }
112                    }
113
114                }
115                else {
116                    missingDeps.add(missingDependencies[index]);
117                }
118            }
119        }
120        catch (URISyntaxException e) {
121            throw new CommandException(ErrorCode.E0906, e.getMessage(), e);
122        }
123        catch (URIHandlerException e) {
124            throw new CommandException(e);
125        }
126        return new ActionDependency(missingDeps, availableDeps);
127    }
128}