Return-Path: X-Original-To: apmail-tomcat-dev-archive@www.apache.org Delivered-To: apmail-tomcat-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 13157DF57 for ; Fri, 9 Nov 2012 19:31:33 +0000 (UTC) Received: (qmail 81876 invoked by uid 500); 9 Nov 2012 19:31:32 -0000 Delivered-To: apmail-tomcat-dev-archive@tomcat.apache.org Received: (qmail 81799 invoked by uid 500); 9 Nov 2012 19:31:32 -0000 Mailing-List: contact dev-help@tomcat.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Tomcat Developers List" Delivered-To: mailing list dev@tomcat.apache.org Received: (qmail 81789 invoked by uid 99); 9 Nov 2012 19:31:32 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 09 Nov 2012 19:31:32 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 09 Nov 2012 19:31:29 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id B46192388980 for ; Fri, 9 Nov 2012 19:31:07 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1407595 - in /tomcat/trunk: java/org/apache/catalina/core/NamingContextListener.java test/org/apache/catalina/core/TestNamingContextListener.java Date: Fri, 09 Nov 2012 19:31:07 -0000 To: dev@tomcat.apache.org From: markt@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121109193107.B46192388980@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: markt Date: Fri Nov 9 19:31:07 2012 New Revision: 1407595 URL: http://svn.apache.org/viewvc?rev=1407595&view=rev Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=54096 env-entry can use any type that has a String or char constructor Modified: tomcat/trunk/java/org/apache/catalina/core/NamingContextListener.java tomcat/trunk/test/org/apache/catalina/core/TestNamingContextListener.java Modified: tomcat/trunk/java/org/apache/catalina/core/NamingContextListener.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/NamingContextListener.java?rev=1407595&r1=1407594&r2=1407595&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/NamingContextListener.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/NamingContextListener.java Fri Nov 9 19:31:07 2012 @@ -21,6 +21,7 @@ package org.apache.catalina.core; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; +import java.lang.reflect.Constructor; import java.net.MalformedURLException; import java.net.URL; import java.util.Collection; @@ -842,7 +843,11 @@ public class NamingContextListener } } } else { - logger.error(sm.getString("naming.invalidEnvEntryType", env.getName())); + value = constructEnvEntry(env.getType(), env.getValue()); + if (value == null) { + logger.error(sm.getString( + "naming.invalidEnvEntryType", env.getName())); + } } } catch (NumberFormatException e) { logger.error(sm.getString("naming.invalidEnvEntryValue", env.getName())); @@ -865,6 +870,33 @@ public class NamingContextListener } + private Object constructEnvEntry(String type, String value) { + try { + Class clazz = Class.forName(type); + Constructor c = null; + try { + c = clazz.getConstructor(String.class); + return c.newInstance(value); + } catch (NoSuchMethodException e) { + // Ignore + } + + if (value.length() != 1) { + return null; + } + + try { + c = clazz.getConstructor(char.class); + return c.newInstance(Character.valueOf(value.charAt(0))); + } catch (NoSuchMethodException e) { + // Ignore + } + } catch (Exception e) { + // Ignore + } + return null; + } + /** * Set the specified local EJBs in the naming context. */ Modified: tomcat/trunk/test/org/apache/catalina/core/TestNamingContextListener.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestNamingContextListener.java?rev=1407595&r1=1407594&r2=1407595&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/core/TestNamingContextListener.java (original) +++ tomcat/trunk/test/org/apache/catalina/core/TestNamingContextListener.java Fri Nov 9 19:31:07 2012 @@ -33,8 +33,13 @@ import org.apache.catalina.startup.Tomca public class TestNamingContextListener extends TomcatBaseTest { - private static final String JNDI_NAME = "TestName"; - private static final String JNDI_VALUE= "Test Value"; + private static final String BUG49132_NAME = "TestName"; + private static final String BUG49132_VALUE = "Test Value"; + + private static final String BUG54096_NameA = "envA"; + private static final String BUG54096_ValueA = "valueA"; + private static final String BUG54096_NameB = "envB"; + private static final String BUG54096_ValueB = "B"; /** * Test JNDI is available to ServletContextListeners. @@ -51,9 +56,9 @@ public class TestNamingContextListener e tomcat.enableNaming(); ContextEnvironment environment = new ContextEnvironment(); - environment.setType(JNDI_VALUE.getClass().getName()); - environment.setName(JNDI_NAME); - environment.setValue(JNDI_VALUE); + environment.setType(BUG49132_VALUE.getClass().getName()); + environment.setName(BUG49132_NAME); + environment.setValue(BUG49132_VALUE); ctx.getNamingResources().addEnvironment(environment); ctx.addApplicationListener(Bug49132Listener.class.getName()); @@ -77,8 +82,8 @@ public class TestNamingContextListener e initCtx = new InitialContext(); javax.naming.Context envCtx = (javax.naming.Context) initCtx.lookup("java:comp/env"); - String value = (String) envCtx.lookup(JNDI_NAME); - if (!JNDI_VALUE.equals(value)) { + String value = (String) envCtx.lookup(BUG49132_NAME); + if (!BUG49132_VALUE.equals(value)) { throw new RuntimeException(); } } catch (NamingException e) { @@ -87,4 +92,95 @@ public class TestNamingContextListener e } } + @Test + public void testBug54096() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + // Must have a real docBase - just use temp + org.apache.catalina.Context ctx = + tomcat.addContext("", System.getProperty("java.io.tmpdir")); + + // Enable JNDI - it is disabled by default + tomcat.enableNaming(); + + ContextEnvironment environmentA = new ContextEnvironment(); + environmentA.setType(Bug54096EnvA.class.getName()); + environmentA.setName(BUG54096_NameA); + environmentA.setValue(BUG54096_ValueA); + ctx.getNamingResources().addEnvironment(environmentA); + + ContextEnvironment environmentB = new ContextEnvironment(); + environmentB.setType(Bug54096EnvB.class.getName()); + environmentB.setName(BUG54096_NameB); + environmentB.setValue(BUG54096_ValueB); + ctx.getNamingResources().addEnvironment(environmentB); + + ctx.addApplicationListener(Bug54096Listener.class.getName()); + + tomcat.start(); + + assertEquals(LifecycleState.STARTED, ctx.getState()); + } + + public static class Bug54096EnvA { + + private final String value; + + public Bug54096EnvA(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + } + + public static class Bug54096EnvB { + + private final char value; + + public Bug54096EnvB(char value) { + this.value = value; + } + + public char getValue() { + return value; + } + } + + public static final class Bug54096Listener implements + ServletContextListener { + + @Override + public void contextDestroyed(ServletContextEvent sce) { + // NOOP + } + + @Override + public void contextInitialized(ServletContextEvent sce) { + javax.naming.Context initCtx; + try { + initCtx = new InitialContext(); + javax.naming.Context envCtx = + (javax.naming.Context) initCtx.lookup("java:comp/env"); + + // Validate entry A + Bug54096EnvA valueA = + (Bug54096EnvA) envCtx.lookup(BUG54096_NameA); + if (!BUG54096_ValueA.equals(valueA.getValue())) { + throw new RuntimeException(); + } + + // Validate entry B + Bug54096EnvB valueB = + (Bug54096EnvB) envCtx.lookup(BUG54096_NameB); + if (BUG54096_ValueB.charAt(0) != valueB.getValue()) { + throw new RuntimeException(); + } + + } catch (NamingException e) { + throw new RuntimeException(e); + } + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org For additional commands, e-mail: dev-help@tomcat.apache.org