2 package net.sourceforge.guacamole.net;
5 * Guacamole - Clientless Remote Desktop
6 * Copyright (C) 2010 Michael Jumper
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 import javax.servlet.ServletContext;
23 import net.sourceforge.guacamole.GuacamoleException;
25 public abstract class Configuration {
27 protected String humanReadableList(Object... values) {
30 for (int i=0; i<values.length; i++) {
35 if (i == values.length -1)
38 list += "\"" + values[i] + "\"";
45 protected String readParameter(String name) throws GuacamoleException {
46 String value = GuacamoleProperties.getProperty(name);
50 protected String readParameter(String name, String defaultValue, String... allowedValues) throws GuacamoleException {
52 String value = GuacamoleProperties.getProperty(name);
54 // Use default if not specified
56 if (defaultValue == null)
57 throw new GuacamoleException("Parameter \"" + name + "\" is required.");
62 // If not restricted to certain values, just return whatever is given.
63 if (allowedValues.length == 0)
66 // If restricted, only return value within given list
67 for (String allowedValue : allowedValues)
68 if (value.equals(allowedValue))
71 throw new GuacamoleException("Parameter \"" + name + "\" must be " + humanReadableList((Object) allowedValues));
74 protected boolean readBooleanParameter(String name, Boolean defaultValue) throws GuacamoleException {
76 String value = GuacamoleProperties.getProperty(name);
78 // Use default if not specified
80 if (defaultValue == null)
81 throw new GuacamoleException("Parameter \"" + name + "\" is required.");
87 if (value.equals("true"))
90 if (value.equals("false"))
93 throw new GuacamoleException("Parameter \"" + name + "\" must be \"true\" or \"false\".");
97 protected int readIntParameter(String name, Integer defaultValue, Integer... allowedValues) throws GuacamoleException {
99 String parmString = GuacamoleProperties.getProperty(name);
101 // Use default if not specified
102 if (parmString== null) {
103 if (defaultValue == null)
104 throw new GuacamoleException("Parameter \"" + name + "\" is required.");
110 int value = Integer.parseInt(parmString);
112 // If not restricted to certain values, just return whatever is given.
113 if (allowedValues.length == 0)
116 // If restricted, only return value within given list
117 for (int allowedValue : allowedValues)
118 if (value == allowedValue)
121 throw new GuacamoleException("Parameter \"" + name + "\" must be " + humanReadableList((Object) allowedValues));
123 catch (NumberFormatException e) {
124 throw new GuacamoleException("Parameter \"" + name + "\" must be an integer.", e);