use of org.apache.catalina.deploy.WebXml in project tomcat70 by apache.
the class TestContextConfigAnnotation method testCheckHandleTypes.
@Test
public void testCheckHandleTypes() throws Exception {
ContextConfig config = new ContextConfig();
config.handlesTypesAnnotations = true;
config.handlesTypesNonAnnotations = true;
// Need a Context, Loader and ClassLoader for checkHandleTypes
StandardContext context = new StandardContext();
context.setLoader(new TesterLoader());
config.context = context;
// Add an SCI that has no interest in any type
SCI sciNone = new SCI();
config.initializerClassMap.put(sciNone, new HashSet<Class<?>>());
// Add an SCI with an interest in Servlets
SCI sciServlet = new SCI();
config.initializerClassMap.put(sciServlet, new HashSet<Class<?>>());
config.typeInitializerMap.put(Servlet.class, new HashSet<ServletContainerInitializer>());
config.typeInitializerMap.get(Servlet.class).add(sciServlet);
// Add an SCI with an interest in Objects - i.e. everything
SCI sciObject = new SCI();
config.initializerClassMap.put(sciObject, new HashSet<Class<?>>());
config.typeInitializerMap.put(Object.class, new HashSet<ServletContainerInitializer>());
config.typeInitializerMap.get(Object.class).add(sciObject);
// Scan Servlet, Filter, Servlet, Listener
WebXml ignore = new WebXml();
File file = paramClassResource("org/apache/catalina/startup/ParamServlet");
config.processAnnotationsFile(file, ignore, false);
file = paramClassResource("org/apache/catalina/startup/ParamFilter");
config.processAnnotationsFile(file, ignore, false);
file = paramClassResource("org/apache/catalina/startup/TesterServlet");
config.processAnnotationsFile(file, ignore, false);
file = paramClassResource("org/apache/catalina/startup/TestListener");
config.processAnnotationsFile(file, ignore, false);
// Check right number of classes were noted to be handled
Assert.assertEquals(0, config.initializerClassMap.get(sciNone).size());
Assert.assertEquals(2, config.initializerClassMap.get(sciServlet).size());
Assert.assertEquals(4, config.initializerClassMap.get(sciObject).size());
}
use of org.apache.catalina.deploy.WebXml in project tomcat70 by apache.
the class TestContextConfigAnnotation method testNoMapping.
@Test
public void testNoMapping() throws Exception {
WebXml webxml = new WebXml();
ContextConfig config = new ContextConfig();
File pFile = paramClassResource("org/apache/catalina/startup/NoMappingParamServlet");
Assert.assertTrue(pFile.exists());
config.processAnnotationsFile(pFile, webxml, false);
ServletDef servletDef = webxml.getServlets().get("param1");
Assert.assertNull(servletDef);
webxml.addServletMapping("/param", "param1");
config.processAnnotationsFile(pFile, webxml, false);
servletDef = webxml.getServlets().get("param1");
Assert.assertNull(servletDef);
}
use of org.apache.catalina.deploy.WebXml in project tomcat70 by apache.
the class TestContextConfigAnnotation method testFilterMapping.
@Test
public void testFilterMapping() throws Exception {
WebXml webxml = new WebXml();
ContextConfig config = new ContextConfig();
File sFile = paramClassResource("org/apache/catalina/startup/ParamServlet");
config.processAnnotationsFile(sFile, webxml, false);
File fFile = paramClassResource("org/apache/catalina/startup/ParamFilter");
config.processAnnotationsFile(fFile, webxml, false);
FilterDef fdef = webxml.getFilters().get("paramFilter");
Assert.assertNotNull(fdef);
Assert.assertEquals("Servlet says: ", fdef.getParameterMap().get("message"));
}
use of org.apache.catalina.deploy.WebXml in project tomcat70 by apache.
the class ContextConfig method webConfig.
/**
* Scan the web.xml files that apply to the web application and merge them
* using the rules defined in the spec. For the global web.xml files,
* where there is duplicate configuration, the most specific level wins. ie
* an application's web.xml takes precedence over the host level or global
* web.xml file.
*/
protected void webConfig() {
/*
* Anything and everything can override the global and host defaults.
* This is implemented in two parts
* - Handle as a web fragment that gets added after everything else so
* everything else takes priority
* - Mark Servlets as overridable so SCI configuration can replace
* configuration from the defaults
*/
/*
* The rules for annotation scanning are not as clear-cut as one might
* think. Tomcat implements the following process:
* - As per SRV.1.6.2, Tomcat will scan for annotations regardless of
* which Servlet spec version is declared in web.xml. The EG has
* confirmed this is the expected behaviour.
* - As per http://java.net/jira/browse/SERVLET_SPEC-36, if the main
* web.xml is marked as metadata-complete, JARs are still processed
* for SCIs.
* - If metadata-complete=true and an absolute ordering is specified,
* JARs excluded from the ordering are also excluded from the SCI
* processing.
* - If an SCI has a @HandlesType annotation then all classes (except
* those in JARs excluded from an absolute ordering) need to be
* scanned to check if they match.
*/
Set<WebXml> defaults = new HashSet<WebXml>();
defaults.add(getDefaultWebXmlFragment());
WebXml webXml = createWebXml();
// Parse context level web.xml
InputSource contextWebXml = getContextWebXmlSource();
parseWebXml(contextWebXml, webXml, false);
ServletContext sContext = context.getServletContext();
// Ordering is important here
// Step 1. Identify all the JARs packaged with the application
// If the JARs have a web-fragment.xml it will be parsed at this
// point.
Map<String, WebXml> fragments = processJarsForWebFragments(webXml);
// Step 2. Order the fragments.
Set<WebXml> orderedFragments = null;
orderedFragments = WebXml.orderWebFragments(webXml, fragments, sContext);
// Step 3. Look for ServletContainerInitializer implementations
if (ok) {
processServletContainerInitializers();
}
if (!webXml.isMetadataComplete() || typeInitializerMap.size() > 0) {
// Step 4. Process /WEB-INF/classes for annotations
if (ok) {
// Hack required by Eclipse's "serve modules without
// publishing" feature since this backs WEB-INF/classes by
// multiple locations rather than one.
NamingEnumeration<Binding> listBindings = null;
try {
try {
listBindings = context.getResources().listBindings("/WEB-INF/classes");
} catch (NameNotFoundException ignore) {
// Safe to ignore
}
while (listBindings != null && listBindings.hasMoreElements()) {
Binding binding = listBindings.nextElement();
if (binding.getObject() instanceof FileDirContext) {
File webInfClassDir = new File(((FileDirContext) binding.getObject()).getDocBase());
processAnnotationsFile(webInfClassDir, webXml, webXml.isMetadataComplete());
} else if ("META-INF".equals(binding.getName())) {
// Skip the META-INF directory from any JARs that have been
// expanded in to WEB-INF/classes (sometimes IDEs do this).
} else {
String resource = "/WEB-INF/classes/" + binding.getName();
try {
URL url = sContext.getResource(resource);
processAnnotationsUrl(url, webXml, webXml.isMetadataComplete());
} catch (MalformedURLException e) {
log.error(sm.getString("contextConfig.webinfClassesUrl", resource), e);
}
}
}
} catch (NamingException e) {
log.error(sm.getString("contextConfig.webinfClassesUrl", "/WEB-INF/classes"), e);
}
}
// those fragments we are going to use
if (ok) {
processAnnotations(orderedFragments, webXml.isMetadataComplete());
}
// Cache, if used, is no longer required so clear it
javaClassCache.clear();
}
if (!webXml.isMetadataComplete()) {
// file.
if (ok) {
ok = webXml.merge(orderedFragments);
}
// Step 7. Apply global defaults
// Have to merge defaults before JSP conversion since defaults
// provide JSP servlet definition.
webXml.merge(defaults);
// Step 8. Convert explicitly mentioned jsps to servlets
if (ok) {
convertJsps(webXml);
}
// Step 9. Apply merged web.xml to Context
if (ok) {
webXml.configureContext(context);
}
} else {
webXml.merge(defaults);
convertJsps(webXml);
webXml.configureContext(context);
}
// Step 9a. Make the merged web.xml available to other
// components, specifically Jasper, to save those components
// from having to re-generate it.
// TODO Use a ServletContainerInitializer for Jasper
String mergedWebXml = webXml.toXml();
sContext.setAttribute(org.apache.tomcat.util.scan.Constants.MERGED_WEB_XML, mergedWebXml);
if (context.getLogEffectiveWebXml()) {
log.info("web.xml:\n" + mergedWebXml);
}
// Step 10. Look for static resources packaged in JARs
if (ok) {
// Spec does not define an order.
// Use ordered JARs followed by remaining JARs
Set<WebXml> resourceJars = new LinkedHashSet<WebXml>();
for (WebXml fragment : orderedFragments) {
resourceJars.add(fragment);
}
for (WebXml fragment : fragments.values()) {
if (!resourceJars.contains(fragment)) {
resourceJars.add(fragment);
}
}
processResourceJARs(resourceJars);
// See also StandardContext.resourcesStart() for
// WEB-INF/classes/META-INF/resources configuration
}
// context
if (ok) {
for (Map.Entry<ServletContainerInitializer, Set<Class<?>>> entry : initializerClassMap.entrySet()) {
if (entry.getValue().isEmpty()) {
context.addServletContainerInitializer(entry.getKey(), null);
} else {
context.addServletContainerInitializer(entry.getKey(), entry.getValue());
}
}
}
}
use of org.apache.catalina.deploy.WebXml in project tomcat70 by apache.
the class ContextConfig method processResourceJARs.
/**
* Scan JARs that contain web-fragment.xml files that will be used to
* configure this application to see if they also contain static resources.
* If static resources are found, add them to the context. Resources are
* added in web-fragment.xml priority order.
*/
protected void processResourceJARs(Set<WebXml> fragments) {
for (WebXml fragment : fragments) {
URL url = fragment.getURL();
Jar jar = null;
try {
// Note: Ignore file URLs for now since only jar URLs will be accepted
if ("jar".equals(url.getProtocol())) {
jar = JarFactory.newInstance(url);
jar.nextEntry();
String entryName = jar.getEntryName();
while (entryName != null) {
if (entryName.startsWith("META-INF/resources/")) {
context.addResourceJarUrl(url);
break;
}
jar.nextEntry();
entryName = jar.getEntryName();
}
} else if ("file".equals(url.getProtocol())) {
FileDirContext fileDirContext = new FileDirContext();
fileDirContext.setDocBase(new File(url.toURI()).getAbsolutePath());
try {
fileDirContext.lookup("META-INF/resources/");
// lookup succeeded
if (context instanceof StandardContext) {
((StandardContext) context).addResourcesDirContext(fileDirContext);
}
} catch (NamingException e) {
// not found, ignore
}
}
} catch (IOException ioe) {
log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName()));
} catch (URISyntaxException e) {
log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName()));
} finally {
if (jar != null) {
jar.close();
}
}
}
}
Aggregations