1 52 53 package freemarker.ext.servlet; 54 55 import javax.servlet.GenericServlet ; 56 import javax.servlet.ServletContext ; 57 58 import freemarker.template.ObjectWrapper; 59 import freemarker.template.TemplateHashModel; 60 import freemarker.template.TemplateModel; 61 import freemarker.template.TemplateModelException; 62 63 68 public final class ServletContextHashModel implements TemplateHashModel 69 { 70 private final GenericServlet servlet; 71 private final ServletContext servletctx; 72 private final ObjectWrapper wrapper; 73 74 public ServletContextHashModel( 75 GenericServlet servlet, ObjectWrapper wrapper) 76 { 77 this.servlet = servlet; 78 this.servletctx = servlet.getServletContext(); 79 this.wrapper = wrapper; 80 } 81 82 86 public ServletContextHashModel( 87 ServletContext servletctx, ObjectWrapper wrapper) 88 { 89 this.servlet = null; 90 this.servletctx = servletctx; 91 this.wrapper = wrapper; 92 } 93 94 public TemplateModel get(String key) throws TemplateModelException 95 { 96 return wrapper.wrap(servletctx.getAttribute(key)); 97 } 98 99 public boolean isEmpty() 100 { 101 return !servletctx.getAttributeNames().hasMoreElements(); 102 } 103 104 108 public GenericServlet getServlet() 109 { 110 return servlet; 111 } 112 } 113 | Popular Tags |