Coverage Report - org.melati.app.AbstractConfigApp
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractConfigApp
89%
52/58
66%
12/18
2
 
 1  
 /*
 2  
  * $Source: /usr/cvsroot/melati/melati/src/main/java/org/melati/app/AbstractConfigApp.java,v $
 3  
  * $Revision: 1.13 $
 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  
 import java.util.Properties;
 53  
 
 54  
 import org.melati.Melati;
 55  
 import org.melati.MelatiConfig;
 56  
 import org.melati.PoemContext;
 57  
 import org.melati.poem.PoemDatabaseFactory;
 58  
 import org.melati.poem.util.ArrayUtils;
 59  
 import org.melati.util.MelatiException;
 60  
 import org.melati.util.MelatiSimpleWriter;
 61  
 import org.melati.util.MelatiWriter;
 62  
 
 63  
 /**
 64  
  * ConfigApp is the simplest way to use Melati.
 65  
  * 
 66  
  * All a ConfigApp does is to configure a Melati. Importantly it does not
 67  
  * establish a Poem session leaving you to do this for yourself.
 68  
  * 
 69  
  * If you want a POEM session established, please extend {@link AbstractPoemApp}.
 70  
  * 
 71  
  * ConfigApp does set up a basic {@link PoemContext} with the Method set, but
 72  
  * not the POEM logicaldatabase, table or troid.
 73  
  * 
 74  
  * The arguments are expected to end with a freeform string telling your
 75  
  * application what it is meant to do. This is automatically made available in
 76  
  * templates as <TT>$melati.Method</TT>.
 77  
  * 
 78  
  * You can change the way these things are determined by overriding
 79  
  * {@link #poemContext}.
 80  
  */
 81  
 
 82  30
 public abstract class AbstractConfigApp implements App {
 83  
 
 84  
   protected static MelatiConfig melatiConfig;
 85  
 
 86  30
   protected PrintStream output = System.out;
 87  
 
 88  
   /**
 89  
    * Initialise.
 90  
    * 
 91  
    * @param args
 92  
    *          the command line arguments
 93  
    * @return a newly created Melati
 94  
    * @throws MelatiException
 95  
    *           if something goes wrong during initialisation
 96  
    */
 97  
   public Melati init(String[] args) throws MelatiException {
 98  30
       melatiConfig = melatiConfig();
 99  30
     String[] argumentsWithoutOutput = applyNamedArguments(args);
 100  30
     MelatiWriter out = new MelatiSimpleWriter(new OutputStreamWriter(output));
 101  30
     Melati melati = new Melati(melatiConfig, out);
 102  30
     melati.setArguments(argumentsWithoutOutput);
 103  30
     melati.setPoemContext(poemContext(melati));
 104  
 
 105  29
     return melati;
 106  
   }
 107  
 
 108  
   /**
 109  
    * Clean up at end of run. Overridden in PoemApp.
 110  
    * 
 111  
    * @param melati
 112  
    *          the melati
 113  
    * @throws IOException if there is an io problem
 114  
    */
 115  
   public void term(Melati melati) throws IOException {
 116  31
     melati.write();
 117  31
   }
 118  
 
 119  
   /**
 120  
    * Set application properties from the default properties file.
 121  
    * 
 122  
    * This method will look for a properties file called
 123  
    * <tt>org.melati.MelatiConfig.properties</tt>; if it finds that the
 124  
    * AccessHandler is an Http handler it will set the access handler to
 125  
    * <code>OpenAccessHandler</code>.
 126  
    * 
 127  
    * Similarly ServletTemplateEngine is changed to TemplateEngine.
 128  
    *  
 129  
    * To override any setting from MelatiConfig.properties, simply override this
 130  
    * method and return a vaild MelatiConfig.
 131  
    * 
 132  
    * eg to use a different AccessHandler from the default:
 133  
    * 
 134  
    * <PRE>
 135  
    * protected MelatiConfig melatiConfig() throws MelatiException {
 136  
    *   MelatiConfig config = super.melatiConfig();
 137  
    *   config.setAccessHandler(new YourAccessHandler());
 138  
    *   return config;
 139  
    * }
 140  
    * </PRE>
 141  
    * 
 142  
    * @throws MelatiException
 143  
    *           if anything goes wrong with Melati
 144  
    */
 145  
   protected MelatiConfig melatiConfig() throws MelatiException {
 146  30
     Properties servletProperties = MelatiConfig.getProperties();
 147  
 
 148  30
     if (servletProperties.getProperty("org.melati.MelatiConfig.templateEngine").equals(
 149  
         "org.melati.template.webmacro.WebmacroServletTemplateEngine"))
 150  0
       servletProperties.setProperty("org.melati.MelatiConfig.templateEngine", 
 151  
           "org.melati.template.webmacro.WebmacroTemplateEngine");
 152  30
     if (servletProperties.getProperty("org.melati.MelatiConfig.templateEngine").equals(
 153  
         "org.melati.template.webmacro.VelocityServletTemplateEngine"))
 154  0
       servletProperties.setProperty("org.melati.MelatiConfig.templateEngine", 
 155  
           "org.melati.template.webmacro.VelocityTemplateEngine");
 156  30
     if (servletProperties.getProperty("org.melati.MelatiConfig.templateEngine").equals(
 157  
         "org.melati.template.webmacro.FreemarkerServletTemplateEngine"))
 158  0
       servletProperties.setProperty("org.melati.MelatiConfig.templateEngine", 
 159  
           "org.melati.template.webmacro.FreemarkerTemplateEngine");
 160  
        
 161  30
     if (servletProperties.getProperty("org.melati.MelatiConfig.accessHandler").equals(
 162  
         "org.melati.login.HttpBasicAuthenticationAccessHandler"))
 163  0
       servletProperties.setProperty("org.melati.MelatiConfig.accessHandler", 
 164  
           "org.melati.login.OpenAccessHandler");
 165  30
     if (servletProperties.getProperty("org.melati.MelatiConfig.accessHandler").equals(
 166  
         "org.melati.login.HttpSessionAccessHandler"))
 167  30
       servletProperties.setProperty("org.melati.MelatiConfig.accessHandler", 
 168  
           "org.melati.login.OpenAccessHandler");
 169  
        
 170  
     
 171  30
     MelatiConfig config = new MelatiConfig(servletProperties);
 172  
 
 173  30
     return config;
 174  
   }
 175  
 
 176  
   /**
 177  
    * Do our thing.
 178  
    */
 179  
   public void run(String[] args) throws Exception {
 180  28
     Melati melati = init(args);
 181  
     try { 
 182  25
       doConfiguredRequest(melati);
 183  
     } finally {  
 184  25
       term(melati);
 185  25
       PoemDatabaseFactory.getPoemShutdownThread().run();
 186  20
     }
 187  20
   }
 188  
 
 189  
   /**
 190  
    * This method <b>SHOULD</b> be overidden.
 191  
    * 
 192  
    * @return the System Administrators name.
 193  
    */
 194  
   public String getSysAdminName() {
 195  1
     return "nobody";
 196  
   }
 197  
 
 198  
   /**
 199  
    * This method <b>SHOULD</b> be overidden.
 200  
    * 
 201  
    * @return the System Administrators email address.
 202  
    */
 203  
   public String getSysAdminEmail() {
 204  1
     return "nobody@nobody.com";
 205  
   }
 206  
 
 207  
   /**
 208  
    * Set up the (@link PoemContext}, but only the Method.
 209  
    * 
 210  
    * @param melati
 211  
    *          the current {@link Melati}
 212  
    * @return a partially configured {@link PoemContext}
 213  
    */
 214  
   protected PoemContext poemContext(Melati melati) {
 215  2
     PoemContext it = new PoemContext();
 216  2
     String[] arguments = melati.getArguments();
 217  2
     if (arguments.length > 0)
 218  2
       it.setMethod(arguments[arguments.length - 1]);
 219  2
     return it;
 220  
   }
 221  
 
 222  
   protected String[] applyNamedArguments(String[] arguments) {
 223  30
     String[] unnamedArguments = new String[] {};
 224  30
     boolean nextIsOutput = false;
 225  208
     for (int i = 0; i < arguments.length; i++) {
 226  178
       if (arguments[i].startsWith("-o"))
 227  28
         nextIsOutput = true;
 228  150
       else if (nextIsOutput)
 229  
         try {
 230  28
           setOutput(arguments[i]);
 231  28
           nextIsOutput = false;
 232  0
         } catch (IOException e) {
 233  0
           throw new RuntimeException("Problem setting output to "
 234  
                   + arguments[i], e);
 235  28
         }
 236  
       else {
 237  122
         unnamedArguments = (String[])ArrayUtils.added(unnamedArguments,
 238  
                 arguments[i]);
 239  
       }
 240  
     }
 241  
 
 242  30
     return unnamedArguments;
 243  
   }
 244  
 
 245  
   public void setOutput(String path) throws IOException {
 246  28
     File outputFile = new File(path).getCanonicalFile();
 247  28
     File parent = new File(outputFile.getParent());
 248  28
     parent.mkdirs();
 249  28
     outputFile.createNewFile();
 250  28
     setOutput(new PrintStream(new FileOutputStream(outputFile)));
 251  28
   }
 252  
 
 253  
   /**
 254  
    * {@inheritDoc}
 255  
    * 
 256  
    * @see org.melati.app.App#setOutput(java.io.PrintStream)
 257  
    */
 258  
   public void setOutput(PrintStream out) {
 259  28
     output = out;
 260  28
   }
 261  
 
 262  
   /**
 263  
    * Instantiate this method to build up your own output.
 264  
    * 
 265  
    * @param melati
 266  
    *          a configured {@link Melati}
 267  
    * @throws Exception
 268  
    *           if anything goes wrong
 269  
    */
 270  
   protected abstract void doConfiguredRequest(final Melati melati)
 271  
           throws Exception;
 272  
 
 273  
 }