001/*
002 * GWTEventService
003 * Copyright (c) 2011 and beyond, strawbill UG (haftungsbeschr?nkt)
004 *
005 * This is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU Lesser General Public License as
007 * published by the Free Software Foundation; either version 3 of
008 * the License, or (at your option) any later version.
009 * Other licensing for GWTEventService may also be possible on request.
010 * Please view the license.txt of the project for more information.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022package de.novanic.eventservice.config.loader;
023
024import de.novanic.eventservice.client.config.ConfigurationException;
025import de.novanic.eventservice.config.EventServiceConfiguration;
026import de.novanic.eventservice.config.RemoteEventServiceConfiguration;
027import de.novanic.eventservice.config.ConfigParameter;
028import de.novanic.eventservice.util.StringUtil;
029import de.novanic.eventservice.util.ServiceUtilException;
030
031import javax.servlet.ServletConfig;
032
033/**
034 * WebDescriptorConfigurationLoader is used by {@link de.novanic.eventservice.config.EventServiceConfigurationFactory}
035 * to load the {@link de.novanic.eventservice.config.EventServiceConfiguration} with the servlet parameters / init-parameters
036 * of the web-descriptor.
037 * 
038 * @author sstrohschein
039 *         <br>Date: 05.03.2009
040 *         <br>Time: 23:37:15
041 */
042public class WebDescriptorConfigurationLoader implements ConfigurationLoader
043{
044    private final ServletConfig myServletConfig;
045
046    /**
047     * Creates a {@link de.novanic.eventservice.config.loader.WebDescriptorConfigurationLoader} with a servlet config
048     * ({@link javax.servlet.ServletConfig}).
049     * @param aServletConfig servlet config
050     */
051    public WebDescriptorConfigurationLoader(ServletConfig aServletConfig) {
052        myServletConfig = aServletConfig;
053    }
054
055    /**
056     * Checks if the configuration is available and can be loaded. If no configuration is available, the load method
057     * {@link ConfigurationLoader#load()} shouldn't called. In the case of {@link WebDescriptorConfigurationLoader} the method
058     * returns true when the init-parameters for the servlet are registered in the web-descriptor.
059     * @return true when available, otherwise false
060     */
061    public boolean isAvailable() {
062        if(myServletConfig != null) {
063            //The configuration is available when at least one parameter is configured.
064            for(ConfigParameter theConfigParameter: ConfigParameter.values()) {
065                final String theParameterValue = myServletConfig.getInitParameter(theConfigParameter.declaration());
066                if(theParameterValue != null) {
067                    return true;
068                }
069            }
070        }
071        return false;
072    }
073
074    /**
075     * Loads the configuration with the {@link WebDescriptorConfigurationLoader}.
076     * @return {@link de.novanic.eventservice.config.EventServiceConfiguration} the loaded configuration
077     * @throws ConfigurationException occurs when the configuration can't be loaded or if it contains unreadable values.
078     */
079    public EventServiceConfiguration load() {
080        if(isAvailable()) {
081            return new RemoteEventServiceConfiguration("Web-Descriptor-Configuration",
082                    readIntParameter(ConfigParameter.MIN_WAITING_TIME_TAG.declaration(), ConfigParameter.FQ_MIN_WAITING_TIME_TAG.declaration()),
083                    readIntParameter(ConfigParameter.MAX_WAITING_TIME_TAG.declaration(), ConfigParameter.FQ_MAX_WAITING_TIME_TAG.declaration()),
084                    readIntParameter(ConfigParameter.TIMEOUT_TIME_TAG.declaration(), ConfigParameter.FQ_TIMEOUT_TIME_TAG.declaration()),
085                    readIntParameter(ConfigParameter.RECONNECT_ATTEMPT_COUNT_TAG.declaration(), ConfigParameter.FQ_RECONNECT_ATTEMPT_COUNT_TAG.declaration()),
086                    readParameter(ConfigParameter.CONNECTION_ID_GENERATOR.declaration(), ConfigParameter.FQ_CONNECTION_ID_GENERATOR.declaration()),
087                    readParameter(ConfigParameter.CONNECTION_STRATEGY_CLIENT_CONNECTOR.declaration(), ConfigParameter.FQ_CONNECTION_STRATEGY_CLIENT_CONNECTOR.declaration()),
088                    readParameter(ConfigParameter.CONNECTION_STRATEGY_SERVER_CONNECTOR.declaration(), ConfigParameter.FQ_CONNECTION_STRATEGY_SERVER_CONNECTOR.declaration()),
089                    readParameter(ConfigParameter.CONNECTION_STRATEGY_ENCODING.declaration(), ConfigParameter.FQ_CONNECTION_STRATEGY_ENCODING.declaration()),
090                    readIntParameter(ConfigParameter.MAX_EVENTS.declaration(), ConfigParameter.FQ_MAX_EVENTS.declaration()));
091        }
092        return null;
093    }
094
095    /**
096     * Checks if the parameter is available.
097     * @param aParameterValue value to check
098     * @return true when it is available, otherwise false
099     */
100    private boolean isAvailable(String aParameterValue) {
101        return aParameterValue != null && aParameterValue.trim().length() > 0;
102    }
103
104    /**
105     * Reads the numeric value of the parameter. When the value isn't numeric, an {@link de.novanic.eventservice.client.config.ConfigurationException} is thrown.
106         * @param aParameterName parameter
107         * @param aParameterNameFQ parameter (full-qualified variant)
108     * @return numeric parameter value
109     * @throws ConfigurationException (when the value isn't numeric)
110     */
111    private Integer readIntParameter(String aParameterName, String aParameterNameFQ) {
112        final String theParameterValue = readParameter(aParameterName, aParameterNameFQ);
113        if(theParameterValue != null) {
114            try {
115                return StringUtil.readInteger(theParameterValue);
116            } catch(ServiceUtilException e) {
117                throw new ConfigurationException("The value of the parameter \"" + aParameterName
118                        + "\" was expected to be numeric, but was \"" + theParameterValue + "\"!", e);
119            }
120        }
121        return null;
122    }
123
124    /**
125     * Reads the value of a servlet parameter.
126     * @param aParameterName servlet parameter
127         * @param aParameterNameFQ parameter (full-qualified variant)
128     * @return value of the servlet parameter
129     */
130    private String readParameter(String aParameterName, String aParameterNameFQ) {
131        if(isAvailable(myServletConfig.getInitParameter(aParameterNameFQ))) {
132            return myServletConfig.getInitParameter(aParameterNameFQ);
133                }
134        return myServletConfig.getInitParameter(aParameterName);
135    }
136}