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 028 StateObjectFactory 029 { 030 031 private ClassResolver _classResolver; 032 033 private String _className; 034 035 public Object createStateObject() 036 { 037 try 038 { 039 Class c = _classResolver.findClass(_className); 040 041 return c.newInstance(); 042 } 043 catch (Exception ex) 044 { 045 throw new ApplicationRuntimeException(StateMessages 046 .unableToInstantiateObject(_className, ex), getLocation(), 047 ex); 048 } 049 } 050 051 public void setClassName(String className) 052 { 053 _className = className; 054 } 055 056 public String getClassName() 057 { 058 return _className; 059 } 060 061 public void setClassResolver(ClassResolver classResolver) 062 { 063 _classResolver = classResolver; 064 } 065 066 }