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