1 13 17 package org.jahia.registries; 18 19 import java.util.Enumeration ; 20 import java.util.Vector ; 21 22 import org.apache.log4j.Logger; 23 import org.jahia.data.fields.JahiaFieldDefinition; 24 import org.jahia.exceptions.JahiaException; 25 import org.jahia.services.cache.Cache; 26 import org.jahia.services.cache.CacheFactory; 27 import org.jahia.services.cache.CacheListener; 28 29 public class JahiaFieldDefinitionsRegistry implements CacheListener { 30 31 private static Logger logger = Logger.getLogger(JahiaFieldDefinitionsRegistry.class); 32 33 private static JahiaFieldDefinitionsRegistry instance = null; 34 35 private static final String KEY_SEPARATOR = "###"; 36 37 public static final String FIELD_DEFINITION_BY_ID_CACHE = "FieldDefinitionsByID"; 38 public static final String FIELD_DEFINITION_BY_SITE_AND_NAME_CACHE = 39 "FieldDefinitionsBySiteAndName"; 40 private Cache fieldDefTable; 41 private Cache fieldDefSiteAndNameTable; 42 43 private JahiaFieldDefinitionsRegistry () { 44 logger.debug("Starting FieldDefinitions Registry"); 45 try { 46 fieldDefTable = CacheFactory.createCache (FIELD_DEFINITION_BY_ID_CACHE); 47 fieldDefTable.registerListener(this); 48 49 fieldDefSiteAndNameTable = CacheFactory.createCache (FIELD_DEFINITION_BY_SITE_AND_NAME_CACHE); 50 fieldDefSiteAndNameTable.registerListener(this); 51 52 } catch (JahiaException je) { 53 logger.error( 54 "Error while creating caches for JahiaFieldDefinition registry.", je); 55 } 56 } 57 58 public static synchronized JahiaFieldDefinitionsRegistry getInstance () { 59 if (instance == null) { 60 instance = new JahiaFieldDefinitionsRegistry(); 61 } 62 return instance; 63 } 64 65 public void init () throws JahiaException { 66 loadAllDefinitions(); 67 } 68 69 private void loadAllDefinitions () 70 throws JahiaException { 71 Vector ids = ServicesRegistry.getInstance().getJahiaFieldService(). 72 getAllFieldDefinitionIDs(); 73 Enumeration fieldDefIDEnum = ids.elements(); 74 while (fieldDefIDEnum.hasMoreElements()) { 75 Integer currentID = ( (Integer ) fieldDefIDEnum.nextElement()); 76 JahiaFieldDefinition curDefinition = ServicesRegistry.getInstance(). 77 getJahiaFieldService(). 78 loadFieldDefinition(currentID. 79 intValue()); 80 addToCache(curDefinition); 81 } 82 } 83 84 private JahiaFieldDefinition loadDefinitionByID (int defID) 85 throws JahiaException { 86 JahiaFieldDefinition curDefinition = ServicesRegistry.getInstance(). 87 getJahiaFieldService(). 88 loadFieldDefinition(defID); 89 if (curDefinition != null) { 90 addToCache(curDefinition); 91 } 92 return curDefinition; 93 } 94 95 private JahiaFieldDefinition loadDefinitionBySiteIDAndName (int siteID, String definitionName) 96 throws JahiaException { 97 JahiaFieldDefinition curDefinition = ServicesRegistry.getInstance(). 98 getJahiaFieldService(). 99 loadFieldDefinition(siteID, definitionName); 100 if (curDefinition != null) { 101 addToCache(curDefinition); 102 } 103 return curDefinition; 104 } 105 106 private synchronized void addToCache(JahiaFieldDefinition curDefinition) { 107 fieldDefTable.put(new Integer (curDefinition.getID()), curDefinition); 108 fieldDefSiteAndNameTable.put(buildCacheKey(curDefinition.getName(), 109 curDefinition.getJahiaID()), curDefinition); 110 } 111 112 public JahiaFieldDefinition getDefinition (int defID) 113 throws JahiaException { 114 JahiaFieldDefinition result = (JahiaFieldDefinition) fieldDefTable.get(new 115 Integer (defID)); 116 if (result == null) { 117 result = loadDefinitionByID(defID); 118 } 119 120 if (result != null) { 121 return result; 122 } 123 String errorMsg = "Could not find the definition ID " + defID; 124 logger.error(errorMsg + " -> BAILING OUT"); 125 throw new JahiaException("Database synchronisation error", 126 errorMsg, JahiaException.REGISTRY_ERROR, 127 JahiaException.CRITICAL_SEVERITY); 128 } 130 131 public JahiaFieldDefinition getDefinition (int siteID, String fieldName) 133 throws JahiaException { 134 JahiaFieldDefinition result = (JahiaFieldDefinition) 135 fieldDefSiteAndNameTable.get( 136 buildCacheKey(fieldName, siteID)); 137 if (result == null) { 138 result = loadDefinitionBySiteIDAndName(siteID, fieldName); 139 } 140 if (result == null) { 141 logger.debug("Couldn't find field definition for siteID=" + siteID + " and name=" + fieldName); 142 } 143 return result; 144 } 146 public void setDefinition (JahiaFieldDefinition theFieldDef) 147 throws JahiaException { 148 149 JahiaFieldDefinition aFieldDef = getDefinition(theFieldDef.getJahiaID(), 150 theFieldDef.getName()); 151 if (aFieldDef != null) { 152 theFieldDef.setID(aFieldDef.getID()); 155 } else { 156 theFieldDef.setID(0); 159 } 160 ServicesRegistry.getInstance().getJahiaFieldService(). 161 saveFieldDefinition(theFieldDef); 162 addToCache(theFieldDef); 163 } 165 public void removeFieldDefinition (int fieldDefID) 166 throws JahiaException { 167 JahiaFieldDefinition fieldDef = getDefinition(fieldDefID); 168 if (fieldDef != null) { 169 synchronized (this){ 170 fieldDefTable.remove(new Integer (fieldDefID)); 171 fieldDefSiteAndNameTable.remove(buildCacheKey(fieldDef.getName(), 172 fieldDef.getJahiaID())); 173 } 174 } 175 176 } 178 private String buildCacheKey (String fieldDefinitionName, int siteID) { 179 StringBuffer result = new StringBuffer (); 180 result.append(fieldDefinitionName); 181 result.append(KEY_SEPARATOR); 182 result.append(siteID); 183 return result.toString(); 184 } 185 186 191 public void onCacheFlush(String cacheName) { 192 if (FIELD_DEFINITION_BY_ID_CACHE.equals(cacheName)) { 193 fieldDefSiteAndNameTable.flush (false); 194 195 } else if (FIELD_DEFINITION_BY_SITE_AND_NAME_CACHE.equals(cacheName)) { 196 fieldDefTable.flush (false); 197 } 198 199 try { 200 loadAllDefinitions(); 201 202 } catch (JahiaException e) { 203 logger.warn("Could not reload the Field Definitions.", e); 204 } 205 } 206 207 public void onCachePut(String cacheName, Object entryKey) { 208 } 210 211 } 212 | Popular Tags |