/[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 1369 - (show annotations) (download)
Wed Apr 20 21:20:58 2011 UTC (13 years, 1 month ago) by torben
File size: 5595 byte(s)
Add support for enum types
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 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("&nbsp;</td>");
71 sb.append("<td>&nbsp;</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 String getCleanTypename(Class<?> cls) {
105
106 if (cls.isEnum()) {
107 String parts[] = cls.getName().split("\\.");
108 if (parts.length > 0)
109 return "Enum: " + parts[parts.length-1];
110 else
111 return "Enum: " + cls.getName();
112 } else {
113 return cls.getName();
114 }
115
116 }
117
118 @Override
119 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
120 String name = req.getParameter("varname");
121 String value = req.getParameter("varvalue");
122
123 String reloadmethod = req.getParameter("reloadmethod");
124
125 try {
126 if (reloadmethod != null) { //reload
127
128 Method method = configObject.getClass().getDeclaredMethod(reloadmethod, (Class<?>[]) null);
129 ReloadConfigMethod anno = method.getAnnotation(ReloadConfigMethod.class);
130 if (anno == null) {
131 resp.sendError(500, "Method is not annotated");
132 return;
133 }
134
135
136 method.setAccessible(true);
137 method.invoke(configObject, (Object[])null);
138
139 } else { //set var
140 value = value.trim();
141 Field field = configObject.getClass().getDeclaredField(name);
142 field.setAccessible(true);
143 ConfigVariable anno = field.getAnnotation(ConfigVariable.class);
144
145 if (anno == null) {
146 resp.sendError(500, "Field is not annotated");
147 return;
148 }
149
150 if (anno.readonly() == true) {
151 resp.sendError(500, "Can't write a read-only field");
152 return;
153 }
154
155 ConfigLoader.setFieldValue(field,configObject,value);
156
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