KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > util > AntServlet


1 package org.tigris.scarab.util;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.BufferedReader JavaDoc;
58 import java.io.File JavaDoc;
59 import java.io.InputStreamReader JavaDoc;
60 import java.io.IOException JavaDoc;
61 import java.io.PrintWriter JavaDoc;
62 import javax.servlet.ServletConfig JavaDoc;
63 import javax.servlet.ServletException JavaDoc;
64 import javax.servlet.http.HttpServlet JavaDoc;
65 import javax.servlet.http.HttpServletRequest JavaDoc;
66 import javax.servlet.http.HttpServletResponse JavaDoc;
67
68
69 public class AntServlet
70     extends HttpServlet JavaDoc
71 {
72     private static String JavaDoc buildCommand =
73         new String JavaDoc ("ant -buildfile");
74
75     private static File JavaDoc buildFile =
76         new File JavaDoc ("../webapps/newapp/WEB-INF/build/build.xml");
77
78     /**
79      * Describe <code>init</code> method here.
80      *
81      * @param config a <code>ServletConfig</code> value
82      * @exception ServletException if an error occurs
83      */

84     public final void init(ServletConfig JavaDoc config)
85         throws ServletException JavaDoc
86     {
87         super.init(config);
88
89         String JavaDoc command = config.getInitParameter("buildCommand");
90         if (command != null)
91         {
92             buildCommand = command;
93             System.out.println ("AntServlet Command: " +
94                 buildCommand);
95         }
96         String JavaDoc file = config.getInitParameter("buildFile");
97         if (file != null)
98         {
99             buildFile = new File JavaDoc(file);
100             System.out.println ("AntServlet File: " +
101                 buildFile.getAbsolutePath());
102         }
103     }
104
105     /**
106      * Describe <code>doGet</code> method here.
107      *
108      * @param req a <code>HttpServletRequest</code> value
109      * @param res a <code>HttpServletResponse</code> value
110      * @exception IOException if an error occurs
111      * @exception ServletException if an error occurs
112      */

113     public final void doGet (HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
114         throws IOException JavaDoc
115     {
116         res.setContentType("text/html");
117
118         /* Retrieve some parameters.
119          * Refresh = the value of refresh tag in seconds.
120          * target = the name of the target to execute with ant.
121          */

122         String JavaDoc refresh = req.getParameter("refresh");
123         String JavaDoc target = req.getParameter("target");
124         
125         if (target == null)
126         {
127             target = new String JavaDoc("");
128         }
129
130         /*
131          * Write output to the client..
132          */

133         PrintWriter JavaDoc out = res.getWriter();
134         try
135         {
136             out.println("<html>");
137             out.println("<head>");
138             out.println("<title>Ant Servlet</title>");
139             
140             // write the refresh tag if necessary
141
if (refresh != null)
142             {
143                 out.println("<meta http-equiv=Refresh content=" + refresh + "/>");
144             }
145     
146             out.println("</head>");
147             out.println("<body bgcolor=\"white\">");
148             out.println("<hr size=\"1\" noshade=\"true\">");
149             out.flush();
150
151             // do the ant command..
152
Runtime JavaDoc runtime = Runtime.getRuntime();
153     
154             int returnValue = 0;
155             BufferedReader JavaDoc in = null;
156             try
157             {
158                 Process JavaDoc pro = runtime.exec(buildCommand + " " + buildFile +
159                     " " + target);
160                 in = new BufferedReader JavaDoc(
161                                        new InputStreamReader JavaDoc(
162                                            pro.getInputStream()));
163                 String JavaDoc inline = null;
164                 out.println("<pre>");
165                 while((inline = in.readLine()) != null)
166                 {
167                     out.println(inline);
168                     out.flush();
169                 }
170                 out.println("</pre>");
171                 out.flush();
172                 returnValue = pro.waitFor();
173                 
174             }
175             catch (Exception JavaDoc ignored)
176             {
177             }
178             finally
179             {
180                 if (in != null)
181                 {
182                     in.close();
183                 }
184             }
185     
186             out.println("<hr size=\"1\" noshade=\"true\">");
187             out.println("Return value from Ant:" + returnValue);
188             out.println("</body>");
189             out.println("</html>");
190             out.flush();
191         }
192         finally
193         {
194             if (out != null)
195             {
196                 out.close();
197             }
198         }
199     }
200 }
201
Popular Tags