1 13 16 package org.jahia.registries; 17 18 import java.util.Enumeration ; 19 import java.util.Hashtable ; 20 import java.util.Vector ; 21 22 import org.apache.log4j.Logger; 23 import org.jahia.data.containers.JahiaContainerDefinition; 24 import org.jahia.data.containers.JahiaContainerSubDefinition; 25 import org.jahia.exceptions.JahiaException; 26 import org.jahia.services.cache.Cache; 27 import org.jahia.services.cache.CacheFactory; 28 import org.jahia.services.cache.CacheListener; 29 import org.jahia.services.containers.JahiaContainersService; 30 31 public class JahiaContainerDefinitionsRegistry implements CacheListener { 32 33 private static Logger logger = Logger.getLogger(JahiaContainerDefinitionsRegistry.class); 34 35 private static JahiaContainerDefinitionsRegistry theObject = null; 36 37 private static final String KEY_SEPARATOR = "###"; 38 39 public static final String CONTAINER_DEFINITION_BY_ID_CACHE = "ContainerDefinitionByID"; 40 public static final String CONTAINER_DEFINITION_BY_SITE_AND_NAME_CACHE = 41 "ContainerDefinitionsBySiteAndName"; 42 private Cache containerDefByID; 43 private Cache containerDefBySiteIDAndName; 44 45 private JahiaContainerDefinitionsRegistry () { 46 logger.debug("Starting ContainerDefinitions Registry"); 47 try { 48 containerDefByID = CacheFactory.createCache(CONTAINER_DEFINITION_BY_ID_CACHE); 49 containerDefByID.registerListener(this); 50 51 52 containerDefBySiteIDAndName = CacheFactory.createCache(CONTAINER_DEFINITION_BY_SITE_AND_NAME_CACHE); 53 containerDefBySiteIDAndName.registerListener(this); 54 55 } catch (JahiaException je) { 56 logger.error( 57 "Error while creating caches for JahiaContainerDefinition registry.", je); 58 } 59 } 61 66 public static synchronized JahiaContainerDefinitionsRegistry getInstance () { 67 if (theObject == null) { 68 theObject = new JahiaContainerDefinitionsRegistry(); 69 } 70 return theObject; 71 } 73 77 public void init () 78 throws JahiaException { 79 loadAllDefinitions(); 80 } 82 88 private void loadAllDefinitions () 89 throws JahiaException { 90 Vector defIDs = ServicesRegistry.getInstance(). 91 getJahiaContainersService( 92 ).getAllContainerDefinitionIDs(); 93 94 95 JahiaContainersService service = 96 ServicesRegistry.getInstance().getJahiaContainersService(); 97 if (service == null) { 98 logger.warn("Could not get the Jahia Containers Service instance!!"); 99 return; 100 } 101 102 for (int i = 0; i < defIDs.size(); i++) { 103 JahiaContainerDefinition currentDefinition = 104 service.loadContainerDefinition (((Integer )defIDs.elementAt(i)).intValue()); 105 106 addToCache(currentDefinition); 107 } 108 } 109 110 private JahiaContainerDefinition loadDefinitionByID (int defID) 111 throws JahiaException { 112 JahiaContainerDefinition currentDefinition = ServicesRegistry. 113 getInstance(). 114 getJahiaContainersService( 115 ).loadContainerDefinition(defID); 116 if (currentDefinition != null) { 117 addToCache(currentDefinition); 118 } 119 return currentDefinition; 120 } 121 122 private JahiaContainerDefinition loadDefinitionBySiteIDAndName (int siteID, 123 String definitionName) 124 throws JahiaException { 125 JahiaContainerDefinition currentDefinition = ServicesRegistry. 126 getInstance(). 127 getJahiaContainersService( 128 ).loadContainerDefinition(siteID, definitionName); 129 if (currentDefinition != null) { 130 addToCache(currentDefinition); 131 } 132 return currentDefinition; 133 } 134 135 private synchronized void addToCache( 136 JahiaContainerDefinition currentDefinition) { 137 containerDefByID.put(new Integer (currentDefinition.getID()), 138 currentDefinition); 139 containerDefBySiteIDAndName.put(buildCacheKey(currentDefinition 140 .getName(), currentDefinition.getJahiaID()), currentDefinition); 141 } 142 152 public JahiaContainerDefinition getDefinition(int defID) throws JahiaException { 153 154 JahiaContainerDefinition currentDef = (JahiaContainerDefinition) containerDefByID.get(new Integer (defID)); 155 if (currentDef == null) { 156 synchronized (this) { 157 currentDef = (JahiaContainerDefinition) containerDefByID.get(new Integer (defID)); 158 if (currentDef == null) { 159 currentDef = loadDefinitionByID(defID); 160 } 161 } 162 } 163 if (currentDef == null) { 164 logger.debug("Couldn't find container definition for ID=" + defID); 165 } 166 return currentDef; 167 168 } 170 183 public JahiaContainerDefinition getDefinition(int siteID, String containerName) throws JahiaException { 184 185 Object cacheKey = buildCacheKey(containerName, siteID); 186 JahiaContainerDefinition currentDef = (JahiaContainerDefinition) containerDefBySiteIDAndName.get(cacheKey); 187 if (currentDef == null) { 188 synchronized (this) { 189 currentDef = (JahiaContainerDefinition) containerDefBySiteIDAndName.get(cacheKey); 190 if (currentDef == null) { 191 currentDef = loadDefinitionBySiteIDAndName(siteID, containerName); 192 } 193 } 194 } 195 if (currentDef == null) { 196 logger.debug("Couldn't find container definition for siteID=" + siteID + " and name=" + containerName); 197 } 198 return currentDef; 199 200 } 202 212 public Vector getDefinitionsInTemplate (int pageDefID) { 213 Vector theDefs = new Vector (); 214 Object [] containerDefKeys = containerDefByID.keys(); 215 for (int i = 0; i < containerDefKeys.length; i++) { 216 Integer currentDefID = (Integer ) containerDefKeys[i]; 217 JahiaContainerDefinition theContainerDef = ( 218 JahiaContainerDefinition) containerDefByID.get(currentDefID); 219 Hashtable subDefs = theContainerDef.getSubDefs(); 220 Enumeration subKeys = subDefs.keys(); 221 while (subKeys.hasMoreElements()) { 222 JahiaContainerSubDefinition theSubDef = ( 223 JahiaContainerSubDefinition) 224 subDefs.get( subKeys.nextElement()); 225 if (theSubDef.getPageDefID() == pageDefID) { 226 theDefs.add(theContainerDef); 227 break; 228 } 229 } 230 } 231 return theDefs; 232 } 234 241 public synchronized void setDefinition (JahiaContainerDefinition 242 theContainerDef) 243 throws JahiaException { 244 JahiaContainerDefinition aContainerDef = 245 getDefinition(theContainerDef.getJahiaID(), 246 theContainerDef.getName()); 247 if (aContainerDef != null) { 248 ServicesRegistry.getInstance().getJahiaContainersService( 250 ).saveContainerDefinition(theContainerDef); 251 containerDefByID.put(new Integer (aContainerDef.getID()), 252 theContainerDef); 253 containerDefBySiteIDAndName.put(buildCacheKey(aContainerDef.getName(), 254 aContainerDef.getJahiaID()), theContainerDef); 255 } else { 256 ServicesRegistry.getInstance().getJahiaContainersService( 258 ).saveContainerDefinition(theContainerDef); 259 containerDefByID.put(new Integer (theContainerDef.getID()), 260 theContainerDef); 261 containerDefBySiteIDAndName.put(buildCacheKey(theContainerDef. 262 getName(), 263 theContainerDef.getJahiaID()), theContainerDef); 264 } 265 } 267 270 public void removeContainerDefinition (int ctnDefID) 271 throws JahiaException { 272 JahiaContainerDefinition currentDef = getDefinition(ctnDefID); 273 if (currentDef != null) { 274 synchronized (this){ 275 containerDefByID.remove(new Integer (ctnDefID)); 276 containerDefBySiteIDAndName.remove(buildCacheKey(currentDef.getName(), 277 currentDef.getJahiaID())); 278 } 279 } 280 } 282 private String buildCacheKey (String containerDefinitionName, int siteID) { 283 StringBuffer result = new StringBuffer (); 284 result.append(containerDefinitionName); 285 result.append(KEY_SEPARATOR); 286 result.append(siteID); 287 return result.toString(); 288 } 289 290 295 public void onCacheFlush(String cacheName) { 296 if (CONTAINER_DEFINITION_BY_ID_CACHE.equals(cacheName)) { 297 containerDefBySiteIDAndName.flush(false); 298 299 } else if (CONTAINER_DEFINITION_BY_SITE_AND_NAME_CACHE.equals(cacheName)) { 300 containerDefByID.flush(false); 301 } 302 303 try { 304 loadAllDefinitions(); 305 306 } catch (JahiaException e) { 307 logger.warn("Could not reload the Field Definitions.", e); 308 } 309 } 310 311 public void onCachePut(String cacheName, Object entryKey) { 312 } 314 315 } 316 | Popular Tags |