Skip to content

Commit b7e780b

Browse files
committed
Refactored WebXmlParser to use Rewrite's ServletRegistationProvider SPI
1 parent 776b39b commit b7e780b

1 file changed

Lines changed: 27 additions & 106 deletions

File tree

  • config-prettyfaces/src/main/java/com/ocpsoft/pretty/faces/config/servlet

‎config-prettyfaces/src/main/java/com/ocpsoft/pretty/faces/config/servlet/WebXmlParser.java‎

Lines changed: 27 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,53 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package com.ocpsoft.pretty.faces.config.servlet;
1817

1918
import java.io.IOException;
20-
import java.io.InputStream;
2119
import java.util.Collection;
22-
import java.util.Iterator;
23-
import java.util.Map;
20+
import java.util.Collections;
21+
import java.util.List;
2422

23+
import javax.faces.webapp.FacesServlet;
2524
import javax.servlet.ServletContext;
26-
import javax.servlet.ServletRegistration;
2725

28-
import org.apache.commons.digester.Digester;
2926
import org.apache.commons.logging.Log;
3027
import org.apache.commons.logging.LogFactory;
28+
import org.ocpsoft.common.pattern.WeightedComparator;
29+
import org.ocpsoft.common.services.ServiceLoader;
30+
import org.ocpsoft.common.util.Iterators;
31+
import org.ocpsoft.rewrite.servlet.ServletRegistration;
32+
import org.ocpsoft.rewrite.servlet.spi.ServletRegistrationProvider;
3133
import org.xml.sax.SAXException;
3234

