| 1 | |
package org.webslinger.types; |
| 2 | |
|
| 3 | |
import java.io.InputStream; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.io.StringReader; |
| 6 | |
import java.net.URL; |
| 7 | |
import java.util.Enumeration; |
| 8 | |
import java.util.HashMap; |
| 9 | |
import javax.servlet.RequestDispatcher; |
| 10 | |
import javax.servlet.ServletConfig; |
| 11 | |
import javax.servlet.ServletContext; |
| 12 | |
import javax.servlet.ServletException; |
| 13 | |
import javax.servlet.ServletRequestWrapper; |
| 14 | |
import javax.servlet.http.HttpServlet; |
| 15 | |
import javax.servlet.http.HttpServletRequest; |
| 16 | |
import javax.servlet.http.HttpServletResponse; |
| 17 | |
|
| 18 | |
import org.apache.commons.vfs.FileObject; |
| 19 | |
|
| 20 | |
import org.codehaus.janino.ClassBodyEvaluator; |
| 21 | |
|
| 22 | |
import org.webslinger.PathContext; |
| 23 | |
import org.webslinger.TypeHandler; |
| 24 | |
import org.webslinger.Webslinger; |
| 25 | |
import org.webslinger.bsf.Compiler; |
| 26 | |
import org.webslinger.io.Charsets; |
| 27 | |
import org.webslinger.io.IOUtil; |
| 28 | |
import org.webslinger.lang.ConcurrentFreezingCache; |
| 29 | |
import org.webslinger.lang.Freezer; |
| 30 | |
import org.webslinger.util.GeneratedResult; |
| 31 | |
import org.webslinger.util.TTLCachedObject; |
| 32 | |
import org.webslinger.util.TTLObject; |
| 33 | |
|
| 34 | 0 | public class servlet extends TypeHandler { |
| 35 | |
protected static Object setAndGet(HttpServletRequest request, String name, Object newValue) { |
| 36 | 0 | Object oldValue = request.getAttribute(name); |
| 37 | 0 | request.setAttribute(name, newValue); |
| 38 | 0 | return oldValue; |
| 39 | |
} |
| 40 | |
|
| 41 | 0 | protected final ConcurrentFreezingCache<PathContext, String, TTLCachedObject<HttpServlet>> servlets = new ConcurrentFreezingCache<PathContext, String, TTLCachedObject<HttpServlet>>(servlet.class, "servlets", null, ConcurrentFreezingCache.SOFT, new Freezer<PathContext, String>() { |
| 42 | |
public String freeze(PathContext pc) { |
| 43 | 0 | return pc.getServletPath(); |
| 44 | |
} |
| 45 | 0 | }) { |
| 46 | |
protected TTLCachedObject<HttpServlet> createValue(Object id, String path) { |
| 47 | 0 | return new CompiledServlet((PathContext) id); |
| 48 | |
} |
| 49 | |
}; |
| 50 | |
|
| 51 | 0 | private final class CompiledServlet extends TTLCachedObject<HttpServlet> implements ServletConfig { |
| 52 | |
private final PathContext pc; |
| 53 | |
|
| 54 | 0 | protected CompiledServlet(PathContext pc) { |
| 55 | 0 | this.pc = pc; |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
protected long getTimestamp(HttpServlet old) throws IOException { |
| 59 | 0 | FileObject file = pc.getFile(); |
| 60 | 0 | return file.exists() ? file.getContent().getLastModifiedTime() : NOT_EXISTANT_TIMESTAMP; |
| 61 | |
} |
| 62 | |
|
| 63 | |
protected GeneratedResult<HttpServlet> generate(HttpServlet old) throws IOException { |
| 64 | |
try { |
| 65 | 0 | GeneratedResult<HttpServlet> result = compile(pc); |
| 66 | 0 | result.object.init(this); |
| 67 | 0 | return result; |
| 68 | 0 | } catch (IOException e) { |
| 69 | 0 | throw e; |
| 70 | 0 | } catch (ServletException e) { |
| 71 | 0 | throw (IOException) new IOException(e.getMessage()).initCause(e); |
| 72 | |
} |
| 73 | |
} |
| 74 | |
|
| 75 | |
public String getServletName() { |
| 76 | 0 | return pc.getServletPath(); |
| 77 | |
} |
| 78 | |
|
| 79 | |
public String getInitParameter(String name) { |
| 80 | |
try { |
| 81 | 0 | return (String) pc.getAttribute("servlet-init-" + name); |
| 82 | 0 | } catch (IOException e) { |
| 83 | 0 | return null; |
| 84 | |
} |
| 85 | |
} |
| 86 | |
|
| 87 | |
public Enumeration getInitParameterNames() { |
| 88 | 0 | throw new UnsupportedOperationException(); |
| 89 | |
} |
| 90 | |
|
| 91 | |
public ServletContext getServletContext() { |
| 92 | 0 | return getContext(); |
| 93 | |
} |
| 94 | |
} |
| 95 | |
|
| 96 | |
protected HttpServlet getServlet(Webslinger webslinger) throws IOException, ServletException { |
| 97 | |
try { |
| 98 | 0 | return servlets.get(webslinger.getPathContext()).getObject(); |
| 99 | 0 | } catch (RuntimeException e) { |
| 100 | 0 | throw e; |
| 101 | 0 | } catch (IOException e) { |
| 102 | 0 | throw e; |
| 103 | 0 | } catch (ServletException e) { |
| 104 | 0 | throw e; |
| 105 | 0 | } catch (Exception e) { |
| 106 | 0 | throw (ServletException) new ServletException(e.getMessage()).initCause(e); |
| 107 | |
} |
| 108 | |
} |
| 109 | |
|
| 110 | |
protected GeneratedResult<HttpServlet> compile(PathContext pc) throws IOException, ServletException { |
| 111 | 0 | FileObject file = pc.getFile(); |
| 112 | 0 | String servletName = pc.getServletPath(); |
| 113 | 0 | long lastModifiedTime = file.getContent().getLastModifiedTime(); |
| 114 | 0 | String content = IOUtil.readString(file.getContent().getInputStream(), Charsets.UTF8); |
| 115 | 0 | ClassBodyEvaluator cbe = new ClassBodyEvaluator(); |
| 116 | 0 | cbe.setExtendedType(HttpServlet.class); |
| 117 | 0 | cbe.setParentClassLoader(getContext().getClassLoader()); |
| 118 | 0 | StringBuilder packageName = new StringBuilder(); |
| 119 | 0 | StringBuilder className = new StringBuilder(); |
| 120 | 0 | Compiler.STANDARD_NAME_ENCODER.makePackageClassName(packageName, className, servletName); |
| 121 | 0 | if (packageName.length() > 0) packageName.append('.'); |
| 122 | 0 | packageName.append(className); |
| 123 | 0 | cbe.setClassName(packageName.toString()); |
| 124 | |
try { |
| 125 | 0 | cbe.cook(servletName, new StringReader(content)); |
| 126 | 0 | Class clz = cbe.getClazz(); |
| 127 | 0 | return new GeneratedResult<HttpServlet>(lastModifiedTime, (HttpServlet) clz.newInstance()); |
| 128 | 0 | } catch (IOException e) { |
| 129 | 0 | throw e; |
| 130 | 0 | } catch (RuntimeException e) { |
| 131 | 0 | throw e; |
| 132 | 0 | } catch (Exception e) { |
| 133 | 0 | throw new ServletException(e.getMessage(), e); |
| 134 | |
} |
| 135 | |
} |
| 136 | |
|
| 137 | |
public Object run(Webslinger webslinger) throws IOException, ServletException { |
| 138 | 0 | HttpServlet servlet = getServlet(webslinger); |
| 139 | 0 | HttpServletRequest request = webslinger.getRequest(); |
| 140 | 0 | HttpServletResponse response = webslinger.getResponse(); |
| 141 | 0 | Object servletPath = setAndGet(request, "javax.servlet.include.servlet_path", webslinger.getCurrentFile().getName().getPath()); |
| 142 | 0 | Object queryString = setAndGet(request, "javax.servlet.include.query_string", null); |
| 143 | 0 | Object pathInfo = setAndGet(request, "javax.servlet.include.path_info", webslinger.getPathInfo()); |
| 144 | 0 | Object oldWebslinger = setAndGet(request, "org.webslinger.Webslinger", webslinger); |
| 145 | |
try { |
| 146 | 0 | servlet.service(request, response); |
| 147 | |
} finally { |
| 148 | 0 | request.setAttribute("javax.servlet.include.servlet_path", servletPath); |
| 149 | 0 | request.setAttribute("javax.servlet.include.query_string", queryString); |
| 150 | 0 | request.setAttribute("javax.servlet.include.path_info", pathInfo); |
| 151 | 0 | request.setAttribute("org.webslinger.Webslinger", oldWebslinger); |
| 152 | 0 | } |
| 153 | 0 | return null; |
| 154 | |
} |
| 155 | |
} |