servletconfigimpl.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 963 行 · 第 1/2 页

JAVA
963
字号
    if (_servletName != null) {    }    else if (getServletNameDefault() != null) {      // server/13f4      _servletName = getServletNameDefault();    }    else if (_protocolConfig != null) {      String protocolName = _protocolConfig.getUri();            setServletName(_servletClassName + "-" + protocolName);    }    else      setServletName(_servletClassName);    // XXX: should only be for web services    if (_jndiName != null) {      validateClass(true);            Object servlet = createServlet(false);      try {	Jndi.bindDeepShort(_jndiName, servlet);      } catch (NamingException e) {	throw new ServletException(e);      }    }    if (_var != null) {      validateClass(true);            Object servlet = createServlet(false);      WebBeansContainer webBeans = WebBeansContainer.create();      webBeans.addSingleton(servlet, _var);    }  }  protected void validateClass(boolean requireClass)    throws ServletException  {    if (_runAt != null || _loadOnStartup >= 0)      requireClass = true;    Thread thread = Thread.currentThread();    ClassLoader loader = thread.getContextClassLoader();    if (_servletClassName == null) {    }    else if (_servletClassName.equals("invoker")) {    }    else {      try {        _servletClass = Class.forName(_servletClassName, false, loader);      } catch (ClassNotFoundException e) {	if (e instanceof CompileException)	  throw error(e);	        log.log(Level.FINER, e.toString(), e);      }      if (_servletClass != null) {      }      else if (requireClass) {        throw error(L.l("'{0}' is not a known servlet.  Servlets belong in the classpath, often in WEB-INF/classes.", _servletClassName));      }      else {        String location = _location != null ? _location : "";        log.warning(L.l(location + "'{0}' is not a known servlet.  Servlets belong in the classpath, often in WEB-INF/classes.", _servletClassName));        return;      }      Config.checkCanInstantiate(_servletClass);      if (Servlet.class.isAssignableFrom(_servletClass)) {      }      else if (_protocolConfig != null) {      }      /*      else if (_servletClass.isAnnotationPresent(WebService.class)) {	// update protocol for "soap"?      }       else if (_servletClass.isAnnotationPresent(WebServiceProvider.class)) {	// update protocol for "soap"?      }      */      else        throw error(L.l("'{0}' must implement javax.servlet.Servlet or have a <protocol>.  All servlets must implement the Servlet interface.", _servletClassName));      /*      if (Modifier.isAbstract(_servletClass.getModifiers()))        throw error(L.l("'{0}' must not be abstract.  Servlets must be fully-implemented classes.", _servletClassName));      if (! Modifier.isPublic(_servletClass.getModifiers()))        throw error(L.l("'{0}' must be public.  Servlets must be public classes.", _servletClassName));      checkConstructor();      */    }  }  /**   * Checks the class constructor for the public-zero arg.   */  public void checkConstructor()    throws ServletException  {    Constructor []constructors = _servletClass.getDeclaredConstructors();    Constructor zeroArg = null;    for (int i = 0; i < constructors.length; i++) {      if (constructors[i].getParameterTypes().length == 0) {        zeroArg = constructors[i];        break;      }    }    if (zeroArg == null)      throw error(L.l("'{0}' must have a zero arg constructor.  Servlets must have public zero-arg constructors.\n{1} is not a valid constructor.", _servletClassName, constructors != null ? constructors[0] : null));    if (! Modifier.isPublic(zeroArg.getModifiers()))        throw error(L.l("'{0}' must be public.  '{1}' must have a public, zero-arg constructor.",                        zeroArg,                        _servletClassName));  }  /**   * Handles a cron alarm callback.   */  public void handleAlarm(Alarm alarm)  {    try {      log.fine(this + " cron");      FilterChain chain = createServletChain();      ServletRequest req = new StubServletRequest();      ServletResponse res = new StubServletResponse();      chain.doFilter(req, res);    } catch (Throwable e) {      log.log(Level.WARNING, e.toString(), e);    } finally {      long nextTime = _runAt.getNextTimeout(Alarm.getCurrentTime());      _alarm.queue(nextTime - Alarm.getCurrentTime());    }  }  public FilterChain createServletChain()    throws ServletException  {    synchronized (this) {      // JSP files need to have separate chains created for each JSP            if (_servletChain != null)	return _servletChain;      else        return createServletChainImpl();    }  }  private FilterChain createServletChainImpl()    throws ServletException  {    String jspFile = getJspFile();    FilterChain servletChain = null;    if (jspFile != null) {      QServlet jsp = (QServlet) _servletManager.createServlet("resin-jsp");      servletChain = new PageFilterChain(_servletContext, jsp, jspFile, this);      return servletChain;    }    validateClass(true);    Class servletClass = getServletClass();    if (servletClass == null) {      throw new IllegalStateException(L.l("servlet class for {0} can't be null",                                          getServletName()));    }    else if (QServlet.class.isAssignableFrom(servletClass)) {      servletChain = new PageFilterChain(_servletContext, (QServlet) createServlet(false));    }    else if (SingleThreadModel.class.isAssignableFrom(servletClass)) {      servletChain = new SingleThreadServletFilterChain(this);    }    else if (_protocolConfig != null) {      servletChain = new WebServiceFilterChain(this);    }    else if (CometServlet.class.isAssignableFrom(servletClass))      servletChain = new CometServletFilterChain(this);    else {      servletChain = new ServletFilterChain(this);    }    if (_roleMap != null)      servletChain = new SecurityRoleMapFilterChain(servletChain, _roleMap);    // server/10a8.  JSP pages need a fresh PageFilterChain    // XXX: lock contention issues with JSPs?    /*    if (! QServlet.class.isAssignableFrom(servletClass))      _servletChain = servletChain;    */    return servletChain;  }  /**   * Instantiates a web service.   *   * @return the initialized servlet.   */  /*  ProtocolServlet createWebServiceSkeleton()    throws ServletException  {    try {      Object service = createServlet(false);      ProtocolServlet skeleton        = (ProtocolServlet) _protocolClass.newInstance();      skeleton.setService(service);      if (_protocolInit != null) {        _protocolInit.configure(skeleton);      }      skeleton.init(this);      return skeleton;    } catch (RuntimeException e) {      throw e;    } catch (ServletException e) {      throw e;    } catch (Exception e) {      throw new ServletException(e);    }  }  */  /**   * Instantiates a servlet given its configuration.   *   * @param servletName the servlet   *   * @return the initialized servlet.   */  Object createServlet(boolean isNew)    throws ServletException  {    // server/102e    if (_servlet != null && ! isNew)      return _servlet;    Object servlet = null;    if (Alarm.getCurrentTime() < _nextInitTime)      throw _initException;    try {      synchronized (this) {	if (! isNew && _servlet != null)	  return _servlet;	  	// XXX: this was outside of the sync block	servlet = createServletImpl();	if (! isNew)	  _servlet = servlet;      }      if (log.isLoggable(Level.FINE))        log.finer("Servlet[" + _servletName + "] active");      //J2EEManagedObject.register(new com.caucho.management.j2ee.Servlet(this));      if (! isNew) {	// If the servlet has an MBean, register it	try {	  Hashtable<String,String> props = new Hashtable<String,String>();	  props.put("type", _servlet.getClass().getSimpleName());	  props.put("name", _servletName);	  Jmx.register(_servlet, props);	} catch (Exception e) {	  log.finest(e.toString());	}	if (_runAt != null && _alarm != null) {	  long nextTime = _runAt.getNextTimeout(Alarm.getCurrentTime());	  _alarm.queue(nextTime - Alarm.getCurrentTime());	}      }      if (log.isLoggable(Level.FINE))        log.finer("Servlet[" + _servletName + "] active");      return servlet;    } catch (ServletException e) {      throw e;    } catch (Throwable e) {      throw new ServletException(e);    }  }  Servlet createProtocolServlet()    throws ServletException  {    try {      Object service = createServletImpl();      if (_protocolFactory == null)	_protocolFactory = _protocolConfig.createFactory();      Servlet servlet = _protocolFactory.createServlet(_servletClass, service);      servlet.init(this);      return servlet;    } catch (RuntimeException e) {      throw e;    } catch (ServletException e) {      throw e;    } catch (Exception e) {      throw new ServletException(e);    }  }  private Object createServletImpl()    throws Exception  {    Class servletClass = getServletClass();    Object servlet;    if (_jspFile != null) {      servlet = createJspServlet(_servletName, _jspFile);      if (servlet == null)        throw new ServletException(L.l("'{0}' is a missing JSP file.",                                       _jspFile));    }    else if (servletClass != null) {      WebBeansContainer webBeans = WebBeansContainer.create();            _comp = (ComponentImpl) webBeans.createTransient(servletClass);            servlet = _comp.createNoInit();    }    else      throw new ServletException(L.l("Null servlet class for '{0}'.",                                     _servletName));    configureServlet(servlet);    try {      if (servlet instanceof Page) {	// server/102i	// page already configured      }      else if (servlet instanceof Servlet) {	Servlet servletObj = (Servlet) servlet;		servletObj.init(this);      }    } catch (UnavailableException e) {      setInitException(e);      throw e;    }    return servlet;  }  /**   *  Configure the servlet (everything that is done after   *  instantiation but before servlet.init()   */  void configureServlet(Object servlet)  {    //InjectIntrospector.configure(servlet);    // Initialize bean properties    ConfigProgram init = getInit();    if (init != null)      init.configure(servlet);    Config.init(servlet);  }  /**   * Instantiates a servlet given its configuration.   *   * @param servletName the servlet   *   * @return the initialized servlet.   */  private Servlet createJspServlet(String servletName, String jspFile)    throws ServletException  {    try {      ServletConfigImpl jspConfig = _servletManager.getServlet("resin-jsp");      QServlet jsp = (QServlet) jspConfig.createServlet(false);      // server/105o      Page page = jsp.getPage(servletName, jspFile, this);      return page;    } catch (ServletException e) {      throw e;    } catch (Exception e) {      throw new ServletException(e);    }  }  void killServlet()  {    Object servlet = _servlet;    _servlet = null;    Alarm alarm = _alarm;    _alarm = null;        if (alarm != null)      alarm.dequeue();        if (_comp != null)      _comp.destroy(servlet);    if (servlet instanceof Servlet) {      ((Servlet) servlet).destroy();    }  }  public void close()  {    killServlet();    _alarm = null;  }  protected ConfigException error(String msg)  {    if (_location != null)      return new LineConfigException(_location + msg);    else      return new ConfigException(msg);  }  protected ConfigException error(String msg, Throwable e)  {    if (_location != null)      return new LineConfigException(_location + msg, e);    else      return new ConfigException(msg, e);  }  protected RuntimeException error(Throwable e)  {    if (_location != null)      return new LineConfigException(_location + e.getMessage(), e);    else      return ConfigException.create(e);  }  /**   * Returns a printable representation of the servlet config object.   */  public String toString()  {    return "ServletConfigImpl[name=" + _servletName + ",class=" + _servletClass + "]";  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?