1 29 30 package com.caucho.server.dispatch; 31 32 import com.caucho.config.ConfigELContext; 33 import com.caucho.config.ConfigException; 34 import com.caucho.config.types.InitProgram; 35 import com.caucho.el.EL; 36 import com.caucho.server.webapp.WebApp; 37 import com.caucho.util.L10N; 38 39 import javax.el.ELContext; 40 import javax.servlet.ServletContext ; 41 import javax.servlet.ServletException ; 42 import java.util.ArrayList ; 43 import java.util.HashMap ; 44 import java.util.logging.Level ; 45 46 49 public class ServletMapping extends ServletConfigImpl { 50 private static final L10N L = new L10N(ServletMapping.class); 51 52 private ArrayList <Mapping> _mappingList 53 = new ArrayList <Mapping>(); 54 private String _urlPattern; 55 private String _urlRegexp; 56 private boolean _isStrictMapping; 57 58 61 public ServletMapping() 62 { 63 } 64 65 68 public void addURLPattern(String pattern) 69 { 70 if (pattern.indexOf('\n') > -1) 71 throw new ConfigException(L.l("`{0}' cannot contain newline", "url-pattern")); 72 73 _mappingList.add(new Mapping(pattern, null)); 74 } 75 76 79 public void addURLRegexp(String pattern) 80 { 81 _mappingList.add(new Mapping(null, pattern)); 82 } 83 84 87 public boolean isStrictMapping() 88 { 89 return _isStrictMapping; 90 } 91 92 95 public void setStrictMapping(boolean isStrictMapping) 96 { 97 _isStrictMapping = isStrictMapping; 98 } 99 100 103 public void init(ServletMapper mapper) 104 throws ServletException 105 { 106 boolean hasInit = false; 107 108 for (int i = 0; i < _mappingList.size(); i++) { 109 Mapping mapping = _mappingList.get(i); 110 111 String urlPattern = mapping.getUrlPattern(); 112 String urlRegexp = mapping.getUrlRegexp(); 113 114 if (getServletName() == null 115 && getServletClassName() != null 116 && urlPattern != null) { 117 setServletName(urlPattern); 118 } 119 120 if (urlPattern != null && ! hasInit) { 121 hasInit = true; 122 super.init(); 123 124 if (getServletClassName() != null) 125 mapper.getServletManager().addServlet(this); 126 } 127 128 if (urlPattern != null) 129 mapper.addUrlMapping(urlPattern, getServletName(), this); 130 else 131 mapper.addUrlRegexp(urlRegexp, this); 132 } 133 134 142 } 143 144 147 String initRegexp(ServletContext webApp, 148 ServletManager manager, 149 ArrayList <String > vars) 150 throws ServletException 151 { 152 ELContext env = EL.getEnvironment(); 153 HashMap <String ,Object > map = new HashMap <String ,Object >(); 154 map.put("regexp", vars); 155 156 ELContext mapEnv = new ConfigELContext(map); 157 158 String rawName = getServletName(); 159 String rawClassName = getServletClassName(); 160 161 if (rawName == null) 162 rawName = rawClassName; 163 164 if (rawClassName == null) 165 rawClassName = rawName; 166 167 try { 168 String servletName = EL.evalString(rawName, mapEnv); 169 170 if (manager.getServletConfig(servletName) != null) 171 return servletName; 172 173 String className = EL.evalString(rawClassName, mapEnv); 174 175 try { 176 WebApp app = (WebApp) getServletContext(); 177 178 Class cl = Class.forName(className, false, app.getClassLoader()); 179 } catch (ClassNotFoundException e) { 180 log.log(Level.WARNING, e.toString(), e); 181 182 return null; 183 } 184 185 ServletConfigImpl config = new ServletConfigImpl(); 186 187 config.setServletName(servletName); 188 config.setServletClass(className); 189 config.setServletContext(webApp); 190 191 InitProgram program = getInit(); 192 if (program != null) 193 program.init(config); 194 195 config.init(); 196 197 manager.addServlet(config); 198 199 return servletName; 200 } catch (RuntimeException e) { 201 throw e; 202 } catch (ServletException e) { 203 throw e; 204 } catch (Throwable e) { 205 throw new ServletException (e); 206 } 207 } 208 209 212 public String toString() 213 { 214 return "ServletMapping[pattern=" + _urlPattern + ",name=" + getServletName() + "]"; 215 } 216 217 static class Mapping { 218 private final String _urlPattern; 219 private final String _urlRegexp; 220 221 Mapping(String urlPattern, String urlRegexp) 222 { 223 _urlPattern = urlPattern; 224 _urlRegexp = urlRegexp; 225 } 226 227 String getUrlPattern() 228 { 229 return _urlPattern; 230 } 231 232 String getUrlRegexp() 233 { 234 return _urlRegexp; 235 } 236 237 public String toString() 238 { 239 return "ServletMapping[" + _urlPattern + ", " + _urlRegexp + "]"; 240 } 241 } 242 } 243 | Popular Tags |