##### Coverage Report - org.melati.app.AbstractConfigApp

Classes in this File | Line Coverage | Branch Coverage | Complexity
AbstractConfigApp |
89% | 50/56
71% | 10/14
| 2.1818181818181817;2.182

1 |  |
     /*
2 |  |
      * $Source: /usr/cvsroot/melati/melati/src/site/resources/withWebmacro/org.melati.app.AbstractConfigApp.html,v $
3 |  |
      * $Revision: 1.1 $
4 |  |
      *
5 |  |
      * Copyright (C) 2005 Tim Pizey
6 |  |
      *
7 |  |
      * Part of Melati (http://melati.org), a framework for the rapid
8 |  |
      * development of clean, maintainable web applications.
9 |  |
      *
10 |  |
      * Melati is free software; Permission is granted to copy, distribute
11 |  |
      * and/or modify this software under the terms either:
12 |  |
      *
13 |  |
      * a) the GNU General Public License as published by the Free Software
14 |  |
      *    Foundation; either version 2 of the License, or (at your option)
15 |  |
      *    any later version,
16 |  |
      *
17 |  |
      *    or
18 |  |
      *
19 |  |
      * b) any version of the Melati Software License, as published
20 |  |
      *    at http://melati.org
21 |  |
      *
22 |  |
      * You should have received a copy of the GNU General Public License and
23 |  |
      * the Melati Software License along with this program;
24 |  |
      * if not, write to the Free Software Foundation, Inc.,
25 |  |
      * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26 |  |
      * GNU General Public License and visit http://melati.org to obtain the
27 |  |
      * Melati Software License.
28 |  |
      *
29 |  |
      * Feel free to contact the Developers of Melati (http://melati.org),
30 |  |
      * if you would like to work out a different arrangement than the options
31 |  |
      * outlined here.  It is our intention to allow Melati to be used by as
32 |  |
      * wide an audience as possible.
33 |  |
      *
34 |  |
      * This program is distributed in the hope that it will be useful,
35 |  |
      * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 |  |
      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37 |  |
      * GNU General Public License for more details.
38 |  |
      *
39 |  |
      * Contact details for copyright holder:
40 |  |
      *
41 |  |
      *     Tim Pizey <timp At paneris.org>
42 |  |
      *     http://paneris.org/~timp
43 |  |
      */
44 |  |
45 |  |
     package org.melati.app;
46 |  |
47 |  |
     import java.io.File;
48 |  |
     import java.io.FileOutputStream;
49 |  |
     import java.io.IOException;
50 |  |
     import java.io.OutputStreamWriter;
51 |  |
     import java.io.PrintStream;
52 |  |
53 |  |
     import org.melati.Melati;
54 |  |
     import org.melati.MelatiConfig;
55 |  |
     import org.melati.PoemContext;
56 |  |
57 |  |
     import org.melati.login.AccessHandler;
58 |  |
     import org.melati.login.HttpBasicAuthenticationAccessHandler;
59 |  |
     import org.melati.login.HttpSessionAccessHandler;
60 |  |
     import org.melati.login.OpenAccessHandler;
61 |  |
     import org.melati.poem.util.ArrayUtils;
62 |  |
     import org.melati.template.webmacro.WebmacroTemplateEngine;
63 |  |
     import org.melati.util.InstantiationPropertyException;
64 |  |
     import org.melati.util.MelatiException;
65 |  |
     import org.melati.util.MelatiWriter;
66 |  |
     import org.melati.util.MelatiSimpleWriter;
67 |  |
68 |  |
     /**
69 |  |
      * ConfigApp is the simplest way to use Melati.
70 |  |
      *
71 |  |
      * All a ConfigApp does is to configure a Melati. Importantly it does not
72 |  |
      * establish a Poem session leaving you to do this for yourself.
73 |  |
      *
74 |  |
      * If you want a POEM session established, please extend {@link AbstractPoemApp}.
75 |  |
      *
76 |  |
      * ConfigApp does set up a basic {@link PoemContext} with the Method set, but
77 |  |
      * not the POEM logicaldatabase, table or troid.
78 |  |
      *
79 |  |
      * The arguments are expected to end with a freeform string telling your
80 |  |
      * application what it is meant to do. This is automatically made available in
81 |  |
      * templates as <TT>$melati.Method</TT>.
82 |  |
      *
83 |  |
      * You can change the way these things are determined by overriding
84 |  |
      * {@link #poemContext}.
85 |  |
      */
