KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > organization > ldap > MembershipLDAPHandler


1 /**
2  * Copyright 2001-2004 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.organization.ldap;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Vector JavaDoc;
12
13 import net.sf.hibernate.Session;
14 import netscape.ldap.LDAPAttribute;
15 import netscape.ldap.LDAPConnection;
16 import netscape.ldap.LDAPEntry;
17 import netscape.ldap.LDAPModification;
18 import netscape.ldap.LDAPModificationSet;
19 import netscape.ldap.LDAPSearchResults;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.exoplatform.services.database.HibernateService;
23 import org.exoplatform.services.database.XResources;
24 import org.exoplatform.services.ldap.LDAPService;
25 import org.exoplatform.services.ldap.LDAPServiceContainer;
26 import org.exoplatform.services.organization.Group;
27 import org.exoplatform.services.organization.Membership;
28 import org.exoplatform.services.organization.MembershipEventListener;
29 import org.exoplatform.services.organization.User;
30 import org.exoplatform.services.organization.impl.MembershipImpl;
31 /**
32  * Created by the eXo platform team
33  * User: Daniel Summer
34  * Date: 28 august 2004
35  *
36  */

37 public class MembershipLDAPHandler extends BaseLDAPHandler {
38     private final String JavaDoc BASEURL = OrganizationLDAPConfig.BASE_URL;
39     private final String JavaDoc PORTALURL = OrganizationLDAPConfig.PORTAL_URL;
40     private final String JavaDoc ROLESURL = OrganizationLDAPConfig.ROLES_URL;
41     private final String JavaDoc USERSURL = OrganizationLDAPConfig.USERS_URL;
42     private LDAPServiceContainer serviceContainer_;
43     private HibernateService hService_;
44     private List JavaDoc listeners_;
45
46     public MembershipLDAPHandler(
47         LDAPServiceContainer serviceContainer,
48         HibernateService hService) {
49         serviceContainer_ = serviceContainer;
50         hService_ = hService;
51         listeners_ = new ArrayList JavaDoc(5);
52     }
53
54     public void addMembershipEventListener(MembershipEventListener listener) {
55         listeners_.add(listener);
56     }
57
58     public void createMembership(Membership m) throws Exception JavaDoc {
59         LDAPService service = null;
60         Session session = null;
61         try {
62             service = serviceContainer_.createLDAPService();
63             session = hService_.openSession();
64             String JavaDoc MY_FILTER = "uid=" + m.getUserName();
65             String JavaDoc MY_SEARCHBASE = USERSURL;
66             LDAPSearchResults res =
67                 service.search(
68                     MY_SEARCHBASE,
69                     LDAPConnection.SCOPE_SUB,
70                     MY_FILTER,
71                     null,
72                     false);
73             List JavaDoc l = getUtil(res);
74             MembershipImpl mi = (MembershipImpl) m;
75             StringBuffer JavaDoc b = new StringBuffer JavaDoc();
76             b
77                 .append(mi.getUserName())
78                 .append(",")
79                 .append(mi.getMembershipType())
80                 .append(",")
81                 .append(mi.getGroupId());
82             LDAPEntry findEntry = (LDAPEntry) l.get(0);
83             LDAPModificationSet mods = new LDAPModificationSet();
84             LDAPAttribute attrRole =
85                 new LDAPAttribute("membership", b.toString());
86             mods.add(LDAPModification.ADD, attrRole);
87
88             preSave(m, true, session);
89             service.modify(findEntry.getDN(), mods);
90             postSave(m, true, session);
91             session.flush();
92         } finally {
93             serviceContainer_.closeLDAPService(service);
94             hService_.closeSession(session);
95         }
96     }
97
98     public void createMembershipEntries(Collection JavaDoc c, Session session)
99         throws Exception JavaDoc {
100         LDAPService service = null;
101         service = serviceContainer_.createLDAPService();
102         Iterator JavaDoc i = c.iterator();
103         while (i.hasNext()) {
104             Membership impl = (Membership) i.next();
105             String JavaDoc MY_FILTER = "uid=" + impl.getUserName();
106             String JavaDoc MY_SEARCHBASE = USERSURL;
107
108             LDAPSearchResults res =
109                 service.search(
110                     MY_SEARCHBASE,
111                     LDAPConnection.SCOPE_SUB,
112                     MY_FILTER,
113                     null,
114                     false);
115             List JavaDoc l = getUtil(res);
116
117             StringBuffer JavaDoc b = new StringBuffer JavaDoc();
118             b
119                 .append(impl.getUserName())
120                 .append(",")
121                 .append(impl.getMembershipType())
122                 .append(",")
123                 .append(impl.getGroupId());
124             LDAPEntry findEntry = (LDAPEntry) l.get(0);
125             LDAPModificationSet mods = new LDAPModificationSet();
126             LDAPAttribute attrRole =
127                 new LDAPAttribute("membership", b.toString());
128             mods.add(LDAPModification.ADD, attrRole);
129             service.modify(findEntry.getDN(), mods);
130         }
131         serviceContainer_.closeLDAPService(service);
132     }
133
134     public void linkMembership(String JavaDoc userName, Group g, Membership m)
135         throws Exception JavaDoc {
136         Group group = null;
137         User user = null;
138         Session session = null;
139         LDAPService service = null;
140
141         try {
142             session = hService_.openSession();
143             service = serviceContainer_.createLDAPService();
144             MembershipImpl impl = (MembershipImpl) m;
145             String JavaDoc MY_FILTER = "uid=" + userName;
146             String JavaDoc MY_SEARCHBASE = USERSURL;
147             LDAPSearchResults res =
148                 service.search(
149                     MY_SEARCHBASE,
150                     LDAPConnection.SCOPE_SUB,
151                     MY_FILTER,
152                     null,
153                     false);
154             List JavaDoc l = getUtil(res);
155
156             boolean isNew = true;
157
158             LDAPEntry findEntry = (LDAPEntry) l.get(0);
159             StringBuffer JavaDoc b = new StringBuffer JavaDoc();
160             b
161                 .append(userName)
162                 .append(",")
163                 .append(m.getMembershipType())
164                 .append(",")
165                 .append(g.getId());
166             String JavaDoc membership = b.toString();
167
168             Vector JavaDoc memberships =
169                 getutil("membership", findEntry.getAttributeSet(), false);
170             for (Iterator JavaDoc i = memberships.iterator(); i.hasNext();) {
171                 String JavaDoc s = (String JavaDoc) i.next();
172                 if (s.equals(membership)) {
173                     isNew = false;
174                 }
175             }
176
177             if (isNew) {
178
179                 LDAPModificationSet mods = new LDAPModificationSet();
180                 LDAPAttribute attrMembership =
181                     new LDAPAttribute("membership", membership);
182                 mods.add(LDAPModification.ADD, attrMembership);
183
184                 preSave(m, isNew, session);
185                 service.modify(findEntry.getDN(), mods);
186                 postSave(m, isNew, session);
187                 session.flush();
188             }
189         } finally {
190             hService_.closeSession(session);
191             serviceContainer_.closeLDAPService(service);
192         }
193
194     }
195
196     public void saveMembership(Membership m) throws Exception JavaDoc {
197         LDAPService service = null;
198         Session session = null;
199         try {
200             service = serviceContainer_.createLDAPService();
201             session = hService_.openSession();
202             String JavaDoc MY_FILTER = "membership=" + m.getId();
203             String JavaDoc MY_SEARCHBASE = USERSURL;
204             String JavaDoc ENTRYDN = MY_FILTER + MY_SEARCHBASE;
205             LDAPSearchResults res =
206                 service.search(
207                     MY_SEARCHBASE,
208                     LDAPConnection.SCOPE_SUB,
209                     MY_FILTER,
210                     null,
211                     false);
212             List JavaDoc l = getUtil(res);
213             MembershipImpl mi = (MembershipImpl) m;
214             StringBuffer JavaDoc b = new StringBuffer JavaDoc();
215             b
216                 .append(mi.getUserName())
217                 .append(",")
218                 .append(mi.getMembershipType())
219                 .append(",")
220                 .append(mi.getGroupId());
221             LDAPEntry findEntry = (LDAPEntry) l.get(0);
222             String JavaDoc[] attrValues =
223                 (String JavaDoc[]) getutil("membership",
224                     findEntry.getAttributeSet(),
225                     false)
226                     .toArray();
227             for (int i = 0; i < attrValues.length; i++) {
228                 if (mi.getId().equals(attrValues[i])) {
229                     attrValues[i] = b.toString();
230                 }
231             }
232             LDAPModificationSet mods = new LDAPModificationSet();
233             mods.add(
234                 LDAPModification.REPLACE,
235                 new LDAPAttribute("membership", attrValues));
236
237             preSave(m, true, session);
238             service.modify(ENTRYDN, mods);
239             postSave(m, true, session);
240             session.flush();
241         } finally {
242             serviceContainer_.closeLDAPService(service);
243             hService_.closeSession(session);
244         }
245
246     }
247
248     public Membership removeMembership(String JavaDoc id) throws Exception JavaDoc {
249         LDAPService service = null;
250         Session session = null;
251         MembershipImpl m = new MembershipImpl();
252         try {
253             service = serviceContainer_.createLDAPService();
254             session = hService_.openSession();
255             String JavaDoc MY_FILTER = "membership=" + m.getId();
256             String JavaDoc MY_SEARCHBASE = USERSURL;
257             String JavaDoc ENTRYDN = MY_FILTER + MY_SEARCHBASE;
258             LDAPSearchResults res =
259                 service.search(
260                     MY_SEARCHBASE,
261                     LDAPConnection.SCOPE_SUB,
262                     MY_FILTER,
263                     null,
264                     false);
265             List JavaDoc l = getUtil(res);
266             LDAPEntry findEntry = (LDAPEntry) l.get(0);
267             Vector JavaDoc memberships =
268                 getutil("membership", findEntry.getAttributeSet(), false);
269
270             for (Iterator JavaDoc i = memberships.iterator(); i.hasNext();) {
271                 String JavaDoc s = (String JavaDoc) i.next();
272                 if (s.equals(id)) {
273                     m.setId(s);
274                     String JavaDoc[] membership = StringUtils.split(s, ",");
275                     m.setUserName(membership[0]);
276                     m.setMembershipType(membership[1]);
277                     m.setGroupId(membership[2]);
278                     LDAPModificationSet mods = new LDAPModificationSet();
279                     mods.add(
280                         LDAPModification.DELETE,
281                         new LDAPAttribute("membership", id));
282
283                     preDelete(m, session);
284                     service.modify(ENTRYDN, mods);
285                     postDelete(m, session);
286                     session.flush();
287                 }
288             }
289
290         } finally {
291             serviceContainer_.closeLDAPService(service);
292             hService_.closeSession(session);
293         }
294         return m;
295     }
296
297     public Membership findMembershipByUserGroupAndType(
298         String JavaDoc userName,
299         String JavaDoc groupId,
300         String JavaDoc type)
301         throws Exception JavaDoc {
302         List JavaDoc memberships = new ArrayList JavaDoc();
303
304         LDAPService service = null;
305         Session session = null;
306         try {
307             service = serviceContainer_.createLDAPService();
308             session = hService_.openSession();
309             String JavaDoc MY_FILTER = "uid=" + userName;
310             String JavaDoc MY_SEARCHBASE = USERSURL;
311
312             LDAPSearchResults res =
313                 service.search(
314                     MY_SEARCHBASE,
315                     LDAPConnection.SCOPE_SUB,
316                     MY_FILTER,
317                     null,
318                     false);
319
320             while (res.hasMoreElements()) {
321                 LDAPEntry findEntry = res.next();
322
323                 String JavaDoc[] attrValues =
324                     (String JavaDoc[]) getutil("membership",
325                         findEntry.getAttributeSet(),
326                         false)
327                         .toArray();
328                 for (int i = 0; i < attrValues.length; i++) {
329                     String JavaDoc[] membership = StringUtils.split(attrValues[i], ",");
330                     if (membership[3].equals(groupId)
331                         && membership[1].equals(groupId)) {
332                         MembershipImpl mi = new MembershipImpl();
333                         mi.setId(attrValues[i]);
334                         mi.setUserName(membership[0]);
335                         mi.setMembershipType(membership[1]);
336                         mi.setGroupId(membership[2]);
337                         memberships.add(mi);
338                     }
339                 }
340             }
341         } finally {
342             serviceContainer_.closeLDAPService(service);
343             hService_.closeSession(session);
344         }
345
346         if (memberships.size() == 0) {
347             return null;
348         } else if (memberships.size() == 1) {
349             return (Membership) memberships.get(0);
350         } else {
351             throw new Exception JavaDoc(
352                 "Expect 0 or 1 membership but found" + memberships.size());
353         }
354     }
355
356     public Collection JavaDoc findMembershipsByUserAndGroup(
357         String JavaDoc userName,
358         String JavaDoc groupId)
359         throws Exception JavaDoc {
360         Collection JavaDoc memberships = new ArrayList JavaDoc();
361
362         LDAPService service = null;
363         Session session = null;
364         try {
365             service = serviceContainer_.createLDAPService();
366             session = hService_.openSession();
367             String JavaDoc MY_FILTER = "uid=" + userName;
368             String JavaDoc MY_SEARCHBASE = USERSURL;
369
370             LDAPSearchResults res =
371                 service.search(
372                     MY_SEARCHBASE,
373                     LDAPConnection.SCOPE_SUB,
374                     MY_FILTER,
375                     null,
376                     false);
377             List JavaDoc l = getUtil(res);
378             LDAPEntry findEntry = (LDAPEntry) l.get(0);
379
380             Vector JavaDoc attrValues =
381                 getutil("membership", findEntry.getAttributeSet(), false);
382             for (Iterator JavaDoc i = attrValues.iterator(); i.hasNext();) {
383                 String JavaDoc s = (String JavaDoc) i.next();
384                 String JavaDoc[] membership = StringUtils.split(s, ",");
385                 if (membership[3].equals(groupId)) {
386                     MembershipImpl mi = new MembershipImpl();
387                     mi.setId(s);
388                     mi.setUserName(membership[0]);
389                     mi.setMembershipType(membership[1]);
390                     mi.setGroupId(membership[2]);
391                     memberships.add(mi);
392                 }
393             }
394
395         } finally {
396             serviceContainer_.closeLDAPService(service);
397             hService_.closeSession(session);
398         }
399         return memberships;
400     }
401
402     public Collection JavaDoc findMembershipsByUser(String JavaDoc userName) throws Exception JavaDoc {
403         List JavaDoc memberships = new ArrayList JavaDoc();
404         LDAPService service = null;
405         Session session = null;
406         try {
407             service = serviceContainer_.createLDAPService();
408             session = hService_.openSession();
409             String JavaDoc MY_FILTER = "uid=" + userName;
410             String JavaDoc MY_SEARCHBASE = USERSURL;
411
412             LDAPSearchResults res =
413                 service.search(
414                     MY_SEARCHBASE,
415                     LDAPConnection.SCOPE_SUB,
416                     MY_FILTER,
417                     null,
418                     false);
419             List JavaDoc l = getUtil(res);
420
421             while (res.hasMoreElements()) {
422                 LDAPEntry findEntry = res.next();
423
424                 String JavaDoc[] attrValues =
425                     (String JavaDoc[]) getutil("membership",
426                         findEntry.getAttributeSet(),
427                         false)
428                         .toArray();
429                 for (int i = 0; i < attrValues.length; i++) {
430                     String JavaDoc[] membership = StringUtils.split(attrValues[i], ",");
431                     MembershipImpl mi = new MembershipImpl();
432                     mi.setId(attrValues[i]);
433                     mi.setUserName(membership[0]);
434                     mi.setMembershipType(membership[1]);
435                     mi.setGroupId(membership[2]);
436                     memberships.add(mi);
437                 }
438             }
439         } finally {
440             serviceContainer_.closeLDAPService(service);
441             hService_.closeSession(session);
442         }
443         return memberships;
444     }
445
446     static void removeMembershipEntriesOfUser(
447         String JavaDoc userName,
448         Session session,
449         LDAPService service)
450         throws Exception JavaDoc {
451
452         String JavaDoc MY_FILTER = "uid=" + userName;
453         String JavaDoc MY_SEARCHBASE = OrganizationLDAPConfig.USERS_URL;
454
455         LDAPSearchResults res =
456             service.search(
457                 MY_SEARCHBASE,
458                 LDAPConnection.SCOPE_SUB,
459                 MY_FILTER,
460                 null,
461                 false);
462
463         LDAPEntry findEntry = (LDAPEntry) res.next();
464         LDAPModificationSet mods = new LDAPModificationSet();
465         LDAPAttribute attrMembership = new LDAPAttribute("membership");
466         mods.add(LDAPModification.DELETE, attrMembership);
467         service.modify(findEntry.getDN(), mods);
468
469     }
470
471     static void removeMembershipEntriesOfGroup(
472         Group group,
473         Session session,
474         LDAPService service)
475         throws Exception JavaDoc {
476         String JavaDoc MY_FILTER = "membership=" + "*" + group.getId();
477         String JavaDoc MY_SEARCHBASE = OrganizationLDAPConfig.USERS_URL;
478
479         LDAPSearchResults res =
480             service.search(
481                 MY_SEARCHBASE,
482                 LDAPConnection.SCOPE_SUB,
483                 MY_FILTER,
484                 null,
485                 false);
486
487         LDAPEntry findEntry = (LDAPEntry) res.next();
488         LDAPModificationSet mods = new LDAPModificationSet();
489         LDAPAttribute attrMembership = new LDAPAttribute("membership");
490         mods.add(LDAPModification.DELETE, attrMembership);
491         service.modify(findEntry.getDN(), mods);
492     }
493
494     Collection JavaDoc findMembershipsByUser(String JavaDoc userName, Session session)
495         throws Exception JavaDoc {
496         List JavaDoc memberships = new ArrayList JavaDoc();
497         LDAPService service = null;
498
499         try {
500
501             String JavaDoc MY_FILTER = "uid=" + userName;
502             String JavaDoc MY_SEARCHBASE = USERSURL;
503
504             LDAPSearchResults res =
505                 service.search(
506                     MY_SEARCHBASE,
507                     LDAPConnection.SCOPE_SUB,
508                     MY_FILTER,
509                     null,
510                     false);
511             List JavaDoc l = getUtil(res);
512
513             while (res.hasMoreElements()) {
514                 LDAPEntry findEntry = res.next();
515
516                 String JavaDoc[] attrValues =
517                     (String JavaDoc[]) getutil("membership",
518                         findEntry.getAttributeSet(),
519                         false)
520                         .toArray();
521                 for (int i = 0; i < attrValues.length; i++) {
522                     String JavaDoc[] membership = StringUtils.split(attrValues[i], ",");
523                     MembershipImpl mi = new MembershipImpl();
524                     mi.setId(attrValues[i]);
525                     mi.setUserName(membership[0]);
526                     mi.setMembershipType(membership[1]);
527                     mi.setGroupId(membership[2]);
528                     memberships.add(mi);
529                 }
530             }
531         } finally {
532             serviceContainer_.closeLDAPService(service);
533         }
534         return memberships;
535     }
536
537     public Collection JavaDoc findMembershipsByGroup(Group group) throws Exception JavaDoc {
538         Collection JavaDoc memberships = null;
539
540         LDAPService service = null;
541         Session session = null;
542         try {
543             service = serviceContainer_.createLDAPService();
544             session = hService_.openSession();
545             String JavaDoc MY_FILTER = "membership=" + "*" + group;
546             String JavaDoc MY_SEARCHBASE = USERSURL;
547
548             LDAPSearchResults res =
549                 service.search(
550                     MY_SEARCHBASE,
551                     LDAPConnection.SCOPE_SUB,
552                     MY_FILTER,
553                     null,
554                     false);
555             List JavaDoc l = getUtil(res);
556
557             while (res.hasMoreElements()) {
558                 LDAPEntry findEntry = res.next();
559
560                 String JavaDoc[] attrValues =
561                     (String JavaDoc[]) getutil("membership",
562                         findEntry.getAttributeSet(),
563                         false)
564                         .toArray();
565                 for (int i = 0; i < attrValues.length; i++) {
566                     String JavaDoc[] membership = StringUtils.split(attrValues[i], ",");
567                     MembershipImpl mi = new MembershipImpl();
568                     mi.setId(attrValues[i]);
569                     mi.setUserName(membership[0]);
570                     mi.setMembershipType(membership[1]);
571                     mi.setGroupId(membership[2]);
572                     memberships.add(mi);
573                 }
574             }
575         } finally {
576             serviceContainer_.closeLDAPService(service);
577             hService_.closeSession(session);
578         }
579         return memberships;
580
581     }
582
583     public Membership findMembership(String JavaDoc id) throws Exception JavaDoc {
584         MembershipImpl membership = null;
585         LDAPService service = null;
586
587         try {
588
589             String JavaDoc MY_FILTER = "membership=" + id;
590             String JavaDoc MY_SEARCHBASE = USERSURL;
591
592             LDAPSearchResults res =
593                 service.search(
594                     MY_SEARCHBASE,
595                     LDAPConnection.SCOPE_SUB,
596                     MY_FILTER,
597                     null,
598                     false);
599             List JavaDoc l = getUtil(res);
600
601             LDAPEntry findEntry = (LDAPEntry) l.get(0);
602             String JavaDoc[] attrValues =
603                 (String JavaDoc[]) getutil("membership",
604                     findEntry.getAttributeSet(),
605                     false)
606                     .toArray();
607             for (int i = 0; i < attrValues.length; i++) {
608                 if (id.equals(attrValues[i])) {
609                     String JavaDoc[] array = StringUtils.split(attrValues[i], ",");
610                     membership = new MembershipImpl();
611                     membership.setId(attrValues[i]);
612                     membership.setUserName(array[0]);
613                     membership.setMembershipType(array[1]);
614                     membership.setGroupId(array[2]);
615
616                 }
617             }
618         } finally {
619             serviceContainer_.closeLDAPService(service);
620         }
621         return membership;
622     }
623
624     private void preSave(Membership membership, boolean isNew, Session session)
625         throws Exception JavaDoc {
626         XResources xresources = new XResources();
627         xresources.addResource(Session.class, session);
628         for (int i = 0; i < listeners_.size(); i++) {
629             MembershipEventListener listener =
630                 (MembershipEventListener) listeners_.get(i);
631             listener.preSave(membership, isNew, xresources);
632         }
633     }
634
635     private void postSave(
636         Membership membership,
637         boolean isNew,
638         Session session)
639         throws Exception JavaDoc {
640         XResources xresources = new XResources();
641         xresources.addResource(Session.class, session);
642         for (int i = 0; i < listeners_.size(); i++) {
643             MembershipEventListener listener =
644                 (MembershipEventListener) listeners_.get(i);
645             listener.postSave(membership, isNew, xresources);
646         }
647     }
648
649     private void preDelete(Membership membership, Session session)
650         throws Exception JavaDoc {
651         XResources xresources = new XResources();
652         xresources.addResource(Session.class, session);
653         for (int i = 0; i < listeners_.size(); i++) {
654             MembershipEventListener listener =
655                 (MembershipEventListener) listeners_.get(i);
656             listener.preDelete(membership, xresources);
657         }
658     }
659
660     private void postDelete(Membership membership, Session session)
661         throws Exception JavaDoc {
662         XResources xresources = new XResources();
663         xresources.addResource(Session.class, session);
664         for (int i = 0; i < listeners_.size(); i++) {
665             MembershipEventListener listener =
666                 (MembershipEventListener) listeners_.get(i);
667             listener.postDelete(membership, xresources);
668         }
669     }
670 }
Popular Tags