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.Ear;
22  import org.apache.tools.ant.types.FileSet;
23  
24  import org.w3c.dom.Document;
25  import org.w3c.dom.NamedNodeMap;
26  import org.w3c.dom.Node;
27  import org.w3c.dom.NodeList;
28  import org.xml.sax.SAXException;
29  
30  import javax.xml.parsers.DocumentBuilder;
31  import javax.xml.parsers.DocumentBuilderFactory;
32  import javax.xml.parsers.ParserConfigurationException;
33  import javax.xml.transform.dom.DOMSource;
34  import javax.xml.transform.stream.StreamResult;
35  import javax.xml.transform.*;
36  import java.io.*;
37  
38  
39  /*** Extension to the regular ear task to include files specified in the libXmlFile. */
40  public class EarAutoLib extends Ear {
41     private final static int APPSERVER_NOT_SPECIFIED = 0;
42     private final static int APPSERVER_ORACLE = 1;
43     private final static int APPSERVER_JBOSS = 2;
44     private final static int APPSERVER_SUNONE = 3;
45     private final static int APPSERVER_WEBLOGIC = 4;
46  
47     private final static String DOCTYPE_PUBLIC = "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN";
48     private final static String DOCTYPE_SYSTEM = "http://java.sun.com/dtd/application_1_3.dtd";
49  
50  
51  
52     /*** Directory which contains the library files. Default is lib. */
53     public File directory;
54  
55     /*** Holds value of property libXmlFile. */
56     private File libXmlFile;
57  
58     /*** Holds value of property appXml. */
59     private File appxml;
60  
61     /*** Holds value of property proxyHost. */
62     private String proxyHost;
63  
64     /*** Holds value of property proxyPort. */
65     private String proxyPort;
66  
67      private String doctypePublic;
68  
69     private String doctypeSystem;
70  
71  
72     /*** Holds the value for the appServer
73      *  0 = not specified
74      *  1 = oracle
75      */
76     private int appServer = 0;
77  
78  
79     /*** Setter for libDir property.
80      * @param directory Directory which contains the library files.
81      */
82     public void setLibdir(File directory) {
83        this.directory = directory;
84     }
85  
86  
87     /*** Method called by ant to execute this task.
88      * @throws BuildException Exception which is thrown to stop the build process and display an error message
89      */
90     public void execute() throws BuildException {
91        System.setProperty("http.proxyHost", proxyHost);
92        System.setProperty("http.proxyPort", proxyPort);
93        if (this.directory == null) {
94           this.directory = new File("lib"); // default
95        }
96        if (!this.directory.exists()) {
97           this.directory.mkdirs();
98        }
99        if (!directory.isDirectory()) {
100          throw new BuildException("lib directory not properly set.");
101       }
102       try {
103          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
104          dbf.setValidating(true);
105          DocumentBuilder db = dbf.newDocumentBuilder();
106          Document doc = db.parse(libXmlFile);
107          db.setEntityResolver(new DTDResolver());
108          Document appXmlDoc = db.parse(appxml);
109          Node rootNode = appXmlDoc.getElementsByTagName("application").item(0);
110          NodeList earTags = doc.getElementsByTagName("ear");
111          if (earTags.getLength() > 0) {
112             NodeList nl = earTags.item(0).getChildNodes();
113             for (int i = 0; i < nl.getLength(); i++) {
114                Node node = nl.item(i);
115                if (!node.getNodeName().equals("lib") && !node.getNodeName().equals("localLib")) continue;
116                NamedNodeMap atts = node.getAttributes();
117                Node att = atts.getNamedItem("file");
118                if (att == null) {
119                   att = atts.getNamedItem("url");
120                }
121                String file = att.getNodeValue();
122                if (file.indexOf("/") > -1) file = file.substring(file.lastIndexOf("/") + 1);
123                log("adding lib " + file);
124                FileSet fs = new FileSet();
125                fs.setDir(directory);
126                fs.setIncludes(file);
127                addFileset(fs);
128 
129                // only add the jar files as java modules to the application.xml file if
130                // the application server is JBoss
131                // This is the default behaviour. Only JBoss can handle the java modules.
132                if (APPSERVER_JBOSS == this.appServer) {
133                   if (file.endsWith(".jar") || file.endsWith(".zip")) {
134                      Node moduleNode = rootNode.appendChild(appXmlDoc.createElement("module"));
135                      Node javaNode = moduleNode.appendChild(appXmlDoc.createElement("java"));
136                      javaNode.appendChild(appXmlDoc.createTextNode(file));
137                   }
138                }
139             }
140          }
141          else {
142             log("Not adding any libs because ear tag is not found in " + libXmlFile.getName());
143          }
144          File tmpFile = File.createTempFile("applicationxml", null);
145          outXML(appXmlDoc, tmpFile);
146          super.setAppxml(tmpFile);
147          super.execute();
148          tmpFile.delete();
149       }
150       catch (ParserConfigurationException pce) {
151          throw new BuildException(pce);
152       }
153       catch (SAXException se) {
154          throw new BuildException(se);
155       }
156       catch (IOException ie) {
157          throw new BuildException(ie);
158       } catch (TransformerException e) {
159          throw new BuildException(e);
160       }
161    }
162 
163     public void outXML(Document doc, File outPutFile) throws TransformerException, IOException {
164         DOMSource domSource = new DOMSource(doc);
165         FileOutputStream fos = new FileOutputStream(outPutFile);
166         StreamResult streamResult = new StreamResult(fos);
167         TransformerFactory tf = TransformerFactory.newInstance();
168         Transformer serializer = tf.newTransformer();
169         if (doctypePublic != null && !doctypePublic.equals("")) {
170             serializer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,doctypePublic);
171         } else {
172             serializer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,this.DOCTYPE_PUBLIC);
173         }
174         if (doctypeSystem != null && !doctypeSystem.equals("")) {
175             serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,doctypeSystem);
176         } else {
177             serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,this.DOCTYPE_SYSTEM);
178         }
179         serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
180         serializer.setOutputProperty(OutputKeys.INDENT, "yes");
181         serializer.transform(domSource, streamResult);
182         fos.flush();
183         fos.close();
184     }
185 
186    /*** Setter for property libXmlFile.
187     * @param libXmlFile The lib file which specifies what files are to be checked out
188     */
189    public void setLibXmlFile(File libXmlFile) {
190       this.libXmlFile = libXmlFile;
191    }
192 
193 
194    /*** Setter for property appXml.
195     * @param appxml the application.xml file.
196     */
197    public void setAppxml(File appxml) {
198       this.appxml = appxml;
199    }
200 
201 
202    /*** Setter for property proxyHost.
203     * @param proxyHost New value of property proxyHost.
204     *
205     */
206    public void setProxyHost(String proxyHost) {
207       this.proxyHost = proxyHost;
208    }
209 
210 
211    /*** Setter for property proxyPort.
212     * @param proxyPort New value of property proxyPort.
213     *
214     */
215    public void setProxyPort(String proxyPort) {
216       this.proxyPort = proxyPort;
217    }
218 
219 
220    /***
221     * Setter for the application server parameter
222     * 0 - not specified
223     * 1 - oracle
224     * @param appServer int representation for the appserver
225     */
226    public void setAppServer(int appServer) {
227       this.appServer = appServer;
228    }
229 
230     /***
231      * Set the system doctype for the Application xml dtd.
232      * @param doctypeSystem
233      */
234     public void setDoctypeSystem(String doctypeSystem) {
235         this.doctypeSystem = doctypeSystem;
236     }
237 
238     /***
239        *Set the publi doctype for the Application xml dtd.
240      * @param doctypePublic
241      */
242     public void setDoctypePublic(String doctypePublic) {
243         this.doctypePublic = doctypePublic;
244     }
245 
246 }