1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
package org.webmacro.directive; |
24 | |
|
25 | |
import org.slf4j.Logger; |
26 | |
import org.slf4j.LoggerFactory; |
27 | |
import org.webmacro.Broker; |
28 | |
import org.webmacro.InitException; |
29 | |
import org.webmacro.Provider; |
30 | |
import org.webmacro.engine.IntrospectionException; |
31 | |
import org.webmacro.util.Settings; |
32 | |
|
33 | |
import java.util.Hashtable; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 6 | public final class DirectiveProvider implements Provider |
39 | |
{ |
40 | |
|
41 | 2 | static Logger _log = LoggerFactory.getLogger(DirectiveProvider.class); |
42 | |
|
43 | |
public static final String DIRECTIVE_KEY = "directive"; |
44 | |
|
45 | |
|
46 | |
|
47 | 6 | private final Hashtable _descriptors = new Hashtable(); |
48 | |
private Broker _broker; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 12 | private class SettingHandler extends Settings.ListSettingHandler |
56 | |
{ |
57 | |
|
58 | |
public void processSetting (String settingKey, String settingValue) |
59 | |
{ |
60 | |
try |
61 | |
{ |
62 | 120 | registerDirective(settingValue, settingKey); |
63 | |
} |
64 | 0 | catch (Exception ce) |
65 | |
{ |
66 | 0 | _log.warn("Exception loading directive " + settingValue, ce); |
67 | 120 | } |
68 | 120 | } |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public final void registerDescriptor (DirectiveDescriptor dd, String dirName) |
89 | |
{ |
90 | 0 | _descriptors.put(dirName, dd); |
91 | 0 | } |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public final void registerDirective (String dirClassName, String dirName) |
101 | |
throws IntrospectionException, InitException |
102 | |
{ |
103 | 120 | Class directive = null; |
104 | |
DirectiveDescriptor templateDesc, newDesc, oldDesc; |
105 | |
try |
106 | |
{ |
107 | 120 | directive = _broker.classForName(dirClassName); |
108 | |
} |
109 | 0 | catch (Exception e) |
110 | |
{ |
111 | 0 | throw new IntrospectionException("No class " + dirClassName, e); |
112 | 120 | } |
113 | |
|
114 | |
|
115 | 120 | if (Directive.class.isAssignableFrom(directive)) |
116 | |
{ |
117 | |
try |
118 | |
{ |
119 | 120 | templateDesc = (DirectiveDescriptor) |
120 | |
directive.getMethod("getDescriptor", (Class[])null).invoke(null, (Object[])null); |
121 | 120 | newDesc = new DirectiveDescriptor(templateDesc.name, |
122 | |
templateDesc.dirClass, |
123 | |
templateDesc.args, |
124 | |
templateDesc.subdirectives); |
125 | 120 | if (newDesc.dirClass == null) |
126 | 120 | newDesc.dirClass = directive; |
127 | |
} |
128 | 0 | catch (Exception e) |
129 | |
{ |
130 | 0 | throw new IntrospectionException("Class " + dirClassName |
131 | |
+ " does not have a getDescriptor() method", e); |
132 | 120 | } |
133 | |
|
134 | |
|
135 | |
|
136 | 120 | Class[] cArg = {Broker.class}; |
137 | |
try |
138 | |
{ |
139 | 120 | java.lang.reflect.Method m = directive.getMethod("init", cArg); |
140 | 6 | Object[] brokerArg = {_broker}; |
141 | |
try |
142 | |
{ |
143 | 6 | m.invoke(null, brokerArg); |
144 | |
} |
145 | 0 | catch (Exception e) |
146 | |
{ |
147 | 0 | _log.warn("Unable to invoke the init method for the directive " |
148 | |
+ directive.getName(), e); |
149 | 6 | } |
150 | |
} |
151 | 114 | catch (Exception e) |
152 | |
{ |
153 | 6 | } |
154 | |
|
155 | 120 | newDesc.name = (dirName != null && !dirName.equals("")) |
156 | |
? dirName : templateDesc.name; |
157 | 120 | oldDesc = (DirectiveDescriptor) _descriptors.get(newDesc.name); |
158 | 120 | if (oldDesc == null) |
159 | |
{ |
160 | 120 | _descriptors.put(newDesc.name, newDesc); |
161 | 120 | _log.info("Registered directive: " + newDesc.name); |
162 | |
} |
163 | 0 | else if (newDesc.dirClass != oldDesc.dirClass) |
164 | |
{ |
165 | 0 | throw new InitException("Attempt to register directive " + directive |
166 | |
+ " failed because " + oldDesc.dirClass.getName() |
167 | |
+ " is already registered for type " + newDesc.name); |
168 | |
} |
169 | |
} |
170 | 120 | } |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
private final DirectiveDescriptor getDescriptor (String directiveName) |
176 | |
{ |
177 | 1288 | return (DirectiveDescriptor) _descriptors.get(directiveName); |
178 | |
} |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public String getType () |
185 | |
{ |
186 | 6 | return DIRECTIVE_KEY; |
187 | |
} |
188 | |
|
189 | |
public void init (Broker broker, Settings config) throws InitException |
190 | |
{ |
191 | 6 | _broker = broker; |
192 | |
try |
193 | |
{ |
194 | 6 | config.processListSetting("Directives", new SettingHandler()); |
195 | |
} |
196 | 0 | catch (Exception e) |
197 | |
{ |
198 | 0 | _log.warn("Error initializing DirectiveProvider", e); |
199 | 0 | throw new InitException("Could not initialize DirectiveProvider", e); |
200 | 6 | } |
201 | 6 | } |
202 | |
|
203 | |
public void destroy () |
204 | |
{ |
205 | 0 | _descriptors.clear(); |
206 | 0 | } |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
public Object get (String name) |
213 | |
{ |
214 | 1288 | return getDescriptor(name); |
215 | |
} |
216 | |
|
217 | |
public void flush () |
218 | |
{ |
219 | 0 | } |
220 | |
} |
221 | |
|
222 | |
|