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.io.IOException;
021    import java.util.Collection;
022    import java.util.HashSet;
023    
024    import org.apache.hadoop.fs.FileSystem;
025    import org.apache.hadoop.conf.Configuration;
026    import org.apache.hadoop.fs.Path;
027    import org.w3c.dom.Node;
028    /**
029     * Class to perform file system operations specified in the prepare block of Workflow
030     *
031     */
032    public class FileSystemActions {
033        private static Collection<String> supportedFileSystems;
034    
035        public FileSystemActions(Collection<String> fileSystems) {
036            supportedFileSystems = fileSystems;
037        }
038    
039        /**
040         * Method to execute the prepare actions based on the command
041         *
042         * @param n Child node of the prepare XML
043         * @throws LauncherException
044         */
045        public void execute(Node n) throws LauncherException {
046            String command = n.getNodeName();
047            if (command.equals("delete")) {
048                delete(new Path(n.getAttributes().getNamedItem("path").getNodeValue().trim()));
049            } else if (command.equals("mkdir")) {
050                mkdir(new Path(n.getAttributes().getNamedItem("path").getNodeValue().trim()));
051            }
052        }
053    
054        // Method to delete the specified file based on the path
055        private void delete(Path path) throws LauncherException {
056            try {
057                validatePath(path, true);
058                FileSystem fs = FileSystem.get(path.toUri(), new Configuration());
059                if (fs.exists(path)) {
060                    if (!fs.delete(path, true)) {
061                        String deleteFailed = "Deletion of path " + path.toString() + " failed.";
062                        System.out.println(deleteFailed);
063                        throw new LauncherException(deleteFailed);
064                    } else {
065                        System.out.println("Deletion of path " + path.toString() + " was successful.");
066                    }
067                }
068            } catch (IOException ex) {
069                throw new LauncherException(ex.getMessage(), ex);
070            }
071    
072        }
073    
074        // Method to create a directory based on the path
075        private void mkdir(Path path) throws LauncherException {
076            try {
077                validatePath(path, true);
078                FileSystem fs = FileSystem.get(path.toUri(), new Configuration());
079                if (!fs.exists(path)) {
080                    if (!fs.mkdirs(path)) {
081                        String mkdirFailed = "Creating directory at " + path + " failed.";
082                        System.out.println(mkdirFailed);
083                        throw new LauncherException(mkdirFailed);
084                    } else {
085                        System.out.println("Creating directory at path " + path + " was successful.");
086                    }
087                }
088            } catch (IOException ex) {
089                throw new LauncherException(ex.getMessage(), ex);
090            }
091        }
092    
093        // Method to validate the path provided for the prepare action
094        private void validatePath(Path path, boolean withScheme) throws LauncherException {
095            String scheme = path.toUri().getScheme();
096            if (withScheme) {
097                if (scheme == null) {
098                    String nullScheme = "Scheme of the path " + path + " is null";
099                    System.out.println(nullScheme);
100                    throw new LauncherException(nullScheme);
101                } else if (supportedFileSystems.size() != 1 || !supportedFileSystems.iterator().next().equals("*")) {
102                    if (!supportedFileSystems.contains(scheme.toLowerCase())) {
103                        String unsupportedScheme = "Scheme of '" + path + "' is not supported.";
104                        System.out.println(unsupportedScheme);
105                        throw new LauncherException(unsupportedScheme);
106                    }
107                }
108            } else if (scheme != null) {
109                String notNullScheme = "Scheme of the path " + path + " is not null as specified.";
110                System.out.println(notNullScheme);
111                throw new LauncherException(notNullScheme);
112            }
113        }
114    }