Author: bpapez
Date: Wed Jan 2 12:17:13 2008
New Revision: 19437
URL: https://svndev.jahia.net/websvn/listing.php?sc=3D1&rev=3D19437&repname=
=3Djahia
Log:
introduce new cache: sitebykey (like in Jahia5) to prevent iterating throug=
h the sitebyid cache each time
Modified:
branches/JAHIA-4-1-BRANCH/src/java/org/jahia/services/sites/JahiaSitesB=
aseService.java
Modified: branches/JAHIA-4-1-BRANCH/src/java/org/jahia/services/sites/Jahia=
SitesBaseService.java
URL: https://svndev.jahia.net/websvn/diff.php?path=3D/branches/JAHIA-4-1-BR=
ANCH/src/java/org/jahia/services/sites/JahiaSitesBaseService.java&rev=3D194=
37&repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-4-1-BRANCH/src/java/org/jahia/services/sites/JahiaSitesB=
aseService.java (original)
+++ branches/JAHIA-4-1-BRANCH/src/java/org/jahia/services/sites/JahiaSitesB=
aseService.java Wed Jan 2 12:17:13 2008
@@ -16,7 +16,9 @@
//
package org.jahia.services.sites;
=
+import java.util.Collection;
import java.util.Enumeration;
+import java.util.Iterator;
import java.util.Vector;
=
import org.apache.log4j.Logger;
@@ -49,9 +51,11 @@
protected static JahiaSitesBaseService instance =3D null;
=
public static final String SITE_CACHE_BYID =3D "JahiaSiteByIDCache";
+ public static final String SITE_CACHE_BYKEY =3D "JahiaSiteByKeyCache";=
=
public static final String SITE_CACHE_BYNAME =3D "JahiaSiteByNameCache=
";
/** The cache in memory */
private Cache siteCacheByID =3D null;
+ private Cache siteCacheByKey =3D null; =
private Cache siteCacheByName =3D null;
=
// list of sites going to be deleted.
@@ -99,17 +103,8 @@
* cache was flushed in the meantime, this method is FALSE !
*/
public Enumeration getSites () throws JahiaException {
- Vector allSites =3D new Vector ();
- JahiaSite site;
- Object[] siteCacheKeys =3D siteCacheByID.keys ();
- for (int i =3D 0; i < siteCacheKeys.length; i++) {
- site =3D (JahiaSite) siteCacheByID.get (siteCacheKeys[i]);
- if (site !=3D null) {
- // site may be null since we also cache null entries for s=
peed.
- allSites.add(site);
- }
- }
-
+ Vector allSites =3D new Vector (loadSitesInCache(mJahiaSettings));
+ =
return allSites.elements ();
}
=
@@ -128,6 +123,8 @@
siteCacheByID.registerListener(this);
siteCacheByName =3D CacheFactory.createCache (SITE_CACHE_BYNAME);
siteCacheByName.registerListener(this);
+ siteCacheByKey =3D CacheFactory.createCache (SITE_CACHE_BYKEY);
+ siteCacheByKey.registerListener(this);
=
try {
loadSitesInCache(jSettings);
@@ -154,20 +151,22 @@
=
// try to load from db
site =3D JahiaSitesPersistance.getInstance().dbGetSite(id);
- synchronized (this) {
+ if (site !=3D null) {
// if the site could be loaded, add it into the cache
- if (!siteCacheByID.containsKey(idObj)) {
- siteCacheByID.put(idObj, site);
- }
- if (site !=3D null && site.getServerName() !=3D null && !siteC=
acheByName.containsKey(site.getServerName())) {
- siteCacheByName.put(site.getServerName(), site);
- }
+ addToCache(site);
}
=
return site;
}
=
-
+ private synchronized void addToCache(JahiaSite site) {
+ siteCacheByID.put(new Integer(site.getID()), site);
+ if (site.getServerName() !=3D null) {
+ siteCacheByName.put(site.getServerName(), site);
+ }
+ siteCacheByKey.put(site.getSiteKey(), site);
+ } =
+ =
/**
* return a site bean looking at it key
* =
@@ -180,19 +179,10 @@
if (siteKey =3D=3D null) {
return null;
}
- JahiaSite site =3D null;
+ JahiaSite site =3D (JahiaSite) siteCacheByKey.get(siteKey);
+ if (site !=3D null)
+ return site;
=
- // try to find the site in the cache
- Object[] siteCacheKeys =3D siteCacheByID.keys();
- for (int i =3D 0; i < siteCacheKeys.length; i++) {
- site =3D (JahiaSite) siteCacheByID.get(siteCacheKeys[i]);
- if (site !=3D null) {
- // site might be null because we cache non-existent entrie=
s too.
- if (siteKey.equals(site.getSiteKey())) {
- return site;
- }
- }
- }
// the site was not found in the cache, try to load it from the
// database.
site =3D JahiaSitesPersistance.getInstance().dbGetSiteByKey(siteKe=
y);
@@ -200,15 +190,7 @@
// if the site could be loaded from the database, add it into the
// cache.
if (site !=3D null) {
- synchronized (this) {
- Integer siteIDObj =3D new Integer(site.getID());
- if (!siteCacheByID.containsKey(siteIDObj)) {
- siteCacheByID.put(siteIDObj, site);
- }
- if (site.getServerName() !=3D null && !siteCacheByName.con=
tainsKey(site.getServerName())) {
- siteCacheByName.put(site.getServerName(), site);
- }
- }
+ addToCache(site);
}
=
return site;
@@ -233,7 +215,7 @@
}
JahiaSite site =3D (JahiaSite) siteCacheByName.get(serverName);
=
- if (siteCacheByName.containsKey(serverName)) {
+ if (site !=3D null) {
return site;
}
// the site was not found in the cache, try to load it from the
@@ -243,15 +225,7 @@
// if the site could be loaded from the database, add it into the
// cache.
if (site !=3D null) {
- synchronized (this) {
- Integer siteIDObj =3D new Integer(site.getID());
- if (!siteCacheByID.containsKey(siteIDObj)) {
- siteCacheByID.put(siteIDObj, site);
- }
- if (!siteCacheByName.containsKey(serverName)) {
- siteCacheByName.put(serverName, site);
- }
- }
+ addToCache(site);
}
=
return site;
@@ -268,29 +242,7 @@
* @return JahiaSite the JahiaSite bean or null
*/
public JahiaSite getSite(String name) throws JahiaException {
- if (name =3D=3D null) {
- return null;
- }
- JahiaSite site =3D (JahiaSite) siteCacheByName.get(name);
-
- if (site !=3D null) {
- return site;
- }
- // try to load the site from the database.
- site =3D JahiaSitesPersistance.getInstance().dbGetSite(name);
-
- if (site !=3D null) {
- synchronized (this) {
- Integer siteIDObj =3D new Integer(site.getID());
- if (!siteCacheByID.containsKey(siteIDObj)) {
- siteCacheByID.put(siteIDObj, site);
- }
- if (!siteCacheByName.containsKey(name)) {
- siteCacheByName.put(name, site);
- }
- }
- }
- return site;
+ return getSiteByServerName(name);
}
=
=
@@ -320,10 +272,8 @@
return false;
}
=
- siteCacheByID.put (new Integer (site.getID ()), site);
- if (site.getServerName() !=3D null) {
- siteCacheByName.put(site.getServerName(), site);
- }
+ addToCache(site);
+ =
return true;
}
=
@@ -340,6 +290,7 @@
JahiaSitesPersistance.getInstance ().dbRemoveSite (site.getID ());
siteCacheByID.remove (new Integer (site.getID ()));
siteCacheByName.remove(site.getServerName());
+ siteCacheByKey.remove(site.getSiteKey());
}
=
=
@@ -350,82 +301,77 @@
*/
public synchronized void updateSite (JahiaSite site) throws JahiaExcep=
tion {
JahiaSitesPersistance.getInstance ().dbUpdateSite (site);
- siteCacheByID.put (new Integer (site.getID ()), site);
- siteCacheByName.put(site.getServerName(), site);
+ addToCache(site);
}
=
=
/**
* load all sites from database in cache
*/
- protected void loadSitesInCache (SettingsBean settingsBean) throws Jah=
iaException {
+ protected Collection loadSitesInCache(SettingsBean settingsBean)
+ throws JahiaException {
=
- Vector sites =3D JahiaSitesPersistance.getInstance ().dbGetSites (=
);
+ Collection sites =3D JahiaSitesPersistance.getInstance().dbGetSite=
s();
=
- //--------------------------------------------------------------
+ // --------------------------------------------------------------
// ACL on templates Patch
//
// 30.01.2002 : NK Patch : give admin permission for root admin grp
- // and site's admin grp
on the site ACL if not set
- // This is needed for
ACL who have as parent , the site's =
ACL
+ // and site's admin grp on the site ACL if not set
+ // This is needed for ACL who have as parent , the site's ACL
//
=
// root admin group
- JahiaGroup rootAdminGrp =3D ServicesRegistry.getInstance ()
- .getJahiaGroupManagerService ().getAdministratorGroup (0);
+ JahiaGroup rootAdminGrp =3D ServicesRegistry.getInstance()
+ .getJahiaGroupManagerService().getAdministratorGroup(0);
=
// site admin group
JahiaGroup siteAdminGrp =3D null;
=
// settings default permissions
JahiaACLEntry adminAclEntry =3D new JahiaACLEntry(
- JahiaBaseACL.ALL_RIGHTS, JahiaACLEntry.ACL_YES);
+ JahiaBaseACL.ALL_RIGHTS, JahiaACLEntry.ACL_YES);
=
// End Patch
- //---------------------------------------------------------------
-
- if (sites !=3D null) {
- int size =3D sites.size ();
- for (int i =3D 0; i < size; i++) {
- JahiaSite site =3D (JahiaSite) sites.get (i);
+ // ---------------------------------------------------------------
=
- //--------------------------------------------------------=
------
+ for (Iterator it =3D sites.iterator(); it.hasNext();) {
+ JahiaSite site =3D (JahiaSite) it.next();
+ if (getSite(site.getID()) =3D=3D null) {
+ // -------------------------------------------------------=
-------
// ACL on templates Patch
//
// 30.01.2002 : NK Patch : give admin permission for root =
admin grp
- // and site's
admin grp on the site ACL if not set
- // This is
needed for ACL who have as parent , the=
site's ACL
+ // and site's admin grp on the site ACL if not set
+ // This is needed for ACL who have as parent , the site's =
ACL
//
=
+ if (!ACLResource.checkAdminAccess(site, rootAdminGrp)) {
=
- if (!ACLResource.checkAdminAccess (site, rootAdminGrp)) {
+ site.getACL().setGroupEntry(rootAdminGrp, adminAclEntr=
y);
=
- site.getACL ().setGroupEntry (rootAdminGrp, adminAclEn=
try);
+ siteAdminGrp =3D ServicesRegistry.getInstance()
+ .getJahiaGroupManagerService()
+ .getAdministratorGroup(site.getID());
=
- siteAdminGrp =3D ServicesRegistry.getInstance ()
- .getJahiaGroupManagerService ().getAdministrat=
orGroup (
- site.getID ());
-
- site.getACL ().setGroupEntry (siteAdminGrp, adminAclEn=
try);
+ site.getACL().setGroupEntry(siteAdminGrp, adminAclEntr=
y);
}
=
// End Patch
- //--------------------------------------------------------=
------
-
+ // -------------------------------------------------------=
-------
=
// start and create the site's new templates folder if not=
exists
- JahiaSiteTools.startTemplateObserver (site, settingsBean);
+ JahiaSiteTools.startTemplateObserver(site, settingsBean);
=
// start and create the site's new webapps folder if not e=
xists
- JahiaSiteTools.startWebAppsObserver (site, settingsBean);
+ JahiaSiteTools.startWebAppsObserver(site, settingsBean);
=
- siteCacheByID.put (new Integer (site.getID ()), site);
- siteCacheByName.put(site.getServerName(), site);
+ addToCache(site);
}
}
+ return sites;
}
=
-
/**
* Add a site to the list of site going to be deleted
*
@@ -529,17 +475,13 @@
* @param cacheName the name of the cache which flushed its items.
*/
public void onCacheFlush(String cacheName) {
- if (SITE_CACHE_BYID.equals(cacheName) ||
- SITE_CACHE_BYNAME.equals(cacheName)) {
- try {
- siteCacheByID.flush(false);
- siteCacheByName.flush(false);
- loadSitesInCache(mJahiaSettings);
-
- } catch (JahiaException e) {
- logger.warn(
- "Could not reload Jahia Sites after the Site Cache=
was flushed!", e);
- }
+ if (SITE_CACHE_BYID.equals(cacheName)
+ || SITE_CACHE_BYNAME.equals(cacheName)
+ || SITE_CACHE_BYKEY.equals(cacheName)) {
+
+ siteCacheByID.flush(false);
+ siteCacheByName.flush(false);
+ siteCacheByKey.flush(false);
}
}
=
_______________________________________________
cvs_list mailing list
[email protected]
http://lists.jahia.org/cgi-bin/mailman/listinfo/cvs_list