001: package org.tigris.scarab.util;
002:
003: /* ====================================================================
004: * The Apache Software License, Version 1.1
005: *
006: * Copyright (c) 2001 The Apache Software Foundation. All rights
007: * reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution,
022: * if any, must include the following acknowledgment:
023: * "This product includes software developed by the
024: * Apache Software Foundation (http://www.apache.org/)."
025: * Alternately, this acknowledgment may appear in the software itself,
026: * if and wherever such third-party acknowledgments normally appear.
027: *
028: * 4. The names "Apache" and "Apache Software Foundation" and
029: * "Apache Turbine" must not be used to endorse or promote products
030: * derived from this software without prior written permission. For
031: * written permission, please contact apache@apache.org.
032: *
033: * 5. Products derived from this software may not be called "Apache",
034: * "Apache Turbine", nor may "Apache" appear in their name, without
035: * prior written permission of the Apache Software Foundation.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048: * SUCH DAMAGE.
049: * ====================================================================
050: *
051: * This software consists of voluntary contributions made by many
052: * individuals on behalf of the Apache Software Foundation. For more
053: * information on the Apache Software Foundation, please see
054: * <http://www.apache.org/>.
055: */
056:
057: import java.io.BufferedReader;
058: import java.io.File;
059: import java.io.InputStreamReader;
060: import java.io.IOException;
061: import java.io.PrintWriter;
062: import javax.servlet.ServletConfig;
063: import javax.servlet.ServletException;
064: import javax.servlet.http.HttpServlet;
065: import javax.servlet.http.HttpServletRequest;
066: import javax.servlet.http.HttpServletResponse;
067:
068: public class AntServlet extends HttpServlet {
069: private static String buildCommand = new String("ant -buildfile");
070:
071: private static File buildFile = new File(
072: "../webapps/newapp/WEB-INF/build/build.xml");
073:
074: /**
075: * Describe <code>init</code> method here.
076: *
077: * @param config a <code>ServletConfig</code> value
078: * @exception ServletException if an error occurs
079: */
080: public final void init(ServletConfig config)
081: throws ServletException {
082: super .init(config);
083:
084: String command = config.getInitParameter("buildCommand");
085: if (command != null) {
086: buildCommand = command;
087: System.out.println("AntServlet Command: " + buildCommand);
088: }
089: String file = config.getInitParameter("buildFile");
090: if (file != null) {
091: buildFile = new File(file);
092: System.out.println("AntServlet File: "
093: + buildFile.getAbsolutePath());
094: }
095: }
096:
097: /**
098: * Describe <code>doGet</code> method here.
099: *
100: * @param req a <code>HttpServletRequest</code> value
101: * @param res a <code>HttpServletResponse</code> value
102: * @exception IOException if an error occurs
103: * @exception ServletException if an error occurs
104: */
105: public final void doGet(HttpServletRequest req,
106: HttpServletResponse res) throws IOException {
107: res.setContentType("text/html");
108:
109: /* Retrieve some parameters.
110: * Refresh = the value of refresh tag in seconds.
111: * target = the name of the target to execute with ant.
112: */
113: String refresh = req.getParameter("refresh");
114: String target = req.getParameter("target");
115:
116: if (target == null) {
117: target = new String("");
118: }
119:
120: /*
121: * Write output to the client..
122: */
123: PrintWriter out = res.getWriter();
124: try {
125: out.println("<html>");
126: out.println("<head>");
127: out.println("<title>Ant Servlet</title>");
128:
129: // write the refresh tag if necessary
130: if (refresh != null) {
131: out.println("<meta http-equiv=Refresh content="
132: + refresh + "/>");
133: }
134:
135: out.println("</head>");
136: out.println("<body bgcolor=\"white\">");
137: out.println("<hr size=\"1\" noshade=\"true\">");
138: out.flush();
139:
140: // do the ant command..
141: Runtime runtime = Runtime.getRuntime();
142:
143: int returnValue = 0;
144: BufferedReader in = null;
145: try {
146: Process pro = runtime.exec(buildCommand + " "
147: + buildFile + " " + target);
148: in = new BufferedReader(new InputStreamReader(pro
149: .getInputStream()));
150: String inline = null;
151: out.println("<pre>");
152: while ((inline = in.readLine()) != null) {
153: out.println(inline);
154: out.flush();
155: }
156: out.println("</pre>");
157: out.flush();
158: returnValue = pro.waitFor();
159:
160: } catch (Exception ignored) {
161: } finally {
162: if (in != null) {
163: in.close();
164: }
165: }
166:
167: out.println("<hr size=\"1\" noshade=\"true\">");
168: out.println("Return value from Ant:" + returnValue);
169: out.println("</body>");
170: out.println("</html>");
171: out.flush();
172: } finally {
173: if (out != null) {
174: out.close();
175: }
176: }
177: }
178: }
|