Path: blob/master/src/java.naming/share/classes/javax/naming/spi/ContinuationDirContext.java
41159 views
/*1* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.naming.spi;2627import java.util.Hashtable;2829import javax.naming.Name;30import javax.naming.NamingEnumeration;31import javax.naming.CompositeName;32import javax.naming.NamingException;33import javax.naming.CannotProceedException;34import javax.naming.OperationNotSupportedException;35import javax.naming.Context;3637import javax.naming.directory.DirContext;38import javax.naming.directory.Attributes;39import javax.naming.directory.SearchControls;40import javax.naming.directory.SearchResult;41import javax.naming.directory.ModificationItem;4243/**44* This class is the continuation context for invoking DirContext methods.45*46* @author Rosanna Lee47* @author Scott Seligman48* @since 1.349*/5051class ContinuationDirContext extends ContinuationContext implements DirContext {5253ContinuationDirContext(CannotProceedException cpe, Hashtable<?,?> env) {54super(cpe, env);55}5657protected DirContextNamePair getTargetContext(Name name)58throws NamingException {5960if (cpe.getResolvedObj() == null)61throw (NamingException)cpe.fillInStackTrace();6263Context ctx = NamingManager.getContext(cpe.getResolvedObj(),64cpe.getAltName(),65cpe.getAltNameCtx(),66env);67if (ctx == null)68throw (NamingException)cpe.fillInStackTrace();6970if (ctx instanceof DirContext)71return new DirContextNamePair((DirContext)ctx, name);7273if (ctx instanceof Resolver) {74Resolver res = (Resolver)ctx;75ResolveResult rr = res.resolveToClass(name, DirContext.class);7677// Reached a DirContext; return result.78DirContext dctx = (DirContext)rr.getResolvedObj();79return (new DirContextNamePair(dctx, rr.getRemainingName()));80}8182// Resolve all the way using lookup(). This may allow the operation83// to succeed if it doesn't require the penultimate context.84Object ultimate = ctx.lookup(name);85if (ultimate instanceof DirContext) {86return (new DirContextNamePair((DirContext)ultimate,87new CompositeName()));88}8990throw (NamingException)cpe.fillInStackTrace();91}9293protected DirContextStringPair getTargetContext(String name)94throws NamingException {9596if (cpe.getResolvedObj() == null)97throw (NamingException)cpe.fillInStackTrace();9899Context ctx = NamingManager.getContext(cpe.getResolvedObj(),100cpe.getAltName(),101cpe.getAltNameCtx(),102env);103104if (ctx instanceof DirContext)105return new DirContextStringPair((DirContext)ctx, name);106107if (ctx instanceof Resolver) {108Resolver res = (Resolver)ctx;109ResolveResult rr = res.resolveToClass(name, DirContext.class);110111// Reached a DirContext; return result.112DirContext dctx = (DirContext)rr.getResolvedObj();113Name tmp = rr.getRemainingName();114String remains = (tmp != null) ? tmp.toString() : "";115return (new DirContextStringPair(dctx, remains));116}117118// Resolve all the way using lookup(). This may allow the operation119// to succeed if it doesn't require the penultimate context.120Object ultimate = ctx.lookup(name);121if (ultimate instanceof DirContext) {122return (new DirContextStringPair((DirContext)ultimate, ""));123}124125throw (NamingException)cpe.fillInStackTrace();126}127128public Attributes getAttributes(String name) throws NamingException {129DirContextStringPair res = getTargetContext(name);130return res.getDirContext().getAttributes(res.getString());131}132133public Attributes getAttributes(String name, String[] attrIds)134throws NamingException {135DirContextStringPair res = getTargetContext(name);136return res.getDirContext().getAttributes(res.getString(), attrIds);137}138139public Attributes getAttributes(Name name) throws NamingException {140DirContextNamePair res = getTargetContext(name);141return res.getDirContext().getAttributes(res.getName());142}143144public Attributes getAttributes(Name name, String[] attrIds)145throws NamingException {146DirContextNamePair res = getTargetContext(name);147return res.getDirContext().getAttributes(res.getName(), attrIds);148}149150public void modifyAttributes(Name name, int mod_op, Attributes attrs)151throws NamingException {152DirContextNamePair res = getTargetContext(name);153res.getDirContext().modifyAttributes(res.getName(), mod_op, attrs);154}155public void modifyAttributes(String name, int mod_op, Attributes attrs)156throws NamingException {157DirContextStringPair res = getTargetContext(name);158res.getDirContext().modifyAttributes(res.getString(), mod_op, attrs);159}160161public void modifyAttributes(Name name, ModificationItem[] mods)162throws NamingException {163DirContextNamePair res = getTargetContext(name);164res.getDirContext().modifyAttributes(res.getName(), mods);165}166public void modifyAttributes(String name, ModificationItem[] mods)167throws NamingException {168DirContextStringPair res = getTargetContext(name);169res.getDirContext().modifyAttributes(res.getString(), mods);170}171172public void bind(Name name, Object obj, Attributes attrs)173throws NamingException {174DirContextNamePair res = getTargetContext(name);175res.getDirContext().bind(res.getName(), obj, attrs);176}177public void bind(String name, Object obj, Attributes attrs)178throws NamingException {179DirContextStringPair res = getTargetContext(name);180res.getDirContext().bind(res.getString(), obj, attrs);181}182183public void rebind(Name name, Object obj, Attributes attrs)184throws NamingException {185DirContextNamePair res = getTargetContext(name);186res.getDirContext().rebind(res.getName(), obj, attrs);187}188public void rebind(String name, Object obj, Attributes attrs)189throws NamingException {190DirContextStringPair res = getTargetContext(name);191res.getDirContext().rebind(res.getString(), obj, attrs);192}193194public DirContext createSubcontext(Name name, Attributes attrs)195throws NamingException {196DirContextNamePair res = getTargetContext(name);197return res.getDirContext().createSubcontext(res.getName(), attrs);198}199200public DirContext createSubcontext(String name, Attributes attrs)201throws NamingException {202DirContextStringPair res = getTargetContext(name);203return204res.getDirContext().createSubcontext(res.getString(), attrs);205}206207public NamingEnumeration<SearchResult> search(Name name,208Attributes matchingAttributes,209String[] attributesToReturn)210throws NamingException {211DirContextNamePair res = getTargetContext(name);212return res.getDirContext().search(res.getName(), matchingAttributes,213attributesToReturn);214}215216public NamingEnumeration<SearchResult> search(String name,217Attributes matchingAttributes,218String[] attributesToReturn)219throws NamingException {220DirContextStringPair res = getTargetContext(name);221return res.getDirContext().search(res.getString(),222matchingAttributes,223attributesToReturn);224}225226public NamingEnumeration<SearchResult> search(Name name,227Attributes matchingAttributes)228throws NamingException {229DirContextNamePair res = getTargetContext(name);230return res.getDirContext().search(res.getName(), matchingAttributes);231}232public NamingEnumeration<SearchResult> search(String name,233Attributes matchingAttributes)234throws NamingException {235DirContextStringPair res = getTargetContext(name);236return res.getDirContext().search(res.getString(),237matchingAttributes);238}239240public NamingEnumeration<SearchResult> search(Name name,241String filter,242SearchControls cons)243throws NamingException {244DirContextNamePair res = getTargetContext(name);245return res.getDirContext().search(res.getName(), filter, cons);246}247248public NamingEnumeration<SearchResult> search(String name,249String filter,250SearchControls cons)251throws NamingException {252DirContextStringPair res = getTargetContext(name);253return res.getDirContext().search(res.getString(), filter, cons);254}255256public NamingEnumeration<SearchResult> search(Name name,257String filterExpr,258Object[] args,259SearchControls cons)260throws NamingException {261DirContextNamePair res = getTargetContext(name);262return res.getDirContext().search(res.getName(), filterExpr, args,263cons);264}265266public NamingEnumeration<SearchResult> search(String name,267String filterExpr,268Object[] args,269SearchControls cons)270throws NamingException {271DirContextStringPair res = getTargetContext(name);272return res.getDirContext().search(res.getString(), filterExpr, args,273cons);274}275276public DirContext getSchema(String name) throws NamingException {277DirContextStringPair res = getTargetContext(name);278return res.getDirContext().getSchema(res.getString());279}280281public DirContext getSchema(Name name) throws NamingException {282DirContextNamePair res = getTargetContext(name);283return res.getDirContext().getSchema(res.getName());284}285286public DirContext getSchemaClassDefinition(String name)287throws NamingException {288DirContextStringPair res = getTargetContext(name);289return res.getDirContext().getSchemaClassDefinition(res.getString());290}291292public DirContext getSchemaClassDefinition(Name name)293throws NamingException {294DirContextNamePair res = getTargetContext(name);295return res.getDirContext().getSchemaClassDefinition(res.getName());296}297}298299class DirContextNamePair {300DirContext ctx;301Name name;302303DirContextNamePair(DirContext ctx, Name name) {304this.ctx = ctx;305this.name = name;306}307308DirContext getDirContext() {309return ctx;310}311312Name getName() {313return name;314}315}316317class DirContextStringPair {318DirContext ctx;319String str;320321DirContextStringPair(DirContext ctx, String str) {322this.ctx = ctx;323this.str = str;324}325326DirContext getDirContext() {327return ctx;328}329330String getString() {331return str;332}333}334335336