001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.dispatch;
031:
032: import com.caucho.util.L10N;
033: import com.caucho.config.*;
034:
035: import javax.annotation.PostConstruct;
036: import javax.servlet.FilterChain;
037: import javax.servlet.Servlet;
038: import javax.servlet.ServletException;
039: import java.util.ArrayList;
040: import java.util.HashMap;
041: import java.util.logging.Level;
042: import java.util.logging.Logger;
043:
044: /**
045: * Manages the servlets.
046: */
047: public class ServletManager {
048: static final Logger log = Logger.getLogger(ServletManager.class
049: .getName());
050: static final L10N L = new L10N(ServletManager.class);
051:
052: private HashMap<String, ServletConfigImpl> _servlets = new HashMap<String, ServletConfigImpl>();
053:
054: private ArrayList<ServletConfigImpl> _servletList = new ArrayList<ServletConfigImpl>();
055:
056: private ArrayList<ServletConfigImpl> _cronList = new ArrayList<ServletConfigImpl>();
057:
058: private boolean _isLazyValidate;
059:
060: /**
061: * Sets true if validation is lazy.
062: */
063: public void setLazyValidate(boolean isLazy) {
064: _isLazyValidate = isLazy;
065: }
066:
067: /**
068: * Adds a servlet to the servlet manager.
069: */
070: public void addServlet(ServletConfigImpl config)
071: throws ServletException {
072: if (config.getServletContext() == null)
073: throw new NullPointerException();
074:
075: config.setServletManager(this );
076:
077: synchronized (_servlets) {
078: if (_servlets.get(config.getServletName()) != null) {
079: for (int i = _servletList.size() - 1; i >= 0; i--) {
080: ServletConfigImpl oldConfig = _servletList.get(i);
081:
082: if (config.getServletName().equals(
083: oldConfig.getServletName())) {
084: _servletList.remove(i);
085: break;
086: }
087: }
088:
089: /* XXX: need something more sophisticated since the
090: * resin.conf needs to override the web.xml
091: * throw new ServletConfigException(L.l("'{0}' is a duplicate servlet-name. Servlets must have a unique servlet-name.", config.getServletName()));
092: */
093: }
094:
095: try {
096: config.validateClass(!_isLazyValidate);
097: } catch (Exception e) {
098: if (log.isLoggable(Level.FINE))
099: log.log(Level.FINE, e.toString(), e);
100: else if (e instanceof ConfigException)
101: log.config(e.getMessage());
102: else
103: log.config(e.toString());
104: }
105:
106: _servlets.put(config.getServletName(), config);
107: _servletList.add(config);
108: }
109: }
110:
111: /**
112: * Adds a servlet to the servlet manager.
113: */
114: public ServletConfigImpl getServlet(String servletName) {
115: return _servlets.get(servletName);
116: }
117:
118: /**
119: * Initialize servlets that need starting at server start.
120: */
121: @PostConstruct
122: public void init() throws ServletException {
123: ArrayList<ServletConfigImpl> loadOnStartup;
124: loadOnStartup = new ArrayList<ServletConfigImpl>();
125:
126: for (int j = 0; j < _servletList.size(); j++) {
127: ServletConfigImpl config = _servletList.get(j);
128:
129: if (config.getLoadOnStartup() == Integer.MIN_VALUE)
130: continue;
131:
132: int i = 0;
133: for (; i < loadOnStartup.size(); i++) {
134: ServletConfigImpl config2 = loadOnStartup.get(i);
135:
136: if (config.getLoadOnStartup() < config2
137: .getLoadOnStartup()) {
138: loadOnStartup.add(i, config);
139: break;
140: }
141: }
142:
143: if (i == loadOnStartup.size())
144: loadOnStartup.add(config);
145:
146: if (config.getRunAt() != null)
147: _cronList.add(config);
148: }
149:
150: for (int i = 0; i < loadOnStartup.size(); i++) {
151: ServletConfigImpl config = loadOnStartup.get(i);
152:
153: try {
154: config.createServlet(false);
155: } catch (ServletException e) {
156: // XXX: should JSP failure also cause a system failure?
157: if (config.getJspFile() == null)
158: throw e;
159: else {
160: log.log(Level.WARNING, e.toString(), e);
161: }
162: }
163: }
164: }
165:
166: /**
167: * Creates the servlet chain for the servlet.
168: */
169: public FilterChain createServletChain(String servletName)
170: throws ServletException {
171: ServletConfigImpl config = _servlets.get(servletName);
172:
173: if (config == null) {
174: throw new ServletConfigException(
175: L
176: .l(
177: "'{0}' is not a known servlet. Servlets must be defined by <servlet> before being used.",
178: servletName));
179: }
180:
181: return config.createServletChain();
182: }
183:
184: /**
185: * Instantiates a servlet given its configuration.
186: *
187: * @param servletName the servlet
188: *
189: * @return the initialized servlet.
190: */
191: public Servlet createServlet(String servletName)
192: throws ServletException {
193: ServletConfigImpl config = _servlets.get(servletName);
194:
195: if (config == null) {
196: throw new ServletException(
197: L
198: .l(
199: "'{0}' is not a known servlet. Servlets must be defined by <servlet> before being used.",
200: servletName));
201: }
202:
203: return (Servlet) config.createServlet(false);
204: }
205:
206: /**
207: * Returns the servlet config.
208: */
209: ServletConfigImpl getServletConfig(String servletName) {
210: return _servlets.get(servletName);
211: }
212:
213: public void destroy() {
214: ArrayList<ServletConfigImpl> servletList;
215: servletList = new ArrayList<ServletConfigImpl>();
216:
217: if (_servletList != null) {
218: synchronized (_servletList) {
219: servletList.addAll(_servletList);
220: }
221: }
222:
223: for (int i = 0; i < servletList.size(); i++) {
224: ServletConfigImpl config = servletList.get(i);
225:
226: try {
227: config.close();
228: } catch (Throwable e) {
229: log.log(Level.FINE, e.toString(), e);
230: }
231: }
232: }
233: }
|