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.ResetEventListener;
024    import org.apache.tapestry.services.ExpressionCache;
025    
026    /**
027     * @author Howard M. Lewis Ship
028     * @since 4.0
029     */
030    public class ExpressionCacheImpl implements ExpressionCache, ResetEventListener
031    {
032        public synchronized void resetEventDidOccur()
033        {
034            _cache.clear();
035        }
036    
037        private Map _cache = new HashMap();
038    
039        public synchronized Object getCompiledExpression(String expression)
040        {
041            Object result = _cache.get(expression);
042    
043            if (result == null)
044            {
045                result = parse(expression);
046                _cache.put(expression, result);
047            }
048    
049            return result;
050        }
051    
052        private Object parse(String expression)
053        {
054            try
055            {
056                return Ognl.parseExpression(expression);
057            }
058            catch (Exception ex)
059            {
060                throw new ApplicationRuntimeException(ImplMessages.unableToParseExpression(
061                        expression,
062                        ex), ex);
063            }
064        }
065    
066    }