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