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.engine; 016 017 import java.util.HashMap; 018 import java.util.Map; 019 020 import org.apache.hivemind.ApplicationRuntimeException; 021 import org.apache.hivemind.ClassResolver; 022 import org.apache.hivemind.Resource; 023 import org.apache.tapestry.IScript; 024 import org.apache.tapestry.Tapestry; 025 import org.apache.tapestry.coerce.ValueConverter; 026 import org.apache.tapestry.event.ResetEventListener; 027 import org.apache.tapestry.script.ScriptParser; 028 import org.apache.tapestry.services.ExpressionEvaluator; 029 import org.apache.tapestry.util.xml.DocumentParseException; 030 031 /** 032 * Provides basic access to scripts available on the classpath. Scripts are cached in memory once 033 * parsed. 034 * 035 * @author Howard Lewis Ship 036 * @since 1.0.2 037 */ 038 039 public class DefaultScriptSource implements IScriptSource, ResetEventListener 040 { 041 private ClassResolver _classResolver; 042 043 /** @since 4.0 */ 044 private ExpressionEvaluator _expressionEvaluator; 045 046 /** @since 4.0 */ 047 private ValueConverter _valueConverter; 048 049 private Map _cache = new HashMap(); 050 051 public synchronized void resetEventDidOccur() 052 { 053 _cache.clear(); 054 } 055 056 public synchronized IScript getScript(Resource resource) 057 { 058 IScript result = (IScript) _cache.get(resource); 059 060 if (result != null) 061 return result; 062 063 result = parse(resource); 064 065 _cache.put(resource, result); 066 067 return result; 068 } 069 070 private IScript parse(Resource resource) 071 { 072 ScriptParser parser = new ScriptParser(_classResolver, _expressionEvaluator, 073 _valueConverter); 074 075 try 076 { 077 return parser.parse(resource); 078 } 079 catch (DocumentParseException ex) 080 { 081 throw new ApplicationRuntimeException(Tapestry.format( 082 "DefaultScriptSource.unable-to-parse-script", 083 resource), ex); 084 } 085 } 086 087 public void setClassResolver(ClassResolver classResolver) 088 { 089 _classResolver = classResolver; 090 } 091 092 /** @since 4.0 */ 093 public void setExpressionEvaluator(ExpressionEvaluator expressionEvaluator) 094 { 095 _expressionEvaluator = expressionEvaluator; 096 } 097 098 /** @since 4.0 */ 099 public void setValueConverter(ValueConverter valueConverter) 100 { 101 _valueConverter = valueConverter; 102 } 103 }