001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.services.impl; 016 017 import java.util.HashMap; 018 import java.util.Map; 019 020 import ognl.Ognl; 021 022 import org.apache.hivemind.ApplicationRuntimeException; 023 import org.apache.tapestry.event.ReportStatusEvent; 024 import org.apache.tapestry.event.ReportStatusListener; 025 import org.apache.tapestry.event.ResetEventListener; 026 import org.apache.tapestry.services.ExpressionCache; 027 028 /** 029 * @author Howard M. Lewis Ship 030 * @since 4.0 031 */ 032 public class ExpressionCacheImpl implements ExpressionCache, ResetEventListener, 033 ReportStatusListener 034 { 035 private String _serviceId; 036 037 private Map _cache = new HashMap(); 038 039 public synchronized void resetEventDidOccur() 040 { 041 _cache.clear(); 042 } 043 044 public void reportStatus(ReportStatusEvent event) 045 { 046 event.title(_serviceId); 047 048 event.property("cached expression count", _cache.size()); 049 event.collection("cached expressions", _cache.keySet()); 050 } 051 052 public synchronized Object getCompiledExpression(String expression) 053 { 054 Object result = _cache.get(expression); 055 056 if (result == null) 057 { 058 result = parse(expression); 059 _cache.put(expression, result); 060 } 061 062 return result; 063 } 064 065 private Object parse(String expression) 066 { 067 try 068 { 069 return Ognl.parseExpression(expression); 070 } 071 catch (Exception ex) 072 { 073 throw new ApplicationRuntimeException(ImplMessages.unableToParseExpression( 074 expression, 075 ex), ex); 076 } 077 } 078 079 public void setServiceId(String serviceId) 080 { 081 _serviceId = serviceId; 082 } 083 084 }