KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > acl > JahiaACLManagerService


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 24-JAN-2002, Jahia Solutions Sarl: Khue Nguyen
37  * 06-AUG-2003, Jahia Solutions Sarl: Fulco Houkes
38  *
39  * ----- END LICENSE BLOCK -----
40  */

41
42
43 package org.jahia.services.acl;
44
45 import org.apache.log4j.Logger;
46 import org.jahia.data.JahiaDOMObject;
47 import org.jahia.exceptions.JahiaException;
48 import org.jahia.exceptions.JahiaInitializationException;
49 import org.jahia.exceptions.database.JahiaDatabaseException;
50 import org.jahia.registries.ServicesRegistry;
51 import org.jahia.services.JahiaService;
52 import org.jahia.services.cache.Cache;
53 import org.jahia.services.cache.CacheFactory;
54 import org.jahia.services.usermanager.JahiaGroup;
55 import org.jahia.services.usermanager.JahiaUser;
56 import org.jahia.settings.SettingsBean;
57
58 import java.util.Vector JavaDoc;
59
60
61 /**
62  * ACL Services
63  *
64  * @author Fulco Houkes
65  * @author MAP
66  * @version 1.2
67  */

68 public class JahiaACLManagerService extends JahiaService {
69
70     /** logging */
71     private static final Logger logger = Logger.getLogger (JahiaACLManagerService.class);
72
73     private static JahiaACLManagerService mACLService;
74
75     // the preloaded container acls by page.
76
public static final String JavaDoc PRELOADED_CTNR_ACL_BY_PAGE_CACHE = "PreloadedCtnrACLByPageCache";
77     // the preloaded field acls by page.
78
public static final String JavaDoc PRELOADED_FIELD_ACL_BY_PAGE_CACHE = "PreloadedFieldACLByPageCache";
79     // the ACL Tree cache name.
80
public static final String JavaDoc ACL_TREE_CACHE = "ACLTreeCache";
81
82     /**
83      * @associates JahiaACL
84      */

85     private Cache mACLCache;
86
87     private Cache mPreloadedContainerACLsByPageCache;
88
89     private Cache mPreloadedFieldACLsByPageCache;
90
91     private AclDBUtils mDBUtils;
92     private SettingsBean jSettings;
93
94
95     /**
96      * Default constructor.
97      *
98      * @throws JahiaException
99      */

100     private JahiaACLManagerService ()
101             throws JahiaException {
102         // Get the service registry instance
103
ServicesRegistry registry = ServicesRegistry.getInstance ();
104         if (registry == null) {
105             throw new JahiaException ("ACL Manager", "ACL manager could not get the Service Registry instance.",
106                     JahiaException.REGISTRY_ERROR, JahiaException.CRITICAL_SEVERITY);
107         }
108
109         try {
110             mPreloadedContainerACLsByPageCache = CacheFactory
111                 .createCache (PRELOADED_CTNR_ACL_BY_PAGE_CACHE);
112             mPreloadedFieldACLsByPageCache = CacheFactory
113                 .createCache (PRELOADED_FIELD_ACL_BY_PAGE_CACHE);
114
115         } catch (JahiaInitializationException e) {
116             logger.warn (e);
117         }
118
119         // Get the ACL DB tools class instance
120
mDBUtils = AclDBUtils.getInstance ();
121     }
122
123
124     /**
125      * Initialization
126      *
127      * @param jSettings_ Settings to let the sevice where to save files, i.e.
128      *
129      * @throws JahiaInitializationException when the initialization process failed
130      */

131     public void init (SettingsBean jSettings_)
132             throws JahiaInitializationException {
133         this.jSettings = jSettings_;
134
135         mACLCache = CacheFactory.createCache (ACL_TREE_CACHE);
136
137         int preloadCount = this.jSettings.getAclPreloadCount ();
138
139         logger.debug ("Preloading " + preloadCount + " ACLs from the database");
140         try {
141             mDBUtils.preloadACLs (preloadCount, mACLCache);
142             mDBUtils.preloadPageACLs(mACLCache);
143             mDBUtils.preloadPageFieldACLs(mACLCache);
144             mDBUtils.preloadContainerListACLs(mACLCache);
145         } catch (JahiaException je) {
146             String JavaDoc msg = "Error while preloading ACLs into cache";
147             logger.warn (msg, je);
148             throw new JahiaInitializationException (msg, je);
149         }
150
151     }
152
153     // Javadoc inherited from parent
154
public synchronized void shutdown ()
155             throws JahiaException {
156         super.shutdown ();
157
158         // flush the ACL cache
159
mACLCache.flush ();
160     }
161
162     /**
163      * Return the singleton instance of the ACL Manager service.
164      *
165      * @return Return the reterence on the ACL Manager Service.
166      */

167     public static synchronized JahiaACLManagerService getInstance () {
168         if (mACLService == null) {
169             try {
170                 mACLService = new JahiaACLManagerService ();
171
172             } catch (JahiaException ex) {
173                 logger.fatal ("Could not instanciate the ACL Manager Service!!");
174                 return null;
175             }
176         }
177         return mACLService;
178     }
179
180
181     /**
182      * Create a new reference on a JahiaACL object.
183      *
184      * @param parent Reference on a parent JahiaACL object. Setting the
185      * parent reference object ot null, means the object will
186      * not be able to inherit access rights from a parent
187      * object.
188      *
189      * @return Return a reference on a new ACL object. Return null if the
190      * ACL object could not be created.
191      */

192     public synchronized JahiaACL createACL (JahiaACL parent) {
193         JahiaACL acl = null;
194
195         int parentAclID = 0;
196         if (parent != null) {
197             parentAclID = parent.getID();
198         }
199
200         // Get the next available user ID
201
int aclID;
202         try {
203             /**
204              * rollback fix on JAHIA-240 because it causes migration problems.
205              */

206             //aclID = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement("jahia_acl");
207
aclID = mDBUtils.getNextID();
208         } catch (JahiaException ex) {
209             return null;
210         }
211
212         // Create the ACL
213
acl = new JahiaACL (aclID, parentAclID, ACLInfo.INHERITANCE);
214
215         // add the new acl in the cache.
216
if (acl != null) {
217             try {
218                 if (mDBUtils.addACLInDB (acl)) {
219                     mACLCache.put (new Integer JavaDoc (acl.getID ()), acl);
220                 } else {
221                     acl = null;
222                 }
223             } catch (JahiaDatabaseException ex) {
224                 acl = null;
225             }
226         }
227
228         return acl;
229     }
230
231
232     /**
233      * Return the specified ACL.
234      *
235      * @param aclID Unique identification number of the required ACL.
236      *
237      * @return Return the reference on the requested ACL
238      *
239      * @throws ACLNotFoundException Throws this excption if the specified ACL is not present in the
240      * system.
241      * @throws JahiaDatabaseException Thorws this exception on any database failure.
242      */

243     public JahiaACL lookupACL (int aclID)
244             throws ACLNotFoundException,
245             JahiaDatabaseException {
246         JahiaACL result = (JahiaACL) mACLCache.get (new Integer JavaDoc (aclID));
247         if (result == null) {
248             // not found in cache, let's try to load it from the database.
249
try {
250                 result = mDBUtils.getACL (aclID);
251                 mACLCache.put (new Integer JavaDoc (aclID), result);
252             } catch (JahiaException je) {
253                 throw new ACLNotFoundException (aclID);
254             }
255         }
256         return result;
257     }
258
259
260     /**
261      * Remove the specified ACL from DB also from cache.
262      *
263      * @param acl the ACL to remove.
264      *
265      * @return true if no problems.
266      */

267     public synchronized boolean deleteACL (JahiaACL acl) {
268         boolean result = false;
269         try {
270             if (mDBUtils.deleteACLFromDB (acl)) {
271                 mACLCache.remove (new Integer JavaDoc (acl.getID ()));
272                 result = true;
273             }
274         } catch (JahiaDatabaseException ex) {
275             result = false;
276         }
277
278         return result;
279     }
280
281     /**
282      * Preloads Container ACLs from database for a given page
283      *
284      * @param pageID the pageID
285      * @throws JahiaException thrown if there was an error while loading ACLs
286      * from the database.
287      */

288     public void preloadContainerACLsByPage(int pageID) throws JahiaException {
289         String JavaDoc pageIDStr = String.valueOf(pageID);
290         if (!mPreloadedContainerACLsByPageCache.containsKey(pageIDStr)) {
291             synchronized (this) {
292                 if (!mPreloadedContainerACLsByPageCache.containsKey(pageIDStr)) {
293                     mDBUtils.preloadContainerACLsByPage(pageID, mACLCache);
294                     this.mPreloadedContainerACLsByPageCache.put(pageIDStr,
295                             pageIDStr);
296                 }
297             }
298         }
299     }
300
301     /**
302      * Preloads field ACLs from database for a given page
303      *
304      * @param pageID the page ID
305      * @throws JahiaException thrown if there was an error while loading ACLs
306      * from the database.
307      */

308     public void preloadFieldACLsByPage(int pageID) throws JahiaException {
309         String JavaDoc pageIDStr = String.valueOf(pageID);
310         if (!mPreloadedFieldACLsByPageCache.containsKey(pageIDStr)) {
311             synchronized (this) {
312                 if (!mPreloadedFieldACLsByPageCache.containsKey(pageIDStr)) {
313                     mDBUtils.preloadFieldACLsByPage(pageID, mACLCache);
314                     this.mPreloadedFieldACLsByPageCache.put(pageIDStr,
315                             pageIDStr);
316                 }
317             }
318         }
319     }
320
321     /**
322      * Remove the specified user from all Jahia ACLs.
323      *
324      * @param user The user in question.
325      *
326      * @return true if no problems.
327      */

328     public synchronized boolean removeUserFromAllACLs (JahiaUser user) {
329         if (user == null) {
330             return false;
331         }
332
333         boolean result = false;
334         try {
335             // remove the entries from the database
336
if (mDBUtils.removeACLUserEntries (user.getName ())) {
337
338                 // The cache need to be flushed
339
mACLCache.flush ();
340                 result = true;
341
342 // JahiaACL acl;
343
// Enumeration enumeration = mACLCache.elements ();
344
// while (enumeration.hasMoreElements ()) {
345
// acl = (JahiaACL)enumeration.nextElement ();
346
// if (acl != null) {
347
// logger.debug ("remove user from ACL : " + acl.getID());
348
// acl.removeUserEntry (user);
349
// }
350
// }
351
// result = true;
352
} else {
353                 result = false;
354             }
355
356         } catch (JahiaDatabaseException ex) {
357             result = false;
358             logger.warn (ex);
359         }
360         return result;
361     }
362
363
364     /**
365      * Remove the specified group from all Jahia ACLs.
366      *
367      * @param group The group in question.
368      *
369      * @return true if no problem.
370      */

371     public synchronized boolean removeGroupFromAllACLs (JahiaGroup group) {
372         if (group == null) {
373             return false;
374         }
375
376         boolean result = false;
377         try {
378             if (mDBUtils.removeACLGroupEntries (group.getName ())) {
379
380                 // The cache need to be flushed
381
mACLCache.flush ();
382                 result = true;
383
384 // JahiaACL acl;
385
// Enumeration enumeration = mACLCache.keys ();
386
// while (enumeration.hasMoreElements ()) {
387
// acl = (JahiaACL)mACLCache.get (enumeration.nextElement ());
388
// if (acl != null) {
389
// acl.removeGroupEntry (group);
390
// }
391
// }
392
// result = true;
393

394             } else {
395                 result = false;
396             }
397         } catch (JahiaDatabaseException ex) {
398             result = false;
399         }
400
401         return result;
402     }
403
404
405     // NK
406
/**
407      * return a DOM document of requested acl and all parents and their own parents...
408      *
409      * @param ids the list of acl ids
410      * @param withParents if true, return parents too
411      *
412      * @return JahiaDOMObject a DOM representation of this object
413      *
414      * @throws JahiaException
415      */

416     public JahiaDOMObject getAclsAsDOM (Vector JavaDoc ids, boolean withParents)
417             throws JahiaException {
418         return mDBUtils.getAclsAsDOM (ids, withParents);
419     }
420
421     // NK
422
/**
423      * return a DOM document of requested acl entries
424      *
425      * @param ids the list of acl ids
426      *
427      * @return JahiaDOMObject a DOM representation of this object
428      *
429      * @throws JahiaException
430      */

431     public JahiaDOMObject getAclEntriesAsDOM (Vector JavaDoc ids)
432             throws JahiaException {
433         return mDBUtils.getAclEntriesAsDOM (ids);
434     }
435
436     public void updateCache(JahiaACL jahiaACL) {
437         mACLCache.put(new Integer JavaDoc(jahiaACL.getID()), jahiaACL);
438     }
439
440 }
441
Popular Tags