86 |  |
87 | 60 |
     public abstract class AbstractConfigApp implements App {
88 |  |
89 |  |
       protected static MelatiConfig melatiConfig;
90 |  |
91 | 60 |
       protected PrintStream output = System.out;
92 |  |
93 |  |
       /**
94 |  |
        * Initialise.
95 |  |
        *
96 |  |
        * @param args
97 |  |
        *          the command line arguments
98 |  |
        * @return a newly created Melati
99 |  |
        * @throws MelatiException
100 |  |
        *           if something goes wrong during initialisation
101 |  |
        */
102 |  |
       public Melati init(String[] args) throws MelatiException {
103 | 60 |
           melatiConfig = melatiConfig();
104 | 60 |
         String[] argumentsWithoutOutput = applyNamedArguments(args);
105 | 60 |
         MelatiWriter out = new MelatiSimpleWriter(new OutputStreamWriter(output));
106 | 60 |
         Melati melati = new Melati(melatiConfig, out);
107 | 60 |
         melati.setArguments(argumentsWithoutOutput);
108 | 60 |
         melati.setPoemContext(poemContext(melati));
109 |  |
110 | 58 |
         return melati;
111 |  |
       }
112 |  |
113 |  |
       /**
114 |  |
        * Clean up at end of run. Overridden in PoemApp.
115 |  |
        *
116 |  |
        * @param melati
117 |  |
        *          the melati
118 |  |
        * @throws IOException if there is an io problem
119 |  |
        */
120 |  |
       public void term(Melati melati) throws IOException {
121 | 62 |
         melati.write();
122 | 62 |
       }
123 |  |
124 |  |
       /**
125 |  |
        * Set application properties from the default properties file.
126 |  |
        *
127 |  |
        * This method will look for a properties file called
128 |  |
        * <tt>org.melati.MelatiConfig.properties</tt>; if it finds that the
129 |  |
        * AccessHandler is an Http handler it will set the access handler to
130 |  |
        * <code>OpenAccessHandler</code>.
131 |  |
        *
132 |  |
        * Similarly ServletTemplateEngine is changed to TemplateEngine.
133 |  |
        *
134 |  |
        * To override any setting from MelatiConfig.properties, simply override this
135 |  |
        * method and return a vaild MelatiConfig.
136 |  |
        *
137 |  |
        * eg to use a different AccessHandler from the default:
138 |  |
        *
139 |  |
        * <PRE>
140 |  |
        * protected MelatiConfig melatiConfig() throws MelatiException {
141 |  |
        *   MelatiConfig config = super.melatiConfig();
142 |  |
        *   config.setAccessHandler(new YourAccessHandler());
143 |  |
        *   return config;
144 |  |
        * }
145 |  |
        * </PRE>
146 |  |
        *
147 |  |
        * @throws MelatiException
148 |  |
        *           if anything goes wrong with Melati
149 |  |
        */
150 |  |
       protected MelatiConfig melatiConfig() throws MelatiException {
151 | 60 |
         MelatiConfig config = new MelatiConfig();
152 |  |
153 | 60 |
         if (config.getAccessHandler() instanceof HttpBasicAuthenticationAccessHandler
154 |  |
                 || config.getAccessHandler() instanceof HttpSessionAccessHandler)
155 |  |
           try {
156 | 60 |
             config.setAccessHandler((AccessHandler)OpenAccessHandler.class
157 |  |
                     .newInstance());
158 | 0 |
           } catch (Exception e) {
159 | 0 |
             throw new InstantiationPropertyException(OpenAccessHandler.class
160 |  |
                     .getName(), e);
161 | 60 |
           }
162 |  |
163 | 60 |
         if (config.getTemplateEngine() instanceof org.melati.template.webmacro.WebmacroServletTemplateEngine)
164 |  |
           try {
165 | 60 |
             config.setTemplateEngine((WebmacroTemplateEngine)WebmacroTemplateEngine.class
166 |  |
                     .newInstance());
167 | 0 |
           } catch (Exception e) {
168 | 0 |
             throw new InstantiationPropertyException(WebmacroTemplateEngine.class
169 |  |
                     .getName(), e);
170 | 60 |
           }
171 |  |
172 | 60 |
         return config;
173 |  |
       }
174 |  |
175 |  |
       /**
176 |  |
        * Do our thing.
177 |  |
        */
178 |  |
       public void run(String[] args) throws Exception {
179 | 56 |
         Melati melati = init(args);
180 |  |
         try {
181 | 50 |
           doConfiguredRequest(melati);
182 |  |
         } finally {
183 | 50 |
           term(melati);
184 | 40 |
         }
185 | 40 |
       }
186 |  |
187 |  |
       /**
188 |  |
        * This method <b>SHOULD</b> be overidden.
189 |  |
        *
190 |  |
        * @return the System Administrators name.
191 |  |
        */
192 |  |
       public String getSysAdminName() {
193 | 2 |
         return "nobody";
194 |  |
       }
195 |  |
196 |  |
       /**
197 |  |
        * This method <b>SHOULD</b> be overidden.
198 |  |
        *
199 |  |
        * @return the System Administrators email address.
200 |  |
        */
201 |  |
       public String getSysAdminEmail() {
202 | 2 |
         return "nobody@nobody.com";
203 |  |
       }
204 |  |
205 |  |
       /**
206 |  |
        * Set up the (@link PoemContext}, but only the Method.
207 |  |
        *
208 |  |
        * @param melati
209 |  |
        *          the current {@link Melati}
210 |  |
        * @return a partially configured {@link PoemContext}
211 |  |
        */
212 |  |
       protected PoemContext poemContext(Melati melati) {
213 | 4 |
         PoemContext it = new PoemContext();
214 | 4 |
         String[] arguments = melati.getArguments();
215 | 4 |
         if (arguments.length > 0)
216 | 4 |
           it.setMethod(arguments[arguments.length - 1]);
217 | 4 |
         return it;
218 |  |
       }
219 |  |
220 |  |
       protected String[] applyNamedArguments(String[] arguments) {
221 | 60 |
         String[] unnamedArguments = new String[] {};
222 | 60 |
         boolean nextIsOutput = false;
223 | 416 |
         for (int i = 0; i < arguments.length; i++) {
224 | 356 |
           if (arguments[i].startsWith("-o"))
225 | 56 |
             nextIsOutput = true;
226 | 300 |
           else if (nextIsOutput)
227 |  |
             try {
228 | 56 |
               setOutput(arguments[i]);
229 | 56 |
               nextIsOutput = false;
230 | 0 |
             } catch (IOException e) {
231 | 0 |
               throw new RuntimeException("Problem setting output to "
232 |  |
                       + arguments[i], e);
233 | 56 |
             }
234 |  |
           else {
235 | 244 |
             unnamedArguments = (String[])ArrayUtils.added(unnamedArguments,
236 |  |
                     arguments[i]);
237 |  |
           }
238 |  |
         }
239 |  |
240 | 60 |
         return unnamedArguments;
241 |  |
       }
242 |  |
243 |  |
       private void setOutput(String path) throws IOException {
244 | 56 |
         File outputFile = new File(path).getCanonicalFile();
245 | 56 |
         File parent = new File(outputFile.getParent());
246 | 56 |
         parent.mkdirs();
247 | 56 |
         outputFile.createNewFile();
248 | 56 |
         setOutput(new PrintStream(new FileOutputStream(outputFile)));
249 | 56 |
       }
250 |  |
251 |  |
       /**
252 |  |
        * {@inheritDoc}
253 |  |
        *
254 |  |
        * @see org.melati.app.App#setOutput(java.io.PrintStream)
255 |  |
        */
256 |  |
       public void setOutput(PrintStream out) {
257 | 56 |
         output = out;
258 | 56 |
       }
259 |  |
260 |  |
       /**
261 |  |
        * Instantiate this method to build up your own output.
262 |  |
        *
263 |  |
        * @param melati
264 |  |
        *          a configured {@link Melati}
265 |  |
        * @throws Exception
266 |  |
        *           if anything goes wrong
267 |  |
        */
268 |  |
       protected abstract void doConfiguredRequest(final Melati melati)
269 |  |
               throws Exception;
270 |  |
271 |  |
     }