001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.util;
022    
023    import java.util.Enumeration;
024    
025    import javax.servlet.FilterConfig;
026    import javax.servlet.ServletConfig;
027    import javax.servlet.ServletContext;
028    
029    import org.granite.logging.Logger;
030    
031    /**
032     * @author Franck WOLFF
033     */
034    public class ServletParams {
035    
036            private static final Logger log = Logger.getLogger(ServletParams.class);
037    
038            public static <T> T get(final ServletContext context, final String name, Class<T> clazz, T defaultValue) {
039                    return get(context, name, clazz, defaultValue, false, true);
040            }
041            public static <T> T get(final ServletConfig config, final String name, Class<T> clazz, T defaultValue) {
042                    return get(config, name, clazz, defaultValue, false, true);
043            }
044            public static <T> T get(final FilterConfig config, final String name, Class<T> clazz, T defaultValue) {
045                    return get(config, name, clazz, defaultValue, false, true);
046            }
047    
048            public static <T> T get(final ServletContext context, final String name, Class<T> clazz, T defaultValue, boolean required, boolean warn) {
049                    ParamGetter getter = new ParamGetter() {
050                            
051                            public Enumeration<String> getNames() {
052                                    return context.getInitParameterNames();
053                            }
054                            
055                            public String getName() {
056                                    return name;
057                            }
058                            
059                            public String getValue() {
060                                    return context.getInitParameter(name);
061                            }
062                    };
063                    return getInitParameter(getter, clazz, defaultValue, required, warn);
064            }
065    
066            public static <T> T get(final ServletConfig config, final String name, Class<T> clazz, T defaultValue, boolean required, boolean warn) {
067                    ParamGetter getter = new ParamGetter() {
068                            
069                            public Enumeration<String> getNames() {
070                                    return config.getInitParameterNames();
071                            }
072                            
073                            public String getName() {
074                                    return name;
075                            }
076                            
077                            public String getValue() {
078                                    return config.getInitParameter(name);
079                            }
080                    };
081                    return getInitParameter(getter, clazz, defaultValue, required, warn);
082            }
083    
084            public static <T> T get(final FilterConfig config, final String name, Class<T> clazz, T defaultValue, boolean required, boolean warn) {
085                    ParamGetter getter = new ParamGetter() {
086                            
087                            public Enumeration<String> getNames() {
088                                    return config.getInitParameterNames();
089                            }
090                            
091                            public String getName() {
092                                    return name;
093                            }
094                            
095                            public String getValue() {
096                                    return config.getInitParameter(name);
097                            }
098                    };
099                    return getInitParameter(getter, clazz, defaultValue, required, warn);
100            }
101            
102            public static boolean contains(FilterConfig config, String name) {
103                    boolean found = false;
104                    Enumeration<String> e = config.getInitParameterNames();
105                    while (e.hasMoreElements()) {
106                            String n = e.nextElement();
107                            if (name.equals(n)) {
108                                    found = true;
109                                    break;
110                            }
111                    }
112                    return found;
113            }
114            
115            public static boolean contains(ServletConfig config, String name) {
116                    boolean found = false;
117                    Enumeration<String> e = config.getInitParameterNames();
118                    while (e.hasMoreElements()) {
119                            String n = e.nextElement();
120                            if (name.equals(n)) {
121                                    found = true;
122                                    break;
123                            }
124                    }
125                    return found;
126            }
127            
128            public static boolean contains(ServletContext context, String name) {
129                    boolean found = false;
130                    Enumeration<String> e = context.getInitParameterNames();
131                    while (e.hasMoreElements()) {
132                            String n = e.nextElement();
133                            if (name.equals(n)) {
134                                    found = true;
135                                    break;
136                            }
137                    }
138                    return found;
139            }
140    
141            private static <T> T getInitParameter(ParamGetter getter, Class<T> clazz, T defaultValue, boolean required, boolean warn) {
142    
143            if (required) {
144                    boolean found = false;
145                    Enumeration<String> e = getter.getNames();
146                    while (e.hasMoreElements()) {
147                            String name = e.nextElement();
148                            if (name.equals(getter.getName())) {
149                                    found = true;
150                                    break;
151                            }
152                    }
153                    if (!found)
154                            throw new RuntimeException("Init parameter " + getter.getName() + " is required in web.xml");
155            }
156                    
157            String sValue = getter.getValue();
158            Object oValue = defaultValue;
159            
160            boolean unsupported = false;
161            if (sValue != null) {
162                    try {
163                            if (clazz == String.class)
164                                    oValue = sValue;
165                            else if (clazz == Integer.class || clazz == Integer.TYPE)
166                                    oValue = Integer.valueOf(sValue);
167                            else if (clazz == Long.class || clazz == Long.TYPE)
168                                    oValue = Long.valueOf(sValue);
169                            else if (clazz == Boolean.class || clazz == Boolean.TYPE) {
170                                    if (!Boolean.TRUE.toString().equalsIgnoreCase(sValue) && !Boolean.FALSE.toString().equalsIgnoreCase(sValue))
171                                            throw new NumberFormatException(sValue);
172                                    oValue = Boolean.valueOf(sValue);
173                            }
174                            else if (clazz == Double.class || clazz == Double.TYPE)
175                                    oValue = Double.valueOf(sValue);
176                            else if (clazz == Float.class || clazz == Float.TYPE)
177                                    oValue = Float.valueOf(sValue);
178                            else if (clazz == Short.class || clazz == Short.TYPE)
179                                    oValue = Short.valueOf(sValue);
180                            else if (clazz == Byte.class || clazz == Byte.TYPE)
181                                    oValue = Byte.valueOf(sValue);
182                            else
183                                    unsupported = true; 
184                    }
185                    catch (Exception e) {
186                            if (warn)
187                                    log.warn(e, "Illegal %s value for %s: %s (using default: %s)", clazz.getSimpleName(), getter.getName(), sValue, defaultValue);
188                    }
189            }
190            
191            if (unsupported)
192                    throw new UnsupportedOperationException("Unsupported value type: " + clazz.getName());
193            
194            @SuppressWarnings("unchecked")
195            T tValue = (T)oValue;
196            
197            return tValue;
198        }
199            
200            private static interface ParamGetter {
201                    
202                    public Enumeration<String> getNames();
203                    public String getName();
204                    public String getValue();
205            }
206    }