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 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 public static final String 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 53 public Object evalutateExpression(String expr, 54 Expression e, 55 JexlContext jc) throws Exception { 56 if ( expr == null || expressionsCache == null ){ 57 return e.evaluate(jc); 58 } 59 60 Object 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 |