KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > fields > ExpressionMarkerCache


1 package org.jahia.data.fields;
2
3 import org.jahia.exceptions.JahiaException;
4 import org.jahia.services.cache.Cache;
5 import org.jahia.services.cache.CacheFactory;
6
7 import org.apache.commons.jexl.Expression;
8 import org.apache.commons.jexl.JexlContext;
9
10 /**
11  * Default Cache for expression marker ( the cache key is the String expression )
12  *
13  * <p>Title: </p>
14  * <p>Description: </p>
15  * <p>Copyright: Copyright (c) 2002</p>
16  * <p>Company: </p>
17  * @author not attributable
18  * @version 1.0
19  */

20 public class ExpressionMarkerCache {
21
22     private static org.apache.log4j.Logger logger =
23         org.apache.log4j.Logger.getLogger(ExpressionMarkerCache.class);
24
25     static ExpressionMarkerCache instance = null;
26
27     // the Default Expression Marker Cache.
28
public static final String JavaDoc EXPRESSION_MARKER_CACHE = "ExpressionMarkerCache";
29     Cache expressionsCache = null;
30
31     protected ExpressionMarkerCache () {
32         try {
33             expressionsCache = CacheFactory.createCache(EXPRESSION_MARKER_CACHE);
34         } catch ( JahiaException je ){
35             logger.debug(" Exception creating cache",je);
36         }
37     }
38
39     public static synchronized ExpressionMarkerCache getInstance(){
40         if ( instance == null ){
41             instance = new ExpressionMarkerCache();
42         }
43         return instance;
44     }
45
46     /**
47      * Return from cache if any, else call ExpressionMarker.getValue()
48      *
49      * @param expr
50      * @param expressionMarker
51      * @return
52      */

53     public Object JavaDoc evalutateExpression(String JavaDoc expr,
54                                       Expression e,
55                                       JexlContext jc) throws Exception JavaDoc{
56         if ( expr == null || expressionsCache == null ){
57             return e.evaluate(jc);
58         }
59
60         Object JavaDoc res = null;
61         if ( expressionsCache != null ){
62             res = expressionsCache.get(expr);
63         }
64         if ( res == null ){
65             res = e.evaluate(jc);
66         }
67         if ( res != null ){
68             expressionsCache.put(expr, res);
69         }
70         return res;
71     }
72 }
73
Popular Tags