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> </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( field.getType().getName() ).append("</td>"); |
51 |
sb.append("<td>").append( anno.description() ).append(" </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 |
String trueSelected = field.get(configObject).toString().equals("true") ? " selected " : ""; |
62 |
String falseSelected = field.get(configObject).toString().equals("false") ? " selected " : ""; |
63 |
String options = String.format("<option %s>true</option><option %s>false</option>", trueSelected, falseSelected); |
64 |
sb.append("<td><select name='varvalue'>").append(options).append("</select></td>"); |
65 |
} else { |
66 |
sb.append("<td><input type='text' name='varvalue' value='").append( "" + field.get(configObject) ).append("' /></td>"); |
67 |
} |
68 |
sb.append("<td><input type='submit' name='Set' value='Set'></td>\n"); |
69 |
} else { |
70 |
sb.append("<td>").append( ""+ field.get(configObject) ).append(" </td>"); |
71 |
sb.append("<td> </td>"); |
72 |
} |
73 |
|
74 |
sb.append("</form></tr>\n"); |
75 |
} |
76 |
|
77 |
sb.append("</table>\n\n"); |
78 |
|
79 |
Method[] methods = configObject.getClass().getDeclaredMethods(); |
80 |
for(Method method : methods) { |
81 |
method.setAccessible(true); |
82 |
|
83 |
ReloadConfigMethod anno = method.getAnnotation(ReloadConfigMethod.class); |
84 |
if (anno == null) |
85 |
continue; |
86 |
|
87 |
sb.append("<form method='post' action='").append( req.getRequestURI() ).append("'>"); |
88 |
sb.append("<input type='hidden' name='reloadmethod' value='").append(method.getName()).append("'>"); |
89 |
sb.append("<input type='submit' name='Reload Config' value='Reload Config'>"); |
90 |
sb.append("</form>\n"); |
91 |
|
92 |
} |
93 |
|
94 |
|
95 |
} catch (IllegalAccessException e) { |
96 |
throw new ServletException(e); |
97 |
} |
98 |
|
99 |
sb.append("</body></html>"); |
100 |
|
101 |
resp.getWriter().print( sb.toString() ); |
102 |
} |
103 |
|
104 |
@Override |
105 |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
106 |
String name = req.getParameter("varname"); |
107 |
String value = req.getParameter("varvalue"); |
108 |
|
109 |
String reloadmethod = req.getParameter("reloadmethod"); |
110 |
|
111 |
try { |
112 |
if (reloadmethod != null) { //reload |
113 |
|
114 |
Method method = configObject.getClass().getDeclaredMethod(reloadmethod, (Class<?>[]) null); |
115 |
ReloadConfigMethod anno = method.getAnnotation(ReloadConfigMethod.class); |
116 |
if (anno == null) { |
117 |
resp.sendError(500, "Method is not annotated"); |
118 |
return; |
119 |
} |
120 |
|
121 |
|
122 |
method.setAccessible(true); |
123 |
method.invoke(configObject, (Object[])null); |
124 |
|
125 |
} else { //set var |
126 |
value = value.trim(); |
127 |
Field field = configObject.getClass().getDeclaredField(name); |
128 |
field.setAccessible(true); |
129 |
ConfigVariable anno = field.getAnnotation(ConfigVariable.class); |
130 |
|
131 |
if (anno == null) { |
132 |
resp.sendError(500, "Field is not annotated"); |
133 |
return; |
134 |
} |
135 |
|
136 |
if (anno.readonly() == true) { |
137 |
resp.sendError(500, "Can't write a read-only field"); |
138 |
return; |
139 |
} |
140 |
|
141 |
ConfigLoader.setFieldValue(field,configObject,value); |
142 |
|
143 |
|
144 |
} //end of set var |
145 |
|
146 |
|
147 |
} catch (NoSuchFieldException e) { |
148 |
resp.sendError(500, "No such field: " + name); |
149 |
} catch (IllegalAccessException e) { //this can't happen |
150 |
throw new ServletException(e); |
151 |
} catch (NoSuchMethodException e) { |
152 |
resp.sendError(500, "No such method: " + reloadmethod); |
153 |
} catch (InvocationTargetException e) { |
154 |
resp.sendError(500, "Could not invoke method: " + reloadmethod); |
155 |
} |
156 |
|
157 |
resp.sendRedirect( req.getRequestURI() ); |
158 |
} |
159 |
|
160 |
} |