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.engine.state;
016    
017    import org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.hivemind.ClassResolver;
019    import org.apache.hivemind.impl.BaseLocatable;
020    
021    /**
022     * Used to instantiate the a state object from a configurable class name.
023     * 
024     * @author Howard M. Lewis Ship
025     * @since 4.0
026     */
027    public class NamedClassStateObjectFactory extends BaseLocatable implements StateObjectFactory
028    {
029        private ClassResolver _classResolver;
030    
031        private String _className;
032    
033        public Object createStateObject()
034        {
035            try
036            {
037                Class c = _classResolver.findClass(_className);
038    
039                return c.newInstance();
040            }
041            catch (Exception ex)
042            {
043                throw new ApplicationRuntimeException(StateMessages.unableToInstantiateObject(
044                        _className,
045                        ex), getLocation(), ex);
046            }
047        }
048    
049        public void setClassName(String className)
050        {
051            _className = className;
052        }
053        
054        public String getClassName()
055        {
056            return _className;
057        }
058        
059        public void setClassResolver(ClassResolver classResolver)
060        {
061            _classResolver = classResolver;
062        }
063    
064     
065    }