/[projects]/WebConfig/src/dk/thoerup/webconfig/ConfigServlet.java
ViewVC logotype

Contents of /WebConfig/src/dk/thoerup/webconfig/ConfigServlet.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1371 - (show annotations) (download)
Thu Apr 21 05:50:38 2011 UTC (13 years, 1 month ago) by torben
File size: 6607 byte(s)
Enum settings should be displayed with a drop down box
1 package dk.thoerup.webconfig;
2
3 import java.io.IOException;
4 import java.lang.reflect.Field;
5 import java.lang.reflect.InvocationTargetException;
6 import java.lang.reflect.Method;
7
8 import javax.servlet.ServletException;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 public class ConfigServlet extends HttpServlet {
14 private static final long serialVersionUID = 1L;
15
16 private Object configObject;
17
18 public void setConfigObject(Object obj) {
19 configObject = obj;
20 }
21
22
23 @Override
24 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
25
26
27 resp.setDateHeader("Expires", 0);
28 resp.setHeader("Pragma", "no-cache");
29 resp.setHeader("Cache-Control", "no-cache");
30 resp.setContentType("text/html");
31
32 StringBuilder sb = new StringBuilder();
33
34 sb.append("<html><body><h3>WebConfig:</h3><table border='1' cellspacing='0'>");
35 sb.append("<tr><th>Field name</th><th>Type</th><th>Description</th><th>Value</th><th>&nbsp;</th></tr>\n");
36
37
38 Field fields[] = configObject.getClass().getDeclaredFields();
39
40 try {
41 for(Field field : fields) {
42 field.setAccessible(true);
43
44 ConfigVariable anno = field.getAnnotation(ConfigVariable.class);
45 if (anno == null)
46 continue;
47
48 sb.append("<tr>");
49 sb.append("<td>").append( field.getName() ).append("</td>");
50 sb.append("<td>").append( getCleanTypename(field.getType()) ).append("</td>");
51 sb.append("<td>").append( anno.description() ).append("&nbsp;</td>");
52
53 sb.append("<form action='").append( req.getRequestURI() ).append("' method='post'>");
54 sb.append("<input type='hidden' name='varname' value='").append( field.getName() ).append("' />");
55
56
57
58
59 if (anno.readonly() == false) {
60 if (field.getType().getName().equals("boolean") ) {
61
62 String options = getBooleanSelectOptions(field,configObject);
63 sb.append("<td><select name='varvalue'>").append(options).append("</select></td>");
64
65 } else if (field.getType().isEnum()) {
66
67 String options = getEnumSelectOptions(field,configObject);
68 sb.append("<td><select name='varvalue'>").append(options).append("</select></td>");
69
70 } else {
71 sb.append("<td><input type='text' name='varvalue' value='").append( "" + field.get(configObject) ).append("' /></td>");
72
73 }
74 sb.append("<td><input type='submit' name='Set' value='Set'></td>\n");
75 } else {
76 sb.append("<td>").append( ""+ field.get(configObject) ).append("&nbsp;</td>");
77 sb.append("<td>&nbsp;</td>");
78 }
79
80 sb.append("</form></tr>\n");
81 }
82
83 sb.append("</table>\n\n");
84
85 Method[] methods = configObject.getClass().getDeclaredMethods();
86 for(Method method : methods) {
87 method.setAccessible(true);
88
89 ReloadConfigMethod anno = method.getAnnotation(ReloadConfigMethod.class);
90 if (anno == null)
91 continue;
92
93 sb.append("<form method='post' action='").append( req.getRequestURI() ).append("'>");
94 sb.append("<input type='hidden' name='reloadmethod' value='").append(method.getName()).append("'>");
95 sb.append("<input type='submit' name='Reload Config' value='Reload Config'>");
96 sb.append("</form>\n");
97
98 }
99
100
101 } catch (IllegalAccessException e) {
102 throw new ServletException(e);
103 }
104
105 sb.append("</body></html>");
106
107 resp.getWriter().print( sb.toString() );
108 }
109
110 String getBooleanSelectOptions(Field field, Object configObject) throws IllegalAccessException {
111 String trueSelected = field.get(configObject).toString().equals("true") ? " selected " : "";
112 String falseSelected = field.get(configObject).toString().equals("false") ? " selected " : "";
113 return String.format("<option %s>true</option><option %s>false</option>", trueSelected, falseSelected);
114 }
115
116 String getEnumSelectOptions(Field field, Object configObject) throws IllegalAccessException {
117 Class<?> cls = field.getType();
118 StringBuilder sb = new StringBuilder();
119 Object constants[] = cls.getEnumConstants();
120
121 String value = field.get(configObject).toString();
122
123 for (int i = 0; i < constants.length; i++) {
124 String current = constants[i].toString();
125 String selected = current.equals(value) ? " selected " : "";
126 sb.append( String.format("<option %s>%s</option>", selected, current) );
127 }
128
129
130 return sb.toString();
131 }
132
133 String getCleanTypename(Class<?> cls) {
134
135 if (cls.isEnum()) {
136 String parts[] = cls.getName().split("\\.");
137 if (parts.length > 0)
138 return "Enum: " + parts[parts.length-1];
139 else
140 return "Enum: " + cls.getName();
141 } else {
142 return cls.getName();
143 }
144
145 }
146
147 @Override
148 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
149 String name = req.getParameter("varname");
150 String value = req.getParameter("varvalue");
151
152 String reloadmethod = req.getParameter("reloadmethod");
153
154 try {
155 if (reloadmethod != null) { //reload
156
157 Method method = configObject.getClass().getDeclaredMethod(reloadmethod, (Class<?>[]) null);
158 ReloadConfigMethod anno = method.getAnnotation(ReloadConfigMethod.class);
159 if (anno == null) {
160 resp.sendError(500, "Method is not annotated");
161 return;
162 }
163
164
165 method.setAccessible(true);
166 method.invoke(configObject, (Object[])null);
167
168 } else { //set var
169 value = value.trim();
170 Field field = configObject.getClass().getDeclaredField(name);
171 field.setAccessible(true);
172 ConfigVariable anno = field.getAnnotation(ConfigVariable.class);
173
174 if (anno == null) {
175 resp.sendError(500, "Field is not annotated");
176 return;
177 }
178
179 if (anno.readonly() == true) {
180 resp.sendError(500, "Can't write a read-only field");
181 return;
182 }
183
184 ConfigLoader.setFieldValue(field,configObject,value);
185
186
187 } //end of set var
188
189
190 } catch (NoSuchFieldException e) {
191 resp.sendError(500, "No such field: " + name);
192 } catch (IllegalAccessException e) { //this can't happen
193 throw new ServletException(e);
194 } catch (NoSuchMethodException e) {
195 resp.sendError(500, "No such method: " + reloadmethod);
196 } catch (InvocationTargetException e) {
197 resp.sendError(500, "Could not invoke method: " + reloadmethod);
198 }
199
200 resp.sendRedirect( req.getRequestURI() );
201 }
202
203 }

  ViewVC Help
Powered by ViewVC 1.1.20