KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > naming > DXEntry


1
2 package com.ca.commons.naming;
3
4 import com.ca.commons.cbutil.*;
5
6 import javax.naming.*;
7 import javax.naming.directory.*;
8
9 import java.util.*;
10
11 /**
12  * This class is a wrapper for DXAttributes, that
13  * includes knowledge of the DN of the attributes
14  * set.
15  */

16  
17
18
19 public class DXEntry extends DXAttributes
20 {
21     /**
22      * The full name of the entry
23      */

24      
25     DN dn = null;
26
27     /**
28      * This status constant implies that the entry is
29      * a 'normal' directory entry.
30      */

31      
32     public static final int NORMAL = 0;
33     
34     /**
35      * This status constant implies that the entry is
36      * a new entry, that has not yet been written to the directory.
37      */

38
39     public static final int NEW = 1;
40
41     /**
42      * This status constant implies that the entry is
43      * a new entry, that has just been written to the directory,
44      * but may not yet have been added to the tree or re-displayed
45      * by the editor.
46      */

47
48     public static final int NEW_WRITTEN = 2;
49
50     private int status = NORMAL;
51
52     /**
53      * Constructors simply chain to DXAttributes...
54      * (except for two which allow for a DN as well...)
55      */

56      
57     public DXEntry() { super(); }
58     public DXEntry(DN dn) { super(); this.dn = dn;}
59     public DXEntry(Attribute a) { super(a); }
60     public DXEntry(Attributes a) { super(a); }
61     public DXEntry(Attributes a, DN dn) { super(a); this.dn = dn;}
62     public DXEntry(Hashtable newAtts) { super(newAtts); }
63     public DXEntry(NamingEnumeration newAtts) { super(newAtts); }
64     public DXEntry(DXEntry copyMe)
65     {
66         super(copyMe);
67         this.dn = copyMe.dn;
68         this.status = copyMe.status;
69     }
70
71
72
73     /**
74      * Intercept a DN being passed as an attribute
75     * (as might happen reading an ldif file);
76      */

77              
78     public Attribute put(Attribute attr)
79     {
80         if (attr.getID().equalsIgnoreCase("dn"))
81         {
82             try
83             {
84                 Object JavaDoc o = attr.get();
85                     
86                 if (o instanceof String JavaDoc)
87                     dn = new DN((String JavaDoc)o);
88
89                 else
90                     dn = new DN(o.toString()); // no idea what to do with it...
91
}
92             catch (NamingException e)
93             {
94                 CBUtility.error("Unexpected exception in DXEntry.put.", e);
95             }
96             return null; // there is by definition no dn attribute...
97
}
98         else
99             return super.put(attr);
100     }
101     
102     /**
103      * This is used to set what stage of the 'new entry' life cycle
104      * an entry is in - one of 'NORMAL', 'NEW', or 'NEW_WRITTEN'.
105      * Almost all entries are 'NORMAL'. When an entry has just been
106      * created by an entry editor, it is 'NEW'. When it has just been
107      * written to the directory, it becomes 'NEW_WRITTEN'.
108      *
109      * @param entryStatus The status of the entry as a constant
110      * (one of {NORMAL, NEW, NEW_WRITTEN} ).
111      */

112      
113     public void setStatus(int entryStatus) { status = entryStatus; }
114     
115     /**
116      * Returns the status of the entry as a status constant. See
117      * status constants for more info.
118      *
119      * @return entry status as one of {NORMAL, NEW, NEW_WRITTEN}.
120      */

121      
122     public int getStatus() { return status; }
123
124     /**
125      * Returns the status of the entry as a readable string. See
126      * status constants for more info.
127      *
128      * @return entry status as one of "Normal", "New", or "Newly Written".
129      */

130      
131     public String JavaDoc getStringStatus()
132     {
133         switch (status)
134         {
135             case NORMAL: return "Normal";
136             
137             case NEW: return "New";
138             
139             case NEW_WRITTEN: return "Newly Written";
140         }
141         
142         return "Unknown";
143     }
144     
145     
146     /**
147      * Returns whether the entry is new, (or just written to the directory),
148      * or whether it already existed in the directory.
149      * @return true if entry status is NEW or NEW_WRITTEN.
150      */

151      
152     public boolean isNewEntry() { return (status==NEW || status == NEW_WRITTEN); }
153     
154     /**
155      * Add a DN directly, without using an attribute.
156      * @param dn the distinguished name to add.
157      * @depricated use @setDN instead
158      */

159     
160     public void putDN(DN dn)
161     {
162         setDN(dn);
163     }
164
165     
166     public void setDN(DN dn)
167     {
168         this.dn = dn;
169     }
170
171     /**
172      * Returns the DN of this entry, or an empty DN if
173      * none has been set.
174      * @return the entry's distinguished name.
175      */

176     
177     public DN getDN()
178     {
179         return (dn==null)?new DN():dn;
180     }
181  
182     /**
183      * Provides a string representation of the entry, as a set of
184      * attributes preceeded by a header of form 'entry = <DN>'
185      */

186     
187     public String JavaDoc toString()
188     {
189         return ("entry = " + getDN().toString() + "\n status: " +
190                 getStringStatus() + "\n" +
191                 super.toString());
192     }
193     
194     /**
195      * Utility class intended for recovering the value of a single
196      * value attribute as a string.
197      * @param id the attribute name
198      * @return the value of the attribute as a string.
199      */

200      
201     public String JavaDoc getString(String JavaDoc id)
202     {
203         Attribute a = get(id);
204         try
205         {
206             return a.get().toString();
207         }
208         catch (Exception JavaDoc e)
209         {
210             return null;
211         }
212     }
213     
214     /**
215      * Returns the Entry's RDN.
216      * @return RDN the lowest RDN of the entry's DN - e.g.
217      * 'cn=fred' in 'cn=fred,ou=Frog Farmers,o=DemoCorp,c=au').
218      */

219      
220     public RDN getRDN()
221     {
222         if (dn == null)
223             return null;
224             
225         return dn.getLowestRDN();
226     }
227
228 }
Popular Tags