001 // Copyright 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.hivemind.impl.BaseLocatable;
019 import org.apache.hivemind.util.Defense;
020 import org.apache.hivemind.util.PropertyUtils;
021 import org.apache.tapestry.IBeanProvider;
022
023 /**
024 * Performs "lightweight" initialization of a bean; this is based on a term (and code) from
025 * HiveMind, where the settings for a bean's properties are expressed as a single string.
026 *
027 * @author Howard M. Lewis Ship
028 * @since 4.0
029 */
030 public class LightweightBeanInitializer extends BaseLocatable implements IBeanInitializer
031 {
032 private final String _initializer;
033
034 public LightweightBeanInitializer(String initializer)
035 {
036 Defense.notNull(initializer, "initializer");
037
038 _initializer = initializer;
039 }
040
041 public void setBeanProperty(IBeanProvider provider, Object bean)
042 {
043 try
044 {
045 PropertyUtils.configureProperties(bean, _initializer);
046 }
047 catch (Exception ex)
048 {
049 throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex);
050 }
051 }
052
053 /**
054 * Returns the initializer itself, rather than any single property name.
055 */
056 public String getPropertyName()
057 {
058 return _initializer;
059 }
060
061 }