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 | 0 | 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 | 0 | static class ReflectiveEngineConstructor implements EngineConstructor |
64 | |
{ |
65 | |
private Class _engineClass; |
66 | |
|
67 | |
ReflectiveEngineConstructor(Class engineClass) |
68 | 0 | { |
69 | 0 | _engineClass = engineClass; |
70 | 0 | } |
71 | |
|
72 | |
public IEngine construct() |
73 | |
{ |
74 | |
try |
75 | |
{ |
76 | 0 | return (IEngine) _engineClass.newInstance(); |
77 | |
} |
78 | 0 | catch (Exception ex) |
79 | |
{ |
80 | 0 | throw new ApplicationRuntimeException(ImplMessages.errorInstantiatingEngine(_engineClass, ex), ex); |
81 | |
} |
82 | |
} |
83 | |
} |
84 | |
|
85 | |
public void initializeService() |
86 | |
{ |
87 | 0 | String engineClassName = _applicationSpecification.getEngineClassName(); |
88 | |
|
89 | |
|
90 | |
|
91 | 0 | if (engineClassName == null) |
92 | 0 | engineClassName = _defaultEngineClassName; |
93 | |
|
94 | 0 | Class engineClass = _classResolver.checkForClass(engineClassName); |
95 | |
|
96 | 0 | if (engineClass == null) |
97 | |
{ |
98 | 0 | _errorLog.error(ImplMessages.engineClassNotFound(engineClassName), null, null); |
99 | 0 | engineClass = BaseEngine.class; |
100 | |
} |
101 | |
|
102 | 0 | _constructor = new ReflectiveEngineConstructor(engineClass); |
103 | 0 | } |
104 | |
|
105 | |
public IEngine constructNewEngineInstance(Locale locale) |
106 | |
{ |
107 | 0 | IEngine result = _constructor.construct(); |
108 | |
|
109 | 0 | result.setLocale(locale); |
110 | |
|
111 | 0 | return result; |
112 | |
} |
113 | |
|
114 | |
public void setApplicationSpecification(IApplicationSpecification specification) |
115 | |
{ |
116 | 0 | _applicationSpecification = specification; |
117 | 0 | } |
118 | |
|
119 | |
public void setClassResolver(ClassResolver resolver) |
120 | |
{ |
121 | 0 | _classResolver = resolver; |
122 | 0 | } |
123 | |
|
124 | |
public void setDefaultEngineClassName(String string) |
125 | |
{ |
126 | 0 | _defaultEngineClassName = string; |
127 | 0 | } |
128 | |
|
129 | |
public void setErrorLog(ErrorLog errorLog) |
130 | |
{ |
131 | 0 | _errorLog = errorLog; |
132 | 0 | } |
133 | |
|
134 | |
} |