1 /*
   2  * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.naming.spi;
  27 
  28 import javax.naming.Name;
  29 import javax.naming.Context;
  30 import javax.naming.CompositeName;
  31 import javax.naming.InvalidNameException;
  32 
  33 /**
  34   * This class represents the result of resolution of a name.
  35   * It contains the object to which name was resolved, and the portion
  36   * of the name that has not been resolved.
  37   *<p>
  38   * A ResolveResult instance is not synchronized against concurrent
  39   * multithreaded access. Multiple threads trying to access and modify
  40   * a single ResolveResult instance should lock the object.
  41   *
  42   * @author Rosanna Lee
  43   * @author Scott Seligman
  44   * @since 1.3
  45   */
  46 public class ResolveResult implements java.io.Serializable {
  47     /**
  48      * Field containing the Object that was resolved to successfully.
  49      * It can be null only when constructed using a subclass.
  50      * Constructors should always initialize this.
  51      * @serial
  52      */
  53     @SuppressWarnings("serial") // Not statically typed as Serializable
  54     protected Object resolvedObj;
  55     /**
  56      * Field containing the remaining name yet to be resolved.
  57      * It can be null only when constructed using a subclass.
  58      * Constructors should always initialize this.
  59      * @serial
  60      */
  61     protected Name remainingName;
  62 
  63     /**
  64       * Constructs an instance of ResolveResult with the
  65       * resolved object and remaining name both initialized to null.
  66       */
  67     protected ResolveResult() {
  68         resolvedObj = null;
  69         remainingName = null;
  70     }
  71 
  72     /**
  73       * Constructs a new instance of ResolveResult consisting of
  74       * the resolved object and the remaining unresolved component.
  75       *
  76       * @param robj The non-null object resolved to.
  77       * @param rcomp The single remaining name component that has yet to be
  78       *                 resolved. Cannot be null (but can be empty).
  79       */
  80     public ResolveResult(Object robj, String rcomp) {
  81         resolvedObj = robj;
  82         try {
  83         remainingName = new CompositeName(rcomp);
  84 //          remainingName.appendComponent(rcomp);
  85         } catch (InvalidNameException e) {
  86             // ignore; shouldn't happen
  87         }
  88     }
  89 
  90     /**
  91       * Constructs a new instance of ResolveResult consisting of
  92       * the resolved Object and the remaining name.
  93       *
  94       * @param robj The non-null Object resolved to.
  95       * @param rname The non-null remaining name that has yet to be resolved.
  96       */
  97     public ResolveResult(Object robj, Name rname) {
  98         resolvedObj = robj;
  99         setRemainingName(rname);
 100     }
 101 
 102     /**
 103      * Retrieves the remaining unresolved portion of the name.
 104      *
 105      * @return The remaining unresolved portion of the name.
 106      *          Cannot be null but empty OK.
 107      * @see #appendRemainingName
 108      * @see #appendRemainingComponent
 109      * @see #setRemainingName
 110      */
 111     public Name getRemainingName() {
 112         return this.remainingName;
 113     }
 114 
 115     /**
 116      * Retrieves the Object to which resolution was successful.
 117      *
 118      * @return The Object to which resolution was successful. Cannot be null.
 119       * @see #setResolvedObj
 120      */
 121     public Object getResolvedObj() {
 122         return this.resolvedObj;
 123     }
 124 
 125     /**
 126       * Sets the remaining name field of this result to name.
 127       * A copy of name is made so that modifying the copy within
 128       * this ResolveResult does not affect <code>name</code> and
 129       * vice versa.
 130       *
 131       * @param name The name to set remaining name to. Cannot be null.
 132       * @see #getRemainingName
 133       * @see #appendRemainingName
 134       * @see #appendRemainingComponent
 135       */
 136     public void setRemainingName(Name name) {
 137         if (name != null)
 138             this.remainingName = (Name)(name.clone());
 139         else {
 140             // ??? should throw illegal argument exception
 141             this.remainingName = null;
 142         }
 143     }
 144 
 145     /**
 146       * Adds components to the end of remaining name.
 147       *
 148       * @param name The components to add. Can be null.
 149       * @see #getRemainingName
 150       * @see #setRemainingName
 151       * @see #appendRemainingComponent
 152       */
 153     public void appendRemainingName(Name name) {
 154 //      System.out.println("appendingRemainingName: " + name.toString());
 155 //      Exception e = new Exception();
 156 //      e.printStackTrace();
 157         if (name != null) {
 158             if (this.remainingName != null) {
 159                 try {
 160                     this.remainingName.addAll(name);
 161                 } catch (InvalidNameException e) {
 162                     // ignore; shouldn't happen for composite name
 163                 }
 164             } else {
 165                 this.remainingName = (Name)(name.clone());
 166             }
 167         }
 168     }
 169 
 170     /**
 171       * Adds a single component to the end of remaining name.
 172       *
 173       * @param name The component to add. Can be null.
 174       * @see #getRemainingName
 175       * @see #appendRemainingName
 176       */
 177     public void appendRemainingComponent(String name) {
 178         if (name != null) {
 179             CompositeName rname = new CompositeName();
 180             try {
 181                 rname.add(name);
 182             } catch (InvalidNameException e) {
 183                 // ignore; shouldn't happen for empty composite name
 184             }
 185             appendRemainingName(rname);
 186         }
 187     }
 188 
 189     /**
 190       * Sets the resolved Object field of this result to obj.
 191       *
 192       * @param obj The object to use for setting the resolved obj field.
 193       *            Cannot be null.
 194       * @see #getResolvedObj
 195       */
 196     public void setResolvedObj(Object obj) {
 197         this.resolvedObj = obj;
 198         // ??? should check for null?
 199     }
 200 
 201     private static final long serialVersionUID = -4552108072002407559L;
 202 }