java/cewolf-1.0/src/main/java/de/laures/cewolf/Configuration.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Feb 2009 21:31:02 +0100
changeset 1 639991d0808a
permissions -rw-r--r--
Rozbalená knihovna verze 1.0
franta-hg@1
     1
/* ================================================================
franta-hg@1
     2
 * Cewolf : Chart enabling Web Objects Framework
franta-hg@1
     3
 * ================================================================
franta-hg@1
     4
 *
franta-hg@1
     5
 * Project Info:  http://cewolf.sourceforge.net
franta-hg@1
     6
 * Project Lead:  Guido Laures (guido@laures.de);
franta-hg@1
     7
 *
franta-hg@1
     8
 * (C) Copyright 2002, by Guido Laures
franta-hg@1
     9
 *
franta-hg@1
    10
 * This library is free software; you can redistribute it and/or modify it under the terms
franta-hg@1
    11
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
franta-hg@1
    12
 * either version 2.1 of the License, or (at your option) any later version.
franta-hg@1
    13
 *
franta-hg@1
    14
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
franta-hg@1
    15
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
franta-hg@1
    16
 * See the GNU Lesser General Public License for more details.
franta-hg@1
    17
 *
franta-hg@1
    18
 * You should have received a copy of the GNU Lesser General Public License along with this
franta-hg@1
    19
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
franta-hg@1
    20
 * Boston, MA 02111-1307, USA.
franta-hg@1
    21
 */
franta-hg@1
    22
franta-hg@1
    23
package de.laures.cewolf;
franta-hg@1
    24
franta-hg@1
    25
import java.util.Enumeration;
franta-hg@1
    26
import java.util.HashMap;
franta-hg@1
    27
import java.util.Map;
franta-hg@1
    28
franta-hg@1
    29
import javax.servlet.ServletConfig;
franta-hg@1
    30
import javax.servlet.ServletContext;
franta-hg@1
    31
franta-hg@1
    32
/**
franta-hg@1
    33
 * This class represents the configuration of the Cewolf framework.
franta-hg@1
    34
 * It is designed as singleton and resists in application context.
franta-hg@1
    35
 * @author glaures
franta-hg@1
    36
 * @since 0.8
franta-hg@1
    37
 */
franta-hg@1
    38
