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