1 package org.tigris.scarab.util; 2 3 56 57 import java.io.BufferedReader ; 58 import java.io.File ; 59 import java.io.InputStreamReader ; 60 import java.io.IOException ; 61 import java.io.PrintWriter ; 62 import javax.servlet.ServletConfig ; 63 import javax.servlet.ServletException ; 64 import javax.servlet.http.HttpServlet ; 65 import javax.servlet.http.HttpServletRequest ; 66 import javax.servlet.http.HttpServletResponse ; 67 68 69 public class AntServlet 70 extends HttpServlet 71 { 72 private static String buildCommand = 73 new String ("ant -buildfile"); 74 75 private static File buildFile = 76 new File ("../webapps/newapp/WEB-INF/build/build.xml"); 77 78 84 public final void init(ServletConfig config) 85 throws ServletException 86 { 87 super.init(config); 88 89 String command = config.getInitParameter("buildCommand"); 90 if (command != null) 91 { 92 buildCommand = command; 93 System.out.println ("AntServlet Command: " + 94 buildCommand); 95 } 96 String file = config.getInitParameter("buildFile"); 97 if (file != null) 98 { 99 buildFile = new File (file); 100 System.out.println ("AntServlet File: " + 101 buildFile.getAbsolutePath()); 102 } 103 } 104 105 113 public final void doGet (HttpServletRequest req, HttpServletResponse res) 114 throws IOException 115 { 116 res.setContentType("text/html"); 117 118 122 String refresh = req.getParameter("refresh"); 123 String target = req.getParameter("target"); 124 125 if (target == null) 126 { 127 target = new String (""); 128 } 129 130 133 PrintWriter out = res.getWriter(); 134 try 135 { 136 out.println("<html>"); 137 out.println("<head>"); 138 out.println("<title>Ant Servlet</title>"); 139 140 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 Runtime runtime = Runtime.getRuntime(); 153 154 int returnValue = 0; 155 BufferedReader in = null; 156 try 157 { 158 Process pro = runtime.exec(buildCommand + " " + buildFile + 159 " " + target); 160 in = new BufferedReader ( 161 new InputStreamReader ( 162 pro.getInputStream())); 163 String 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 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 |