package dk.thoerup.webconfig; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ConfigServlet extends HttpServlet { private static final long serialVersionUID = 1L; private Object configObject; public void setConfigObject(Object obj) { configObject = obj; } @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setDateHeader("Expires", 0); resp.setHeader("Pragma", "no-cache"); resp.setHeader("Cache-Control", "no-cache"); resp.setContentType("text/html"); StringBuilder sb = new StringBuilder(); sb.append("

WebConfig:

"); sb.append("\n"); Field fields[] = configObject.getClass().getDeclaredFields(); try { for(Field field : fields) { field.setAccessible(true); ConfigVariable anno = field.getAnnotation(ConfigVariable.class); if (anno == null) continue; sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); sb.append(""); if (anno.readonly() == false) sb.append("\n"); else sb.append(""); sb.append("\n"); field.getType().getName(); } } catch (IllegalAccessException e) { throw new ServletException(e); } sb.append("
Field nameTypeDescriptionValue 
").append( field.getName() ).append("").append( field.getType().getName() ).append("").append( anno.description() ).append(" 
 
"); resp.getWriter().print( sb.toString() ); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("varname"); String value = req.getParameter("varvalue"); try { Field field = configObject.getClass().getDeclaredField(name); field.setAccessible(true); ConfigVariable anno = field.getAnnotation(ConfigVariable.class); if (anno == null) { resp.sendError(500, "Field is not annotated"); return; } if (anno.readonly() == true) { resp.sendError(500, "Can't write a read-only field"); return; } if (field.getType().getName().equals("int") ) { field.setInt(configObject, Integer.parseInt(value) ); } else if (field.getType().getName().equals("boolean") ) { field.setBoolean(configObject, Boolean.parseBoolean(value) ); } else if (field.getType().getName().equals("long") ) { field.setLong(configObject, Long.parseLong(value) ); } else if (field.getType().getName().equals("short") ) { field.setShort(configObject, Short.parseShort(value) ); } else if (field.getType().getName().equals("double") ) { field.setDouble(configObject, Double.parseDouble(value) ); } else if (field.getType().getName().equals("float") ) { field.setFloat(configObject, Float.parseFloat(value) ); } else if (field.getType().getName().equals("char") ) { field.setFloat(configObject, value.charAt(0) ); } else { field.set(configObject, value ); } } catch (NoSuchFieldException e) { resp.sendError(500, "No such field: " + name); } catch (IllegalAccessException e) { //this can't happen throw new ServletException(e); } resp.sendRedirect( req.getRequestURI() ); } }