KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > sync > ldap > worker > CreateMissingUsers


1 package de.webman.sync.ldap.worker;
2
3 import de.webman.sync.Worker;
4 import de.webman.sync.ACLAdaptor;
5 import de.webman.sync.SyncException;
6 import de.webman.sync.ldap.LDAPAdaptor;
7 import java.util.List JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Enumeration JavaDoc;
10 import java.util.HashSet JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import org.apache.log4j.Category;
14 import de.webman.acl.UserFactory;
15 import de.webman.acl.Profile;
16 import de.webman.acl.ProfileFactory;
17 import com.teamkonzept.lib.TKException;
18 import com.teamkonzept.lib.TKVector;
19
20
21 /**
22  * Gets a list of all users in ldap and creates all users, missing in the
23  * webman system, assign the appropriate webmangroups and properties.
24  *
25  * @author <a HREF="mailto:gregor@webman.de">Gregor Klinke</a>
26  * @version $Revision: 1.2 $
27  **/

28 public class CreateMissingUsers
29     implements Worker
30 {
31     /* $Id: CreateMissingUsers.java,v 1.2 2002/04/12 14:56:02 gregor Exp $ */
32
33     /**
34      * logging facility
35      **/

36     public static Category cat = Category.getInstance(CreateMissingUsers.class);
37
38     /**
39      * constants for the statistics message counting
40      **/

41     private static final int STAT_NEW = 1;
42
43     /**
44      * constants for the statistics message counting
45      **/

46     private static final int STAT_CREATED = 2;
47
48     /**
49      * constants for the statistics message counting
50      **/

51     private static final int STAT_WARNING = 3;
52
53     /**
54      * constants for the statistics message counting
55      **/

56     private static final int STAT_IGNORED = 4;
57
58     /**
59      * constants for the statistics message counting
60      **/

61     private static final int STAT_FAILED = 5;
62
63     /**
64      * constants for the statistics message counting
65      **/

66     private static final int STAT_SLOTS = 6;
67     
68     /**
69      * the main start method for this worker
70      * @param adaptor the access control gate to use
71      * @throws SyncException a fatal error occured
72      **/

73     public void run(ACLAdaptor adaptor)
74         throws SyncException
75     {
76         cat.debug("synchronize for new users");
77
78         HashMap JavaDoc groups = null;
79         try {
80             /* get a list of all groups in the system */
81             TKVector pfvect = ProfileFactory.getInstance().getProfiles();
82             groups = new HashMap JavaDoc(pfvect.size());
83             
84             /* copy all profiles into a hash table for faster lookup */
85             for (Enumeration JavaDoc en = pfvect.elements(); en.hasMoreElements(); ) {
86                 Profile p = (Profile)en.nextElement();
87                 
88                 if (p.isProfile())
89                     groups.put(p.getLogin(), p);
90             }
91         }
92         catch (Exception JavaDoc e) {
93             throw new SyncException(e);
94         }
95         
96         HashSet JavaDoc wmlogins = null;
97         
98         /* request a list of known webman users */
99         try {
100             TKVector webman_users = UserFactory.getInstance().getUsers();
101             wmlogins = new HashSet JavaDoc(webman_users.size());
102             
103             /* put all logins from webman_users in a hash set, so we can
104                easily (and fast check if any user is missing!) */

105             for (Enumeration JavaDoc en = webman_users.elements(); en.hasMoreElements(); ) {
106                 de.webman.acl.User wmu = (de.webman.acl.User)en.nextElement();
107                 
108                 wmlogins.add(wmu.getLogin());
109             }
110         }
111         catch (TKException te) {
112             throw new SyncException(te);
113         }
114         
115         List JavaDoc ldap_users = adaptor.getAllUsers();
116         int[] statistics = new int[STAT_SLOTS]; // statistics
117
for (Iterator JavaDoc it = ldap_users.iterator(); it.hasNext(); ) {
118             de.webman.sync.User ldu = (de.webman.sync.User)it.next();
119             
120             if (wmlogins.contains(ldu.getWebmanName()))
121                 cat.debug ("user found in webman: '" + ldu.getWebmanName() + "'");
122             else {
123                 statistics[STAT_NEW]++;
124                 /* ignore webman users, if they are listed */
125                 if ((adaptor instanceof LDAPAdaptor) &&
126                     ((LDAPAdaptor)adaptor).ignoreUsers().contains(ldu.getWebmanName())) {
127                     statistics[STAT_IGNORED]++;
128                 }
129                 else if (createNewUser(ldu, groups, statistics))
130                     statistics[STAT_CREATED]++;
131             }
132         }
133
134         cat.info("synchronize for new users successfully; STATISTICS:\n" +
135                  ldap_users.size() + " found in LDAP system,\n" +
136                  wmlogins.size() + " found in webman,\n" +
137                  statistics[STAT_NEW] + " to allocated new,\n" +
138                  statistics[STAT_CREATED] + " created successfully\n" +
139                  statistics[STAT_FAILED] + " user creations failed,\n" +
140                  statistics[STAT_WARNING] + " group assignment warnings,\n" +
141                  statistics[STAT_IGNORED] + " users ignored");
142     }
143     
144
145     /**
146      * creates a new user in webman
147      * @param user the ldap user representation
148      * @param groups the dictionary with preloaded/cached groups in webman
149      * @param statistics statistics counting messages
150      * @return <code>true</code> if creating the user was successfull.
151      **/

152     private boolean createNewUser(de.webman.sync.User user, Map JavaDoc groups,
153                                   int[] statistics) {
154         cat.info ("NEW USER: '" + user.getWebmanName() + "'");
155         
156         try {
157             de.webman.acl.User wmu = null;
158         
159             wmu = UserFactory.getInstance().createUser(user.getWebmanName(),
160                                                        user.getDisplayName());
161             
162             /* iterate on the assigned webman groups from the ldap sync
163                user, and assign the user to those groups (but don't forget
164                to check if those groups are available in webman!) */

165             for (Iterator JavaDoc it = user.getGroups().iterator(); it.hasNext(); ) {
166                 String JavaDoc ldapgroup = (String JavaDoc)it.next();
167                 
168                 if (groups.containsKey(ldapgroup)) {
169                     assignUserToGroup((Profile)groups.get(ldapgroup), wmu);
170                 }
171                 else {
172                     cat.warn("unknown webman-group found in LDAP: '" + ldapgroup + "' " +
173                              "(user-dn: '" + user.getID() + "')");
174                     statistics[STAT_WARNING] ++;
175                 }
176             }
177         }
178         catch (TKException tke) {
179             cat.error("creation of new users '" + user.getWebmanName() + "' [='" +
180                       user.getID() + "'] failed (" + tke + ")");
181             statistics[STAT_FAILED]++;
182         }
183         
184         return true;
185     }
186         
187
188     private boolean assignUserToGroup(Profile group, de.webman.acl.Login wmuser)
189     {
190         try {
191             cat.info("assign user '" + wmuser.getLogin() + "' to group '" +
192                      group.getLogin() + "' ");
193             group.addChild(wmuser);
194             ProfileFactory.getInstance().modifyProfile(group);
195             return true;
196         }
197         catch (Exception JavaDoc e) {
198             cat.warn("can't assign user '" + wmuser.getLogin() + "' to group '" +
199                      group.getLogin() + "'");
200             return false;
201         }
202     }
203
204 }
205
Popular Tags