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.ByteArrayInputStream;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.util.Collection;
024    
025    import org.apache.hadoop.conf.Configuration;
026    import org.apache.oozie.service.HadoopAccessorService;
027    import org.xml.sax.SAXException;
028    import org.w3c.dom.Document;
029    import org.w3c.dom.NodeList;
030    
031    import javax.xml.parsers.DocumentBuilderFactory;
032    import javax.xml.parsers.DocumentBuilder;
033    import javax.xml.parsers.ParserConfigurationException;
034    
035    /**
036     * Utility class to perform operations on the prepare block of Workflow
037     *
038     */
039    public class PrepareActionsDriver {
040    
041        /**
042         * Method to parse the prepare XML and execute the corresponding prepare actions
043         *
044         * @param prepareXML Prepare XML block in string format
045         * @throws LauncherException
046         */
047        static void doOperations(Collection<String> supportedFileSystems, String prepareXML) throws LauncherException {
048            try {
049                Document doc = getDocumentFromXML(prepareXML);
050                doc.getDocumentElement().normalize();
051    
052                // Get the list of child nodes, basically, each one corresponding to a separate action
053                NodeList nl = doc.getDocumentElement().getChildNodes();
054                FileSystemActions fsActions = new FileSystemActions(supportedFileSystems);
055    
056                for (int i = 0; i < nl.getLength(); ++i) {
057                    String commandType = "";
058                    /* Logic to find the command type goes here
059                    commandType = ..........;
060                    */
061                    // As of now, the available prepare action is of type hdfs. Hence, assigning the value directly
062                    commandType = "hdfs";
063                    if (commandType.equalsIgnoreCase("hdfs")) {
064                        fsActions.execute(nl.item(i));
065                    } /*else if(commandType.equalsIgnoreCase("hcat")) {     //Other command types go here
066                        hCatActions.execute(nl.item(i));
067                      }*/
068                }
069            } catch (IOException ioe) {
070                throw new LauncherException(ioe.getMessage(), ioe);
071            } catch (SAXException saxe) {
072                throw new LauncherException(saxe.getMessage(), saxe);
073            } catch (ParserConfigurationException pce) {
074                throw new LauncherException(pce.getMessage(), pce);
075            }
076        }
077    
078        // Method to return the document from the prepare XML block
079        static Document getDocumentFromXML(String prepareXML) throws ParserConfigurationException, SAXException,
080                IOException {
081            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
082            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
083            InputStream is = new ByteArrayInputStream(prepareXML.getBytes("UTF-8"));
084            return docBuilder.parse(is);
085        }
086    }