1 16 package org.apache.cocoon.generation; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 import java.io.PipedInputStream ; 21 import java.io.PipedOutputStream ; 22 import java.util.Enumeration ; 23 24 import javax.servlet.ServletConfig ; 25 import javax.servlet.ServletContext ; 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.avalon.framework.component.Component; 31 import org.apache.avalon.framework.CascadingRuntimeException; 32 import org.apache.avalon.framework.logger.Logger; 33 34 import org.apache.cocoon.ProcessingException; 35 import org.apache.cocoon.environment.http.HttpEnvironment; 36 37 import org.apache.excalibur.source.Source; 38 import org.apache.excalibur.xml.sax.SAXParser; 39 40 import org.xml.sax.InputSource ; 41 import org.xml.sax.SAXException ; 42 43 51 public class PhpGenerator extends ServletGenerator { 52 53 56 class config implements ServletConfig { 57 ServletContext c; 58 public config(ServletContext c) {this.c = c; } 59 60 public String getServletName() { return "PhpGenerator"; } 61 public Enumeration getInitParameterNames() 62 { return c.getInitParameterNames(); } 63 public ServletContext getServletContext() { return c; } 64 public String getInitParameter(String name) { return null; } 65 } 66 67 71 public class PhpServlet extends net.php.servlet implements Runnable { 72 73 String input; 74 OutputStream output; 75 HttpServletRequest request; 76 HttpServletResponse response; 77 ServletException exception = null; 78 Logger logger; 79 80 protected PhpServlet( Logger logger ) { 81 this.logger = logger; 82 } 83 public void setInput(String input) { 84 this.input = input; 85 } 86 87 public void setOutput(OutputStream output) { 88 this.output = output; 89 } 90 91 public void setRequest(HttpServletRequest request) { 92 this.request = request; 93 } 94 95 public void setResponse(HttpServletResponse response) { 96 this.response = response; 97 } 98 99 public void write(String data) { 100 try { 101 output.write(data.getBytes()); 102 } catch (IOException e) { 103 logger.debug("PhpGenerator.write()", e); 104 throw new CascadingRuntimeException("PhpGenerator.write()", e); 105 } 106 } 107 108 109 110 111 112 public void run() { 113 try { 114 service(request, response, input); 115 } catch (ServletException e) { 116 logger.error("PhpGenerator.run()", e); 117 this.exception = e; 118 } 119 120 try { 121 output.close(); 122 } catch (IOException e) { 123 logger.error("PhpGenerator.run():SHOULD NEVER HAPPEN", e); 124 } 126 } 127 128 } 129 130 133 public void generate() throws IOException , SAXException , ProcessingException { 134 135 HttpServletResponse httpResponse = 137 (HttpServletResponse )this.objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT); 138 HttpServletRequest httpRequest = 139 (HttpServletRequest )this.objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT); 140 if (httpResponse == null || httpRequest == null) { 141 throw new ProcessingException("HttpServletRequest or HttpServletResponse object not available"); 142 } 143 144 Source inputSource = null; 146 SAXParser parser = null; 147 try { 148 inputSource = this.resolver.resolveURI(this.source); 149 String systemId = inputSource.getURI(); 150 if (!systemId.startsWith("file:/")) 151 throw new IOException ("protocol not supported: " + systemId); 152 153 PipedInputStream input = new PipedInputStream (); 155 156 PhpServlet php = new PhpServlet( getLogger() ); 158 php.init(new config((ServletContext )this.objectModel.get(HttpEnvironment.HTTP_SERVLET_CONTEXT))); 159 php.setInput(systemId.substring(6)); 160 php.setOutput(new PipedOutputStream (input)); 161 php.setRequest(httpRequest); 162 php.setResponse(httpResponse); 163 new Thread (php).start(); 164 165 parser = (SAXParser)this.manager.lookup(SAXParser.ROLE); 167 parser.parse(new InputSource (input), this.xmlConsumer); 168 169 php.destroy(); 171 } catch (IOException e) { 172 throw e; 173 } catch (Exception e) { 174 throw new ProcessingException(e.toString(), e); 175 } finally { 176 this.resolver.release( inputSource ); 177 this.manager.release( (Component)parser ); 178 } 179 } 180 } 181 | Popular Tags |