001 package railo.cli.servlet; 002 003 import java.io.File; 004 import java.io.FileInputStream; 005 import java.io.IOException; 006 import java.io.InputStream; 007 import java.net.MalformedURLException; 008 import java.net.URL; 009 import java.util.Enumeration; 010 import java.util.Map; 011 import java.util.Set; 012 013 import javax.servlet.RequestDispatcher; 014 import javax.servlet.Servlet; 015 import javax.servlet.ServletContext; 016 import javax.servlet.ServletException; 017 018 import railo.cli.util.EnumerationWrapper; 019 020 public class ServletContextImpl implements ServletContext { 021 private Map<String,Object> attributes; 022 private Map<String, String> parameters; 023 private int majorVersion; 024 private int minorVersion; 025 private File root; 026 027 028 public ServletContextImpl(File root,Map<String,Object> attributes,Map<String, String> parameters,int majorVersion, int minorVersion) { 029 this.root=root; 030 this.attributes=attributes; 031 this.parameters=parameters; 032 this.majorVersion=majorVersion; 033 this.minorVersion=minorVersion; 034 } 035 036 /** 037 * @see javax.servlet.ServletContext#getAttribute(java.lang.String) 038 */ 039 public Object getAttribute(String key) { 040 return attributes.get(key); 041 } 042 043 /** 044 * @see javax.servlet.ServletContext#getAttributeNames() 045 */ 046 public Enumeration getAttributeNames() { 047 return new EnumerationWrapper(attributes); 048 } 049 050 /** 051 * @see javax.servlet.ServletContext#getInitParameter(java.lang.String) 052 */ 053 public String getInitParameter(String key) { 054 return parameters.get(key); 055 } 056 057 /** 058 * @see javax.servlet.ServletContext#getInitParameterNames() 059 */ 060 public Enumeration getInitParameterNames() { 061 return new EnumerationWrapper(parameters); 062 } 063 064 /** 065 * @see javax.servlet.ServletContext#getMajorVersion() 066 */ 067 public int getMajorVersion() { 068 return majorVersion; 069 } 070 071 /** 072 * @see javax.servlet.ServletContext#getMinorVersion() 073 */ 074 public int getMinorVersion() { 075 return minorVersion; 076 } 077 078 /** 079 * @see javax.servlet.ServletContext#getMimeType(java.lang.String) 080 */ 081 public String getMimeType(String file) { 082 throw notSupported("getMimeType(String file)"); 083 // TODO 084 //return ResourceUtil.getMimeType(config.getResource(file),null); 085 } 086 087 /** 088 * @see javax.servlet.ServletContext#getRealPath(java.lang.String) 089 */ 090 public String getRealPath(String realpath) { 091 return getRealFile(realpath).getAbsolutePath(); 092 } 093 094 /** 095 * @see javax.servlet.ServletContext#getResource(java.lang.String) 096 */ 097 public URL getResource(String realpath) throws MalformedURLException { 098 File file = getRealFile(realpath); 099 return file.toURL(); 100 } 101 102 /** 103 * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String) 104 */ 105 public InputStream getResourceAsStream(String realpath) { 106 try { 107 return new FileInputStream(getRealFile(realpath)); 108 } catch (IOException e) { 109 return null; 110 } 111 } 112 113 public File getRealFile(String realpath) { 114 return new File(root,realpath); 115 } 116 117 public File getRoot() { 118 return root; 119 } 120 121 public Set getResourcePaths(String realpath) { 122 throw notSupported("getResourcePaths(String realpath)"); 123 } 124 125 public RequestDispatcher getRequestDispatcher(String path) { 126 throw notSupported("getNamedDispatcher(String name)"); 127 } 128 129 public ServletContext getContext(String key) { 130 // TODO ? 131 return this; 132 } 133 134 public RequestDispatcher getNamedDispatcher(String name) { 135 // TODO Auto-generated method stub 136 throw notSupported("getNamedDispatcher(String name)"); 137 } 138 139 /** 140 * @see javax.servlet.ServletContext#log(java.lang.String, java.lang.Throwable) 141 */ 142 public void log(String msg, Throwable t) {// TODO better 143 if(t==null)System.out.println(msg); 144 else System.out.println(msg+":\n"+t.getMessage()); 145 146 147 //if(t==null)log.log(Log.LEVEL_INFO, "ServletContext", msg); 148 //else log.log(Log.LEVEL_ERROR, "ServletContext", msg+":\n"+ExceptionUtil.getStacktrace(t,false)); 149 } 150 151 /** 152 * @see javax.servlet.ServletContext#log(java.lang.Exception, java.lang.String) 153 */ 154 public void log(Exception e, String msg) { 155 log(msg,e); 156 } 157 158 /** 159 * @see javax.servlet.ServletContext#log(java.lang.String) 160 */ 161 public void log(String msg) { 162 log(msg,null); 163 } 164 165 /** 166 * @see javax.servlet.ServletContext#removeAttribute(java.lang.String) 167 */ 168 public void removeAttribute(String key) { 169 attributes.remove(key); 170 } 171 172 /** 173 * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object) 174 */ 175 public void setAttribute(String key, Object value) { 176 attributes.put(key, value); 177 } 178 179 180 181 182 public String getServletContextName() { 183 // can return null 184 return null; 185 } 186 187 public String getServerInfo() { 188 // deprecated 189 throw notSupported("getServlet()"); 190 } 191 192 public Servlet getServlet(String arg0) throws ServletException { 193 // deprecated 194 throw notSupported("getServlet()"); 195 } 196 197 public Enumeration getServletNames() { 198 // deprecated 199 throw notSupported("getServlet()"); 200 } 201 202 public Enumeration getServlets() { 203 // deprecated 204 throw notSupported("getServlet()"); 205 } 206 207 private RuntimeException notSupported(String method) { 208 throw new RuntimeException(new ServletException("method "+method+" not supported")); 209 } 210 211 }