KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > store > ldap > LdapGroupStore


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.server.store.ldap;
20
21 import java.util.*;
22
23 import org.lucane.common.concepts.*;
24 import org.lucane.server.store.*;
25
26 import javax.naming.*;
27 import javax.naming.directory.*;
28
29 /* ou=groups,dc=lucane,dc=org
30  * |
31  * ou=groups
32  * |-- cn=admins
33  * | member=allUsers
34  * | member=grantedUsers
35  * |-- cn=othergroups
36  *
37  * ou=users
38  * |-- cn=admins
39  * | member=admin
40  *
41  * ou=plugins
42  * |-- cn=admins
43  * | member=org.lucane.applications.administrator
44  * | member=...
45  *
46  * ou=services
47  * |-- cn=admins
48  * | member=org.lucane.applications.administrator
49  */

50
51 public class LdapGroupStore extends GroupStore
52 {
53     private Store store;
54     
55     private DirContext groupContext;
56     private DirContext userContext;
57     private DirContext pluginContext;
58     private DirContext serviceContext;
59     private HashMap mapping;
60     private HashMap attributes;
61     
62     public LdapGroupStore(Store store, LdapConfig config)
63     throws Exception JavaDoc
64     {
65         this.store = store;
66         
67         Hashtable ht = new Hashtable();
68         ht.put(DirContext.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
69         ht.put(DirContext.SECURITY_AUTHENTICATION, config.getAuthType());
70         ht.put(DirContext.SECURITY_PRINCIPAL, config.getAuthBindDn());
71         ht.put(DirContext.SECURITY_CREDENTIALS, config.getAuthPassword());
72         
73         ht.put(DirContext.PROVIDER_URL, config.getLdapUrl() + "ou=groups," +config.getGroupsDn());
74         this.groupContext = new InitialDirContext(ht);
75         ht.put(DirContext.PROVIDER_URL, config.getLdapUrl() + "ou=users," +config.getGroupsDn());
76         this.userContext = new InitialDirContext(ht);
77         ht.put(DirContext.PROVIDER_URL, config.getLdapUrl() + "ou=plugins," +config.getGroupsDn());
78         this.pluginContext = new InitialDirContext(ht);
79         ht.put(DirContext.PROVIDER_URL, config.getLdapUrl() + "ou=services," +config.getGroupsDn());
80         this.serviceContext = new InitialDirContext(ht);
81     
82         this.attributes = config.getGroupsAttributes();
83         this.mapping = config.getGroupsMapping();
84     }
85     
86     /**
87      * Does the store has any data ?
88      *
89      * @return true if the store is already created
90      */

91     public boolean isInitialized()
92     throws Exception JavaDoc
93     {
94         return getAllGroups().hasNext();
95     }
96
97     public void storeGroup(GroupConcept group)
98     throws Exception JavaDoc
99     {
100         //store group
101
BasicAttributes attrs = getBasicAttributes(group);
102    
103         //store group links
104
Iterator i = group.getParents();
105         BasicAttribute member = new BasicAttribute((String JavaDoc)mapping.get("member"), "cn=null");
106         while(i.hasNext())
107         {
108             GroupConcept parent = (GroupConcept)i.next();
109             member.add("cn=" + parent.getName());
110         }
111         attrs.put(member);
112         groupContext.createSubcontext((String JavaDoc)mapping.get("name") + '=' + group.getName(), attrs);
113     
114         //store user links
115
attrs = getBasicAttributes(group);
116         i = group.getUsers();
117         member = new BasicAttribute((String JavaDoc)mapping.get("member"), "cn=null");
118         while(i.hasNext())
119         {
120             UserConcept user = (UserConcept)i.next();
121             member.add("cn=" + user.getName());
122         }
123         attrs.put(member);
124         userContext.createSubcontext((String JavaDoc)mapping.get("name") + '=' + group.getName(), attrs);
125         
126         //store services links
127
attrs = getBasicAttributes(group);
128         i = group.getServices();
129         member = new BasicAttribute((String JavaDoc)mapping.get("member"), "cn=null");
130         while(i.hasNext())
131         {
132             ServiceConcept service = (ServiceConcept)i.next();
133             member.add("cn=" + service.getName());
134         }
135         attrs.put(member);
136         serviceContext.createSubcontext((String JavaDoc)mapping.get("name") + '=' + group.getName(), attrs);
137         
138         //store plugins links
139
attrs = getBasicAttributes(group);
140         i = group.getPlugins();
141         member = new BasicAttribute((String JavaDoc)mapping.get("member"), "cn=null");
142         while(i.hasNext())
143         {
144             PluginConcept plugin = (PluginConcept)i.next();
145             member.add("cn=" + plugin.getName());
146         }
147         attrs.put(member);
148         pluginContext.createSubcontext((String JavaDoc)mapping.get("name") + '=' + group.getName(), attrs);
149         
150     }
151
152     public void updateGroup(GroupConcept group)
153     throws Exception JavaDoc
154     {
155         removeGroupOnly(group);
156         storeGroup(group);
157     }
158
159     public void removeGroup(GroupConcept group)
160     throws Exception JavaDoc
161     {
162         removeGroupOnly(group);
163         removeGroupLinks(group.getName());
164     }
165
166     public GroupConcept getGroup(String JavaDoc name)
167     throws Exception JavaDoc
168     {
169         GroupConcept group = null;
170         
171         String JavaDoc groupKey = (String JavaDoc)mapping.get("name") + '=' + name;
172         Attributes attrs = groupContext.getAttributes(groupKey);
173         
174         name = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("name")).get();
175         group = new GroupConcept(name);
176
177         try {
178             String JavaDoc description = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("description")).get();
179             group.setDescription(description);
180         } catch(Exception JavaDoc e) {
181             //no description
182
}
183             
184         setGroupLinks(group);
185         setUserLinks(group);
186         setPluginLinks(group);
187         setServiceLinks(group);
188
189         return group;
190     }
191
192     public Iterator getAllGroups()
193     throws Exception JavaDoc
194     {
195         ArrayList groups = new ArrayList();
196         
197         NamingEnumeration list = groupContext.list("");
198         while(list.hasMore())
199         {
200             NameClassPair pair = (NameClassPair)list.next();
201             String JavaDoc name = pair.getName();
202             name = name.substring(name.indexOf('=')+1);
203             
204             groups.add(getGroup(name));
205         }
206         
207         return groups.iterator();
208     }
209
210     //-- protected methods
211
protected void removeServiceLinks(String JavaDoc name)
212     throws Exception JavaDoc
213     {
214         String JavaDoc memberKey = (String JavaDoc)mapping.get("name") + "=" + name;
215
216         BasicAttributes attrs = new BasicAttributes();
217         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("member"), memberKey));
218         NamingEnumeration e = serviceContext.search("", attrs);
219         while(e.hasMoreElements())
220         {
221             NameClassPair pair = (NameClassPair)e.nextElement();
222             serviceContext.modifyAttributes(pair.getName(), DirContext.REMOVE_ATTRIBUTE, attrs);
223         }
224     }
225
226     protected void removePluginLinks(String JavaDoc name)
227     throws Exception JavaDoc
228     {
229         String JavaDoc memberKey = (String JavaDoc)mapping.get("name") + "=" + name;
230
231         BasicAttributes attrs = new BasicAttributes();
232         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("member"), memberKey));
233         NamingEnumeration e = pluginContext.search("", attrs);
234         while(e.hasMoreElements())
235         {
236             NameClassPair pair = (NameClassPair)e.nextElement();
237             pluginContext.modifyAttributes(pair.getName(), DirContext.REMOVE_ATTRIBUTE, attrs);
238         }
239     }
240
241     protected void removeUserLinks(String JavaDoc name)
242     throws Exception JavaDoc
243     {
244         String JavaDoc memberKey = (String JavaDoc)mapping.get("name") + "=" + name;
245
246         BasicAttributes attrs = new BasicAttributes();
247         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("member"), memberKey));
248         NamingEnumeration e = userContext.search("", attrs);
249         while(e.hasMoreElements())
250         {
251             NameClassPair pair = (NameClassPair)e.nextElement();
252             userContext.modifyAttributes(pair.getName(), DirContext.REMOVE_ATTRIBUTE, attrs);
253         }
254     }
255
256     protected void removeGroupLinks(String JavaDoc name)
257     throws Exception JavaDoc
258     {
259         String JavaDoc memberKey = (String JavaDoc)mapping.get("name") + "=" + name;
260
261         BasicAttributes attrs = new BasicAttributes();
262         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("member"), memberKey));
263         NamingEnumeration e = groupContext.search("", attrs);
264         while(e.hasMoreElements())
265         {
266             NameClassPair pair = (NameClassPair)e.nextElement();
267             groupContext.modifyAttributes(pair.getName(), DirContext.REMOVE_ATTRIBUTE, attrs);
268         }
269     }
270
271     //-- private methods
272

