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 public abstract class AbstractConfigApp implements App {
85
86 protected static MelatiConfig melatiConfig;
87
88 protected PrintStream output = System.out;
89
90
91
92
93
94
95
96
97
98
99 public Melati init(String[] args) throws MelatiException {
100 melatiConfig = melatiConfig();
101 String[] argumentsWithoutOutput = applyNamedArguments(args);
102 MelatiWriter out = new MelatiSimpleWriter(new OutputStreamWriter(output));
103 Melati melati = new Melati(melatiConfig, out);
104 melati.setArguments(argumentsWithoutOutput);
105 melati.setPoemContext(poemContext(melati));
106
107 return melati;
108 }
109
110
111
112
113
114
115
116
117 public void term(Melati melati) throws IOException {
118 melati.write();
119 }
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 Properties servletProperties = MelatiConfig.getProperties();
149
150 if (servletProperties.getProperty("org.melati.MelatiConfig.templateEngine").equals(
151 "org.melati.template.webmacro.WebmacroServletTemplateEngine"))
152 servletProperties.setProperty("org.melati.MelatiConfig.templateEngine",
153 "org.melati.template.webmacro.WebmacroTemplateEngine");
154 if (servletProperties.getProperty("org.melati.MelatiConfig.templateEngine").equals(
155 "org.melati.template.webmacro.VelocityServletTemplateEngine"))
156 servletProperties.setProperty("org.melati.MelatiConfig.templateEngine",
157 "org.melati.template.webmacro.VelocityTemplateEngine");
158 if (servletProperties.getProperty("org.melati.MelatiConfig.templateEngine").equals(
159 "org.melati.template.webmacro.FreemarkerServletTemplateEngine"))
160 servletProperties.setProperty("org.melati.MelatiConfig.templateEngine",
161 "org.melati.template.webmacro.FreemarkerTemplateEngine");
162
163 if (servletProperties.getProperty("org.melati.MelatiConfig.accessHandler").equals(
164 "org.melati.login.HttpBasicAuthenticationAccessHandler"))
165 servletProperties.setProperty("org.melati.MelatiConfig.accessHandler",
166 "org.melati.login.OpenAccessHandler");
167 if (servletProperties.getProperty("org.melati.MelatiConfig.accessHandler").equals(
168 "org.melati.login.HttpSessionAccessHandler"))
169 servletProperties.setProperty("org.melati.MelatiConfig.accessHandler",
170 "org.melati.login.OpenAccessHandler");
171
172
173 MelatiConfig config = new MelatiConfig(servletProperties);
174
175 return config;
176 }
177
178
179
180
181 public void run(String[] args) throws Exception {
182 Melati melati = init(args);
183 try {
184 doConfiguredRequest(melati);
185 } finally {
186 term(melati);
187 PoemDatabaseFactory.getPoemShutdownThread().run();
188 }
189 }
190
191
192
193
194
195
196 public String getSysAdminName() {
197 return "nobody";
198 }
199
200
201
202
203
204
205 public String getSysAdminEmail() {
206 return "nobody@nobody.com";
207 }
208
209
210
211
212
213
214
215
216 protected PoemContext poemContext(Melati melati) {
217 PoemContext it = new PoemContext();
218 String[] arguments = melati.getArguments();
219 if (arguments.length > 0)
220 it.setMethod(arguments[arguments.length - 1]);
221 return it;
222 }
223
224 protected String[] applyNamedArguments(String[] arguments) {
225 String[] unnamedArguments = new String[] {};
226 boolean nextIsOutput = false;
227 for (int i = 0; i < arguments.length; i++) {
228 if (arguments[i].startsWith("-o"))
229 nextIsOutput = true;
230 else if (nextIsOutput) {
231 setOutput(arguments[i]);
232 nextIsOutput = false;
233 } else {
234 unnamedArguments = (String[])ArrayUtils.added(unnamedArguments,
235 arguments[i]);
236 }
237 }
238
239 return unnamedArguments;
240 }
241
242 public void setOutput(String path) {
243 File outputFile;
244 try {
245 outputFile = new File(path).getCanonicalFile();
246 } catch (IOException e) {
247 throw new MelatiIOException(e);
248 }
249 File parent = new File(outputFile.getParent());
250 parent.mkdirs();
251 try {
252 outputFile.createNewFile();
253 } catch (IOException e) {
254 throw new MelatiIOException(
255 "Could not create " + outputFile.getAbsolutePath(), e);
256 }
257 try {
258 setOutput(new PrintStream(new FileOutputStream(outputFile)));
259 } catch (FileNotFoundException e) {
260 throw new MelatiIOException(e);
261 }
262 }
263
264
265
266
267
268
269 public void setOutput(PrintStream out) {
270 output = out;
271 }
272
273
274
275
276
277
278
279
280
281 protected abstract void doConfiguredRequest(final Melati melati)
282 throws Exception;
283
284 }