KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > acl > LoginFactory


1 package de.webman.acl;
2
3 import com.teamkonzept.lib.ConfigurationManager;
4 import com.teamkonzept.lib.ErrorCodes;
5 import com.teamkonzept.lib.PropertyManager;
6 import com.teamkonzept.lib.TKConfigurationException;
7 import com.teamkonzept.lib.TKException;
8 import com.teamkonzept.lib.TKReg;
9 import com.teamkonzept.lib.TKVector;
10 import de.webman.acl.db.*;
11 import de.webman.acl.resolver.ResolverFactory;
12 import com.teamkonzept.webman.mainint.events.TKUserException;
13 import com.teamkonzept.webman.mainint.events.UserCodes;
14 import com.teamkonzept.webman.mainint.WebmanExceptionHandler;
15
16 /**
17  * Factory for login, i.e. user or profile objects.
18  *
19  * @version 1.0
20  * @since 1.0
21  * @author © 2001 Webman AG
22  */

23 public class LoginFactory
24     extends ObjectFactoryBase
25     implements ObjectFactory
26 {
27
28     // $Header: /cvsroot/webman-cms/source/webman/de/webman/acl/LoginFactory.java,v 1.1.6.1 2002/05/30 10:23:54 uli Exp $
29

30     // Constants
31

32     /**
33      * Singleton instance.
34      */

35     private static LoginFactory SINGLETON = null;
36
37
38     // Attributes
39

40     /**
41      * Regular expression for valid logins.
42      */

43     private String JavaDoc validLogins = null;
44
45
46     // Constructors
47

48     /**
49      * Inhibits instantiation from outside.
50      */

51     private LoginFactory ()
52     {
53         super();
54     }
55
56
57     // Instance
58

59     /**
60      * Returns the singleton instance of the factory.
61      *
62      * @return the singleton instance of the factory.
63      * @exception com.teamkonzept.lib.TKException if an error occured during initialization.
64      */

65     public static synchronized final LoginFactory getInstance ()
66         throws TKException
67     {
68         if (SINGLETON == null)
69         {
70             SINGLETON = new LoginFactory();
71             SINGLETON.configurationChanged();
72             ConfigurationManager.getInstance()
73                                 .registerConfigurationListener(SINGLETON,
74                                                                PROPERTY_GROUP_NAME);
75         }
76
77         return SINGLETON;
78     }
79
80
81     // Method implementations
82

83     /**
84      * Returns the login object database interface.
85      *
86      * @return the login object database interface.
87      */

88     public final ObjectDBInterface getDBInterface ()
89     {
90         return LoginDBInterface.getInstance();
91     }
92
93     /**
94      * Returns a login data wrapper.
95      *
96      * @param id the ID of the login.
97      * @return a login data wrapper.
98      */

99     public final ObjectDBData getDBData (Integer JavaDoc id)
100     {
101         return new LoginDBData(id, null, null, null);
102     }
103
104     /**
105      * Returns a login data wrapper.
106      *
107      * @param object the login.
108      * @return a login data wrapper.
109      */

110     public final ObjectDBData getDBData (WMObject object)
111     {
112         return new LoginDBData((Login) object);
113     }
114
115     /**
116      * Builds a concrete login object, i.e. user or profile object.
117      *
118      * @param data the initial login data.
119      * @return a concrete user or profile object.
120      */

121     public final WMObject buildObject (ObjectDBData data)
122     {
123         return ((LoginDBData) data).isUser()
124                                      ? (Login) new User((LoginDBData) data)
125                                      : (Login) new Profile((LoginDBData) data);
126     }
127
128
129     // Overridden methods
130

131     /**
132      * Informs the factory about changes in its configuration.
133      *
134      * @exception com.teamkonzept.lib.TKException if an error occured during configuration.
135      */

136     public synchronized final void configurationChanged ()
137         throws TKException
138     {
139         // Common configuration value loading.
140
super.configurationChanged();
141
142         // Special configuration value loading.
143
try
144         {
145             // Obtain property manager.
146
PropertyManager manager = PropertyManager.getPropertyManager(PROPERTY_GROUP_NAME);
147
148             // Get validity property.
149
this.validLogins = manager.getValue(PROPERTY_VALID_LOGINS, DEFAULT_VALID_LOGINS);
150         }
151         catch (TKConfigurationException tkce)
152         {
153             // Fall back to default.
154
this.validLogins = DEFAULT_VALID_LOGINS;
155         }
156     }
157
158     /**
159      * Modifies the given object.
160      *
161      * @param object the object.
162      * @exception com.teamkonzept.lib.TKException if an error occured during object modification.
163      */

164     public final void modifyObject (WMObject object)
165         throws TKException
166     {
167         try
168         {
169             // Downcast for convenience.
170
Login login = (Login) object;
171
172             // Intercept special case for parent order modification.
173
if (login.isModifiedParents())
174             {
175                 // Create appropriate data.
176
ObjectDBData data = getDBData(login);
177                 data.setQuery(LoginDBInterface.WM_PROFILE_UPDATE);
178                 data.setPrototype(new ProfileCollectionDBData(login.getID()));
179                 data.setCollection(login.getParentOrder());
180
181                 // Database update.
182
getDBInterface().updateDependent(data);
183
184                 // Remove update information.
185
login.updatedParents();
186             }
187
188             // Call generic modification method in *every* case.
189
super.modifyObject(object);
190         }
191         catch (Exception JavaDoc x)
192         {
193             throw WebmanExceptionHandler.getException(x);
194         }
195     }
196
197     /**
198      * Deletes the given object.
199      *
200      * @param object the object.
201      * @exception com.teamkonzept.lib.TKException if an error occured during object deletion.
202      */

203     public void deleteObject (WMObject object)
204         throws TKException
205     {
206         ResolverFactory.getInstance().removeResolver((Login) object);
207
208         super.deleteObject(object);
209     }
210
211
212     // Convenience methods
213

214     /**
215      * Retrieves all known login objects.
216      *
217      * @return all known login objects.
218      * @exception com.teamkonzept.lib.TKException if an error occured during login object retrieval.
219      */

220     public final TKVector getLogins ()
221         throws TKException
222     {
223         return getObjects();
224     }
225
226
227     /**
228      * Retrieves all login objects referencing the given login object.
229      *
230      * @param login the login object.
231      * @return all login objects referencing the given login object.
232      * @exception com.teamkonzept.lib.TKException if an error occured during login object retrieval.
233      */

234     public final TKVector getLogins (Login login)
235         throws TKException
236     {
237         TKVector objects = null;
238
239         try
240         {
241             // Database lookup.
242
objects = getObjects(getLoginIDs(login));
243         }
244         catch (Exception JavaDoc x)
245         {
246             throw WebmanExceptionHandler.getException(x);
247         }
248
249         return objects;
250     }
251
252     /**
253      * Retrieves the specified login object.
254      *
255      * @param id the ID of the login object.
256      * @return the specified login object.
257      * @exception com.teamkonzept.lib.TKException if an error occured during login object retrieval.
258      */

259     public final Login getLogin (Integer JavaDoc id)
260         throws TKException
261     {
262         return (Login) getObject(id);
263     }
264
265     /**
266      * Retrieves the specified login object.
267      *
268      * @param login the login of the login object.
269      * @return the specified login object.
270      * @exception com.teamkonzept.lib.TKException if an error occured during login object retrieval.
271      */

272     public final Login getLogin (String JavaDoc login)
273         throws TKException
274     {
275         Login object = null;
276
277         try
278         {
279             // Create appropriate data.
280
LoginDBData data = new LoginDBData(null, login, null, null);
281             data.setQuery(LoginDBInterface.WM_USER_SELECT_BY_LOGIN);
282             data.setPrototype(new ObjectCollectionDBData(null,
283                                                            null,
284                                                            LoginDBInterface.PRIMARY_KEY_NAME,
285                                                            null));
286
287             // Database lookup.
288
TKVector id = getObjectIDs(data);
289
290             if (id != null && id.size() == 1)
291             {
292                 object = (Login) getObject((Integer JavaDoc) id.firstElement());
293             }
294             else
295             {
296                 throw new TKUserException("The user or group '" + login + "' is unknown.",
297                                           UserCodes.LOGIN_UNKNOWN,
298                                           ErrorCodes.USER_SEVERITY,
299                                           true,
300                                           new Object JavaDoc[]{login},
301                                           null);
302             }
303         }
304         catch (Exception JavaDoc x)
305         {
306             throw WebmanExceptionHandler.getException(x);
307         }
308
309         return object;
310     }
311
312     /**
313      * Retrieves the IDs of all login objects referencing the given login object.
314      *
315      * @param login the login object.
316      * @return the IDs of all login objects referencing the given login object.
317      * @exception com.teamkonzept.lib.TKException if an error occured during login object retrieval.
318      */

319     public final TKVector getLoginIDs (Login login)
320         throws TKException
321     {
322         TKVector proxies = null;
323
324         try
325         {
326             // Create appropriate data.
327
LoginDBData data = new LoginDBData(null, null, null, null);
328             data.setQuery(LoginDBInterface.WM_PROFILE_SELECT_BY_PROFILE);
329             data.setPrototype(new ObjectCollectionDBData(LoginDBInterface.DEPENDENT_KEY_NAME,
330                                                            login.getID(),
331                                                            LoginDBInterface.PRIMARY_KEY_NAME,
332                                                            null));
333
334             // Database lookup.
335
proxies = getObjectIDs(data);
336         }
337         catch (Exception JavaDoc x)
338         {
339             throw WebmanExceptionHandler.getException(x);
340         }
341
342         return proxies;
343     }
344
345     /**
346      * Modifies the given login object.
347      *
348      * @param login the login object to be modified.
349      * @exception com.teamkonzept.lib.TKException if an error occured during login object modification.
350      */

351     public final void modifyLogin (Login login)
352         throws TKException
353     {
354         modifyObject(login);
355     }
356
357     /**
358      * Deletes the given login object.
359      *
360      * @param login the login object to be deleted.
361      * @exception com.teamkonzept.lib.TKException if an error occured during login object deletion.
362      */

363     public final void deleteLogin (Login login)
364         throws TKException
365     {
366         deleteObject(login);
367     }
368
369     /**
370      * Checks wether the given login string is valid.
371      *
372      * @param login the login string to be checked.
373      * @exception com.teamkonzept.lib.TKException if the login string is invalid.
374      */

375     public final void checkLogin (String JavaDoc login)
376         throws TKException
377     {
378         try
379         {
380             if (login == null ||
381                 ! TKReg.getMatcher().matches(login, TKReg.getCompiler().compile(this.validLogins)))
382             {
383                 throw new TKUserException("The login '" + login + "' is not valid.",
384                                           UserCodes.INVALID_LOGIN_TOKEN,
385                                           ErrorCodes.USER_SEVERITY,
386                                           true,
387                                           new Object JavaDoc[]{login},
388                                           null);
389             }
390         }
391         catch (Exception JavaDoc x)
392         {
393             throw WebmanExceptionHandler.getException(x);
394         }
395     }
396
397 }
398
Popular Tags