33-
import com.ocpsoft.pretty.faces.config.PrettyConfigParser;
34-
import com.ocpsoft.pretty.faces.util.EmptyEntityResolver;
35-
3635
/**
37-
* Digester-based implementation of {@link PrettyConfigParser}.
36+
* This class is used to detect the mapping of the {@link FacesServlet}. The new implementation is based on Rewrite's
37+
* {@link ServletRegistrationProvider} SPI. This class should not be removed, because 3rd party frameworks like Seam
38+
* Faces are using it.
3839
*
3940
* @author Lincoln Baxter, III <lincoln@ocpsoft.com>
41+
* @author Christian Kaltepoth
4042
*/
4143
public class WebXmlParser
4244
{
4345
private static final Log log = LogFactory.getLog(WebXmlParser.class);
4446

4547
private static final String FACES_SERVLET = "javax.faces.webapp.FacesServlet";
46-
private static final String WEB_XML_PATH = "/WEB-INF/web.xml";
47-
public static final String CONFIG_FORCE_PARSING = "com.ocpsoft.pretty.DISABLE_SERVLET_3.0_SUPPORT";
4848

49-
String facesMapping = null;
49+
private String facesMapping = null;
5050

5151
public void parse(final ServletContext context) throws IOException, SAXException
5252
{
53-
/*
54-
* forceParsing must come first to avoid bug with WebLogic not fully implementing the servlet spec.
55-
*/
56-
if (!forceParsing(context) && context.getMajorVersion() >= 3)
57-
{
58-
Map<String, ? extends ServletRegistration> servlets = context.getServletRegistrations();
59-
if (servlets != null)
60-
{
61-
for (ServletRegistration s : servlets.values())
53+
54+
List<ServletRegistrationProvider> providers = Iterators.asList(
55+
ServiceLoader.loadTypesafe(ServletRegistrationProvider.class).iterator());
56+
Collections.sort(providers, new WeightedComparator());
57+
58+
for (ServletRegistrationProvider provider : providers) {
59+
List<ServletRegistration> registrations = provider.getServletRegistrations(context);
60+
if (registrations != null) {
61+
62+
for (ServletRegistration s : registrations)
6263
{
6364
if (s.getClassName().equalsIgnoreCase(FACES_SERVLET))
6465
{
@@ -70,97 +71,16 @@ public void parse(final ServletContext context) throws IOException, SAXException
7071
}
7172
}
7273
}
73-
}
74-
75-
if (facesMapping == null)
76-
{
77-
log.warn("Faces Servlet (javax.faces.webapp.FacesServlet) not found in web context - cannot configure PrettyFaces DynaView");
78-
}
79-
}
80-
else
81-
{
82-
InputStream in = context.getResourceAsStream(WEB_XML_PATH);
83-
if (in == null)
84-
{
85-
log.warn("No " + WEB_XML_PATH + " found - cannot configure PrettyFaces DynaView");
86-
}
87-
88-
WebXml webXml = new WebXml();
89-
if (in != null)
90-
{
91-
Digester digester = getConfiguredDigester();
92-
digester.push(webXml);
93-
digester.parse(in);
94-
processConfig(webXml);
95-
}
96-
}
97-
98-
log.trace("Completed parsing web.xml");
99-
}
100-
101-
private boolean forceParsing(ServletContext context)
102-
{
103-
String forceParsing = context.getInitParameter(CONFIG_FORCE_PARSING);
104-
if ((forceParsing != null) && forceParsing.trim().equalsIgnoreCase("true"))
105-
{
106-
log.debug("Servlet 3.0 API has been disabled! PrettyFaces will parse web.xml manually.");
107-
return true;
108-
}
109-
return false;
110-
}
11174

112-
private void processConfig(final WebXml webXml)
113-
{
114-
ServletDefinition facesServlet = null;
115-
if (webXml != null)
116-
{
117-
Iterator<ServletDefinition> si = webXml.getServlets().iterator();
118-
while ((facesServlet == null) && si.hasNext())
119-
{
120-
ServletDefinition servlet = si.next();
121-
if (FACES_SERVLET.equals(servlet.getServletClass()))
75+
if (facesMapping == null)
12276
{
123-
facesServlet = servlet;
124-
Iterator<ServletMapping> mi = webXml.getServletMappings().iterator();
125-
while ((facesMapping == null) && mi.hasNext())
126-
{
127-
ServletMapping mapping = mi.next();
128-
if (facesServlet.getServletName().equals(mapping.getServletName()))
129-
{
130-
facesMapping = mapping.getUrlPattern().trim();
131-
}
132-
}
77+
log.warn("Faces Servlet (javax.faces.webapp.FacesServlet) not found in web context - cannot configure PrettyFaces DynaView");
13378
}
79+
13480
}
13581
}
13682
}
13783

138-
private Digester getConfiguredDigester()
139-
{
140-
final Digester digester = new Digester();
141-
142-
/*
143-
* We use the context class loader to resolve classes. This fixes
144-
* ClassNotFoundExceptions on Geronimo.
145-
*/
146-
digester.setUseContextClassLoader(true);
147-
148-
// prevent downloading of DTDs
149-
digester.setEntityResolver(new EmptyEntityResolver());
150-
151-
digester.addObjectCreate("web-app/servlet", ServletDefinition.class);
152-
digester.addCallMethod("web-app/servlet/servlet-name", "setServletName", 0);
153-
digester.addCallMethod("web-app/servlet/servlet-class", "setServletClass", 0);
154-
digester.addSetNext("web-app/servlet", "addServlet");
155-
156-
digester.addObjectCreate("web-app/servlet-mapping", ServletMapping.class);
157-
digester.addCallMethod("web-app/servlet-mapping/servlet-name", "setServletName", 0);
158-
digester.addCallMethod("web-app/servlet-mapping/url-pattern", "setUrlPattern", 0);
159-
digester.addSetNext("web-app/servlet-mapping", "addServletMapping");
160-
161-
return digester;
162-
}
163-
16484
public boolean isFacesPresent()
16585
{
16686
return facesMapping != null;
@@ -174,4 +94,5 @@ public String getFacesMapping()
17494
}
17595
return "";
17696
}
97+
17798
}

0 commit comments

Comments
 (0)