001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.jetty6;
017:
018: import java.util.Collection;
019: import java.util.Arrays;
020:
021: import org.apache.geronimo.gbean.GBeanInfo;
022: import org.apache.geronimo.gbean.GBeanInfoBuilder;
023: import org.apache.geronimo.gbean.ReferenceCollection;
024: import org.apache.geronimo.gbean.ReferenceCollectionEvent;
025: import org.apache.geronimo.gbean.ReferenceCollectionListener;
026: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
027: import org.mortbay.jetty.Handler;
028: import org.mortbay.jetty.servlet.FilterMapping;
029: import org.mortbay.jetty.servlet.ServletHandler;
030:
031: /**
032: * @version $Rev: 609448 $ $Date: 2008-01-06 14:31:09 -0800 (Sun, 06 Jan 2008) $
033: */
034: public class JettyFilterMapping extends FilterMapping {
035:
036: private final String[] urlPatterns;
037: private final boolean requestDispatch;
038: private final boolean forwardDispatch;
039: private final boolean includeDispatch;
040: private final boolean errorDispatch;
041: private final JettyFilterHolder jettyFilterHolder;
042: private final Collection<ServletNameSource> jettyServletHolders;
043: private final JettyFilterMapping previous;
044: private final JettyServletRegistration jettyServletRegistration;
045:
046: //todo use an interface for endpoints.
047: public JettyFilterMapping() {
048: this .urlPatterns = null;
049: this .requestDispatch = false;
050: this .forwardDispatch = false;
051: this .includeDispatch = false;
052: this .errorDispatch = false;
053: this .jettyFilterHolder = null;
054: this .jettyServletHolders = null;
055: this .previous = null;
056: this .jettyServletRegistration = null;
057: }
058:
059: public JettyFilterMapping(String[] urlPatterns,
060: boolean requestDispatch, boolean forwardDispatch,
061: boolean includeDispatch, boolean errorDispatch,
062: JettyFilterHolder jettyFilterHolder,
063: Collection<ServletNameSource> jettyServletHolders,
064: JettyFilterMapping previous,
065: JettyServletRegistration jettyServletRegistration) {
066: this .urlPatterns = urlPatterns;
067: this .requestDispatch = requestDispatch;
068: this .forwardDispatch = forwardDispatch;
069: this .includeDispatch = includeDispatch;
070: this .errorDispatch = errorDispatch;
071: this .jettyFilterHolder = jettyFilterHolder;
072: this .jettyServletHolders = jettyServletHolders;
073: this .previous = previous;
074: this .jettyServletRegistration = jettyServletRegistration;
075:
076: if (jettyServletRegistration != null) {
077: assert jettyServletHolders != null ^ urlPatterns != null;
078:
079: String filterName = jettyFilterHolder.getFilterName();
080: int dispatches = 0;
081: if (requestDispatch) {
082: dispatches |= Handler.REQUEST;
083: }
084: if (forwardDispatch) {
085: dispatches |= Handler.FORWARD;
086: }
087: if (includeDispatch) {
088: dispatches |= Handler.INCLUDE;
089: }
090: if (errorDispatch) {
091: dispatches |= Handler.ERROR;
092: }
093:
094: setFilterName(filterName);
095: setDispatches(dispatches);
096: setPathSpecs(urlPatterns);
097: if (jettyServletHolders != null) {
098: resetServlets();
099: if (jettyServletHolders instanceof ReferenceCollection) {
100: ((ReferenceCollection) jettyServletHolders)
101: .addReferenceCollectionListener(new ReferenceCollectionListener() {
102:
103: public void memberAdded(
104: ReferenceCollectionEvent event) {
105: resetServlets();
106: resetJettyFilterMappings();
107: }
108:
109: public void memberRemoved(
110: ReferenceCollectionEvent event) {
111: resetServlets();
112: resetJettyFilterMappings();
113: }
114: });
115: }
116: }
117:
118: jettyServletRegistration.getServletHandler()
119: .addFilterMapping(this );
120: }
121: }
122:
123: private void resetJettyFilterMappings() {
124: //This causes jetty to recompute the filter to servlet mappings based on the
125: //current servlet names in the filter mappings. Pretty inefficient.
126: ServletHandler servletHandler = jettyServletRegistration
127: .getServletHandler();
128: FilterMapping[] filterMappings = servletHandler
129: .getFilterMappings();
130: FilterMapping[] copy = filterMappings.clone();
131: servletHandler.setFilterMappings(copy);
132: }
133:
134: private void resetServlets() {
135: String[] servletNames = new String[jettyServletHolders.size()];
136: int i = 0;
137: for (ServletNameSource jettyServletHolder : jettyServletHolders) {
138: servletNames[i++] = jettyServletHolder.getServletName();
139: }
140: setServletNames(servletNames);
141: }
142:
143: public String[] getUrlPatterns() {
144: return urlPatterns;
145: }
146:
147: public boolean isRequestDispatch() {
148: return requestDispatch;
149: }
150:
151: public boolean isForwardDispatch() {
152: return forwardDispatch;
153: }
154:
155: public boolean isIncludeDispatch() {
156: return includeDispatch;
157: }
158:
159: public boolean isErrorDispatch() {
160: return errorDispatch;
161: }
162:
163: public JettyFilterHolder getFilter() {
164: return jettyFilterHolder;
165: }
166:
167: public Collection<ServletNameSource> getServlets() {
168: return jettyServletHolders;
169: }
170:
171: public JettyFilterMapping getPrevious() {
172: return previous;
173: }
174:
175: public JettyServletRegistration getJettyServletRegistration() {
176: return jettyServletRegistration;
177: }
178:
179: public static final GBeanInfo GBEAN_INFO;
180:
181: static {
182: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
183: JettyFilterMapping.class,
184: NameFactory.URL_WEB_FILTER_MAPPING);
185: infoBuilder.addAttribute("urlPatterns", String[].class, true);
186: infoBuilder
187: .addAttribute("requestDispatch", boolean.class, true);
188: infoBuilder
189: .addAttribute("forwardDispatch", boolean.class, true);
190: infoBuilder
191: .addAttribute("includeDispatch", boolean.class, true);
192: infoBuilder.addAttribute("errorDispatch", boolean.class, true);
193:
194: infoBuilder.addReference("Filter", JettyFilterHolder.class,
195: NameFactory.WEB_FILTER);
196: infoBuilder.addReference("Servlets", JettyServletHolder.class,
197: NameFactory.SERVLET);
198: infoBuilder.addReference("Previous", JettyFilterMapping.class,
199: NameFactory.URL_WEB_FILTER_MAPPING);
200: infoBuilder.addReference("JettyServletRegistration",
201: JettyServletRegistration.class, NameFactory.WEB_MODULE);
202:
203: infoBuilder.setConstructor(new String[] { "urlPatterns",
204: "requestDispatch", "forwardDispatch",
205: "includeDispatch", "errorDispatch", "Filter",
206: "Servlets", "Previous", "JettyServletRegistration" });
207:
208: GBEAN_INFO = infoBuilder.getBeanInfo();
209: }
210:
211: public static GBeanInfo getGBeanInfo() {
212: return GBEAN_INFO;
213: }
214: }
|