Removed native components (now in own repositories)
[guacamole.git] / web / guacamole-common / src / main / java / net / sourceforge / guacamole / net / Configuration.java
1
2 package net.sourceforge.guacamole.net;
3
4 /*
5  *  Guacamole - Clientless Remote Desktop
6  *  Copyright (C) 2010  Michael Jumper
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 import javax.servlet.ServletContext;
23 import net.sourceforge.guacamole.GuacamoleException;
24
25 public abstract class Configuration {
26
27     protected String humanReadableList(Object... values) {
28
29         String list = "";
30         for (int i=0; i<values.length; i++) {
31
32             if (i >= 1)
33                 list += ", ";
34
35             if (i == values.length -1)
36                 list += " or ";
37
38             list += "\"" + values[i] + "\"";
39         }
40
41         return list;
42
43     }
44
45     protected String readParameter(String name) throws GuacamoleException {
46         String value = GuacamoleProperties.getProperty(name);
47         return value;
48     }
49
50     protected String readParameter(String name, String defaultValue, String... allowedValues) throws GuacamoleException {
51
52         String value = GuacamoleProperties.getProperty(name);
53
54         // Use default if not specified
55         if (value == null) {
56             if (defaultValue == null)
57                 throw new GuacamoleException("Parameter \"" + name + "\" is required.");
58
59             return defaultValue;
60         }
61
62         // If not restricted to certain values, just return whatever is given.
63         if (allowedValues.length == 0)
64             return value;
65
66         // If restricted, only return value within given list
67         for (String allowedValue : allowedValues)
68             if (value.equals(allowedValue))
69                 return value;
70
71         throw new GuacamoleException("Parameter \"" + name + "\" must be " + humanReadableList((Object) allowedValues));
72     }
73
74     protected boolean readBooleanParameter(String name, Boolean defaultValue) throws GuacamoleException {
75
76         String value = GuacamoleProperties.getProperty(name);
77
78         // Use default if not specified
79         if (value == null) {
80             if (defaultValue == null)
81                 throw new GuacamoleException("Parameter \"" + name + "\" is required.");
82
83             return defaultValue;
84         }
85
86         value = value.trim();
87         if (value.equals("true"))
88             return true;
89
90         if (value.equals("false"))
91             return false;
92
93         throw new GuacamoleException("Parameter \"" + name + "\" must be \"true\" or \"false\".");
94
95     }
96
97     protected int readIntParameter(String name, Integer defaultValue, Integer... allowedValues) throws GuacamoleException {
98
99         String parmString = GuacamoleProperties.getProperty(name);
100
101         // Use default if not specified
102         if (parmString== null) {
103             if (defaultValue == null)
104                 throw new GuacamoleException("Parameter \"" + name + "\" is required.");
105
106             return defaultValue;
107         }
108
109         try {
110             int value = Integer.parseInt(parmString);
111
112             // If not restricted to certain values, just return whatever is given.
113             if (allowedValues.length == 0)
114                 return value;
115
116             // If restricted, only return value within given list
117             for (int allowedValue : allowedValues)
118                 if (value == allowedValue)
119                     return value;
120
121             throw new GuacamoleException("Parameter \"" + name + "\" must be " + humanReadableList((Object) allowedValues));
122         }
123         catch (NumberFormatException e) {
124             throw new GuacamoleException("Parameter \"" + name + "\" must be an integer.", e);
125         }
126
127     }
128
129 }