KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > dispatch > ServletManager


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
37 import javax.servlet.Servlet JavaDoc;
38 import javax.servlet.ServletException JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * Manages the servlets.
46  */

47 public class ServletManager {
48   static final Logger JavaDoc log = Log.open(ServletManager.class);
49   static final L10N L = new L10N(ServletManager.class);
50
51   private HashMap JavaDoc<String JavaDoc,ServletConfigImpl> _servlets
52     = new HashMap JavaDoc<String JavaDoc,ServletConfigImpl>();
53   
54   private ArrayList JavaDoc<ServletConfigImpl> _servletList
55     = new ArrayList JavaDoc<ServletConfigImpl>();
56   
57   private ArrayList JavaDoc<ServletConfigImpl> _cronList
58   = new ArrayList JavaDoc<ServletConfigImpl>();
59
60   private boolean _isLazyValidate;
61
62   /**
63    * Sets true if validation is lazy.
64    */

65   public void setLazyValidate(boolean isLazy)
66   {
67     _isLazyValidate = isLazy;
68   }
69
70   /**
71    * Adds a servlet to the servlet manager.
72    */

73   public void addServlet(ServletConfigImpl config)
74     throws ServletException JavaDoc
75   {
76     if (config.getServletContext() == null)
77       throw new NullPointerException JavaDoc();
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     /* XXX: need something more sophisticated since the
93      * resin.conf needs to override the web.xml
94      * throw new ServletConfigException(L.l("'{0}' is a duplicate servlet-name. Servlets must have a unique servlet-name.", config.getServletName()));
95      */

96       }
97     
98       config.validateClass(! _isLazyValidate);
99     
100       _servlets.put(config.getServletName(), config);
101       _servletList.add(config);
102     }
103   }
104
105   /**
106    * Adds a servlet to the servlet manager.
107    */

108   public ServletConfigImpl getServlet(String JavaDoc servletName)
109   {
110     return _servlets.get(servletName);
111   }
112
113   /**
114    * Initialize servlets that need starting at server start.
115    */

116   @PostConstruct
117   public void init()
118     throws ServletException JavaDoc
119   {
120     ArrayList JavaDoc<ServletConfigImpl> loadOnStartup;
121     loadOnStartup = new ArrayList JavaDoc<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 JavaDoc e) {
152         log.log(Level.WARNING, e.toString(), e);
153
154     // XXX: should JSP failure also cause a system failure?
155
if (config.getJspFile() == null)
156       throw e;
157       }
158     }
159   }
160
161   /**
162    * Creates the servlet chain for the servlet.
163    */

164   public FilterChain JavaDoc createServletChain(String JavaDoc servletName)
165     throws ServletException JavaDoc
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   /**
177    * Instantiates a servlet given its configuration.
178    *
179    * @param servletName the servlet
180    *
181    * @return the initialized servlet.
182    */

183   public Servlet JavaDoc createServlet(String JavaDoc servletName)
184     throws ServletException JavaDoc
185   {
186     ServletConfigImpl config = _servlets.get(servletName);
187
188     if (config == null) {
189       throw new ServletException JavaDoc(L.l("'{0}' is not a known servlet. Servlets must be defined by <servlet> before being used.", servletName));
190     }
191
192     return (Servlet JavaDoc) config.createServlet(false);
193   }
194
195   /**
196    * Returns the servlet config.
197    */

198   ServletConfigImpl getServletConfig(String JavaDoc servletName)
199   {
200     return _servlets.get(servletName);
201   }
202
203   public void destroy()
204   {
205     ArrayList JavaDoc<ServletConfigImpl> servletList;
206     servletList = new ArrayList JavaDoc<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 JavaDoc e) {
220         log.log(Level.FINE, e.toString(), e);
221       }
222     }
223   }
224 }
225
Popular Tags