1 16 package org.mortbay.jetty.servlet; 17 18 19 import java.io.IOException ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.UnavailableException ; 27 import javax.servlet.http.HttpServlet ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletRequestWrapper ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.apache.commons.logging.Log; 33 import org.mortbay.log.LogFactory; 34 import org.mortbay.util.LogSupport; 35 import org.mortbay.util.URI; 36 37 38 59 public class Invoker extends HttpServlet 60 { 61 private static Log log = LogFactory.getLog(Invoker.class); 62 63 private ServletHandler _servletHandler; 64 private Map.Entry _invokerEntry; 65 private Map _parameters; 66 private boolean _nonContextServlets; 67 private boolean _verbose; 68 69 70 public void init() 71 { 72 ServletContext config=getServletContext(); 73 _servletHandler=((ServletHandler.Context)config).getServletHandler(); 74 75 Enumeration e = getInitParameterNames(); 76 while(e.hasMoreElements()) 77 { 78 String param=(String )e.nextElement(); 79 String value=getInitParameter(param); 80 String lvalue=value.toLowerCase(); 81 if ("nonContextServlets".equals(param)) 82 { 83 _nonContextServlets=value.length()>0 && lvalue.startsWith("t"); 84 } 85 if ("verbose".equals(param)) 86 { 87 _verbose=value.length()>0 && lvalue.startsWith("t"); 88 } 89 else 90 { 91 if (_parameters==null) 92 _parameters=new HashMap (); 93 _parameters.put(param,value); 94 } 95 } 96 } 97 98 99 protected void service(HttpServletRequest request, HttpServletResponse response) 100 throws ServletException , IOException 101 { 102 boolean included=false; 104 String servlet_path=(String )request.getAttribute(Dispatcher.__INCLUDE_SERVLET_PATH); 105 if (servlet_path==null) 106 servlet_path=request.getServletPath(); 107 else 108 included=true; 109 String path_info = (String )request.getAttribute(Dispatcher.__INCLUDE_PATH_INFO); 110 if (path_info==null) 111 path_info=request.getPathInfo(); 112 113 String servlet = path_info; 115 if (servlet==null || servlet.length()<=1 ) 116 { 117 response.sendError(404); 118 return; 119 } 120 121 int i0=servlet.charAt(0)=='/'?1:0; 122 int i1=servlet.indexOf('/',i0); 123 servlet=i1<0?servlet.substring(i0):servlet.substring(i0,i1); 124 125 ServletHolder holder=_servletHandler.getServletHolder(servlet); 127 if (holder!=null) 128 { 129 _servletHandler.addServletHolder(holder); 131 _servletHandler.mapPathToServlet(URI.addPaths(servlet_path,servlet)+"/*", holder.getName()); 132 } 133 else 134 { 135 if (servlet.endsWith(".class")) 137 servlet=servlet.substring(0,servlet.length()-6); 138 if (servlet==null || servlet.length()==0) 139 { 140 response.sendError(404); 141 return; 142 } 143 144 synchronized(_servletHandler) 145 { 146 if (_invokerEntry==null) 148 _invokerEntry=_servletHandler.getHolderEntry(servlet_path); 149 150 String path=URI.addPaths(servlet_path,servlet); 152 Map.Entry entry = _servletHandler.getHolderEntry(path); 153 154 if (entry!=null && entry!=_invokerEntry) 155 { 156 holder=(ServletHolder)entry.getValue(); 158 } 159 else 160 { 161 holder=new ServletHolder(_servletHandler,servlet,servlet); 163 164 if (_parameters!=null) 165 holder.putAll(_parameters); 166 167 try {holder.start();} 168 catch (Exception e) 169 { 170 log.debug(LogSupport.EXCEPTION,e); 171 throw new UnavailableException (e.toString()); 172 } 173 174 if (!_nonContextServlets) 176 { 177 Object s=holder.getServlet(); 178 179 if (_servletHandler.getClassLoader()!= 180 s.getClass().getClassLoader()) 181 { 182 holder.stop(); 183 log.warn("Dynamic servlet "+s+ 184 " not loaded from context "+ 185 request.getContextPath()); 186 throw new UnavailableException ("Not in context"); 187 } 188 } 189 190 if (_verbose) 192 log("Dynamic load '"+servlet+"' at "+path); 193 _servletHandler.addServletHolder(holder); 194 _servletHandler.mapPathToServlet(path+"/*",holder.getName()); 195 _servletHandler.mapPathToServlet(path+".class/*",holder.getName()); 196 } 197 } 198 199 } 200 201 if (holder!=null) 202 holder.handle(new Request (request,included,servlet,servlet_path,path_info), 203 response); 204 else 205 response.sendError(404); 206 207 } 208 209 210 class Request extends HttpServletRequestWrapper 211 { 212 String _servletPath; 213 String _pathInfo; 214 boolean _included; 215 216 217 Request(HttpServletRequest request, 218 boolean included, 219 String name, 220 String servletPath, 221 String pathInfo) 222 { 223 super(request); 224 _included=included; 225 _servletPath=URI.addPaths(servletPath,name); 226 _pathInfo=pathInfo.substring(name.length()+1); 227 if (_pathInfo.length()==0) 228 _pathInfo=null; 229 } 230 231 232 public String getServletPath() 233 { 234 if (_included) 235 return super.getServletPath(); 236 return _servletPath; 237 } 238 239 240 public String getPathInfo() 241 { 242 if (_included) 243 return super.getPathInfo(); 244 return _pathInfo; 245 } 246 247 248 public Object getAttribute(String name) 249 { 250 if (_included) 251 { 252 if (name.equals(Dispatcher.__INCLUDE_REQUEST_URI)) 253 return URI.addPaths(URI.addPaths(getContextPath(),_servletPath),_pathInfo); 254 if (name.equals(Dispatcher.__INCLUDE_PATH_INFO)) 255 return _pathInfo; 256 if (name.equals(Dispatcher.__INCLUDE_SERVLET_PATH)) 257 return _servletPath; 258 } 259 return super.getAttribute(name); 260 } 261 } 262 } 263 | Popular Tags |