Author: pero
Date: Wed Apr 28 22:16:49 2010
New Revision: 939114
URL: http://svn.apache.org/viewvc?rev=939114&view=rev
Log:
web.xml and fragments overwrite annotation parameters (s. Servlet API Sepc Nov
2009 Section 8.2.3.3 pages 80-84)
not yet complete -- filter support is missing!
Added:
tomcat/trunk/test/org/apache/catalina/startup/DuplicateMappingParamServlet.java
(with props)
tomcat/trunk/test/org/apache/catalina/startup/NoMappingParamServlet.java
(with props)
tomcat/trunk/test/org/apache/catalina/startup/ParamServlet.java (with
props)
tomcat/trunk/test/org/apache/catalina/startup/TestContextConfigAnnotation.java
(with props)
Modified:
tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java
Modified: tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java?rev=939114&r1=939113&r2=939114&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java (original)
+++ tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java Wed Apr 28
22:16:49 2010
@@ -1890,17 +1890,31 @@ public class ContextConfig
protected void processAnnotationWebServlet(String className,
AnnotationEntry ae, WebXml fragment) {
- if (fragment.getServlets().containsKey(className)) {
- // Skip this annotation. Entry in web.xml takes priority
- return;
+ String servletName = null;
+ // must search for name s. Spec Servlet API 3.0 - 8.2.3.3.n.ii page 81
+ ElementValuePair[] evps = ae.getElementValuePairs();
+ for (ElementValuePair evp : evps) {
+ String name = evp.getNameString();
+ if ("name".equals(name)) {
+ servletName = evp.getValue().stringifyValue();
+ break;
+ }
+ }
+ if(servletName == null) {
+ // classname is default servletName as annotation has no name!
+ servletName = className;
+ }
+ ServletDef servletDef = fragment.getServlets().get(servletName);
+ boolean isWebXMLservletDef = servletDef != null;
+ if(!isWebXMLservletDef) {
+ servletDef = new ServletDef();
+ servletDef.setServletName(servletName);
+ servletDef.setServletClass(className);
}
boolean urlPatternsSet = false;
- ServletDef servletDef = new ServletDef();
- servletDef.setServletName(className);
- servletDef.setServletClass(className);
String[] urlPatterns = null;
- ElementValuePair[] evps = ae.getElementValuePairs();
+ //ElementValuePair[] evps = ae.getElementValuePairs();
for (ElementValuePair evp : evps) {
String name = evp.getNameString();
if ("value".equals(name) || "urlPatterns".equals(name)) {
@@ -1910,38 +1924,64 @@ public class ContextConfig
}
urlPatternsSet = true;
urlPatterns = processAnnotationsStringArray(evp.getValue());
- } else if ("name".equals(name)) {
- servletDef.setServletName(evp.getValue().stringifyValue());
} else if ("description".equals(name)) {
- servletDef.setDescription(evp.getValue().stringifyValue());
+ if(servletDef.getDescription() == null) {
+
servletDef.setDescription(evp.getValue().stringifyValue());
+ }
} else if ("displayName".equals(name)) {
- servletDef.setDisplayName(evp.getValue().stringifyValue());
+ if(servletDef.getDisplayName() == null) {
+
servletDef.setDisplayName(evp.getValue().stringifyValue());
+ }
} else if ("largeIcon".equals(name)) {
- servletDef.setLargeIcon(evp.getValue().stringifyValue());
+ if(servletDef.getLargeIcon() == null) {
+
servletDef.setLargeIcon(evp.getValue().stringifyValue());
+ }
} else if ("smallIcon".equals(name)) {
- servletDef.setSmallIcon(evp.getValue().stringifyValue());
+ if(servletDef.getSmallIcon() == null) {
+
servletDef.setSmallIcon(evp.getValue().stringifyValue());
+ }
} else if ("asyncSupported".equals(name)) {
- servletDef.setAsyncSupported(evp.getValue().stringifyValue());
- } else if ("loadOnStartup".equals(name)) {
- servletDef.setLoadOnStartup(evp.getValue().stringifyValue());
+ if(servletDef.getAsyncSupported() == null) {
+
servletDef.setAsyncSupported(evp.getValue().stringifyValue());
+ }
+ } else if ("loadOnStartup".equals(name)) {
+ if(servletDef.getLoadOnStartup() == null) {
+
servletDef.setLoadOnStartup(evp.getValue().stringifyValue());
+ }
} else if ("initParams".equals(name)) {
Map<String,String> initParams =
processAnnotationWebInitParams(evp.getValue());
- for (Map.Entry<String, String> entry : initParams.entrySet()) {
- servletDef.addInitParameter(entry.getKey(),
- entry.getValue());
+ if(isWebXMLservletDef) {
+ Map<String,String> webXMLInitParams =
servletDef.getParameterMap();
+ for (Map.Entry<String, String> entry :
initParams.entrySet()) {
+ if (webXMLInitParams.get(entry.getKey()) ==
null) {
+
servletDef.addInitParameter(entry.getKey(),
+ entry.getValue());
+ }
+ }
+ }
+ else {
+ for (Map.Entry<String, String> entry :
initParams
+ .entrySet()) {
+
servletDef.addInitParameter(entry.getKey(), entry
+ .getValue());
+ }
}
} else {
// Ignore
}
}
- if (urlPatterns != null) {
- fragment.addServlet(servletDef);
- for (String urlPattern : urlPatterns) {
- fragment.addServletMapping(urlPattern,
- servletDef.getServletName());
- }
- }
+ if(!isWebXMLservletDef && urlPatterns != null) {
+ fragment.addServlet(servletDef);
+ }
+ if(urlPatternsSet) {
+ if
(!fragment.getServletMappings().containsValue(servletName)) {
+ for (String urlPattern : urlPatterns) {
+ fragment.addServletMapping(urlPattern,
servletName);
+ }
+ }
+ }
+
}
protected void processAnnotationWebFilter(String className,
Added:
tomcat/trunk/test/org/apache/catalina/startup/DuplicateMappingParamServlet.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/DuplicateMappingParamServlet.java?rev=939114&view=auto
==============================================================================
---
tomcat/trunk/test/org/apache/catalina/startup/DuplicateMappingParamServlet.java
(added)
+++
tomcat/trunk/test/org/apache/catalina/startup/DuplicateMappingParamServlet.java
Wed Apr 28 22:16:49 2010
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.startup;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebInitParam;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+*
+* @author Peter Rossbach
+* @version $Revision$ $Date$
+*/
+...@webservlet(value = "/annotation/overwrite", urlPatterns = {"/param2"},
name= "param", initParams = {
+ @WebInitParam(name = "foo", value = "Hello"),
+ @WebInitParam(name = "bar", value = "World!") })
+public class DuplicateMappingParamServlet extends HttpServlet {
+
+ public void doGet(HttpServletRequest req, HttpServletResponse res)
+ throws IOException, ServletException
+ {
+ PrintWriter out = res.getWriter();
+ out.print("<p>" + getInitParameter("foo") + " " +
getInitParameter("bar") + "</p>");
+ }
+}
Propchange:
tomcat/trunk/test/org/apache/catalina/startup/DuplicateMappingParamServlet.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tomcat/trunk/test/org/apache/catalina/startup/DuplicateMappingParamServlet.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added: tomcat/trunk/test/org/apache/catalina/startup/NoMappingParamServlet.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/NoMappingParamServlet.java?rev=939114&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/startup/NoMappingParamServlet.java
(added)
+++ tomcat/trunk/test/org/apache/catalina/startup/NoMappingParamServlet.java
Wed Apr 28 22:16:49 2010
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.startup;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebInitParam;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+*
+* @author Peter Rossbach
+* @version $Revision$ $Date$
+*/
+...@webservlet(name= "param1", initParams = {
+ @WebInitParam(name = "foo", value = "Hello"),
+ @WebInitParam(name = "bar", value = "World!") })
+public class NoMappingParamServlet extends HttpServlet {
+
+ public void doGet(HttpServletRequest req, HttpServletResponse res)
+ throws IOException, ServletException
+ {
+ PrintWriter out = res.getWriter();
+ out.print("<p>" + getInitParameter("foo") + " " +
getInitParameter("bar") + "</p>");
+ }
+}
Propchange:
tomcat/trunk/test/org/apache/catalina/startup/NoMappingParamServlet.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tomcat/trunk/test/org/apache/catalina/startup/NoMappingParamServlet.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added: tomcat/trunk/test/org/apache/catalina/startup/ParamServlet.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/ParamServlet.java?rev=939114&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/startup/ParamServlet.java (added)
+++ tomcat/trunk/test/org/apache/catalina/startup/ParamServlet.java Wed Apr 28
22:16:49 2010
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.startup;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebInitParam;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ *
+ * @author Peter Rossbach
+ * @version $Revision$ $Date$
+ */
+...@webservlet(value = "/annotation/overwrite", name= "param", initParams = {
+ @WebInitParam(name = "foo", value = "Hello"),
+ @WebInitParam(name = "bar", value = "World!") },
+ displayName="param", description="param",
+ largeIcon="paramLarge.png",smallIcon="paramSmall.png",
+ loadOnStartup= 0, asyncSupported= false)
+public class ParamServlet extends HttpServlet {
+
+ public void doGet(HttpServletRequest req, HttpServletResponse res)
+ throws IOException, ServletException
+ {
+ PrintWriter out = res.getWriter();
+ out.print("<p>" + getInitParameter("foo") + " " +
getInitParameter("bar") + "</p>");
+ }
+}
Propchange: tomcat/trunk/test/org/apache/catalina/startup/ParamServlet.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: tomcat/trunk/test/org/apache/catalina/startup/ParamServlet.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added:
tomcat/trunk/test/org/apache/catalina/startup/TestContextConfigAnnotation.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TestContextConfigAnnotation.java?rev=939114&view=auto
==============================================================================
---
tomcat/trunk/test/org/apache/catalina/startup/TestContextConfigAnnotation.java
(added)
+++
tomcat/trunk/test/org/apache/catalina/startup/TestContextConfigAnnotation.java
Wed Apr 28 22:16:49 2010
@@ -0,0 +1,170 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.startup;
+
+import java.io.File;
+
+import junit.framework.TestCase;
+
+import org.apache.catalina.deploy.ServletDef;
+import org.apache.catalina.deploy.WebXml;
+
+/**
+* Check Servlet 3.0 Spec 8.2.3.3: Override annotation parameter from web.xml
or fragment.
+*
+* @author Peter Rossbach
+* @version $Revision$ $Date$
+*/
+public class TestContextConfigAnnotation extends TestCase {
+
+ public void testAnnotation() throws Exception {
+ WebXml webxml = new WebXml();
+ ContextConfig config = new ContextConfig();
+ File pFile =
paramServletClassResource("org/apache/catalina/startup/ParamServlet");
+ assertTrue(pFile.exists());
+ config.processAnnotationsFile(pFile, webxml);
+ ServletDef servletDef = webxml.getServlets().get("param");
+ assertNotNull(servletDef);
+ assertEquals("Hello", servletDef.getParameterMap().get("foo"));
+ assertEquals("World!", servletDef.getParameterMap().get("bar"));
+ assertEquals("param", webxml.getServletMappings().get(
+ "/annotation/overwrite"));
+
+ assertEquals("param", servletDef.getDescription());
+ assertEquals("param", servletDef.getDisplayName());
+ assertEquals("paramLarge.png", servletDef.getLargeIcon());
+ assertEquals("paramSmall.png", servletDef.getSmallIcon());
+ assertEquals(Boolean.FALSE, servletDef.getAsyncSupported());
+ assertEquals(new Integer(0), servletDef.getLoadOnStartup());
+ assertNull(servletDef.getEnabled());
+ assertNull(servletDef.getJspFile());
+
+ }
+
+ public void testOverwriteAnnotation() throws Exception {
+ WebXml webxml = new WebXml();
+ ServletDef servletDef = new ServletDef();
+ servletDef.setServletName("param");
+
servletDef.setServletClass("org.apache.catalina.startup.ParamServlet");
+ servletDef.addInitParameter("foo", "tomcat");
+ servletDef.setDescription("Description");
+ servletDef.setDisplayName("DisplayName");
+ servletDef.setLargeIcon("LargeIcon");
+ servletDef.setSmallIcon("SmallIcon");
+ servletDef.setAsyncSupported("true");
+ servletDef.setLoadOnStartup("1");
+
+ webxml.addServlet(servletDef);
+ webxml.addServletMapping("/param", "param");
+ ContextConfig config = new ContextConfig();
+ File pFile =
paramServletClassResource("org/apache/catalina/startup/ParamServlet");
+ assertTrue(pFile.exists());
+ config.processAnnotationsFile(pFile, webxml);
+
+ assertEquals(servletDef, webxml.getServlets().get("param"));
+
+ assertEquals("tomcat", servletDef.getParameterMap().get("foo"));
+ assertEquals("param",
webxml.getServletMappings().get("/param"));
+ // annotation mapping not added s. Servlet Spec 3.0 (Nov 2009)
8.2.3.3.vi page 81
+ assertNull(webxml.getServletMappings().get(
+ "/annotation/overwrite"));
+
+ assertEquals("Description", servletDef.getDescription());
+ assertEquals("DisplayName", servletDef.getDisplayName());
+ assertEquals("LargeIcon", servletDef.getLargeIcon());
+ assertEquals("SmallIcon", servletDef.getSmallIcon());
+ assertEquals(Boolean.TRUE, servletDef.getAsyncSupported());
+ assertEquals(new Integer(1), servletDef.getLoadOnStartup());
+ assertNull(servletDef.getEnabled());
+ assertNull(servletDef.getJspFile());
+ }
+
+ public void testNoMapping() throws Exception {
+ WebXml webxml = new WebXml();
+ ContextConfig config = new ContextConfig();
+ File pFile =
paramServletClassResource("org/apache/catalina/startup/NoMappingParamServlet");
+ assertTrue(pFile.exists());
+ config.processAnnotationsFile(pFile, webxml);
+ ServletDef servletDef = webxml.getServlets().get("param1");
+ assertNull(servletDef);
+
+ webxml.addServletMapping("/param", "param1");
+ config.processAnnotationsFile(pFile, webxml);
+ servletDef = webxml.getServlets().get("param1");
+ assertNull(servletDef);
+
+ }
+
+ public void testSetupWebXMLNoMapping() throws Exception {
+ WebXml webxml = new WebXml();
+ ServletDef servletDef = new ServletDef();
+ servletDef.setServletName("param1");
+ servletDef
+
.setServletClass("org.apache.catalina.startup.NoMappingParamServlet");
+ servletDef.addInitParameter("foo", "tomcat");
+
+ webxml.addServlet(servletDef);
+ webxml.addServletMapping("/param", "param1");
+ ContextConfig config = new ContextConfig();
+ File pFile =
paramServletClassResource("org/apache/catalina/startup/NoMappingParamServlet");
+ assertTrue(pFile.exists());
+ config.processAnnotationsFile(pFile, webxml);
+ assertEquals("tomcat", servletDef.getParameterMap().get("foo"));
+ assertEquals("World!", servletDef.getParameterMap().get("bar"));
+ ServletDef servletDef1 = webxml.getServlets().get("param1");
+ assertNotNull(servletDef1);
+ assertEquals(servletDef, servletDef1);
+ }
+
+ public void testDuplicateMapping() throws Exception {
+ WebXml webxml = new WebXml();
+ ContextConfig config = new ContextConfig();
+ File pFile =
paramServletClassResource("org/apache/catalina/startup/DuplicateMappingParamServlet");
+ assertTrue(pFile.exists());
+ try {
+ config.processAnnotationsFile(pFile, webxml);
+ fail();
+ } catch (IllegalArgumentException ex) {
+ // ingore
+ }
+ ServletDef servletDef = webxml.getServlets().get("param");
+ assertNull(servletDef);
+ }
+
+ /**
+ * Find newest class resource at eclipse and ant standard class output
dirs!
+ * @param className
+ * @return File Resource
+ */
+ private File paramServletClassResource(String className) {
+ File antFile = new File("output/testclasses/" + className +
".class");
+ File eclipseFile = new File(".settings/output/" + className +
".class");
+ if (antFile.exists()) {
+ if (eclipseFile.exists()) {
+ if (antFile.lastModified() >=
eclipseFile.lastModified()) {
+ return antFile;
+ } else {
+ return eclipseFile;
+ }
+ } else {
+ return antFile;
+ }
+ } else {
+ return eclipseFile;
+ }
+ }
+}
Propchange:
tomcat/trunk/test/org/apache/catalina/startup/TestContextConfigAnnotation.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tomcat/trunk/test/org/apache/catalina/startup/TestContextConfigAnnotation.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]