1 29 30 package com.caucho.server.dispatch; 31 32 import com.caucho.log.Log; 33 import com.caucho.util.L10N; 34 35 import javax.annotation.PostConstruct; 36 import javax.servlet.FilterChain ; 37 import javax.servlet.Servlet ; 38 import javax.servlet.ServletException ; 39 import java.util.ArrayList ; 40 import java.util.HashMap ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 47 public class ServletManager { 48 static final Logger log = Log.open(ServletManager.class); 49 static final L10N L = new L10N(ServletManager.class); 50 51 private HashMap <String ,ServletConfigImpl> _servlets 52 = new HashMap <String ,ServletConfigImpl>(); 53 54 private ArrayList <ServletConfigImpl> _servletList 55 = new ArrayList <ServletConfigImpl>(); 56 57 private ArrayList <ServletConfigImpl> _cronList 58 = new ArrayList <ServletConfigImpl>(); 59 60 private boolean _isLazyValidate; 61 62 65 public void setLazyValidate(boolean isLazy) 66 { 67 _isLazyValidate = isLazy; 68 } 69 70 73 public void addServlet(ServletConfigImpl config) 74 throws ServletException 75 { 76 if (config.getServletContext() == null) 77 throw new NullPointerException (); 78 79 config.setServletManager(this); 80 81 synchronized (_servlets) { 82 if (_servlets.get(config.getServletName()) != null) { 83 for (int i = _servletList.size() - 1; i >= 0; i--) { 84 ServletConfigImpl oldConfig = _servletList.get(i); 85 86 if (config.getServletName().equals(oldConfig.getServletName())) { 87 _servletList.remove(i); 88 break; 89 } 90 } 91 92 96 } 97 98 config.validateClass(! _isLazyValidate); 99 100 _servlets.put(config.getServletName(), config); 101 _servletList.add(config); 102 } 103 } 104 105 108 public ServletConfigImpl getServlet(String servletName) 109 { 110 return _servlets.get(servletName); 111 } 112 113 116 @PostConstruct 117 public void init() 118 throws ServletException 119 { 120 ArrayList <ServletConfigImpl> loadOnStartup; 121 loadOnStartup = new ArrayList <ServletConfigImpl>(); 122 123 for (int j = 0; j < _servletList.size(); j++) { 124 ServletConfigImpl config = _servletList.get(j); 125 126 if (config.getLoadOnStartup() == Integer.MIN_VALUE) 127 continue; 128 129 int i = 0; 130 for (; i < loadOnStartup.size(); i++) { 131 ServletConfigImpl config2 = loadOnStartup.get(i); 132 133 if (config.getLoadOnStartup() < config2.getLoadOnStartup()) { 134 loadOnStartup.add(i, config); 135 break; 136 } 137 } 138 139 if (i == loadOnStartup.size()) 140 loadOnStartup.add(config); 141 142 if (config.getRunAt() != null) 143 _cronList.add(config); 144 } 145 146 for (int i = 0; i < loadOnStartup.size(); i++) { 147 ServletConfigImpl config = loadOnStartup.get(i); 148 149 try { 150 config.createServlet(false); 151 } catch (ServletException e) { 152 log.log(Level.WARNING, e.toString(), e); 153 154 if (config.getJspFile() == null) 156 throw e; 157 } 158 } 159 } 160 161 164 public FilterChain createServletChain(String servletName) 165 throws ServletException 166 { 167 ServletConfigImpl config = _servlets.get(servletName); 168 169 if (config == null) { 170 throw new ServletConfigException(L.l("'{0}' is not a known servlet. Servlets must be defined by <servlet> before being used.", servletName)); 171 } 172 173 return config.createServletChain(); 174 } 175 176 183 public Servlet createServlet(String servletName) 184 throws ServletException 185 { 186 ServletConfigImpl config = _servlets.get(servletName); 187 188 if (config == null) { 189 throw new ServletException (L.l("'{0}' is not a known servlet. Servlets must be defined by <servlet> before being used.", servletName)); 190 } 191 192 return (Servlet ) config.createServlet(false); 193 } 194 195 198 ServletConfigImpl getServletConfig(String servletName) 199 { 200 return _servlets.get(servletName); 201 } 202 203 public void destroy() 204 { 205 ArrayList <ServletConfigImpl> servletList; 206 servletList = new ArrayList <ServletConfigImpl>(); 207 208 if (_servletList != null) { 209 synchronized (_servletList) { 210 servletList.addAll(_servletList); 211 } 212 } 213 214 for (int i = 0; i < servletList.size(); i++) { 215 ServletConfigImpl config = servletList.get(i); 216 217 try { 218 config.close(); 219 } catch (Throwable e) { 220 log.log(Level.FINE, e.toString(), e); 221 } 222 } 223 } 224 } 225 | Popular Tags |