KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > usermanager > JahiaLDAPGroup


1 package org.jahia.services.usermanager;
2
3 import org.jahia.exceptions.JahiaException;
4 import org.jahia.exceptions.database.JahiaDatabaseConnectionException;
5 import org.jahia.exceptions.database.JahiaDatabaseException;
6 import org.jahia.registries.ServicesRegistry;
7
8 import java.security.Principal JavaDoc;
9 import java.util.*;
10
11
12 /**
13  * <p>Title: </p>
14  * <p>Description: </p>
15  * <p>Copyright: Copyright (c) 2003</p>
16  * <p>Company: </p>
17  *
18  * @author Viceic Predrag <Predrag.Viceic@ci.unil.ch>
19  * @version 1.0
20  */

21
22 public class JahiaLDAPGroup extends JahiaGroup {
23
24     static final public String JavaDoc GROUPKEY_LDAP_PREFIX = "{ldap}";
25
26     /** Group's unique identification number */
27     protected int mID;
28
29     /** User Member type designation * */
30     protected static int mUSERTYPE = 1;
31
32     /** Group Member type designation * */
33     protected static int mGROUPTYPE = 2;
34
35     /** Group home page property * */
36     private static final String JavaDoc mHOMEPAGE_PROP = "group_homepage";
37
38     /** Group additional parameters. */
39     private Properties mProperties = new Properties ();
40
41     // LDAP dynamic group (groupOfURLs)
42
private boolean dynamic;
43
44     private boolean preloadedGroups;
45
46     private Set notMembers = new HashSet();
47
48
49     /**
50      * Instanciate a new JahiaDBGroup object.
51      *
52      * @throws JahiaException This class need to access the Services Registry and the DB Pool
53      * Service. If any of this services can't be accessed, a
54      * JahiaException is thrown.
55      * @param siteID The site id
56      * @param dynamic
57      * @param preloadedGroups
58      */

59     protected JahiaLDAPGroup(int id, String JavaDoc groupname, String JavaDoc groupKey, int siteID,
60                              Hashtable members, Properties properties, boolean dynamic, boolean preloadedGroups)
61             throws JahiaException {
62         ServicesRegistry registry = ServicesRegistry.getInstance ();
63         if (registry == null) {
64             throw new JahiaException ("Jahia Internal Error",
65                     "JahiaLDAPGroup Could not get the Service Registry instance",
66                     JahiaException.SERVICE_ERROR, JahiaException.CRITICAL_SEVERITY);
67         }
68
69         mID = id;
70         mGroupname = groupname;
71         mGroupKey = GROUPKEY_LDAP_PREFIX + groupKey;
72         mSiteID = siteID;
73
74         if (members != null) {
75             mMembers = members;
76         }
77
78         if (properties != null) {
79             mProperties = properties;
80         }
81         this.dynamic = dynamic;
82         this.preloadedGroups = preloadedGroups;
83     }
84
85
86     /**
87      * Returns the group's home page id.
88      * -1 : undefined
89      *
90      * @return int The group homepage id.
91      */

92
93     public int getHomepageID () {
94         if (mProperties != null) {
95
96             try {
97                 // Get the home page from the Jahia DB.
98
// By default an external group is represented with a -1 group ID.
99
String JavaDoc value = mProperties.getProperty (mHOMEPAGE_PROP);
100
101                 if (value == null)
102                     return -1;
103                 return Integer.parseInt (value);
104             } catch (Throwable JavaDoc t) {
105                 t.printStackTrace ();
106             }
107         }
108         return -1;
109
110     }
111
112     public boolean setHomepageID (int id) {
113         if (!removeProperty (mHOMEPAGE_PROP))
114             return false;
115         // Set the home page into the Jahia DB.
116
// By default an external group is represented with a -1 group ID.
117
return setProperty (mHOMEPAGE_PROP, String.valueOf (id));
118
119     }
120
121     public synchronized boolean removeProperty (String JavaDoc key) {
122         boolean result = false;
123
124         if ((key != null) && (key.length () > 0)) {
125             // Remove these lines if LDAP problem --------------------
126
JahiaGroupDBUtils utils = JahiaGroupDBUtils.getInstance ();
127             if (utils != null) {
128                 try {
129                     result = utils.removeProperty (key, -1, getProviderName (), getGroupKey ());
130                 }
131                         // general database exeception
132
catch (JahiaDatabaseException ex) {
133                     // false will be returned automaticaly
134
}
135
136                         // database connection failure
137
catch (JahiaDatabaseConnectionException ex) {
138                     // false will be returned automaticaly
139
}
140
141                         // catch all the Jahia exceptions
142
catch (JahiaException ex) {
143                     // false will be returned automaticaly
144
}
145             }
146         }
147
148         if (result) {
149             mProperties.remove (key);
150         }
151         // End remove --------------------
152
return result;
153
154     }
155
156     public Properties getProperties () {
157         return mProperties;
158     }
159
160     public boolean removeMember (Principal JavaDoc user) {
161         /**@todo Must check this*/
162         return false;
163     }
164
165     public String JavaDoc getProperty (String JavaDoc key) {
166         if ((mProperties != null) && (key != null)) {
167             return mProperties.getProperty (key);
168         }
169         return null;
170
171     }
172
173     public boolean addMember (Principal JavaDoc user) {
174         /**@todo Must check this*/
175         return false;
176     }
177
178     public synchronized boolean setProperty (String JavaDoc key, String JavaDoc value) {
179         boolean result = false;
180
181         if ((key != null) && (value != null)) {
182             // Remove these lines if LDAP problem --------------------
183
JahiaGroupDBUtils utils = JahiaGroupDBUtils.getInstance ();
184             if (utils != null) {
185                 try {
186                     if (getProperty (key) == null) {
187                         result =
188                                 utils.addProperty (key, value, -1, getProviderName (),
189                                         getGroupKey ());
190                     } else {
191                         result =
192                                 utils.updateProperty (key, value, -1, getProviderName (),
193                                         getGroupKey ());
194                     }
195                 }
196                         // general database exeception
197
catch (JahiaDatabaseException ex) {
198                     // false will be returned automaticaly
199
}
200
201                         // database connection failure
202
catch (JahiaDatabaseConnectionException ex) {
203                     // false will be returned automaticaly
204
}
205
206                         // catch all the Jahia exceptions
207
catch (JahiaException ex) {
208                     // false will be returned automaticaly
209
}
210             }
211             // End remove --------------------
212
if (result) {
213                 mProperties.setProperty (key, value);
214                 ServicesRegistry.getInstance().getJahiaGroupManagerService().updateCache(this);
215             }
216         }
217         return result;
218
219     }
220
221     public String JavaDoc getProviderName () {
222         return JahiaGroupManagerLDAPProvider.PROVIDER_NAME;
223     }
224
225     public boolean isDynamic() {
226         return dynamic;
227     }
228
229     public String JavaDoc toString () {
230
231         StringBuffer JavaDoc output = new StringBuffer JavaDoc ("Details of group [" + mGroupname + "] :\n");
232
233         output.append (" - ID : " + Integer.toString (mID) + "\n");
234
235         output.append (" - properties :");
236
237         Enumeration names = mProperties.propertyNames ();
238         String JavaDoc name;
239         if (names.hasMoreElements ()) {
240             output.append ("\n");
241             while (names.hasMoreElements ()) {
242                 name = (String JavaDoc) names.nextElement ();
243                 output.append (
244                         " " + name + " -> [" + (String JavaDoc) mProperties.getProperty (name) + "]\n");
245             }
246         } else {
247             output.append (" -no properties-\n");
248         }
249
250         // Add the user members useranames detail
251
output.append (" - members : ");
252         names = mMembers.keys ();
253         if (names.hasMoreElements ()) {
254             while (names.hasMoreElements ()) {
255                 output.append (names.nextElement () + "/");
256             }
257         } else {
258             output.append (" -no members-\n");
259         }
260
261         return output.toString ();
262     }
263
264     public boolean equals (Object JavaDoc another) {
265         if (another instanceof Principal JavaDoc) {
266             if (another != null) {
267                 return (getName ().equals (((Principal JavaDoc) another).getName ()));
268             }
269         }
270         return false;
271     }
272
273     //-------------------------------------------------------------------------
274
public int hashCode () {
275         return mID;
276     }
277
278
279     public void setSiteID (int id) {
280         mSiteID = id;
281     }
282
283     public boolean isMember(Principal JavaDoc principal) {
284         if (super.isMember(principal)) {
285             return true;
286         }
287         if (!preloadedGroups && principal instanceof JahiaUser) {
288             if (notMembers.contains(principal.getName())) {
289                 return false;
290             }
291             if (JahiaGroupManagerLDAPProvider.getInstance().getUserMembership((JahiaUser)principal).contains(getGroupKey())) {
292                 mMembers.put(principal.getName(), principal);
293                 return true;
294             } else {
295                 notMembers.add(principal.getName());
296             }
297         }
298         return false;
299     }
300 }
301
Popular Tags