/[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 1329 - (show annotations) (download)
Wed Apr 20 04:45:28 2011 UTC (13 years, 1 month ago) by torben
File size: 5716 byte(s)
Make sure the method is annotated before attempt to call it
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 ReloadConfig anno = method.getAnnotation(ReloadConfig.class);
109 if (anno == null) {
110 resp.sendError(500, "Method is not annotated");
111 return;
112 }
113
114
115 method.setAccessible(true);
116 method.invoke(configObject, (Object[])null);
117
118 } else { //set var
119 value = value.trim();
120 Field field = configObject.getClass().getDeclaredField(name);
121 field.setAccessible(true);
122 ConfigVariable anno = field.getAnnotation(ConfigVariable.class);
123
124 if (anno == null) {
125 resp.sendError(500, "Field is not annotated");
126 return;
127 }
128
129 if (anno.readonly() == true) {
130 resp.sendError(500, "Can't write a read-only field");
131 return;
132 }
133
134
135
136 if (field.getType().getName().equals("int") ) {
137 field.setInt(configObject, Integer.parseInt(value) );
138 } else if (field.getType().getName().equals("boolean") ) {
139 value = value.toLowerCase();
140 if (! (value.equals("true") || value.equals("false"))) {
141 resp.sendError(500, "Only 'true' or 'false' as values for boolean field");
142 return;
143 }
144 field.setBoolean(configObject, Boolean.parseBoolean(value) );
145 } else if (field.getType().getName().equals("long") ) {
146 field.setLong(configObject, Long.parseLong(value) );
147 } else if (field.getType().getName().equals("short") ) {
148 field.setShort(configObject, Short.parseShort(value) );
149 } else if (field.getType().getName().equals("double") ) {
150 field.setDouble(configObject, Double.parseDouble(value) );
151 } else if (field.getType().getName().equals("float") ) {
152 field.setFloat(configObject, Float.parseFloat(value) );
153 } else if (field.getType().getName().equals("char") ) {
154 field.setFloat(configObject, value.charAt(0) );
155 } else {
156 field.set(configObject, value );
157 }
158 } //end of set var
159
160
161 } catch (NoSuchFieldException e) {
162 resp.sendError(500, "No such field: " + name);
163 } catch (IllegalAccessException e) { //this can't happen
164 throw new ServletException(e);
165 } catch (NoSuchMethodException e) {
166 resp.sendError(500, "No such method: " + reloadmethod);
167 } catch (InvocationTargetException e) {
168 resp.sendError(500, "Could not invoke method: " + reloadmethod);
169 }
170
171 resp.sendRedirect( req.getRequestURI() );
172 }
173
174 }

  ViewVC Help
Powered by ViewVC 1.1.20