View Javadoc

1   /*   Copyright (C) 2004 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.tools.ant.taskdefs;
19  
20  import org.apache.tools.ant.BuildException;
21  import org.apache.tools.ant.taskdefs.War;
22  import org.apache.tools.ant.types.ZipFileSet;
23  import org.w3c.dom.Document;
24  import org.w3c.dom.NamedNodeMap;
25  import org.w3c.dom.Node;
26  import org.w3c.dom.NodeList;
27  import org.xml.sax.SAXException;
28  
29  import javax.xml.parsers.DocumentBuilder;
30  import javax.xml.parsers.DocumentBuilderFactory;
31  import javax.xml.parsers.ParserConfigurationException;
32  import java.io.File;
33  import java.io.IOException;
34  
35  /*** Extension to the regular war task to include files specified in the libXmlFile. */
36  public class WarAutoLib extends War {
37  
38     /*** Directory which contains the library files. Default is lib. */
39     public File directory;
40  
41     /*** Holds value of property libXmlFile. */
42     private File libXmlFile;
43  
44  
45     /*** Setter for libDir property.
46      * @param directory Directory which contains the library files.
47      */
48     public void setLibdir(File directory) {
49        // Note: f will automatically be absolute (resolved from project basedir).
50        this.directory = directory;
51     }
52  
53  
54     /*** Method called by ant to execute this task.
55      * @throws BuildException Exception which is thrown to stop the build process and display an error message
56      */
57     public void execute() throws BuildException {
58        if (this.directory == null) {
59           directory = new File("lib"); // default
60        }
61        if (!this.directory.exists()) {
62           this.directory.mkdirs();
63        }
64        if (!directory.isDirectory()) {
65           throw new BuildException("lib directory not properly set.");
66        }
67        try {
68           DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
69           dbf.setValidating(true);
70           DocumentBuilder db = dbf.newDocumentBuilder();
71           Document doc = db.parse(libXmlFile);
72           doc.normalize();
73           NodeList warTags = doc.getElementsByTagName("war");
74           if (warTags.getLength() > 0) {
75              NodeList nl = warTags.item(0).getChildNodes();
76              for (int i = 0; i < nl.getLength(); i++) {
77                 Node node = nl.item(i);
78                 if (!node.getNodeName().equals("lib") && !node.getNodeName().equals("localLib")) continue;
79                 NamedNodeMap atts = node.getAttributes();
80                 Node att = atts.getNamedItem("file");
81                 if (att == null) {
82                    att = atts.getNamedItem("url");
83                 }
84                 String filename = att.getNodeValue();
85                 if (filename.indexOf("/") > -1) {
86                    filename = filename.substring(filename.lastIndexOf("/") + 1);
87                 }
88                 File file = new File(directory.getAbsolutePath(), filename);
89                 log("adding lib " + filename);
90                 ZipFileSet zfs = new ZipFileSet();
91                 zfs.setFile(file);
92                 addLib(zfs);
93              }
94           }
95           else {
96              log("Not adding any libs because war tag is not found in " + libXmlFile.getName());
97           }
98           super.execute();
99        }
100       catch (ParserConfigurationException pce) {
101          throw new BuildException(pce);
102       }
103       catch (SAXException se) {
104          throw new BuildException(se);
105       }
106       catch (IOException ie) {
107          throw new BuildException(ie);
108       }
109    }
110 
111 
112    /*** Setter for property libXmlFile.
113     * @param libXmlFile The lib file which specifies what files are to be checked out
114     */
115    public void setLibXmlFile(File libXmlFile) {
116       this.libXmlFile = libXmlFile;
117    }
118 
119 }