1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
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.hivemind.ErrorLog; |
22 |
| import org.apache.tapestry.IEngine; |
23 |
| import org.apache.tapestry.engine.BaseEngine; |
24 |
| import org.apache.tapestry.services.EngineFactory; |
25 |
| import org.apache.tapestry.spec.IApplicationSpecification; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| public class EngineFactoryImpl implements EngineFactory |
36 |
| { |
37 |
| private IApplicationSpecification _applicationSpecification; |
38 |
| |
39 |
| private String _defaultEngineClassName; |
40 |
| |
41 |
| private EngineConstructor _constructor; |
42 |
| |
43 |
| private ClassResolver _classResolver; |
44 |
| |
45 |
| private ErrorLog _errorLog; |
46 |
| |
47 |
| interface EngineConstructor |
48 |
| { |
49 |
| IEngine construct(); |
50 |
| } |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| static class ReflectiveEngineConstructor implements EngineConstructor |
57 |
| { |
58 |
| private Class _engineClass; |
59 |
| |
60 |
43
| ReflectiveEngineConstructor(Class engineClass)
|
61 |
| { |
62 |
43
| _engineClass = engineClass;
|
63 |
| } |
64 |
| |
65 |
51
| public IEngine construct()
|
66 |
| { |
67 |
51
| try
|
68 |
| { |
69 |
51
| return (IEngine) _engineClass.newInstance();
|
70 |
| } |
71 |
| catch (Exception ex) |
72 |
| { |
73 |
1
| throw new ApplicationRuntimeException(ImplMessages.errorInstantiatingEngine(
|
74 |
| _engineClass, |
75 |
| ex), ex); |
76 |
| } |
77 |
| } |
78 |
| } |
79 |
| |
80 |
43
| public void initializeService()
|
81 |
| { |
82 |
43
| String engineClassName = _applicationSpecification.getEngineClassName();
|
83 |
| |
84 |
| |
85 |
| |
86 |
43
| if (engineClassName == null)
|
87 |
35
| engineClassName = _defaultEngineClassName;
|
88 |
| |
89 |
43
| Class engineClass = _classResolver.checkForClass(engineClassName);
|
90 |
| |
91 |
43
| if (engineClass == null)
|
92 |
| { |
93 |
1
| _errorLog.error(ImplMessages.engineClassNotFound(engineClassName), null, null);
|
94 |
1
| engineClass = BaseEngine.class;
|
95 |
| } |
96 |
| |
97 |
43
| _constructor = new ReflectiveEngineConstructor(engineClass);
|
98 |
| } |
99 |
| |
100 |
51
| public IEngine constructNewEngineInstance(Locale locale)
|
101 |
| { |
102 |
51
| IEngine result = _constructor.construct();
|
103 |
| |
104 |
50
| result.setLocale(locale);
|
105 |
| |
106 |
50
| return result;
|
107 |
| } |
108 |
| |
109 |
43
| public void setApplicationSpecification(IApplicationSpecification specification)
|
110 |
| { |
111 |
43
| _applicationSpecification = specification;
|
112 |
| } |
113 |
| |
114 |
43
| public void setClassResolver(ClassResolver resolver)
|
115 |
| { |
116 |
43
| _classResolver = resolver;
|
117 |
| } |
118 |
| |
119 |
41
| public void setDefaultEngineClassName(String string)
|
120 |
| { |
121 |
41
| _defaultEngineClassName = string;
|
122 |
| } |
123 |
| |
124 |
40
| public void setErrorLog(ErrorLog errorLog)
|
125 |
| { |
126 |
40
| _errorLog = errorLog;
|
127 |
| } |
128 |
| |
129 |
| } |