View Javadoc
1   /*
2    * Copyright (c) 1999, 2004, 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      protected Object resolvedObj;
54      /**
55       * Field containing the remaining name yet to be resolved.
56       * It can be null only when constructed using a subclass.
57       * Constructors should always initialize this.
58       * @serial
59       */
60      protected Name remainingName;
61  
62      /**
63        * Constructs an instance of ResolveResult with the
64        * resolved object and remaining name both initialized to null.
65        */
66      protected ResolveResult() {
67          resolvedObj = null;
68          remainingName = null;
69      }
70  
71      /**
72        * Constructs a new instance of ResolveResult consisting of
73        * the resolved object and the remaining unresolved component.
74        *
75        * @param robj The non-null object resolved to.
76        * @param rcomp The single remaining name component that has yet to be
77        *                 resolved. Cannot be null (but can be empty).
78        */
79      public ResolveResult(Object robj, String rcomp) {
80          resolvedObj = robj;
81          try {
82          remainingName = new CompositeName(rcomp);
83  //          remainingName.appendComponent(rcomp);
84          } catch (InvalidNameException e) {
85              // ignore; shouldn't happen
86          }
87      }
88  
89      /**
90        * Constructs a new instance of ResolveResult consisting of
91        * the resolved Object and the remaining name.
92        *
93        * @param robj The non-null Object resolved to.
94        * @param rname The non-null remaining name that has yet to be resolved.
95        */
96      public ResolveResult(Object robj, Name rname) {
97          resolvedObj = robj;
98          setRemainingName(rname);
99      }
100 
101     /**
102      * Retrieves the remaining unresolved portion of the name.
103      *
104      * @return The remaining unresolved portion of the name.
105      *          Cannot be null but empty OK.
106      * @see #appendRemainingName
107      * @see #appendRemainingComponent
108      * @see #setRemainingName
109      */
110     public Name getRemainingName() {
111         return this.remainingName;
112     }
113 
114     /**
115      * Retrieves the Object to which resolution was successful.
116      *
117      * @return The Object to which resolution was successful. Cannot be null.
118       * @see #setResolvedObj
119      */
120     public Object getResolvedObj() {
121         return this.resolvedObj;
122     }
123 
124     /**
125       * Sets the remaining name field of this result to name.
126       * A copy of name is made so that modifying the copy within
127       * this ResolveResult does not affect <code>name</code> and
128       * vice versa.
129       *
130       * @param name The name to set remaining name to. Cannot be null.
131       * @see #getRemainingName
132       * @see #appendRemainingName
133       * @see #appendRemainingComponent
134       */
135     public void setRemainingName(Name name) {
136         if (name != null)
137             this.remainingName = (Name)(name.clone());
138         else {
139             // ??? should throw illegal argument exception
140             this.remainingName = null;
141         }
142     }
143 
144     /**
145       * Adds components to the end of remaining name.
146       *
147       * @param name The components to add. Can be null.
148       * @see #getRemainingName
149       * @see #setRemainingName
150       * @see #appendRemainingComponent
151       */
152     public void appendRemainingName(Name name) {
153 //      System.out.println("appendingRemainingName: " + name.toString());
154 //      Exception e = new Exception();
155 //      e.printStackTrace();
156         if (name != null) {
157             if (this.remainingName != null) {
158                 try {
159                     this.remainingName.addAll(name);
160                 } catch (InvalidNameException e) {
161                     // ignore; shouldn't happen for composite name
162                 }
163             } else {
164                 this.remainingName = (Name)(name.clone());
165             }
166         }
167     }
168 
169     /**
170       * Adds a single component to the end of remaining name.
171       *
172       * @param name The component to add. Can be null.
173       * @see #getRemainingName
174       * @see #appendRemainingName
175       */
176     public void appendRemainingComponent(String name) {
177         if (name != null) {
178             CompositeName rname = new CompositeName();
179             try {
180                 rname.add(name);
181             } catch (InvalidNameException e) {
182                 // ignore; shouldn't happen for empty composite name
183             }
184             appendRemainingName(rname);
185         }
186     }
187 
188     /**
189       * Sets the resolved Object field of this result to obj.
190       *
191       * @param obj The object to use for setting the resolved obj field.
192       *            Cannot be null.
193       * @see #getResolvedObj
194       */
195     public void setResolvedObj(Object obj) {
196         this.resolvedObj = obj;
197         // ??? should check for null?
198     }
199 
200     private static final long serialVersionUID = -4552108072002407559L;
201 }