1 package org.jahia.services.sites; 18 19 import java.util.Enumeration ; 20 import java.util.Vector ; 21 22 import org.apache.log4j.Logger; 23 import org.jahia.data.JahiaDOMObject; 24 import org.jahia.exceptions.JahiaException; 25 import org.jahia.exceptions.JahiaInitializationException; 26 import org.jahia.registries.ServicesRegistry; 27 import org.jahia.services.acl.ACLResource; 28 import org.jahia.services.acl.JahiaACLEntry; 29 import org.jahia.services.acl.JahiaBaseACL; 30 import org.jahia.services.cache.Cache; 31 import org.jahia.services.cache.CacheFactory; 32 import org.jahia.services.cache.CacheListener; 33 import org.jahia.services.filemanager.JahiaFilemanagerService; 34 import org.jahia.services.usermanager.JahiaGroup; 35 import org.jahia.settings.SettingsBean; 36 37 38 43 public class JahiaSitesBaseService extends JahiaSitesService implements CacheListener { 44 45 46 final private static Logger logger = Logger.getLogger (JahiaSitesBaseService.class); 47 48 49 protected static JahiaSitesBaseService instance = null; 50 51 public static final String SITE_CACHE_BYID = "JahiaSiteByIDCache"; 52 public static final String SITE_CACHE_BYNAME = "JahiaSiteByNameCache"; 53 54 private Cache siteCacheByID = null; 55 private Cache siteCacheByName = null; 56 57 private Vector sitesToDelete = new Vector (); 60 61 62 protected SettingsBean mJahiaSettings = null; 63 64 65 70 protected JahiaSitesBaseService () throws JahiaException { 71 logger.info ("***** Starting the Jahia Sites Base Service *****"); 72 73 setServiceName ("JahiaSitesBaseService"); 74 } 75 76 77 84 public static synchronized JahiaSitesBaseService getInstance () 85 throws JahiaException { 86 if (instance == null) { 87 instance = new JahiaSitesBaseService (); 88 } 89 return instance; 90 } 91 92 93 101 public Enumeration getSites () throws JahiaException { 102 Vector allSites = new Vector (); 103 JahiaSite site; 104 Object [] siteCacheKeys = siteCacheByID.keys (); 105 for (int i = 0; i < siteCacheKeys.length; i++) { 106 site = (JahiaSite) siteCacheByID.get (siteCacheKeys[i]); 107 if (site != null) { 108 allSites.add(site); 110 } 111 } 112 113 return allSites.elements (); 114 } 115 116 117 120 public void init (SettingsBean jSettings) 121 throws JahiaInitializationException { 122 if (!isInitialized ()) { 123 mIsServiceInitialized = true; 124 mJahiaSettings = jSettings; 125 } 126 127 siteCacheByID = CacheFactory.createCache (SITE_CACHE_BYID); 128 siteCacheByID.registerListener(this); 129 siteCacheByName = CacheFactory.createCache (SITE_CACHE_BYNAME); 130 siteCacheByName.registerListener(this); 131 132 try { 133 loadSitesInCache(jSettings); 134 } catch (JahiaException je) { 135 throw new JahiaInitializationException("Error while loading sites in cache", je); 136 } 137 138 initFilemanagers (); 139 } 140 141 148 public JahiaSite getSite(int id) throws JahiaException { 149 Integer idObj = new Integer (id); 150 JahiaSite site = (JahiaSite) siteCacheByID.get(idObj); 151 if (site != null) { 152 return site; 153 } 154 155 site = JahiaSitesPersistance.getInstance().dbGetSite(id); 157 synchronized (this) { 158 if (!siteCacheByID.containsKey(idObj)) { 160 siteCacheByID.put(idObj, site); 161 } 162 if (site != null && site.getServerName() != null && !siteCacheByName.containsKey(site.getServerName())) { 163 siteCacheByName.put(site.getServerName(), site); 164 } 165 } 166 167 return site; 168 } 169 170 171 179 public JahiaSite getSiteByKey(String siteKey) throws JahiaException { 180 if (siteKey == null) { 181 return null; 182 } 183 JahiaSite site = null; 184 185 Object [] siteCacheKeys = siteCacheByID.keys(); 187 for (int i = 0; i < siteCacheKeys.length; i++) { 188 site = (JahiaSite) siteCacheByID.get(siteCacheKeys[i]); 189 if (site != null) { 190 if (siteKey.equals(site.getSiteKey())) { 192 return site; 193 } 194 } 195 } 196 site = JahiaSitesPersistance.getInstance().dbGetSiteByKey(siteKey); 199 200 if (site != null) { 203 synchronized (this) { 204 Integer siteIDObj = new Integer (site.getID()); 205 if (!siteCacheByID.containsKey(siteIDObj)) { 206 siteCacheByID.put(siteIDObj, site); 207 } 208 if (site.getServerName() != null && !siteCacheByName.containsKey(site.getServerName())) { 209 siteCacheByName.put(site.getServerName(), site); 210 } 211 } 212 } 213 214 return site; 215 } 216 217 230 public JahiaSite getSiteByServerName(String serverName) throws JahiaException { 231 if (serverName == null) { 232 return null; 233 } 234 JahiaSite site = (JahiaSite) siteCacheByName.get(serverName); 235 236 if (siteCacheByName.containsKey(serverName)) { 237 return site; 238 } 239 site = JahiaSitesPersistance.getInstance().dbGetSiteByServerName(serverName); 242 243 if (site != null) { 246 synchronized (this) { 247 Integer siteIDObj = new Integer (site.getID()); 248 if (!siteCacheByID.containsKey(siteIDObj)) { 249 siteCacheByID.put(siteIDObj, site); 250 } 251 if (!siteCacheByName.containsKey(serverName)) { 252 siteCacheByName.put(serverName, site); 253 } 254 } 255 } 256 257 return site; 258 } 259 260 261 270 public JahiaSite getSite(String name) throws JahiaException { 271 if (name == null) { 272 return null; 273 } 274 JahiaSite site = (JahiaSite) siteCacheByName.get(name); 275 276 if (site != null) { 277 return site; 278 } 279 site = JahiaSitesPersistance.getInstance().dbGetSite(name); 281 282 if (site != null) { 283 synchronized (this) { 284 Integer siteIDObj = new Integer (site.getID()); 285 if (!siteCacheByID.containsKey(siteIDObj)) { 286 siteCacheByID.put(siteIDObj, site); 287 } 288 if (!siteCacheByName.containsKey(name)) { 289 siteCacheByName.put(name, site); 290 } 291 } 292 } 293 return site; 294 } 295 296 297 305 public synchronized boolean addSite (JahiaSite site) 306 throws JahiaException { 307 308 if (site == null) { 309 return false; 310 } 311 312 313 if (getSite (site.getServerName ()) == null 315 && getSiteByKey (site.getSiteKey ()) == null) { 316 317 JahiaSitesPersistance.getInstance ().dbAddSite (site); 318 319 if (site.getID () == -1) { 320 return false; 321 } 322 323 siteCacheByID.put (new Integer (site.getID ()), site); 324 if (site.getServerName() != null) { 325 siteCacheByName.put(site.getServerName(), site); 326 } 327 return true; 328 } 329 330 return false; 331 } 332 333 334 339 public synchronized void removeSite (JahiaSite site) throws JahiaException { 340 JahiaSitesPersistance.getInstance ().dbRemoveSite (site.getID ()); 341 siteCacheByID.remove (new Integer (site.getID ())); 342 siteCacheByName.remove(site.getServerName()); 343 } 344 345 346 351 public synchronized void updateSite (JahiaSite site) throws JahiaException { 352 JahiaSitesPersistance.getInstance ().dbUpdateSite (site); 353 siteCacheByID.put (new Integer (site.getID ()), site); 354 siteCacheByName.put(site.getServerName(), site); 355 } 356 357 358 361 protected void loadSitesInCache (SettingsBean settingsBean) throws JahiaException { 362 363 Vector sites = JahiaSitesPersistance.getInstance ().dbGetSites (); 364 365 373 JahiaGroup rootAdminGrp = ServicesRegistry.getInstance () 375 .getJahiaGroupManagerService ().getAdministratorGroup (0); 376 377 JahiaGroup siteAdminGrp = null; 379 380 JahiaACLEntry adminAclEntry = new JahiaACLEntry( 382 JahiaBaseACL.ALL_RIGHTS, JahiaACLEntry.ACL_YES); 383 384 387 if (sites != null) { 388 int size = sites.size (); 389 for (int i = 0; i < size; i++) { 390 JahiaSite site = (JahiaSite) sites.get (i); 391 392 400 401 if (!ACLResource.checkAdminAccess (site, rootAdminGrp)) { 402 403 site.getACL ().setGroupEntry (rootAdminGrp, adminAclEntry); 404 405 siteAdminGrp = ServicesRegistry.getInstance () 406 .getJahiaGroupManagerService ().getAdministratorGroup ( 407 site.getID ()); 408 409 site.getACL ().setGroupEntry (siteAdminGrp, adminAclEntry); 410 } 411 412 415 416 JahiaSiteTools.startTemplateObserver (site, settingsBean); 418 419 JahiaSiteTools.startWebAppsObserver (site, settingsBean); 421 422 siteCacheByID.put (new Integer (site.getID ()), site); 423 siteCacheByName.put(site.getServerName(), site); 424 } 425 } 426 } 427 428 429 434 public synchronized void addSiteToDelete (int siteID) { 435 Integer I = new Integer (siteID); 436 if (!this.sitesToDelete.contains (I)) { 437 this.sitesToDelete.add (I); 438 } 439 } 440 441 446 public synchronized void removeSiteToDelete (int siteID) { 447 this.sitesToDelete.remove (new Integer (siteID)); 448 } 449 450 457 public synchronized boolean isSiteToBeDeleted (int siteID) { 458 return (this.sitesToDelete.contains (new Integer (siteID))); 459 } 460 461 public int getNbSites () 464 throws JahiaException { 465 return JahiaSitesPersistance.getInstance ().getNbSites (); 466 } 467 468 469 474 public JahiaDOMObject getSiteAsDOM (int siteID) throws JahiaException { 475 476 return JahiaSitesPersistance.getInstance ().getSiteAsDOM (siteID); 477 478 } 479 480 481 486 public JahiaDOMObject getSitePropsAsDOM (int siteID) throws JahiaException { 487 488 return JahiaSitesPersistance.getInstance ().getSitePropsAsDOM (siteID); 489 490 } 491 492 493 496 public void initFilemanagers () { 497 498 try { 499 JahiaFilemanagerService fmngServ = ServicesRegistry.getInstance () 500 .getJahiaFilemanagerService (); 501 if (fmngServ == null) { 502 return; 503 } 504 Enumeration enumeration = getSites (); 505 JahiaSite site = null; 506 while (enumeration.hasMoreElements ()) { 507 site = (JahiaSite) enumeration.nextElement (); 508 fmngServ.createDirectory (site); 509 } 510 } catch (Throwable t) { 511 t.printStackTrace (); 512 } 513 } 514 515 521 public String getProperty (int siteID, String key) 522 throws JahiaException { 523 return JahiaSitesPersistance.getInstance ().getProperty (siteID, key); 524 } 525 526 531 public void onCacheFlush(String cacheName) { 532 if (SITE_CACHE_BYID.equals(cacheName) || 533 SITE_CACHE_BYNAME.equals(cacheName)) { 534 try { 535 siteCacheByID.flush(false); 536 siteCacheByName.flush(false); 537 loadSitesInCache(mJahiaSettings); 538 539 } catch (JahiaException e) { 540 logger.warn( 541 "Could not reload Jahia Sites after the Site Cache was flushed!", e); 542 } 543 } 544 } 545 546 public void onCachePut(String cacheName, Object entryKey) { 547 } 549 550 } 551 | Popular Tags |