View Javadoc

1   /*
2    * Copyright (c) 2004 UNINETT FAS
3    * 
4    * This program is free software; you can
5    * redistribute it and/or modify it under the terms of the GNU General Public
6    * License as published by the Free Software Foundation; either version 2 of the
7    * License, or (at your option) any later version. This program is distributed
8    * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
9    * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   * See the GNU General Public License for more details. You should have received
11   * a copy of the GNU General Public License along with this program; if not,
12   * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
13   * Boston, MA 02111-1307, USA.
14   * 
15   * $Id: MoriaServlet.java,v 1.1 2006/01/16 16:06:43 indal Exp $
16   */
17  
18  package no.feide.moria.servlet;
19  
20  import javax.servlet.http.HttpServlet;
21  
22  import no.feide.moria.log.MessageLogger;
23  
24  import java.util.Properties;
25  
26  /***
27   * 
28   * @author Eva Indal
29   *
30   */
31  public class MoriaServlet extends HttpServlet {
32  
33      /***
34       * Get the config from the context. The configuration is expected to be set
35       * by the controller before requests are sent to this servlet.
36       *
37       * @in 
38       * @return The configuration.
39       * @throws IllegalStateException
40       *          If the config is not properly set.
41       *
42       */
43      public Properties getServletConfig(String[] required_parameters, MessageLogger log) throws IllegalStateException {
44          final Properties config;
45  
46          /* Validate config */
47          try {
48              config = (Properties) getServletContext().getAttribute(RequestUtil.PROP_CONFIG);
49          } catch (ClassCastException e) {
50              throw new IllegalStateException("Config is not correctly set in context.");
51          }
52          if (config == null)
53              throw new IllegalStateException("Config is not set in context.");
54  
55          // Are we missing some required properties?
56          for (int i = 0; i < required_parameters.length; i++) {
57              String parvalue = config.getProperty(required_parameters[i]);
58              if ((parvalue == null) || (parvalue.equals(""))) {
59                      log.logCritical("Required parameter '" + required_parameters[i] + "' is not set");
60                      throw new IllegalStateException();
61              }
62          }
63          return config;    
64      }
65  }