Source Code Cross Referenced for LdapName.java in  » 6.0-JDK-Core » naming » javax » naming » ldap » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » naming » javax.naming.ldap 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package javax.naming.ldap;
027
028        import javax.naming.Name;
029        import javax.naming.InvalidNameException;
030
031        import java.util.Enumeration;
032        import java.util.Collection;
033        import java.util.ArrayList;
034        import java.util.List;
035        import java.util.Iterator;
036        import java.util.ListIterator;
037        import java.util.Collections;
038
039        import java.io.ObjectOutputStream;
040        import java.io.ObjectInputStream;
041        import java.io.IOException;
042
043        /**
044         * This class represents a distinguished name as specified by
045         * <a href="http://ietf.org//rfc/rfc2253.txt">RFC 2253</a>.
046         * A distinguished name, or DN, is composed of an ordered list of
047         * components called <em>relative distinguished name</em>s, or RDNs.
048         * Details of a DN's syntax are described in RFC 2253.
049         *<p>
050         * This class resolves a few ambiguities found in RFC 2253
051         * as follows:
052         * <ul>
053         * <li> RFC 2253 leaves the term "whitespace" undefined. The
054         *      ASCII space character 0x20 (" ") is used in its place.
055         * <li> Whitespace is allowed on either side of ',', ';', '=', and '+'.
056         *      Such whitespace is accepted but not generated by this code,
057         *      and is ignored when comparing names.
058         * <li> AttributeValue strings containing '=' or non-leading '#'
059         *      characters (unescaped) are accepted.
060         * </ul>
061         *<p>
062         * String names passed to <code>LdapName</code> or returned by it
063         * use the full Unicode character set. They may also contain
064         * characters encoded into UTF-8 with each octet represented by a
065         * three-character substring such as "\\B4".
066         * They may not, however, contain characters encoded into UTF-8 with
067         * each octet represented by a single character in the string:  the
068         * meaning would be ambiguous.
069         *<p>
070         * <code>LdapName</code> will properly parse all valid names, but
071         * does not attempt to detect all possible violations when parsing
072         * invalid names.  It is "generous" in accepting invalid names.
073         * The "validity" of a name is determined ultimately when it
074         * is supplied to an LDAP server, which may accept or
075         * reject the name based on factors such as its schema information
076         * and interoperability considerations.
077         *<p>
078         * When names are tested for equality, attribute types, both binary
079         * and string values, are case-insensitive.
080         * String values with different but equivalent usage of quoting,
081         * escaping, or UTF8-hex-encoding are considered equal.  The order of
082         * components in multi-valued RDNs (such as "ou=Sales+cn=Bob") is not
083         * significant.
084         * <p>
085         * The components of a LDAP name, that is, RDNs, are numbered. The
086         * indexes of a LDAP name with n RDNs range from 0 to n-1.
087         * This range may be written as [0,n).
088         * The right most RDN is at index 0, and the left most RDN is at
089         * index n-1. For example, the distinguished name:
090         * "CN=Steve Kille, O=Isode Limited, C=GB" is numbered in the following
091         * sequence ranging from 0 to 2: {C=GB, O=Isode Limited, CN=Steve Kille}. An
092         * empty LDAP name is represented by an empty RDN list.
093         *<p>
094         * Concurrent multithreaded read-only access of an instance of
095         * <tt>LdapName</tt> need not be synchronized.
096         *<p>
097         * Unless otherwise noted, the behavior of passing a null argument
098         * to a constructor or method in this class will cause a
099         * NullPointerException to be thrown.
100         *
101         * @author Scott Seligman
102         * @version 1.14 07/05/05
103         * @since 1.5
104         */
105
106        public class LdapName implements  Name {
107
108            // private transient ArrayList<Rdn> rdns;	// parsed name components
109
110            private transient ArrayList rdns; // parsed name components
111            private transient String unparsed; // if non-null, the DN in unparsed form
112            private static final long serialVersionUID = -1595520034788997356L;
113
114            /**
115             * Constructs an LDAP name from the given distinguished name.
116             *
117             * @param name  This is a non-null distinguished name formatted
118             * according to the rules defined in
119             * <a href="http://ietf.org/rfc/rfc2253.txt">RFC 2253</a>.
120             *
121             * @throws InvalidNameException if a syntax violation is detected.
122             * @see Rdn#escapeValue(Object value)
123             */
124            public LdapName(String name) throws InvalidNameException {
125                unparsed = name;
126                parse();
127            }
128
129            /**
130             * Constructs an LDAP name given its parsed RDN components.
131             * <p>
132             * The indexing of RDNs in the list follows the numbering of
133             * RDNs described in the class description.
134             *
135             * @param rdns The non-null list of <tt>Rdn</tt>s forming this LDAP name.
136             */
137            public LdapName(List<Rdn> rdns) {
138
139                // if (rdns instanceof ArrayList<Rdn>) {
140                //	this.rdns = rdns.clone();
141                // } else if (rdns instanceof List<Rdn>) {
142                //	this.rdns = new ArrayList<Rdn>(rdns);
143                // } else {
144                //	throw IllegalArgumentException(
145                //		"Invalid entries, list entries must be of type Rdn");
146                //  }
147
148                this .rdns = new ArrayList(rdns.size());
149                for (int i = 0; i < rdns.size(); i++) {
150                    Object obj = rdns.get(i);
151                    if (!(obj instanceof  Rdn)) {
152                        throw new IllegalArgumentException(
153                                "Entry:"
154                                        + obj
155                                        + "  not a valid type;list entries must be of type Rdn");
156                    }
157                    this .rdns.add(obj);
158                }
159            }
160
161            /*
162             * Constructs an LDAP name given its parsed components (the elements
163             * of "rdns" in the range [beg,end)) and, optionally
164             * (if "name" is not null), the unparsed DN.
165             *
166             */
167            // private LdapName(String name, List<Rdn> rdns, int beg, int end) {
168            private LdapName(String name, ArrayList rdns, int beg, int end) {
169                unparsed = name;
170                // this.rdns = rdns.subList(beg, end);
171
172                List sList = rdns.subList(beg, end);
173                this .rdns = new ArrayList(sList);
174            }
175
176            /**
177             * Retrieves the number of components in this LDAP name.
178             * @return The non-negative number of components in this LDAP name.
179             */
180            public int size() {
181                return rdns.size();
182            }
183
184            /**
185             * Determines whether this LDAP name is empty.
186             * An empty name is one with zero components.
187             * @return true if this LDAP name is empty, false otherwise.
188             */
189            public boolean isEmpty() {
190                return rdns.isEmpty();
191            }
192
193            /**
194             * Retrieves the components of this name as an enumeration
195             * of strings. The effect of updates to this name on this enumeration
196             * is undefined. If the name has zero components, an empty (non-null)
197             * enumeration is returned.
198             * The order of the components returned by the enumeration is same as
199             * the order in which the components are numbered as described in the
200             * class description.
201             *
202             * @return A non-null enumeration of the components of this LDAP name.
203             * Each element of the enumeration is of class String.
204             */
205            public Enumeration<String> getAll() {
206                final Iterator iter = rdns.iterator();
207
208                return new Enumeration<String>() {
209                    public boolean hasMoreElements() {
210                        return iter.hasNext();
211                    }
212
213                    public String nextElement() {
214                        return iter.next().toString();
215                    }
216                };
217            }
218
219            /**
220             * Retrieves a component of this LDAP name as a string.
221             * @param  posn The 0-based index of the component to retrieve.
222             *		    Must be in the range [0,size()).
223             * @return The non-null component at index posn.
224             * @exception IndexOutOfBoundsException if posn is outside the
225             *		specified range.
226             */
227            public String get(int posn) {
228                return rdns.get(posn).toString();
229            }
230
231            /**
232             * Retrieves an RDN of this LDAP name as an Rdn.
233             * @param	posn The 0-based index of the RDN to retrieve.
234             * 		Must be in the range [0,size()).
235             * @return The non-null RDN at index posn.
236             * @exception IndexOutOfBoundsException if posn is outside the
237             *		  specified range.
238             */
239            public Rdn getRdn(int posn) {
240                return (Rdn) rdns.get(posn);
241            }
242
243            /**
244             * Creates a name whose components consist of a prefix of the
245             * components of this LDAP name.
246             * Subsequent changes to this name will not affect the name
247             * that is returned and vice versa.
248             * @param  posn	The 0-based index of the component at which to stop.
249             *			Must be in the range [0,size()].
250             * @return	An instance of <tt>LdapName</tt> consisting of the
251             *		components at indexes in the range [0,posn).
252             *		If posn is zero, an empty LDAP name is returned.
253             * @exception   IndexOutOfBoundsException
254             *		    If posn is outside the specified range.
255             */
256            public Name getPrefix(int posn) {
257                try {
258                    return new LdapName(null, rdns, 0, posn);
259                } catch (IllegalArgumentException e) {
260                    throw new IndexOutOfBoundsException("Posn: " + posn
261                            + ", Size: " + rdns.size());
262                }
263            }
264
265            /**
266             * Creates a name whose components consist of a suffix of the
267             * components in this LDAP name.
268             * Subsequent changes to this name do not affect the name that is
269             * returned and vice versa.
270             *
271             * @param  posn	The 0-based index of the component at which to start.
272             * 			Must be in the range [0,size()].
273             * @return	An instance of <tt>LdapName</tt> consisting of the
274             *		components at indexes in the range [posn,size()).
275             *		If posn is equal to size(), an empty LDAP name is
276             *		returned.
277             * @exception IndexOutOfBoundsException
278             *		If posn is outside the specified range.
279             */
280            public Name getSuffix(int posn) {
281                try {
282                    return new LdapName(null, rdns, posn, rdns.size());
283                } catch (IllegalArgumentException e) {
284                    throw new IndexOutOfBoundsException("Posn: " + posn
285                            + ", Size: " + rdns.size());
286                }
287            }
288
289            /**
290             * Determines whether this LDAP name starts with a specified LDAP name
291             * prefix.
292             * A name <tt>n</tt> is a prefix if it is equal to
293             * <tt>getPrefix(n.size())</tt>--in other words this LDAP
294             * name starts with 'n'. If n is null or not a RFC2253 formatted name
295             * as described in the class description, false is returned.
296             *
297             * @param n The LDAP name to check.
298             * @return  true if <tt>n</tt> is a prefix of this LDAP name,
299             * false otherwise.
300             * @see #getPrefix(int posn)
301             */
302            public boolean startsWith(Name n) {
303                if (n == null) {
304                    return false;
305                }
306                int len1 = rdns.size();
307                int len2 = n.size();
308                return (len1 >= len2 && matches(0, len2, n));
309            }
310
311            /**
312             * Determines whether the specified RDN sequence forms a prefix of this
313             * LDAP name.  Returns true if this LdapName is at least as long as rdns,
314             * and for every position p in the range [0, rdns.size()) the component
315             * getRdn(p) matches rdns.get(p). Returns false otherwise. If rdns is
316             * null, false is returned.
317             *
318             * @param rdns The sequence of <tt>Rdn</tt>s to check.
319             * @return  true if <tt>rdns</tt> form a prefix of this LDAP name,
320             * 		false otherwise.
321             */
322            public boolean startsWith(List<Rdn> rdns) {
323                if (rdns == null) {
324                    return false;
325                }
326                int len1 = this .rdns.size();
327                int len2 = rdns.size();
328                return (len1 >= len2 && doesListMatch(0, len2, rdns));
329            }
330
331            /**
332             * Determines whether this LDAP name ends with a specified
333             * LDAP name suffix.
334             * A name <tt>n</tt> is a suffix if it is equal to
335             * <tt>getSuffix(size()-n.size())</tt>--in other words this LDAP
336             * name ends with 'n'. If n is null or not a RFC2253 formatted name
337             * as described in the class description, false is returned.
338             *
339             * @param n The LDAP name to check.
340             * @return true if <tt>n</tt> is a suffix of this name, false otherwise.
341             * @see #getSuffix(int posn)
342             */
343            public boolean endsWith(Name n) {
344                if (n == null) {
345                    return false;
346                }
347                int len1 = rdns.size();
348                int len2 = n.size();
349                return (len1 >= len2 && matches(len1 - len2, len1, n));
350            }
351
352            /**
353             * Determines whether the specified RDN sequence forms a suffix of this
354             * LDAP name.  Returns true if this LdapName is at least as long as rdns,
355             * and for every position p in the range [size() - rdns.size(), size())
356             * the component getRdn(p) matches rdns.get(p). Returns false otherwise.
357             * If rdns is null, false is returned.
358             *
359             * @param rdns The sequence of <tt>Rdn</tt>s to check.
360             * @return  true if <tt>rdns</tt> form a suffix of this LDAP name,
361             *		false otherwise.
362             */
363            public boolean endsWith(List<Rdn> rdns) {
364                if (rdns == null) {
365                    return false;
366                }
367                int len1 = this .rdns.size();
368                int len2 = rdns.size();
369                return (len1 >= len2 && doesListMatch(len1 - len2, len1, rdns));
370            }
371
372            private boolean doesListMatch(int beg, int end, List rdns) {
373                for (int i = beg; i < end; i++) {
374                    if (!this .rdns.get(i).equals(rdns.get(i - beg))) {
375                        return false;
376                    }
377                }
378                return true;
379            }
380
381            /*
382             * Helper method for startsWith() and endsWith().
383             * Returns true if components [beg,end) match the components of "n".
384             * If "n" is not an LdapName, each of its components is parsed as
385             * the string form of an RDN.
386             * The following must hold:  end - beg == n.size().
387             */
388            private boolean matches(int beg, int end, Name n) {
389                if (n instanceof  LdapName) {
390                    LdapName ln = (LdapName) n;
391                    return doesListMatch(beg, end, ln.rdns);
392                } else {
393                    for (int i = beg; i < end; i++) {
394                        Rdn rdn;
395                        String rdnString = n.get(i - beg);
396                        try {
397                            rdn = (new Rfc2253Parser(rdnString)).parseRdn();
398                        } catch (InvalidNameException e) {
399                            return false;
400                        }
401                        if (!rdn.equals(rdns.get(i))) {
402                            return false;
403                        }
404                    }
405                }
406                return true;
407            }
408
409            /**
410             * Adds the components of a name -- in order -- to the end of this name.
411             *
412             * @param	suffix The non-null components to add.
413             * @return  The updated name (not a new instance).
414             *
415             * @throws	InvalidNameException if <tt>suffix</tt> is not a valid LDAP
416             * 		name, or if the addition of the components would violate the
417             *		syntax rules of this LDAP name.
418             */
419            public Name addAll(Name suffix) throws InvalidNameException {
420                return addAll(size(), suffix);
421            }
422
423            /**
424             * Adds the RDNs of a name -- in order -- to the end of this name.
425             *
426             * @param	suffixRdns The non-null suffix <tt>Rdn</tt>s to add.
427             * @return  The updated name (not a new instance).
428             */
429            public Name addAll(List<Rdn> suffixRdns) {
430                return addAll(size(), suffixRdns);
431            }
432
433            /**
434             * Adds the components of a name -- in order -- at a specified position
435             * within this name. Components of this LDAP name at or after the
436             * index (if any) of the first new component are shifted up
437             * (away from index 0) to accomodate the new components.
438             *
439             * @param suffix	The non-null components to add.
440             * @param posn	The index at which to add the new component.
441             *			Must be in the range [0,size()].
442             *
443             * @return  The updated name (not a new instance).
444             *
445             * @throws	InvalidNameException if <tt>suffix</tt> is not a valid LDAP
446             * 		name, or if the addition of the components would violate the
447             *		syntax rules of this LDAP name.
448             * @throws	IndexOutOfBoundsException.
449             *		If posn is outside the specified range.
450             */
451            public Name addAll(int posn, Name suffix)
452                    throws InvalidNameException {
453                unparsed = null; // no longer valid
454                if (suffix instanceof  LdapName) {
455                    LdapName s = (LdapName) suffix;
456                    rdns.addAll(posn, s.rdns);
457                } else {
458                    Enumeration comps = suffix.getAll();
459                    while (comps.hasMoreElements()) {
460                        rdns.add(posn++, (new Rfc2253Parser((String) comps
461                                .nextElement()).parseRdn()));
462                    }
463                }
464                return this ;
465            }
466
467            /**
468             * Adds the RDNs of a name -- in order -- at a specified position
469             * within this name. RDNs of this LDAP name at or after the
470             * index (if any) of the first new RDN are shifted up (away from index 0) to
471             * accomodate the new RDNs.
472             *
473             * @param suffixRdns	The non-null suffix <tt>Rdn</tt>s to add.
474             * @param posn		The index at which to add the suffix RDNs.
475             *				Must be in the range [0,size()].
476             *
477             * @return  The updated name (not a new instance).
478             * @throws	IndexOutOfBoundsException.
479             *		If posn is outside the specified range.
480             */
481            public Name addAll(int posn, List<Rdn> suffixRdns) {
482                unparsed = null;
483                for (int i = 0; i < suffixRdns.size(); i++) {
484                    Object obj = suffixRdns.get(i);
485                    if (!(obj instanceof  Rdn)) {
486                        throw new IllegalArgumentException(
487                                "Entry:"
488                                        + obj
489                                        + "  not a valid type;suffix list entries must be of type Rdn");
490                    }
491                    rdns.add(i + posn, obj);
492                }
493                return this ;
494            }
495
496            /**
497             * Adds a single component to the end of this LDAP name.
498             *
499             * @param comp	The non-null component to add.
500             * @return		The updated LdapName, not a new instance.
501             *			Cannot be null.
502             * @exception	InvalidNameException If adding comp at end of the name
503             *			would violate the name's syntax.
504             */
505            public Name add(String comp) throws InvalidNameException {
506                return add(size(), comp);
507            }
508
509            /**
510             * Adds a single RDN to the end of this LDAP name.
511             *
512             * @param comp	The non-null RDN to add.
513             *
514             * @return		The updated LdapName, not a new instance.
515             *			Cannot be null.
516             */
517            public Name add(Rdn comp) {
518                return add(size(), comp);
519            }
520
521            /**
522             * Adds a single component at a specified position within this
523             * LDAP name.
524             * Components of this LDAP name at or after the index (if any) of the new
525             * component are shifted up by one (away from index 0) to accommodate
526             * the new component.
527             *
528             * @param  comp	The non-null component to add.
529             * @param  posn	The index at which to add the new component.
530             *			Must be in the range [0,size()].
531             * @return		The updated LdapName, not a new instance.
532             *			Cannot be null.
533             * @exception	IndexOutOfBoundsException.
534             *			If posn is outside the specified range.
535             * @exception	InvalidNameException If adding comp at the
536             *			specified position would violate the name's syntax.
537             */
538            public Name add(int posn, String comp) throws InvalidNameException {
539                Rdn rdn = (new Rfc2253Parser(comp)).parseRdn();
540                rdns.add(posn, rdn);
541                unparsed = null; // no longer valid
542                return this ;
543            }
544
545            /**
546             * Adds a single RDN at a specified position within this
547             * LDAP name.
548             * RDNs of this LDAP name at or after the index (if any) of the new
549             * RDN are shifted up by one (away from index 0) to accommodate
550             * the new RDN.
551             *
552             * @param  comp	The non-null RDN to add.
553             * @param  posn	The index at which to add the new RDN.
554             * 			Must be in the range [0,size()].
555             * @return		The updated LdapName, not a new instance.
556             *			Cannot be null.
557             * @exception	IndexOutOfBoundsException
558             *			If posn is outside the specified range.
559             */
560            public Name add(int posn, Rdn comp) {
561                if (comp == null) {
562                    throw new NullPointerException("Cannot set comp to null");
563                }
564                rdns.add(posn, comp);
565                unparsed = null; // no longer valid
566                return this ;
567            }
568
569            /**
570             * Removes a component from this LDAP name.
571             * The component of this name at the specified position is removed.
572             * Components with indexes greater than this position (if any)
573             * are shifted down (toward index 0) by one.
574             *
575             * @param posn	The index of the component to remove.
576             *			Must be in the range [0,size()).
577             * @return		The component removed (a String).
578             *
579             * @throws		IndexOutOfBoundsException
580             *			if posn is outside the specified range.
581             * @throws		InvalidNameException if deleting the component
582             *			would violate the syntax rules of the name.
583             */
584            public Object remove(int posn) throws InvalidNameException {
585                unparsed = null; // no longer valid
586                return rdns.remove(posn).toString();
587            }
588
589            /**
590             * Retrieves the list of relative distinguished names.
591             * The contents of the list are unmodifiable.
592             * The indexing of RDNs in the returned list follows the numbering of
593             * RDNs as described in the class description.
594             * If the name has zero components, an empty list is returned.
595             *
596             * @return	The name as a list of RDNs which are instances of
597             *		the class {@link Rdn Rdn}.
598             */
599            public List<Rdn> getRdns() {
600                return Collections.unmodifiableList(rdns);
601            }
602
603            /**
604             * Generates a new copy of this name.
605             * Subsequent changes to the components of this name will not
606             * affect the new copy, and vice versa.
607             *
608             * @return A copy of the this LDAP name.
609             */
610            public Object clone() {
611                return new LdapName(unparsed, rdns, 0, rdns.size());
612            }
613
614            /**
615             * Returns a string representation of this LDAP name in a format
616             * defined by <a href="http://ietf.org/rfc/rfc2253.txt">RFC 2253</a>
617             * and described in the class description. If the name has zero
618             * components an empty string is returned.
619             *
620             * @return The string representation of the LdapName.
621             */
622            public String toString() {
623                if (unparsed != null) {
624                    return unparsed;
625                }
626                StringBuilder builder = new StringBuilder();
627                int size = rdns.size();
628                if ((size - 1) >= 0) {
629                    builder.append((Rdn) rdns.get(size - 1));
630                }
631                for (int next = size - 2; next >= 0; next--) {
632                    builder.append(',');
633                    builder.append((Rdn) rdns.get(next));
634                }
635                unparsed = builder.toString();
636                return unparsed;
637            }
638
639            /**
640             * Determines whether two LDAP names are equal.
641             * If obj is null or not an LDAP name, false is returned.
642             * <p>
643             * Two LDAP names are equal if each RDN in one is equal
644             * to the corresponding RDN in the other. This implies
645             * both have the same number of RDNs, and each RDN's
646             * equals() test against the corresponding RDN in the other
647             * name returns true. See {@link Rdn#equals(Object obj)}
648             * for a definition of RDN equality.
649             *
650             * @param  obj	The possibly null object to compare against.
651             * @return		true if obj is equal to this LDAP name,
652             *			false otherwise.
653             * @see #hashCode
654             */
655            public boolean equals(Object obj) {
656                // check possible shortcuts
657                if (obj == this ) {
658                    return true;
659                }
660                if (!(obj instanceof  LdapName)) {
661                    return false;
662                }
663                LdapName that = (LdapName) obj;
664                if (rdns.size() != that.rdns.size()) {
665                    return false;
666                }
667                if (unparsed != null
668                        && unparsed.equalsIgnoreCase(that.unparsed)) {
669                    return true;
670                }
671                // Compare RDNs one by one for equality
672                for (int i = 0; i < rdns.size(); i++) {
673                    // Compare a single pair of RDNs.
674                    Rdn rdn1 = (Rdn) rdns.get(i);
675                    Rdn rdn2 = (Rdn) that.rdns.get(i);
676                    if (!rdn1.equals(rdn2)) {
677                        return false;
678                    }
679                }
680                return true;
681            }
682
683            /**
684             * Compares this LdapName with the specified Object for order.
685             * Returns a negative integer, zero, or a positive integer as this
686             * Name is less than, equal to, or greater than the given Object.
687             * <p>
688             * If obj is null or not an instance of LdapName, ClassCastException
689             * is thrown.
690             * <p>
691             * Ordering of LDAP names follows the lexicographical rules for
692             * string comparison, with the extension that this applies to all
693             * the RDNs in the LDAP name. All the RDNs are lined up in their
694             * specified order and compared lexicographically.
695             * See {@link Rdn#compareTo(Object obj) Rdn.compareTo(Object obj)}
696             * for RDN comparison rules.
697             * <p>
698             * If this LDAP name is lexicographically lesser than obj,
699             * a negative number is returned.
700             * If this LDAP name is lexicographically greater than obj,
701             * a positive number is returned.
702             * @param obj The non-null LdapName instance to compare against.
703             *
704             * @return  A negative integer, zero, or a positive integer as this Name
705             * 		is less than, equal to, or greater than the given obj.
706             * @exception ClassCastException if obj is null or not a LdapName.
707             */
708            public int compareTo(Object obj) {
709
710                if (!(obj instanceof  LdapName)) {
711                    throw new ClassCastException("The obj is not a LdapName");
712                }
713
714                // check possible shortcuts
715                if (obj == this ) {
716                    return 0;
717                }
718                LdapName that = (LdapName) obj;
719
720                if (unparsed != null
721                        && unparsed.equalsIgnoreCase(that.unparsed)) {
722                    return 0;
723                }
724
725                // Compare RDNs one by one, lexicographically.
726                int minSize = Math.min(rdns.size(), that.rdns.size());
727                for (int i = 0; i < minSize; i++) {
728                    // Compare a single pair of RDNs.
729                    Rdn rdn1 = (Rdn) rdns.get(i);
730                    Rdn rdn2 = (Rdn) that.rdns.get(i);
731
732                    int diff = rdn1.compareTo(rdn2);
733                    if (diff != 0) {
734                        return diff;
735                    }
736                }
737                return (rdns.size() - that.rdns.size()); // longer DN wins
738            }
739
740            /**
741             * Computes the hash code of this LDAP name.
742             * The hash code is the sum of the hash codes of individual RDNs
743             * of this  name.
744             *
745             * @return An int representing the hash code of this name.
746             * @see #equals
747             */
748            public int hashCode() {
749                // Sum up the hash codes of the components.
750                int hash = 0;
751
752                // For each RDN...
753                for (int i = 0; i < rdns.size(); i++) {
754                    Rdn rdn = (Rdn) rdns.get(i);
755                    hash += rdn.hashCode();
756                }
757                return hash;
758            }
759
760            /**
761             * Serializes only the unparsed DN, for compactness and to avoid
762             * any implementation dependency.
763             *
764             * @serialData	The DN string
765             */
766            private void writeObject(ObjectOutputStream s)
767                    throws java.io.IOException {
768                s.defaultWriteObject();
769                s.writeObject(toString());
770            }
771
772            private void readObject(ObjectInputStream s)
773                    throws java.io.IOException, ClassNotFoundException {
774                s.defaultReadObject();
775                unparsed = (String) s.readObject();
776                try {
777                    parse();
778                } catch (InvalidNameException e) {
779                    // shouldn't happen
780                    throw new java.io.StreamCorruptedException("Invalid name: "
781                            + unparsed);
782                }
783            }
784
785            private void parse() throws InvalidNameException {
786                // rdns = (ArrayList<Rdn>) (new RFC2253Parser(unparsed)).getDN();
787
788                rdns = (ArrayList) (new Rfc2253Parser(unparsed)).parseDn();
789            }
790        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.