1 |
package dk.thoerup.webconfig; |
package dk.thoerup.webconfig; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.lang.annotation.Annotation; |
|
4 |
import java.lang.reflect.Field; |
import java.lang.reflect.Field; |
5 |
|
import java.lang.reflect.InvocationTargetException; |
6 |
|
import java.lang.reflect.Method; |
7 |
|
|
8 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
9 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
64 |
sb.append("<td> </td>"); |
sb.append("<td> </td>"); |
65 |
} |
} |
66 |
|
|
67 |
sb.append("</form></tr>\n"); |
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 |
|
|
|
|
|
|
field.getType().getName(); |
|
85 |
} |
} |
86 |
|
|
87 |
|
|
88 |
} catch (IllegalAccessException e) { |
} catch (IllegalAccessException e) { |
89 |
throw new ServletException(e); |
throw new ServletException(e); |
90 |
} |
} |
91 |
|
|
92 |
sb.append("</table></body></html>"); |
sb.append("</body></html>"); |
93 |
|
|
94 |
resp.getWriter().print( sb.toString() ); |
resp.getWriter().print( sb.toString() ); |
95 |
} |
} |
97 |
@Override |
@Override |
98 |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
99 |
String name = req.getParameter("varname"); |
String name = req.getParameter("varname"); |
100 |
String value = req.getParameter("varvalue").trim(); |
String value = req.getParameter("varvalue"); |
101 |
|
|
102 |
|
String reloadmethod = req.getParameter("reloadmethod"); |
103 |
|
|
104 |
try { |
try { |
105 |
Field field = configObject.getClass().getDeclaredField(name); |
if (reloadmethod != null) { //reload |
106 |
field.setAccessible(true); |
|
107 |
ConfigVariable anno = field.getAnnotation(ConfigVariable.class); |
Method method = configObject.getClass().getDeclaredMethod(reloadmethod, (Class<?>[]) null); |
108 |
|
method.setAccessible(true); |
109 |
if (anno == null) { |
method.invoke(configObject, (Object[])null); |
110 |
resp.sendError(500, "Field is not annotated"); |
|
111 |
return; |
} else { //set var |
112 |
} |
value = value.trim(); |
113 |
|
Field field = configObject.getClass().getDeclaredField(name); |
114 |
if (anno.readonly() == true) { |
field.setAccessible(true); |
115 |
resp.sendError(500, "Can't write a read-only field"); |
ConfigVariable anno = field.getAnnotation(ConfigVariable.class); |
116 |
return; |
|
117 |
} |
if (anno == null) { |
118 |
|
resp.sendError(500, "Field is not annotated"); |
119 |
|
return; |
120 |
|
} |
121 |
if (field.getType().getName().equals("int") ) { |
|
122 |
field.setInt(configObject, Integer.parseInt(value) ); |
if (anno.readonly() == true) { |
123 |
} else if (field.getType().getName().equals("boolean") ) { |
resp.sendError(500, "Can't write a read-only field"); |
|
value = value.toLowerCase(); |
|
|
if (! (value.equals("true") || value.equals("false"))) { |
|
|
resp.sendError(500, "Only 'true' or 'false' as values for boolean field"); |
|
124 |
return; |
return; |
125 |
} |
} |
126 |
field.setBoolean(configObject, Boolean.parseBoolean(value) ); |
|
127 |
} else if (field.getType().getName().equals("long") ) { |
|
128 |
field.setLong(configObject, Long.parseLong(value) ); |
|
129 |
} else if (field.getType().getName().equals("short") ) { |
if (field.getType().getName().equals("int") ) { |
130 |
field.setShort(configObject, Short.parseShort(value) ); |
field.setInt(configObject, Integer.parseInt(value) ); |
131 |
} else if (field.getType().getName().equals("double") ) { |
} else if (field.getType().getName().equals("boolean") ) { |
132 |
field.setDouble(configObject, Double.parseDouble(value) ); |
value = value.toLowerCase(); |
133 |
} else if (field.getType().getName().equals("float") ) { |
if (! (value.equals("true") || value.equals("false"))) { |
134 |
field.setFloat(configObject, Float.parseFloat(value) ); |
resp.sendError(500, "Only 'true' or 'false' as values for boolean field"); |
135 |
} else if (field.getType().getName().equals("char") ) { |
return; |
136 |
field.setFloat(configObject, value.charAt(0) ); |
} |
137 |
} else { |
field.setBoolean(configObject, Boolean.parseBoolean(value) ); |
138 |
field.set(configObject, value ); |
} 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) { |
} catch (NoSuchFieldException e) { |
155 |
resp.sendError(500, "No such field: " + name); |
resp.sendError(500, "No such field: " + name); |
156 |
} catch (IllegalAccessException e) { //this can't happen |
} catch (IllegalAccessException e) { //this can't happen |
157 |
throw new ServletException(e); |
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() ); |
resp.sendRedirect( req.getRequestURI() ); |