KickJava   Java API By Example, From Geeks To Geeks.

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


1 package de.webman.acl;
2
3 import com.teamkonzept.lib.ConfigurationManager;
4 import com.teamkonzept.lib.TKException;
5 import com.teamkonzept.lib.TKVector;
6 import de.webman.acl.db.*;
7 import de.webman.acl.resolver.ResolverFactory;
8 import com.teamkonzept.webman.mainint.WebmanExceptionHandler;
9
10 /**
11  * Factory for policy objects.
12  *
13  * @version 1.0
14  * @since 1.0
15  * @author © 2001 Webman AG
16  */

17 public class PolicyFactory
18     extends ObjectFactoryBase
19     implements ObjectFactory
20 {
21
22     // $Header: /cvsroot/webman-cms/source/webman/de/webman/acl/PolicyFactory.java,v 1.1 2001/08/20 08:25:07 mischa Exp $
23

24     // Constants
25

26     /**
27      * Singleton instance.
28      */

29     private static PolicyFactory SINGLETON = null;
30
31
32     // Constructors
33

34     /**
35      * Inhibits instantiation from outside.
36      */

37     private PolicyFactory ()
38     {
39         super();
40     }
41
42
43     // Instance
44

45     /**
46      * Returns the singleton instance of the factory.
47      *
48      * @return the singleton instance of the factory.
49      * @exception com.teamkonzept.lib.TKException if an error occured during initialization.
50      */

51     public static synchronized final PolicyFactory getInstance ()
52         throws TKException
53     {
54         if (SINGLETON == null)
55         {
56             SINGLETON = new PolicyFactory();
57             SINGLETON.configurationChanged();
58             ConfigurationManager.getInstance()
59                                 .registerConfigurationListener(SINGLETON,
60                                                                PROPERTY_GROUP_NAME);
61         }
62
63         return SINGLETON;
64     }
65
66
67     // Method implementations
68

69     /**
70      * Returns the policy database interface.
71      *
72      * @return the policy database interface.
73      */

74     public final ObjectDBInterface getDBInterface ()
75     {
76         return PolicyDBInterface.getInstance();
77     }
78
79     /**
80      * Returns a policy data wrapper.
81      *
82      * @param id the ID of the policy.
83      * @return a policy data wrapper.
84      */

85     public final ObjectDBData getDBData (Integer JavaDoc id)
86     {
87         return new PolicyDBData(id, null, null, null, null, null, null);
88     }
89
90     /**
91      * Returns a policy data wrapper.
92      *
93      * @param object the policy.
94      * @return a policy data wrapper.
95      */

96     public final ObjectDBData getDBData (WMObject object)
97     {
98         return new PolicyDBData((Policy) object);
99     }
100
101     /**
102      * Builds a concrete policy object.
103      *
104      * @param data the initial policy data.
105      * @return a concrete policy object.
106      */

107     public final WMObject buildObject (ObjectDBData data)
108     {
109         return new Policy((PolicyDBData) data);
110     }
111
112
113     // Convenience methods
114

115     /**
116      * Retrieves the specified policy.
117      *
118      * @param id the ID of the policy.
119      * @return the specified policy.
120      * @exception com.teamkonzept.lib.TKException if an error occured during policy retrieval.
121      */

122     public final Policy getPolicy (Integer JavaDoc id)
123         throws TKException
124     {
125         return (Policy) getObject(id);
126     }
127
128     /**
129      * Retrieves all known policies.
130      *
131      * @return all known policies.
132      * @exception com.teamkonzept.lib.TKException if an error occured during policy retrieval.
133      */

134     public final TKVector getPolicies ()
135         throws TKException
136     {
137         return getObjects();
138     }
139
140     /**
141      * Retrieves all policy IDs referenced by the specified login.
142      *
143      * @param login the ID of the login (<I>required</I>).
144      * @param context the ID of the context (<I>optional</I>).
145      * @param type the object type (<I>optional</I>).
146      * @param reference the object reference (<I>optional</I>).
147      * @return all policy IDs referenced by the specified login.
148      * @exception com.teamkonzept.lib.TKException if an error occured during policy retrieval.
149      */

150     public final TKVector getPolicyProxies (Integer JavaDoc login,
151                                             Integer JavaDoc context,
152                                             Integer JavaDoc type,
153                                             Integer JavaDoc reference)
154         throws TKException
155     {
156         TKVector proxies = null;
157
158         try
159         {
160             // Create appropriate data.
161
PolicyDBData data = new PolicyDBData(null,
162                                                      login,
163                                                      null,
164                                                      context,
165                                                      type,
166                                                      reference,
167                                                      null);
168             // Choose appropriate query.
169
Class JavaDoc choice = PolicyDBInterface.WM_POLICY_SELECT_BY_USER;
170
171             if (context != null)
172             {
173                 choice = PolicyDBInterface.WM_POLICY_SELECT_BY_USER_CONTEXT;
174
175                 if (type != null)
176                 {
177                     if (Policy.CONTENT_TREE_ID.equals(type))
178                     {
179                         choice = PolicyDBInterface.WM_POLICY_SELECT_BY_CONTENT_TREE;
180                     }
181                     else if (Policy.SITE_TREE_ID.equals(type))
182                     {
183                         choice = PolicyDBInterface.WM_POLICY_SELECT_BY_SITE_TREE;
184                     }
185                     else
186                     {
187                         choice = PolicyDBInterface.WM_POLICY_SELECT_BY_USER_CONTEXT_TYPE;
188
189                         if (reference != null)
190                         {
191                             choice = PolicyDBInterface.WM_POLICY_SELECT_BY_USER_CONTEXT_TYPE_REFERENCE;
192                         }
193                     }
194                 }
195             }
196
197             data.setQuery(choice);
198             data.setPrototype(new ObjectCollectionDBData(null,
199                                                            null,
200                                                            PolicyDBInterface.PRIMARY_KEY_NAME,
201                                                            null));
202
203             // Database lookup.
204
proxies = getObjectIDs(data);
205         }
206         catch (Exception JavaDoc x)
207         {
208             throw WebmanExceptionHandler.getException(x);
209         }
210
211         return proxies;
212     }
213
214     /**
215      * Retrieves all policy IDs referenced by the specified event.
216      *
217      * @param event the ID of the event (<I>required</I>).
218      * @param login the ID of the login (<I>required</I>).
219      * @param context the ID of the context (<I>required</I>).
220      * @param type the object type (<I>optional</I>).
221      * @param reference the object reference (<I>optional</I>).
222      * @param access the access mode of the policy.
223      * @return all policy IDs referenced by the specified event.
224      * @exception com.teamkonzept.lib.TKException if an error occured during policy retrieval.
225      */

226     public final TKVector getPolicyProxies (Integer JavaDoc event,
227                                             Integer JavaDoc login,
228                                             Integer JavaDoc context,
229                                             Integer JavaDoc type,
230                                             Integer JavaDoc reference,
231                                             boolean access)
232         throws TKException
233     {
234         TKVector proxies = null;
235
236         try
237         {
238             // Create appropriate data.
239
PolicyDBData data = new PolicyDBData(null,
240                                                      login,
241                                                      null,
242                                                      context,
243                                                      type,
244                                                      reference,
245                                                      access
246                                                         ? PolicyDBInterface.MODE_ALLOW
247                                                         : PolicyDBInterface.MODE_DENY);
248             // Choose appropriate query.
249
Class JavaDoc choice = PolicyDBInterface.WM_POLICY_SELECT_BY_ATOMIC_EVENT;
250
251             if (Policy.CONTENT_TREE_ID.equals(type))
252             {
253                 choice = PolicyDBInterface.WM_POLICY_SELECT_BY_CONTENT_TREE_EVENT;
254             }
255             else if (Policy.SITE_TREE_ID.equals(type))
256             {
257                 choice = PolicyDBInterface.WM_POLICY_SELECT_BY_SITE_TREE_EVENT;
258             }
259
260             data.setQuery(choice);
261             data.setPrototype(new ObjectCollectionDBData(EventDBInterface.PRIMARY_KEY_NAME,
262                                                            event,
263                                                            PolicyDBInterface.PRIMARY_KEY_NAME,
264                                                            null));
265
266             // Database lookup.
267
proxies = getObjectIDs(data);
268         }
269         catch (Exception JavaDoc x)
270         {
271             throw WebmanExceptionHandler.getException(x);
272         }
273
274         return proxies;
275     }
276
277     /**
278      * Creates the specified policy.
279      *
280      * @param login the login of the policy.
281      * @param role the role of the policy.
282      * @param context the context of the policy.
283      * @param type the object type of the policy.
284      * @param reference the object reference of the policy.
285      * @param access the access mode of the policy.
286      * @return the specified policy.
287      * @exception com.teamkonzept.lib.TKException if a policy with the specified
288      * attributes already exists or an error occured during policy creation.
289      */

290     public final Policy createPolicy (Login login,
291                                         Role role,
292                                         Context context,
293                                         Integer JavaDoc type,
294                                         Integer JavaDoc reference,
295                                         boolean access)
296         throws TKException
297     {
298         return (Policy) createObject(new PolicyDBData(null,
299                                                           login.getID(),
300                                                           role.getID(),
301                                                           context.getID(),
302                                                           type,
303                                                           reference,
304                                                           access
305                                                             ? PolicyDBInterface.MODE_ALLOW
306                                                             : PolicyDBInterface.MODE_DENY));
307     }
308
309     /**
310      * Modifies the given policy.
311      *
312      * @param policy the policy to be modified.
313      * @exception com.teamkonzept.lib.TKException if an error occured during policy modification.
314      */

315     public final void modifyPolicy (Policy policy)
316         throws TKException
317     {
318         if (policy.isModifiedAssociations())
319         {
320             ResolverFactory.getInstance().removeResolver(policy.getLogin());
321         }
322
323         modifyObject(policy);
324     }
325
326     /**
327      * Deletes the given policy.
328      *
329      * @param policy the policy to be deleted.
330      * @exception com.teamkonzept.lib.TKException if an error occured during policy deletion.
331      */

332     public final void deletePolicy (Policy policy)
333         throws TKException
334     {
335         ResolverFactory.getInstance().removeResolver(policy.getLogin());
336
337         deleteObject(policy);
338     }
339
340 }
341
Popular Tags