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.bean; 016 017 import org.apache.hivemind.ApplicationRuntimeException; 018 import org.apache.tapestry.IBeanProvider; 019 import org.apache.tapestry.IComponent; 020 import org.apache.tapestry.services.ExpressionEvaluator; 021 022 /** 023 * Initializes a helper bean property from an OGNL expression (relative to the bean's 024 * {@link IComponent}). 025 * 026 * @author Howard Lewis Ship 027 * @since 2.2 028 */ 029 030 public class ExpressionBeanInitializer extends AbstractBeanInitializer 031 { 032 protected String _expression; 033 034 private final ExpressionEvaluator _evaluator; 035 036 public ExpressionBeanInitializer(ExpressionEvaluator evaluator) 037 { 038 _evaluator = evaluator; 039 } 040 041 public void setBeanProperty(IBeanProvider provider, Object bean) 042 { 043 IComponent component = provider.getComponent(); 044 045 try 046 { 047 Object value = _evaluator.read(component, _expression); 048 049 setBeanProperty(bean, value); 050 } 051 catch (Exception ex) 052 { 053 throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex); 054 } 055 } 056 057 /** @since 3.0 * */ 058 059 public String getExpression() 060 { 061 return _expression; 062 } 063 064 /** @since 3.0 * */ 065 066 public void setExpression(String expression) 067 { 068 _expression = expression; 069 } 070 071 }