Coverage Report - org.webslinger.ext.types.php
 
Classes in this File Line Coverage Branch Coverage Complexity
php
75%
36/48
100%
2/2
0
php$CompiledPHP
60%
6/10
N/A
0
 
 1  
 package org.webslinger.ext.types;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.util.HashMap;
 5  
 import java.util.Map;
 6  
 import javax.servlet.ServletException;
 7  
 import javax.servlet.ServletRequest;
 8  
 import javax.servlet.ServletResponse;
 9  
 import javax.servlet.http.HttpServletRequest;
 10  
 import javax.servlet.http.HttpServletResponse;
 11  
 
 12  
 import org.webslinger.TypeHandler;
 13  
 import org.webslinger.Webslinger;
 14  
 import org.webslinger.WebslingerServletContext;
 15  
 import org.webslinger.ext.quercus.VFSDelegatePath;
 16  
 import org.webslinger.util.GeneratedResult;
 17  
 import org.webslinger.util.TTLCachedObject;
 18  
 import org.webslinger.util.TTLObject;
 19  
 import org.webslinger.vfs.VFSDelegate;
 20  
 
 21  
 import com.caucho.quercus.Quercus;
 22  
 import com.caucho.quercus.QuercusExitException;
 23  
 import com.caucho.quercus.env.Env;
 24  
 import com.caucho.quercus.page.QuercusPage;
 25  
 import com.caucho.vfs.WriterStreamImpl;
 26  
 import com.caucho.vfs.WriteStream;
 27  
 
 28  3
 public class php extends TypeHandler {
 29  
     static {
 30  1
         TTLObject.setDefaultTTLForClass(CompiledPHP.class, 1000);
 31  1
     }
 32  
 
 33  3
     protected Quercus quercus = new Quercus();
 34  3
     protected HashMap<Object, CompiledPHP> pages = new HashMap<Object, CompiledPHP>();
 35  
     protected VFSDelegate<Object, Object, ?> vfsDelegate;
 36  
 
 37  
     public void init(WebslingerServletContext context, String type) throws IOException {
 38  3
         super.init(context, type);
 39  3
         vfsDelegate = context.getVFSDelegate();
 40  3
         quercus.setCompile(true);
 41  3
         quercus.setLazyCompile(true);
 42  
         try {
 43  3
             quercus.setPwd(new VFSDelegatePath(vfsDelegate, context.getWWW()));
 44  0
         } catch (IOException e) {
 45  0
             throw e;
 46  0
         } catch (RuntimeException e) {
 47  0
             throw e;
 48  0
         } catch (Exception e) {
 49  0
             throw (IOException) new IOException(e.getMessage()).initCause(e);
 50  3
         }
 51  3
     }
 52  
 
 53  3
     protected class CompiledPHP extends TTLCachedObject<QuercusPage> {
 54  
         protected final Object id;
 55  
 
 56  3
         protected CompiledPHP(Object id) {
 57  3
             this.id = id;
 58  3
         }
 59  
 
 60  
         protected GeneratedResult<QuercusPage> generate(QuercusPage old) throws IOException {
 61  
             try {
 62  3
                 return new GeneratedResult<QuercusPage>(getTimestamp(old), quercus.parse(new VFSDelegatePath(vfsDelegate, id)));
 63  0
             } catch (RuntimeException e) {
 64  0
                 throw e;
 65  0
             } catch (Exception e) {
 66  0
                 throw (IOException) new IOException(e.getMessage()).initCause(e);
 67  
             }
 68  
         }
 69  
 
 70  
         protected long getTimestamp(QuercusPage old) throws IOException {
 71  6
             return vfsDelegate.getLastModifiedTime(id);
 72  
         }
 73  
 
 74  
     }
 75  
 
 76  
     protected QuercusPage getPage(Webslinger webslinger) throws IOException, ServletException {
 77  
         CompiledPHP page;
 78  3
         Object id = vfsDelegate.getId(webslinger);
 79  3
         synchronized (quercus) {
 80  3
             page = pages.get(id);
 81  3
             if (page == null) {
 82  3
                 page = new CompiledPHP(id);
 83  3
                 pages.put(id, page);
 84  
             }
 85  3
         }
 86  3
         return page.getObject();
 87  
     }
 88  
 
 89  
     public Object run(Webslinger webslinger) throws IOException, ServletException {
 90  3
         QuercusPage page = getPage(webslinger);
 91  3
         WriterStreamImpl writerStreamImpl = new WriterStreamImpl();
 92  3
         writerStreamImpl.setWriter(webslinger.getResponse().getWriter());
 93  3
         writerStreamImpl.setWriteEncoding("UTF-8");
 94  3
         Env env = null;
 95  
         try {
 96  3
             env = quercus.createEnv(page, new WriteStream(writerStreamImpl), webslinger.getRequest(), webslinger.getResponse());
 97  3
             env.setGlobalValue("webslinger", env.wrapJava(webslinger));
 98  3
             env.setGlobalValue("request", env.wrapJava(webslinger.getRequest()));
 99  3
             env.setGlobalValue("response", env.wrapJava(webslinger.getResponse()));
 100  3
             env.start();
 101  3
             webslinger.getResponse().setContentType("text/html");
 102  3
             return page.executeTop(env);
 103  0
         } catch (QuercusExitException e) {
 104  
             // ignore, this is thrown when php calls 'exit'
 105  0
             return null;
 106  0
         } catch (RuntimeException e) {
 107  0
             throw e;
 108  0
         } catch (Exception e) {
 109  0
             throw (ServletException) new ServletException(e.getMessage()).initCause(e);
 110  
         } finally {
 111  3
             if (env != null) {
 112  3
                 env.getOriginalOut().flush();
 113  3
                 env.close();
 114  
             }
 115  
         }
 116  
     }
 117  
 }