1 |
package dk.thoerup.webconfig; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.lang.annotation.Annotation; |
5 |
import java.lang.reflect.Field; |
6 |
|
7 |
import javax.servlet.ServletException; |
8 |
import javax.servlet.http.HttpServlet; |
9 |
import javax.servlet.http.HttpServletRequest; |
10 |
import javax.servlet.http.HttpServletResponse; |
11 |
|
12 |
public class ConfigServlet extends HttpServlet { |
13 |
private static final long serialVersionUID = 1L; |
14 |
|
15 |
private Object configObject; |
16 |
|
17 |
public void setConfigObject(Object obj) { |
18 |
configObject = obj; |
19 |
} |
20 |
|
21 |
|
22 |
@Override |
23 |
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
24 |
|
25 |
|
26 |
resp.setDateHeader("Expires", 0); |
27 |
resp.setHeader("Pragma", "no-cache"); |
28 |
resp.setHeader("Cache-Control", "no-cache"); |
29 |
resp.setContentType("text/html"); |
30 |
|
31 |
StringBuilder sb = new StringBuilder(); |
32 |
|
33 |
sb.append("<html><body><h3>WebConfig:</h3><table border='1' cellspacing='0'>"); |
34 |
sb.append("<tr><th>Field name</th><th>Type</th><th>Description</th><th>Value</th><th> </th></tr>\n"); |
35 |
|
36 |
|
37 |
Field fields[] = configObject.getClass().getDeclaredFields(); |
38 |
|
39 |
try { |
40 |
for(Field field : fields) { |
41 |
field.setAccessible(true); |
42 |
|
43 |
ConfigVariable anno = field.getAnnotation(ConfigVariable.class); |
44 |
if (anno == null) |
45 |
continue; |
46 |
|
47 |
sb.append("<tr>"); |
48 |
sb.append("<td>").append( field.getName() ).append("</td>"); |
49 |
sb.append("<td>").append( field.getType().getName() ).append("</td>"); |
50 |
sb.append("<td>").append( anno.description() ).append(" </td>"); |
51 |
|
52 |
sb.append("<form action='").append( req.getRequestURI() ).append("' method='post'>"); |
53 |
sb.append("<td><input type='text' name='varvalue' value='").append( ""+ field.get(configObject) ).append("' /></td>"); |
54 |
sb.append("<input type='hidden' name='varname' value='").append( field.getName() ).append("' />"); |
55 |
|
56 |
if (anno.readonly() == false) |
57 |
sb.append("<td><input type='submit' name='Set' value='Set'></td>\n"); |
58 |
else |
59 |
sb.append("<td> </td>"); |
60 |
|
61 |
sb.append("</form></tr>\n"); |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
field.getType().getName(); |
67 |
} |
68 |
} catch (IllegalAccessException e) { |
69 |
throw new ServletException(e); |
70 |
} |
71 |
|
72 |
sb.append("</table></body></html>"); |
73 |
|
74 |
resp.getWriter().print( sb.toString() ); |
75 |
} |
76 |
|
77 |
@Override |
78 |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
79 |
String name = req.getParameter("varname"); |
80 |
String value = req.getParameter("varvalue"); |
81 |
|
82 |
try { |
83 |
Field field = configObject.getClass().getDeclaredField(name); |
84 |
field.setAccessible(true); |
85 |
ConfigVariable anno = field.getAnnotation(ConfigVariable.class); |
86 |
|
87 |
if (anno == null) |
88 |
throw new ServletException("Field is not annotated"); |
89 |
|
90 |
if (anno.readonly() == true) |
91 |
throw new ServletException("Can't write a read-only field"); |
92 |
|
93 |
|
94 |
if (field.getType().getName().equals("int") ) { |
95 |
field.setInt(configObject, Integer.parseInt(value) ); |
96 |
} else if (field.getType().getName().equals("boolean") ) { |
97 |
field.setBoolean(configObject, Boolean.parseBoolean(value) ); |
98 |
} else if (field.getType().getName().equals("long") ) { |
99 |
field.setLong(configObject, Long.parseLong(value) ); |
100 |
} else if (field.getType().getName().equals("short") ) { |
101 |
field.setShort(configObject, Short.parseShort(value) ); |
102 |
} else if (field.getType().getName().equals("double") ) { |
103 |
field.setDouble(configObject, Double.parseDouble(value) ); |
104 |
} else if (field.getType().getName().equals("float") ) { |
105 |
field.setFloat(configObject, Float.parseFloat(value) ); |
106 |
} else if (field.getType().getName().equals("char") ) { |
107 |
field.setFloat(configObject, value.charAt(0) ); |
108 |
} else { |
109 |
field.set(configObject, value ); |
110 |
} |
111 |
|
112 |
|
113 |
|
114 |
} catch (NoSuchFieldException e) { |
115 |
throw new ServletException(e); |
116 |
} catch (IllegalAccessException e) { //this can't happen |
117 |
throw new ServletException(e); |
118 |
} |
119 |
|
120 |
resp.sendRedirect( req.getRequestURI() ); |
121 |
} |
122 |
|
123 |
} |