KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.ConfigELContext;
33 import com.caucho.config.ConfigException;
34 import com.caucho.config.types.InitProgram;
35 import com.caucho.el.EL;
36 import com.caucho.server.webapp.WebApp;
37 import com.caucho.util.L10N;
38
39 import javax.el.ELContext;
40 import javax.servlet.ServletContext JavaDoc;
41 import javax.servlet.ServletException JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.logging.Level JavaDoc;
45
46 /**
47  * Configuration for a servlet.
48  */

49 public class ServletMapping extends ServletConfigImpl {
50   private static final L10N L = new L10N(ServletMapping.class);
51
52   private ArrayList JavaDoc<Mapping> _mappingList
53     = new ArrayList JavaDoc<Mapping>();
54   private String JavaDoc _urlPattern;
55   private String JavaDoc _urlRegexp;
56   private boolean _isStrictMapping;
57   
58   /**
59    * Creates a new servlet mapping object.
60    */

61   public ServletMapping()
62   {
63   }
64
65   /**
66    * Sets the url pattern
67    */

68   public void addURLPattern(String JavaDoc pattern)
69   {
70     if (pattern.indexOf('\n') > -1)
71       throw new ConfigException(L.l("`{0}' cannot contain newline", "url-pattern"));
72
73     _mappingList.add(new Mapping(pattern, null));
74   }
75
76   /**
77    * Sets the url regexp
78    */

79   public void addURLRegexp(String JavaDoc pattern)
80   {
81     _mappingList.add(new Mapping(null, pattern));
82   }
83
84   /**
85    * True if strict mapping should be enabled.
86    */

87   public boolean isStrictMapping()
88   {
89     return _isStrictMapping;
90   }
91
92   /**
93    * Set if strict mapping should be enabled.
94    */

95   public void setStrictMapping(boolean isStrictMapping)
96   {
97     _isStrictMapping = isStrictMapping;
98   }
99
100   /**
101    * initialize.
102    */

103   public void init(ServletMapper mapper)
104     throws ServletException JavaDoc
105   {
106     boolean hasInit = false;
107
108     for (int i = 0; i < _mappingList.size(); i++) {
109       Mapping mapping = _mappingList.get(i);
110
111       String JavaDoc urlPattern = mapping.getUrlPattern();
112       String JavaDoc urlRegexp = mapping.getUrlRegexp();
113
114       if (getServletName() == null
115       && getServletClassName() != null
116       && urlPattern != null) {
117     setServletName(urlPattern);
118       }
119
120       if (urlPattern != null && ! hasInit) {
121     hasInit = true;
122     super.init();
123
124     if (getServletClassName() != null)
125       mapper.getServletManager().addServlet(this);
126       }
127
128       if (urlPattern != null)
129     mapper.addUrlMapping(urlPattern, getServletName(), this);
130       else
131     mapper.addUrlRegexp(urlRegexp, this);
132     }
133
134     /*
135     if (_urlRegexp == null) {
136       if (getServletName() == null && getServletClassName() != null) {
137     // server/13f4
138       }
139
140     }
141     */

142   }
143
144   /**
145    * Initialize for a regexp.
146    */

147   String JavaDoc initRegexp(ServletContext JavaDoc webApp,
148             ServletManager manager,
149             ArrayList JavaDoc<String JavaDoc> vars)
150     throws ServletException JavaDoc
151   {
152     ELContext env = EL.getEnvironment();
153     HashMap JavaDoc<String JavaDoc,Object JavaDoc> map = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
154     map.put("regexp", vars);
155
156     ELContext mapEnv = new ConfigELContext(map);
157
158     String JavaDoc rawName = getServletName();
159     String JavaDoc rawClassName = getServletClassName();
160
161     if (rawName == null)
162       rawName = rawClassName;
163
164     if (rawClassName == null)
165       rawClassName = rawName;
166
167     try {
168       String JavaDoc servletName = EL.evalString(rawName, mapEnv);
169
170       if (manager.getServletConfig(servletName) != null)
171     return servletName;
172       
173       String JavaDoc className = EL.evalString(rawClassName, mapEnv);
174
175       try {
176     WebApp app = (WebApp) getServletContext();
177
178     Class JavaDoc cl = Class.forName(className, false, app.getClassLoader());
179       } catch (ClassNotFoundException JavaDoc e) {
180     log.log(Level.WARNING, e.toString(), e);
181
182     return null;
183       }
184
185       ServletConfigImpl config = new ServletConfigImpl();
186
187       config.setServletName(servletName);
188       config.setServletClass(className);
189       config.setServletContext(webApp);
190
191       InitProgram program = getInit();
192       if (program != null)
193     program.init(config);
194
195       config.init();
196
197       manager.addServlet(config);
198
199       return servletName;
200     } catch (RuntimeException JavaDoc e) {
201       throw e;
202     } catch (ServletException JavaDoc e) {
203       throw e;
204     } catch (Throwable JavaDoc e) {
205       throw new ServletException JavaDoc(e);
206     }
207   }
208
209   /**
210    * Returns a printable representation of the servlet config object.
211    */

212   public String JavaDoc toString()
213   {
214     return "ServletMapping[pattern=" + _urlPattern + ",name=" + getServletName() + "]";
215   }
216
217   static class Mapping {
218     private final String JavaDoc _urlPattern;
219     private final String JavaDoc _urlRegexp;
220
221     Mapping(String JavaDoc urlPattern, String JavaDoc urlRegexp)
222     {
223       _urlPattern = urlPattern;
224       _urlRegexp = urlRegexp;
225     }
226
227     String JavaDoc getUrlPattern()
228     {
229       return _urlPattern;
230     }
231
232     String JavaDoc getUrlRegexp()
233     {
234       return _urlRegexp;
235     }
236
237     public String JavaDoc toString()
238     {
239       return "ServletMapping[" + _urlPattern + ", " + _urlRegexp + "]";
240     }
241   }
242 }
243
Popular Tags