1 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 ; 59 60 61 68 public class JahiaACLManagerService extends JahiaService { 69 70 71 private static final Logger logger = Logger.getLogger (JahiaACLManagerService.class); 72 73 private static JahiaACLManagerService mACLService; 74 75 public static final String PRELOADED_CTNR_ACL_BY_PAGE_CACHE = "PreloadedCtnrACLByPageCache"; 77 public static final String PRELOADED_FIELD_ACL_BY_PAGE_CACHE = "PreloadedFieldACLByPageCache"; 79 public static final String ACL_TREE_CACHE = "ACLTreeCache"; 81 82 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 100 private JahiaACLManagerService () 101 throws JahiaException { 102 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 mDBUtils = AclDBUtils.getInstance (); 121 } 122 123 124 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 msg = "Error while preloading ACLs into cache"; 147 logger.warn (msg, je); 148 throw new JahiaInitializationException (msg, je); 149 } 150 151 } 152 153 public synchronized void shutdown () 155 throws JahiaException { 156 super.shutdown (); 157 158 mACLCache.flush (); 160 } 161 162 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 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 int aclID; 202 try { 203 206 aclID = mDBUtils.getNextID(); 208 } catch (JahiaException ex) { 209 return null; 210 } 211 212 acl = new JahiaACL (aclID, parentAclID, ACLInfo.INHERITANCE); 214 215 if (acl != null) { 217 try { 218 if (mDBUtils.addACLInDB (acl)) { 219 mACLCache.put (new Integer (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 243 public JahiaACL lookupACL (int aclID) 244 throws ACLNotFoundException, 245 JahiaDatabaseException { 246 JahiaACL result = (JahiaACL) mACLCache.get (new Integer (aclID)); 247 if (result == null) { 248 try { 250 result = mDBUtils.getACL (aclID); 251 mACLCache.put (new Integer (aclID), result); 252 } catch (JahiaException je) { 253 throw new ACLNotFoundException (aclID); 254 } 255 } 256 return result; 257 } 258 259 260 267 public synchronized boolean deleteACL (JahiaACL acl) { 268 boolean result = false; 269 try { 270 if (mDBUtils.deleteACLFromDB (acl)) { 271 mACLCache.remove (new Integer (acl.getID ())); 272 result = true; 273 } 274 } catch (JahiaDatabaseException ex) { 275 result = false; 276 } 277 278 return result; 279 } 280 281 288 public void preloadContainerACLsByPage(int pageID) throws JahiaException { 289 String 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 308 public void preloadFieldACLsByPage(int pageID) throws JahiaException { 309 String 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 328 public synchronized boolean removeUserFromAllACLs (JahiaUser user) { 329 if (user == null) { 330 return false; 331 } 332 333 boolean result = false; 334 try { 335 if (mDBUtils.removeACLUserEntries (user.getName ())) { 337 338 mACLCache.flush (); 340 result = true; 341 342 } 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 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 mACLCache.flush (); 382 result = true; 383 384 394 } else { 395 result = false; 396 } 397 } catch (JahiaDatabaseException ex) { 398 result = false; 399 } 400 401 return result; 402 } 403 404 405 416 public JahiaDOMObject getAclsAsDOM (Vector ids, boolean withParents) 417 throws JahiaException { 418 return mDBUtils.getAclsAsDOM (ids, withParents); 419 } 420 421 431 public JahiaDOMObject getAclEntriesAsDOM (Vector ids) 432 throws JahiaException { 433 return mDBUtils.getAclEntriesAsDOM (ids); 434 } 435 436 public void updateCache(JahiaACL jahiaACL) { 437 mACLCache.put(new Integer (jahiaACL.getID()), jahiaACL); 438 } 439 440 } 441 | Popular Tags |