1 7 package org.jboss.boot.servlets; 8 9 import java.io.BufferedInputStream ; 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.FileNotFoundException ; 13 import java.io.FileOutputStream ; 14 import java.io.InputStream ; 15 import java.io.IOException ; 16 import java.net.URL ; 17 import java.util.Enumeration ; 18 import java.util.jar.JarEntry ; 19 import java.util.jar.JarFile ; 20 import java.util.jar.JarOutputStream ; 21 import java.util.jar.Manifest ; 22 import javax.servlet.ServletConfig ; 23 import javax.servlet.ServletException ; 24 import javax.servlet.ServletOutputStream ; 25 import javax.servlet.http.HttpServlet ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.xml.transform.TransformerFactory ; 29 import javax.xml.transform.Transformer ; 30 import javax.xml.transform.TransformerConfigurationException ; 31 import javax.xml.transform.TransformerException ; 32 import javax.xml.transform.Source ; 33 import javax.xml.transform.stream.StreamSource ; 34 import javax.xml.transform.stream.StreamResult ; 35 36 import gnu.regexp.RE; 37 import gnu.regexp.REException; 38 39 import org.jboss.logging.Logger; 40 41 50 public class BootServlet extends HttpServlet 51 { 52 static Logger log = Logger.getLogger(BootServlet.class); 53 static File configBase; 54 static File transformBase; 55 static RE variableRE; 56 static TransformerFactory xsltFactory; 57 58 60 public void init(ServletConfig config) throws ServletException 61 { 62 super.init(config); 63 String configBaseDir = config.getInitParameter("configBaseDir"); 64 if( configBaseDir == null ) 65 throw new ServletException ("No configBaseDir parameter specified"); 66 configBase = new File (configBaseDir); 67 if( configBase.exists() == false || configBase.isDirectory() == false ) 68 throw new ServletException ("The configBaseDir("+configBaseDir+") does not exist is not a directory"); 69 log.info("Using configBaseDir = "+configBase.getAbsolutePath()); 70 71 String transformBaseDir = config.getInitParameter("transformBaseDir"); 72 if( transformBaseDir == null ) 73 log.warn("No transformBaseDir parameter specified, using classpath resources"); 74 else 75 { 76 transformBase = new File (transformBaseDir); 77 if( transformBase.exists() == false || transformBase.isDirectory() == false ) 78 throw new ServletException ("The transformBaseDir("+transformBaseDir+") does not exist is not a directory"); 79 log.info("Using transformBaseDir = "+transformBase.getAbsolutePath()); 80 } 81 82 String reExp = config.getInitParameter("variableRE"); 83 if( reExp == null ) 84 reExp = "([^$]+)?\\${([^}]+)}([^$]+)?"; 85 try 86 { 87 variableRE = new RE(reExp); 88 Util.setVariableRE(variableRE); 89 log.info("Using variableRE = "+variableRE.toString()); 90 } 91 catch(REException e) 92 { 93 throw new ServletException ("Failed to init variableRE", e); 94 } 95 96 xsltFactory = TransformerFactory.newInstance(); 97 } 98 99 103 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 104 throws ServletException , IOException , TransformerException 105 { 106 String pathInfo = request.getPathInfo(); 107 log.trace("Begin, pathInfo = "+pathInfo); 108 if( pathInfo.charAt(0) == '/' ) 109 pathInfo = pathInfo.substring(1); 110 ServletOutputStream out = response.getOutputStream(); 111 if( isJar(pathInfo) ) 112 transformJarFile(out, pathInfo); 113 else if( isXml(pathInfo) ) 114 transformFile(out, pathInfo); 115 else 116 copy(out, pathInfo); 117 log.trace("End, pathInfo = "+pathInfo); 118 } 119 120 124 protected void doGet(HttpServletRequest request, HttpServletResponse response) 125 throws ServletException , IOException 126 { 127 try 128 { 129 processRequest(request, response); 130 } 131 catch(TransformerException e) 132 { 133 throw new ServletException ("XLST failure", e); 134 } 135 } 136 137 141 protected void doPost(HttpServletRequest request, HttpServletResponse response) 142 throws ServletException , IOException 143 { 144 try 145 { 146 processRequest(request, response); 147 } 148 catch(TransformerException e) 149 { 150 throw new ServletException ("XLST failure", e); 151 } 152 } 153 154 public String getServletInfo() 155 { 156 return "JBoss net boot servlet"; 157 } 158 159 static boolean isJar(String path) 160 { 161 boolean isJar = path.endsWith(".jar") 162 | path.endsWith(".war") 163 | path.endsWith(".ear") 164 | path.endsWith(".rar") 165 | path.endsWith(".sar"); 166 return isJar; 167 } 168 static boolean isXml(String path) 169 { 170 return path.endsWith(".xml"); 171 } 172 173 static void copy(ServletOutputStream out, String path) throws IOException 174 { 175 File content = new File (configBase, path); 176 FileInputStream fis = new FileInputStream (content); 177 BufferedInputStream bis = new BufferedInputStream (fis); 178 byte[] buffer = new byte[2048]; 179 int bytes = bis.read(buffer); 180 while( bytes > 0 ) 181 { 182 out.write(buffer, 0, bytes); 183 bytes = bis.read(buffer); 184 } 185 bis.close(); 186 out.close(); 187 } 188 static void transformFile(ServletOutputStream out, String path) 189 throws IOException , TransformerException 190 { 191 File serviceFile = new File (configBase, path); 193 FileInputStream fisXML = new FileInputStream (serviceFile); 194 Source xmlSource = new StreamSource (fisXML); 195 InputStream fisXSL = null; 196 if( transformBase != null ) 197 { 198 File hostXSL = new File (transformBase, "default.xsl"); 199 fisXSL = new FileInputStream (hostXSL); 200 } 201 else 202 { 203 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 204 URL hostXSL = loader.getResource("default.xsl"); 205 if( hostXSL == null ) 206 throw new FileNotFoundException ("Failed to find resource: default.xsl"); 207 fisXSL = hostXSL.openStream(); 208 } 209 Source xslSource = new StreamSource (fisXSL); 210 Transformer transformer = xsltFactory.newTransformer(xslSource); 212 transformer.transform(xmlSource, new StreamResult (out)); 214 out.close(); 215 } 216 static void transformJarFile(ServletOutputStream out, String path) 217 throws IOException , TransformerException 218 { 219 TransformerFactory xsltFactory = TransformerFactory.newInstance(); 220 File serviceFile = new File (configBase, path); 222 JarFile sarFile = new JarFile (serviceFile); 223 Manifest mf = sarFile.getManifest(); 224 Enumeration entries = sarFile.entries(); 225 JarOutputStream jos = new JarOutputStream (out, mf); 226 File hostXSL = new File (transformBase, "default.xsl"); 227 byte[] buffer = new byte[2048]; 228 229 while( entries.hasMoreElements() ) 230 { 231 JarEntry entry = (JarEntry ) entries.nextElement(); 232 InputStream is = sarFile.getInputStream(entry); 233 String name = entry.getName(); 234 log.trace("Begin entry: "+name+", size="+entry.getSize()); 235 if( name.equals("META-INF/MANIFEST.MF") ) 236 continue; 237 if( name.endsWith(".xml") == false ) 238 { 239 jos.putNextEntry(entry); 240 int totalBytes = 0; 242 int bytes = is.read(buffer); 243 while( bytes > 0 ) 244 { 245 jos.write(buffer, 0, bytes); 246 totalBytes += bytes; 247 bytes = is.read(buffer); 248 } 249 is.close(); 250 jos.flush(); 251 } 252 else 253 { 254 JarEntry newEntry = new JarEntry (name); 255 newEntry.setTime(entry.getTime()); 256 jos.putNextEntry(newEntry); 257 Source xmlSource = new StreamSource (is); 258 FileInputStream fisXSL = new FileInputStream (hostXSL); 259 Source xslSource = new StreamSource (fisXSL); 260 Transformer transformer = xsltFactory.newTransformer(xslSource); 262 transformer.transform(xmlSource, new StreamResult (jos)); 264 } 265 jos.closeEntry(); 266 log.trace("End entry: "+name); 267 } 268 269 jos.close(); 270 } 271 272 } 273 | Popular Tags |