1 15 package org.apache.tapestry.services.impl; 16 17 import java.util.Locale ; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.ClassResolver; 21 import org.apache.tapestry.IEngine; 22 import org.apache.tapestry.services.EngineFactory; 23 import org.apache.tapestry.spec.IApplicationSpecification; 24 25 33 public class EngineFactoryImpl implements EngineFactory 34 { 35 private IApplicationSpecification _applicationSpecification; 36 private String _defaultEngineClassName; 37 private EngineConstructor _constructor; 38 private ClassResolver _classResolver; 39 40 interface EngineConstructor 41 { 42 IEngine construct(); 43 } 44 45 46 50 static class ReflectiveEngineConstructor implements EngineConstructor 51 { 52 private Class _engineClass; 53 54 ReflectiveEngineConstructor(Class engineClass) 55 { 56 _engineClass = engineClass; 57 } 58 59 public IEngine construct() 60 { 61 try 62 { 63 return (IEngine) _engineClass.newInstance(); 64 } 65 catch (Exception ex) 66 { 67 throw new ApplicationRuntimeException( 68 ImplMessages.errorInstantiatingEngine(_engineClass, ex), 69 ex); 70 } 71 } 72 } 73 74 public void initializeService() 75 { 76 String engineClassName = _applicationSpecification.getEngineClassName(); 77 78 80 if (engineClassName == null) 81 engineClassName = _defaultEngineClassName; 82 83 Class engineClass = _classResolver.findClass(engineClassName); 84 85 _constructor = new ReflectiveEngineConstructor(engineClass); 86 } 87 88 public IEngine constructNewEngineInstance(Locale locale) 89 { 90 IEngine result = _constructor.construct(); 91 92 result.setLocale(locale); 93 94 return result; 95 } 96 97 public void setApplicationSpecification(IApplicationSpecification specification) 98 { 99 _applicationSpecification = specification; 100 } 101 102 public void setClassResolver(ClassResolver resolver) 103 { 104 _classResolver = resolver; 105 } 106 107 public void setDefaultEngineClassName(String string) 108 { 109 _defaultEngineClassName = string; 110 } 111 112 } 113 | Popular Tags |