/[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 1328 - (show annotations) (download)
Wed Apr 20 04:41:18 2011 UTC (13 years ago) by torben
File size: 5535 byte(s)
Add option for reloading config by annotating a method
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( field.getType().getName() ).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 sb.append("<td><input type='text' name='varvalue' value='").append( ""+ field.get(configObject) ).append("' /></td>");
61 sb.append("<td><input type='submit' name='Set' value='Set'></td>\n");
62 } else {
63 sb.append("<td>").append( ""+ field.get(configObject) ).append("&nbsp;</td>");
64 sb.append("<td>&nbsp;</td>");
65 }
66
67 sb.append("</form></tr>\n");
68 }
69
70 sb.append("</table>\n\n");
71
72 Method[] methods = configObject.getClass().getDeclaredMethods();
73 for(Method method : methods) {
74 method.setAccessible(true);
75
76 ReloadConfig anno = method.getAnnotation(ReloadConfig.class);
77 if (anno == null)
78 continue;
79
80 sb.append("<form method='post' action='").append( req.getRequestURI() ).append("'>");
81 sb.append("<input type='hidden' name='reloadmethod' value='").append(method.getName()).append("'>");
82 sb.append("<input type='submit' name='Reload Config' value='Reload Config'>");
83 sb.append("</form>\n");
84
85 }
86
87
88 } catch (IllegalAccessException e) {
89 throw new ServletException(e);
90 }
91
92 sb.append("</body></html>");
93
94 resp.getWriter().print( sb.toString() );
95 }
96
97 @Override
98 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
99 String name = req.getParameter("varname");
100 String value = req.getParameter("varvalue");
101
102 String reloadmethod = req.getParameter("reloadmethod");
103
104 try {
105 if (reloadmethod != null) { //reload
106
107 Method method = configObject.getClass().getDeclaredMethod(reloadmethod, (Class<?>[]) null);
108 method.setAccessible(true);
109 method.invoke(configObject, (Object[])null);
110
111 } else { //set var
112 value = value.trim();
113 Field field = configObject.getClass().getDeclaredField(name);
114 field.setAccessible(true);
115 ConfigVariable anno = field.getAnnotation(ConfigVariable.class);
116
117 if (anno == null) {
118 resp.sendError(500, "Field is not annotated");
119 return;
120 }
121
122 if (anno.readonly() == true) {
123 resp.sendError(500, "Can't write a read-only field");
124 return;
125 }
126
127
128
129 if (field.getType().getName().equals("int") ) {
130 field.setInt(configObject, Integer.parseInt(value) );
131 } else if (field.getType().getName().equals("boolean") ) {
132 value = value.toLowerCase();
133 if (! (value.equals("true") || value.equals("false"))) {
134 resp.sendError(500, "Only 'true' or 'false' as values for boolean field");
135 return;
136 }
137 field.setBoolean(configObject, Boolean.parseBoolean(value) );
138 } else if (field.getType().getName().equals("long") ) {
139 field.setLong(configObject, Long.parseLong(value) );
140 } else if (field.getType().getName().equals("short") ) {
141 field.setShort(configObject, Short.parseShort(value) );
142 } else if (field.getType().getName().equals("double") ) {
143 field.setDouble(configObject, Double.parseDouble(value) );
144 } else if (field.getType().getName().equals("float") ) {
145 field.setFloat(configObject, Float.parseFloat(value) );
146 } else if (field.getType().getName().equals("char") ) {
147 field.setFloat(configObject, value.charAt(0) );
148 } else {
149 field.set(configObject, value );
150 }
151 } //end of set var
152
153
154 } catch (NoSuchFieldException e) {
155 resp.sendError(500, "No such field: " + name);
156 } catch (IllegalAccessException e) { //this can't happen
157 throw new ServletException(e);
158 } catch (NoSuchMethodException e) {
159 resp.sendError(500, "No such method: " + reloadmethod);
160 } catch (InvocationTargetException e) {
161 resp.sendError(500, "Could not invoke method: " + reloadmethod);
162 }
163
164 resp.sendRedirect( req.getRequestURI() );
165 }
166
167 }

  ViewVC Help
Powered by ViewVC 1.1.20