From 64c220e63dd04d5c781822a5de79dea1ad5caa56 Mon Sep 17 00:00:00 2001 From: markt Date: Mon, 1 Mar 2010 23:14:12 +0000 Subject: [PATCH] Correct fix for https://issues.apache.org/bugzilla/show_bug.cgi?id=48050 NamingContext.createSubcontext method returns Context with wrong name git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@917784 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/naming/LocalStrings.properties | 1 + java/org/apache/naming/NamingContext.java | 32 +++++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/java/org/apache/naming/LocalStrings.properties b/java/org/apache/naming/LocalStrings.properties index 87313757c..f5c229f6c 100644 --- a/java/org/apache/naming/LocalStrings.properties +++ b/java/org/apache/naming/LocalStrings.properties @@ -19,6 +19,7 @@ contextBindings.noContextBoundToCL=No naming context bound to this class loader selectorContext.noJavaUrl=This context must be accessed through a java: URL selectorContext.methodUsingName=Call to method ''{0}'' with a Name of ''{1}'' selectorContext.methodUsingString=Call to method ''{0}'' with a String of ''{1}'' +namingContext.createSubContextInvalid=Unable to create context with name [{0}] as a sub-context of the context named [{1}] namingContext.contextExpected=Name is not bound to a Context namingContext.failResolvingReference=Unexpected exception resolving reference namingContext.nameNotBound=Name {0} is not bound in this Context diff --git a/java/org/apache/naming/NamingContext.java b/java/org/apache/naming/NamingContext.java index eabd744d8..e3edb6606 100644 --- a/java/org/apache/naming/NamingContext.java +++ b/java/org/apache/naming/NamingContext.java @@ -505,11 +505,14 @@ public class NamingContext implements Context { /** * Creates and binds a new context. Creates a new context with the given - * name and binds it in the target context (that named by all but - * terminal atomic component of the name). All intermediate contexts and - * the target context must already exist. + * name and binds it in this context. + * + * @param name The name of the context to create; may not be empty. If the + * name consists of a single component then the sub-context is + * created with a prefix of the name of this context. If the + * name has multiple components then name.prefix(name.size()-1) + * must be the name of this context. * - * @param name the name of the context to create; may not be empty * @return the newly created context * @exception NameAlreadyBoundException if name is already bound * @exception InvalidAttributesException if creation of the subcontext @@ -520,8 +523,25 @@ public class NamingContext implements Context { throws NamingException { checkWritable(); - Context newContext = new NamingContext(env, name.get(name.size() - 1)); - bind(name, newContext); + String contextName = null; + + if (name.size() == 1) { + if (this.name.endsWith("/")) { + contextName = this.name + name.get(0); + } else { + contextName = this.name + "/" + name.get(0); + } + } else { + if (!name.getPrefix(name.size()-1).toString().equals(name)) { + throw new NamingException( + sm.getString("namingContext.createSubContextInvalid", + name, this.name)); + } + contextName = name.toString(); + } + + Context newContext = new NamingContext(env, contextName); + bind(name.getSuffix(name.size() -1), newContext); return newContext; } -- 2.11.0