1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
package org.melati.app; |
46 | |
|
47 | |
import java.io.File; |
48 | |
import java.io.FileNotFoundException; |
49 | |
import java.io.FileOutputStream; |
50 | |
import java.io.IOException; |
51 | |
import java.io.OutputStreamWriter; |
52 | |
import java.io.PrintStream; |
53 | |
import java.util.Properties; |
54 | |
|
55 | |
import org.melati.Melati; |
56 | |
import org.melati.MelatiConfig; |
57 | |
import org.melati.PoemContext; |
58 | |
import org.melati.poem.PoemDatabaseFactory; |
59 | |
import org.melati.poem.util.ArrayUtils; |
60 | |
import org.melati.util.MelatiException; |
61 | |
import org.melati.util.MelatiIOException; |
62 | |
import org.melati.util.MelatiSimpleWriter; |
63 | |
import org.melati.util.MelatiWriter; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | 30 | public abstract class AbstractConfigApp implements App { |
85 | |
|
86 | |
protected static MelatiConfig melatiConfig; |
87 | |
|
88 | 30 | protected PrintStream output = System.out; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public Melati init(String[] args) throws MelatiException { |
100 | 30 | melatiConfig = melatiConfig(); |
101 | 30 | String[] argumentsWithoutOutput = applyNamedArguments(args); |
102 | 30 | MelatiWriter out = new MelatiSimpleWriter(new OutputStreamWriter(output)); |
103 | 30 | Melati melati = new Melati(melatiConfig, out); |
104 | 30 | melati.setArguments(argumentsWithoutOutput); |
105 | 30 | melati.setPoemContext(poemContext(melati)); |
106 | |
|
107 | 29 | return melati; |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public void term(Melati melati) throws IOException { |
118 | 31 | melati.write(); |
119 | 31 | } |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
protected MelatiConfig melatiConfig() throws MelatiException { |
148 | 30 | Properties servletProperties = MelatiConfig.getProperties(); |
149 | |
|
150 | 30 | if (servletProperties.getProperty("org.melati.MelatiConfig.templateEngine").equals( |
151 | |
"org.melati.template.webmacro.WebmacroServletTemplateEngine")) |
152 | 30 | servletProperties.setProperty("org.melati.MelatiConfig.templateEngine", |
153 | |
"org.melati.template.webmacro.WebmacroTemplateEngine"); |
154 | 30 | if (servletProperties.getProperty("org.melati.MelatiConfig.templateEngine").equals( |
155 | |
"org.melati.template.webmacro.VelocityServletTemplateEngine")) |
156 | 0 | servletProperties.setProperty("org.melati.MelatiConfig.templateEngine", |
157 | |
"org.melati.template.webmacro.VelocityTemplateEngine"); |
158 | 30 | if (servletProperties.getProperty("org.melati.MelatiConfig.templateEngine").equals( |
159 | |
"org.melati.template.webmacro.FreemarkerServletTemplateEngine")) |
160 | 0 | servletProperties.setProperty("org.melati.MelatiConfig.templateEngine", |
161 | |
"org.melati.template.webmacro.FreemarkerTemplateEngine"); |
162 | |
|
163 | 30 | if (servletProperties.getProperty("org.melati.MelatiConfig.accessHandler").equals( |
164 | |
"org.melati.login.HttpBasicAuthenticationAccessHandler")) |
165 | 0 | servletProperties.setProperty("org.melati.MelatiConfig.accessHandler", |
166 | |
"org.melati.login.OpenAccessHandler"); |
167 | 30 | if (servletProperties.getProperty("org.melati.MelatiConfig.accessHandler").equals( |
168 | |
"org.melati.login.HttpSessionAccessHandler")) |
169 | 30 | servletProperties.setProperty("org.melati.MelatiConfig.accessHandler", |
170 | |
"org.melati.login.OpenAccessHandler"); |
171 | |
|
172 | |
|
173 | 30 | MelatiConfig config = new MelatiConfig(servletProperties); |
174 | |
|
175 | 30 | return config; |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public void run(String[] args) throws Exception { |
182 | 28 | Melati melati = init(args); |
183 | |
try { |
184 | 25 | doConfiguredRequest(melati); |
185 | |
} finally { |
186 | 25 | term(melati); |
187 | 25 | PoemDatabaseFactory.getPoemShutdownThread().run(); |
188 | 20 | } |
189 | 20 | } |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
public String getSysAdminName() { |
197 | 1 | return "nobody"; |
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
public String getSysAdminEmail() { |
206 | 1 | return "nobody@nobody.com"; |
207 | |
} |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
protected PoemContext poemContext(Melati melati) { |
217 | 2 | PoemContext it = new PoemContext(); |
218 | 2 | String[] arguments = melati.getArguments(); |
219 | 2 | if (arguments.length > 0) |
220 | 2 | it.setMethod(arguments[arguments.length - 1]); |
221 | 2 | return it; |
222 | |
} |
223 | |
|
224 | |
protected String[] applyNamedArguments(String[] arguments) { |
225 | 30 | String[] unnamedArguments = new String[] {}; |
226 | 30 | boolean nextIsOutput = false; |
227 | 208 | for (int i = 0; i < arguments.length; i++) { |
228 | 178 | if (arguments[i].startsWith("-o")) |
229 | 28 | nextIsOutput = true; |
230 | 150 | else if (nextIsOutput) { |
231 | 28 | setOutput(arguments[i]); |
232 | 28 | nextIsOutput = false; |
233 | |
} else { |
234 | 122 | unnamedArguments = (String[])ArrayUtils.added(unnamedArguments, |
235 | |
arguments[i]); |
236 | |
} |
237 | |
} |
238 | |
|
239 | 30 | return unnamedArguments; |
240 | |
} |
241 | |
|
242 | |
public void setOutput(String path) { |
243 | |
File outputFile; |
244 | |
try { |
245 | 28 | outputFile = new File(path).getCanonicalFile(); |
246 | 0 | } catch (IOException e) { |
247 | 0 | throw new MelatiIOException(e); |
248 | 28 | } |
249 | 28 | File parent = new File(outputFile.getParent()); |
250 | 28 | parent.mkdirs(); |
251 | |
try { |
252 | 28 | outputFile.createNewFile(); |
253 | 0 | } catch (IOException e) { |
254 | 0 | throw new MelatiIOException( |
255 | 0 | "Could not create " + outputFile.getAbsolutePath(), e); |
256 | 28 | } |
257 | |
try { |
258 | 28 | setOutput(new PrintStream(new FileOutputStream(outputFile))); |
259 | 0 | } catch (FileNotFoundException e) { |
260 | 0 | throw new MelatiIOException(e); |
261 | 28 | } |
262 | 28 | } |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
public void setOutput(PrintStream out) { |
270 | 28 | output = out; |
271 | 28 | } |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
protected abstract void doConfiguredRequest(final Melati melati) |
282 | |
throws Exception; |
283 | |
|
284 | |
} |