273     private void setServiceLinks(GroupConcept group)
274     throws Exception JavaDoc
275     {
276         String JavaDoc groupKey = (String JavaDoc)mapping.get("name") + '=' + group.getName();
277         Attributes attrs = serviceContext.getAttributes(groupKey);
278
279         Attribute attr = attrs.get((String JavaDoc)mapping.get("member"));
280         NamingEnumeration list = attr.getAll();
281                 
282         while(list.hasMore())
283         {
284             String JavaDoc name = (String JavaDoc)list.next();
285             name = name.substring(name.indexOf('=')+1);
286             if(name.equals("null"))
287                 continue;
288             
289             ServiceConcept service = store.getServiceStore().getService(name);
290             group.addService(service);
291         }
292     }
293
294     private void setPluginLinks(GroupConcept group)
295     throws Exception JavaDoc
296     {
297         String JavaDoc groupKey = (String JavaDoc)mapping.get("name") + '=' + group.getName();
298         Attributes attrs = pluginContext.getAttributes(groupKey);
299
300         Attribute attr = attrs.get((String JavaDoc)mapping.get("member"));
301         NamingEnumeration list = attr.getAll();
302                 
303         while(list.hasMore())
304         {
305             String JavaDoc name = (String JavaDoc)list.next();
306             name = name.substring(name.indexOf('=')+1);
307             if(name.equals("null"))
308                 continue;
309             
310             PluginConcept plugin = store.getPluginStore().getPlugin(name);
311             group.addPlugin(plugin);
312         }
313     }
314
315     private void setUserLinks(GroupConcept group)
316     throws Exception JavaDoc
317     {
318         String JavaDoc groupKey = (String JavaDoc)mapping.get("name") + '=' + group.getName();
319         Attributes attrs = userContext.getAttributes(groupKey);
320
321         Attribute attr = attrs.get((String JavaDoc)mapping.get("member"));
322         NamingEnumeration list = attr.getAll();
323                 
324         while(list.hasMore())
325         {
326             String JavaDoc name = (String JavaDoc)list.next();
327             name = name.substring(name.indexOf('=')+1);
328             if(name.equals("null"))
329                 continue;
330             
331             UserConcept user = store.getUserStore().getUser(name);
332             group.addUser(user);
333         }
334     }
335
336     private void setGroupLinks(GroupConcept group)
337     throws Exception JavaDoc
338     {
339         String JavaDoc groupKey = (String JavaDoc)mapping.get("name") + '=' + group.getName();
340         Attributes attrs = groupContext.getAttributes(groupKey);
341
342         Attribute attr = attrs.get((String JavaDoc)mapping.get("member"));
343         NamingEnumeration list = attr.getAll();
344                 
345         while(list.hasMore())
346         {
347             String JavaDoc name = (String JavaDoc)list.next();
348             name = name.substring(name.indexOf('=')+1);
349             if(name.equals("null"))
350                 continue;
351             
352             GroupConcept parent = store.getGroupStore().getGroup(name);
353             group.addParent(parent);
354         }
355     }
356     
357     private void removeGroupOnly(GroupConcept group)
358     throws Exception JavaDoc
359     {
360         //delete group & group links
361
groupContext.destroySubcontext((String JavaDoc)mapping.get("name") + '=' + group.getName());
362         
363         //delete user links
364
userContext.destroySubcontext((String JavaDoc)mapping.get("name") + '=' + group.getName());
365         
366         //delete services links
367
serviceContext.destroySubcontext((String JavaDoc)mapping.get("name") + '=' + group.getName());
368         
369         //delete plugins links
370
pluginContext.destroySubcontext((String JavaDoc)mapping.get("name") + '=' + group.getName());
371     }
372     
373     private BasicAttributes getBasicAttributes(GroupConcept group)
374     {
375         BasicAttributes attrs = new BasicAttributes();
376         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("name"), group.getName()));
377         if(group.getDescription() != null && group.getDescription().length() > 0)
378             attrs.put(new BasicAttribute((String JavaDoc)mapping.get("description"), group.getDescription()));
379
380         //other attributes
381
Iterator keys = attributes.keySet().iterator();
382         while(keys.hasNext())
383         {
384             String JavaDoc key = (String JavaDoc)keys.next();
385             attrs.put(new BasicAttribute(key, attributes.get(key)));
386         }
387         
388         return attrs;
389     }
390 }
Popular Tags