public class Configuration {
franta-hg@1
    39
franta-hg@1
    40
	public static final String KEY = Configuration.class.getName();
franta-hg@1
    41
	private static final String DEFAULT_OVERLIB_URL = "overlib.js";
franta-hg@1
    42
	private static final String DEFAULT_STORAGE = "de.laures.cewolf.storage.TransientSessionStorage";
franta-hg@1
    43
franta-hg@1
    44
	private String overlibURL = DEFAULT_OVERLIB_URL;
franta-hg@1
    45
	private boolean debugged = false;
franta-hg@1
    46
franta-hg@1
    47
	private String storageClassName = DEFAULT_STORAGE;
franta-hg@1
    48
	private Storage storage = null;
franta-hg@1
    49
	private Map parameters = new HashMap();
franta-hg@1
    50
franta-hg@1
    51
	/** package protected constructor triggered by servlet */
franta-hg@1
    52
	protected Configuration(ServletContext ctx) {
franta-hg@1
    53
        ctx.log("configuring cewolf app..");
franta-hg@1
    54
        ctx.setAttribute(KEY, this);
franta-hg@1
    55
franta-hg@1
    56
        //retrieve the init config params
franta-hg@1
    57
        ServletConfig config = (ServletConfig) ctx.getAttribute(CewolfRenderer.INIT_CONFIG);
franta-hg@1
    58
        if (config != null)
franta-hg@1
    59
        {
franta-hg@1
    60
            Enumeration initParams = config.getInitParameterNames();
franta-hg@1
    61
            try {
franta-hg@1
    62
                while (initParams.hasMoreElements()) {
franta-hg@1
    63
                    String param = (String) initParams.nextElement();
franta-hg@1
    64
                    String value = config.getInitParameter(param);
franta-hg@1
    65
                    if ("debug".equalsIgnoreCase(param)) {
franta-hg@1
    66
                        debugged = Boolean.valueOf(value).booleanValue();
franta-hg@1
    67
                    } else if ("overliburl".equalsIgnoreCase(param)) {
franta-hg@1
    68
                        overlibURL = value;
franta-hg@1
    69
                    } else if ("storage".equalsIgnoreCase(param)) {
franta-hg@1
    70
                        storageClassName = value;
franta-hg@1
    71
                    } else {
franta-hg@1
    72
                        ctx.log(param + " parameter is ignored.");
franta-hg@1
    73
                    }
franta-hg@1
    74
                    parameters.put(param,value);
franta-hg@1
    75
                }
franta-hg@1
    76
            } catch (Throwable t) {
franta-hg@1
    77
                ctx.log("Error in Cewolf config.", t);
franta-hg@1
    78
            }            
franta-hg@1
    79
        }
franta-hg@1
    80
        else {
franta-hg@1
    81
        	ctx.log("Cewolf Misconfiguration. You should add a <load-on-startup> tag "
franta-hg@1
    82
        			+ "to your web.xml for the Cewolf rendering servlet.\n"
franta-hg@1
    83
					+ "A default Configuration will be used if not.");
franta-hg@1
    84
        }
franta-hg@1
    85
        
franta-hg@1
    86
		try {
franta-hg@1
    87
			initStorage(ctx);
franta-hg@1
    88
		} catch (CewolfException ex) {
franta-hg@1
    89
			ctx.log("exception during storage init from class " + storageClassName);
franta-hg@1
    90
			ctx.log("using " + DEFAULT_STORAGE);
franta-hg@1
    91
			storageClassName = DEFAULT_STORAGE;
franta-hg@1
    92
			try {
franta-hg@1
    93
				initStorage(ctx);
franta-hg@1
    94
			} catch(CewolfException cwex){
franta-hg@1
    95
				cwex.printStackTrace();
franta-hg@1
    96
				throw new RuntimeException(storageClassName + ".init() threw exception.");
franta-hg@1
    97
			}
franta-hg@1
    98
		}
franta-hg@1
    99
		ctx.log("using storage class " + storageClassName);
franta-hg@1
   100
		ctx.log("using overlibURL " + overlibURL);
franta-hg@1
   101
		ctx.log("debugging is turned " + (debugged ? "on" : "off"));
franta-hg@1
   102
		ctx.log("...done.");
franta-hg@1
   103
	}
franta-hg@1
   104
	
franta-hg@1
   105
	private void initStorage(ServletContext ctx) throws CewolfException {
franta-hg@1
   106
		try {
franta-hg@1
   107
			storage = (Storage)Class.forName(storageClassName).newInstance();
franta-hg@1
   108
		} catch(Exception ex){
franta-hg@1
   109
			ex.printStackTrace();
franta-hg@1
   110
			throw new CewolfException(ex.getMessage());
franta-hg@1
   111
		}
franta-hg@1
   112
		storage.init(ctx);
franta-hg@1
   113
	}
franta-hg@1
   114
franta-hg@1
   115
	private Configuration() {
franta-hg@1
   116
	}
franta-hg@1
   117
franta-hg@1
   118
	/**
franta-hg@1
   119
	 * Factory method. If no Configuration had been initialized before, a new
franta-hg@1
   120
	 * one is created, stored in ctx and returned to the caller.
franta-hg@1
   121
	 * @param ctx the servlet context from where to retrieve the Configuration
franta-hg@1
   122
	 * object.
franta-hg@1
   123
	 * @return the config object
franta-hg@1
   124
	 */
franta-hg@1
   125
	public static Configuration getInstance(ServletContext ctx) {
franta-hg@1
   126
		Configuration config = null;
franta-hg@1
   127
        config = (Configuration) ctx.getAttribute(KEY);
franta-hg@1
   128
        
franta-hg@1
   129
        if (config == null)
franta-hg@1
   130
        {
franta-hg@1
   131
            ctx.log("No Configuration for this context.  Initializing.");
franta-hg@1
   132
            config = new Configuration(ctx);
franta-hg@1
   133
            ctx.setAttribute(KEY, config);
franta-hg@1
   134
        }
franta-hg@1
   135
franta-hg@1
   136
		return config;
franta-hg@1
   137
	}
franta-hg@1
   138
        
franta-hg@1
   139
	/**
franta-hg@1
   140
	 * Checks if debugging is configured to be turned on. Configured by
franta-hg@1
   141
	 * init param <code>debug</code> in web.xml.
franta-hg@1
   142
	 * @return <code>true</code> if a debugging is on, else <code>false</false>
franta-hg@1
   143
	 */
franta-hg@1
   144
	public boolean isDebugged() {
franta-hg@1
   145
		return debugged;
franta-hg@1
   146
	}
franta-hg@1
   147
franta-hg@1
   148
	/**
franta-hg@1
   149
	 * Returns the location of the overlib.js relative to webapp's root.
franta-hg@1
   150
	 * Configured by init param <code>overliburl</code> in web.xml. Defaults to
franta-hg@1
   151
	 * <code>overlib.js</code>
franta-hg@1
   152
	 * @return String
franta-hg@1
   153
	 */
franta-hg@1
   154
	public String getOverlibURL() {
franta-hg@1
   155
		return overlibURL;
franta-hg@1
   156
	}
franta-hg@1
   157
franta-hg@1
   158
	public Storage getStorage() {
franta-hg@1
   159
		return storage;
franta-hg@1
   160
	}
franta-hg@1
   161
	
franta-hg@1
   162
	/**
franta-hg@1
   163
	 * Get the initialization parameters from Cewolf servlet.
franta-hg@1
   164
	 * @return The parameter map (String->String) values
franta-hg@1
   165
	 */
franta-hg@1
   166
	public Map getParameters() {
franta-hg@1
   167
		return parameters;
franta-hg@1
   168
	}
franta-hg@1
   169
}