KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.dispatch;
30
31 import com.caucho.config.BuilderProgram;
32 import com.caucho.config.BuilderProgramContainer;
33 import com.caucho.config.ConfigELContext;
34 import com.caucho.config.types.RawString;
35 import com.caucho.el.EL;
36 import com.caucho.util.L10N;
37
38 import javax.el.ELContext;
39 import javax.servlet.ServletContext JavaDoc;
40 import javax.servlet.ServletException JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.HashMap JavaDoc;
43
44 /**
45  * Configuration for a servlet regexp.
46  */

47 public class ServletRegexp {
48   private static final L10N L = new L10N(ServletRegexp.class);
49
50   private String JavaDoc _urlRegexp;
51
52   private String JavaDoc _servletName;
53   private String JavaDoc _servletClassName;
54   
55   // The configuration program
56
private BuilderProgramContainer _program = new BuilderProgramContainer();
57   
58   /**
59    * Creates a new servlet regexp object.
60    */

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

68   public void setURLRegexp(String JavaDoc pattern)
69   {
70     _urlRegexp = pattern;
71   }
72
73   /**
74    * Gets the url regexp
75    */

76   public String JavaDoc getURLRegexp()
77   {
78     return _urlRegexp;
79   }
80
81   /**
82    * Sets the servlet name.
83    */

84   public void setServletName(RawString string)
85   {
86     _servletName = string.getValue();
87   }
88
89   /**
90    * Sets the servlet name.
91    */

92   public String JavaDoc getServletName()
93   {
94     return _servletName;
95   }
96
97   /**
98    * Sets the servlet class name.
99    */

100   public void setServletClass(RawString string)
101   {
102     _servletClassName = string.getValue();
103   }
104
105   /**
106    * Gets the servlet class name.
107    */

108   public String JavaDoc getServletClass()
109   {
110     return _servletClassName;
111   }
112
113   /**
114    * Adds to the builder program.
115    */

116   public void addBuilderProgram(BuilderProgram program)
117   {
118     _program.addProgram(program);
119   }
120
121   /**
122    * Returns the program.
123    */

124   public BuilderProgram getBuilderProgram()
125   {
126     return _program;
127   }
128
129   /**
130    * Initialize for a regexp.
131    */

132   String JavaDoc initRegexp(ServletContext JavaDoc application,
133             ServletManager manager,
134             ArrayList JavaDoc<String JavaDoc> vars)
135     throws ServletException JavaDoc
136   {
137     ELContext env = EL.getEnvironment();
138     
139     HashMap JavaDoc<String JavaDoc,Object JavaDoc> map = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
140     map.put("regexp", vars);
141
142     ELContext mapEnv = new ConfigELContext(map);
143
144     String JavaDoc rawName = _servletName;
145     String JavaDoc rawClassName = _servletClassName;
146
147     if (rawName == null)
148       rawName = rawClassName;
149
150     try {
151       String JavaDoc servletName = EL.evalString(rawName, mapEnv);
152
153       if (manager.getServletConfig(servletName) != null)
154     return servletName;
155       
156       String JavaDoc className = EL.evalString(rawClassName, mapEnv);
157
158       ServletConfigImpl config = new ServletConfigImpl();
159
160       config.setServletName(servletName);
161       config.setServletClass(className);
162       config.setServletContext(application);
163
164       _program.configure(config);
165
166       config.init();
167
168       manager.addServlet(config);
169
170       return servletName;
171     } catch (RuntimeException JavaDoc e) {
172       throw e;
173     } catch (ServletException JavaDoc e) {
174       throw e;
175     } catch (Throwable JavaDoc e) {
176       throw new ServletException JavaDoc(e);
177     }
178   }
179
180   /**
181    * Returns a printable representation of the servlet config object.
182    */

183   public String JavaDoc toString()
184   {
185     return "ServletRegexp[" + _urlRegexp + "]";
186   }
187 }
188
Popular Tags