KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > registries > JahiaContainerDefinitionsRegistry


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 //
14
// JahiaContainerDefinitionsRegistry
15

16 package org.jahia.registries;
17
18 import java.util.Enumeration JavaDoc;
19 import java.util.Hashtable JavaDoc;
20 import java.util.Vector JavaDoc;
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 JavaDoc KEY_SEPARATOR = "###";
38
39     public static final String JavaDoc CONTAINER_DEFINITION_BY_ID_CACHE = "ContainerDefinitionByID";
40     public static final String JavaDoc 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     } // end constructor
60

61     /***
62      * registry accessor
63      * @return the registry object
64      *
65      */

66     public static synchronized JahiaContainerDefinitionsRegistry getInstance () {
67         if (theObject == null) {
68             theObject = new JahiaContainerDefinitionsRegistry();
69         }
70         return theObject;
71     } // end getInstance
72

73     /***
74      * calls the loadAllDefinitions method
75      *
76      */

77     public void init ()
78         throws JahiaException {
79         loadAllDefinitions();
80     } // end init
81

82     /***
83      * loads all container definitions from the database
84      *
85      * calls load_container_definitions in JahaiContainersDBService
86      *
87      */

88     private void loadAllDefinitions ()
89         throws JahiaException {
90         Vector JavaDoc 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 JavaDoc)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 JavaDoc 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 JavaDoc(currentDefinition.getID()),
138             currentDefinition);
139         containerDefBySiteIDAndName.put(buildCacheKey(currentDefinition
140             .getName(), currentDefinition.getJahiaID()), currentDefinition);
141     }
142     /***
143      * gets a definition in the registry through its definition ID
144      *
145      * @param defID the definition ID
146      * @return a JahiaContainerDefinition object; null if not found
147      * @see org.jahia.data.containers.JahiaContainerDefinition
148      *
149      * @exception JahiaException raises a critical JahiaException if no definition found
150      *
151      */

152     public JahiaContainerDefinition getDefinition(int defID) throws JahiaException {
153
154         JahiaContainerDefinition currentDef = (JahiaContainerDefinition) containerDefByID.get(new Integer JavaDoc(defID));
155         if (currentDef == null) {
156             synchronized (this) {
157                 currentDef = (JahiaContainerDefinition) containerDefByID.get(new Integer JavaDoc(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     } // end getDefinition
169

170     /***************************************************************************
171      * gets a definition in the registry through its page definition id and
172      * container name
173      *
174      * @param siteID
175      * the site id
176      * @param containerName
177      * the container name
178      * @return a JahiaContainerDefinition object; null if not found
179      * @see org.jahia.data.containers.JahiaContainerDefinition
180      *
181      *
182      */

183     public JahiaContainerDefinition getDefinition(int siteID, String JavaDoc containerName) throws JahiaException {
184
185         Object JavaDoc 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     } // end getDefinition
201

202     /***************************************************************************
203      * gets all definitions of a page template in the registry
204      *
205      * @param pageDefID
206      * the page definition id
207      * @return a JahiaContainerDefinition object; null if not found
208      * @see org.jahia.data.containers.JahiaContainerDefinition
209      *
210      *
211      */

212     public Vector JavaDoc getDefinitionsInTemplate (int pageDefID) {
213         Vector JavaDoc theDefs = new Vector JavaDoc();
214         Object JavaDoc[] containerDefKeys = containerDefByID.keys();
215         for (int i = 0; i < containerDefKeys.length; i++) {
216             Integer JavaDoc currentDefID = (Integer JavaDoc) containerDefKeys[i];
217             JahiaContainerDefinition theContainerDef = (
218                 JahiaContainerDefinition) containerDefByID.get(currentDefID);
219             Hashtable JavaDoc subDefs = theContainerDef.getSubDefs();
220             Enumeration JavaDoc 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     } // end getDefinitionsInTemplate
233

234     /***
235      * sets a definition in the registry, and synchronizes it with the database
236      *
237      * @param theContainerDef the JahiaContainerDefinition object to set
238      * @see org.jahia.data.containers.JahiaContainerDefinition
239      *
240      */

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             // container definition already exists, just have to update
249
ServicesRegistry.getInstance().getJahiaContainersService(
250                 ).saveContainerDefinition(theContainerDef);
251             containerDefByID.put(new Integer JavaDoc(aContainerDef.getID()),
252                                  theContainerDef);
253             containerDefBySiteIDAndName.put(buildCacheKey(aContainerDef.getName(),
254                 aContainerDef.getJahiaID()), theContainerDef);
255         } else {
256             // container definition doesn't exist, need to add to registry
257
ServicesRegistry.getInstance().getJahiaContainersService(
258                 ).saveContainerDefinition(theContainerDef);
259             containerDefByID.put(new Integer JavaDoc(theContainerDef.getID()),
260                                  theContainerDef);
261             containerDefBySiteIDAndName.put(buildCacheKey(theContainerDef.
262                 getName(),
263                 theContainerDef.getJahiaID()), theContainerDef);
264         }
265     } // end setDefinition
266

267     /***
268      * remove a Container Definition
269      */

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 JavaDoc(ctnDefID));
276             containerDefBySiteIDAndName.remove(buildCacheKey(currentDef.getName(),
277                 currentDef.getJahiaID()));
278             }
279         }
280     } // end removeContainerDefinition
281

282     private String JavaDoc buildCacheKey (String JavaDoc containerDefinitionName, int siteID) {
283         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
284         result.append(containerDefinitionName);
285         result.append(KEY_SEPARATOR);
286         result.append(siteID);
287         return result.toString();
288     }
289
290     /**
291      * This method is called each time the cache flushes its items.
292      *
293      * @param cacheName the name of the cache which flushed its items.
294      */

295     public void onCacheFlush(String JavaDoc 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 JavaDoc cacheName, Object JavaDoc entryKey) {
312         // do nothing;
313
}
314
315 }
316
Popular Tags