1 package org.astrogrid.common.j2ee.environment;
2
3 import java.net.URL;
4 import java.util.Enumeration;
5 import javax.servlet.RequestDispatcher;
6 import javax.servlet.ServletContext;
7 import javax.servlet.ServletException;
8 import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 /***
15 * A servlet to support configuration of a web application's environment.
16 * The servlet constructs and maintains a Java bean of type
17 * {@link org.astrogrid.common.j2ee.environment.Environment} which is
18 * exposed to other web components via the servlet context.
19 * <p>
20 * On initialization, the servlet loads web.xml, the web
21 * application's deployment-descriptor and creates from it the
22 * Environment bean. The bean is set as an attribute in the
23 * servlet context, which makes it available to JSPs via this
24 * construct:
25 * <pre>
26 * <jsp:useBean class="org.astrogrid.common.j2ee.environment"
27 * id="environment" scope="application"/>
28 * </pre>
29 * <p>
30 * An HTTP request to this servlet is treated as an instruction to update
31 * the values of environment entries in the Environment bean. Where the name
32 * of a parameter matches the name of an EnvEntry in the Environment, the
33 * parameter's value (it is treated as a scalar value) is set as the
34 * replacementValue property for the EnvEntry bean.
35 *
36 * @author Guy Rixon
37 */
38 public class EnvironmentServlet extends HttpServlet {
39
40 private static Log log = LogFactory.getLog(EnvironmentServlet.class);
41
42 /***
43 * The bean representing the overall environment.
44 * The purpose of the servlet is to maintain this bean.
45 */
46 private Environment environment;
47
48 /***
49 * The web resource to which control is transfered after this
50 * servlet finishes processing a request.
51 */
52 private String nextResource;
53
54 /*** Creates a new instance of EnvironmentServlet */
55 public EnvironmentServlet() {
56 this.environment = new Environment();
57 }
58
59 public void init() throws ServletException {
60 log.debug("Initializing the EnvironmentServlet...");
61 ServletContext context = this.getServletContext();
62 try {
63 this.nextResource = this.getInitParameter("next.resource");
64 if (this.nextResource == null) {
65 this.nextResource = "/admin/environment-main.jsp";
66 }
67 log.debug("The EnvironmentServlet redirects to " + this.nextResource);
68
69 URL webDotXmlUrl = context.getResource("/WEB-INF/web.xml");
70 this.environment.setDeploymentDescriptor(webDotXmlUrl.toString());
71 context.setAttribute("environment", this.environment);
72
73 String rootUri = context.getResource("/").toString();
74 String rootUri2 = rootUri.substring(0, rootUri.length()-1);
75 int slashIndex = rootUri2.lastIndexOf("/");
76 this.environment.setContextPath(rootUri2.substring(slashIndex));
77 }
78 catch (Exception e) {
79 log.warn("The EnvironmentServlet failed during initialization: " + e);
80 throw new ServletException(e);
81 }
82 }
83
84 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
85 this.doPost(request, response);
86 }
87
88 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
89 try {
90
91
92
93 Enumeration paramNames = request.getParameterNames();
94 while (paramNames.hasMoreElements()) {
95 String name = (String)paramNames.nextElement();
96 String value = request.getParameter(name);
97 this.updateEnvEntry(name, value);
98 }
99
100
101 RequestDispatcher dispatcher = request.getRequestDispatcher(this.nextResource);
102 dispatcher.forward(request, response);
103 }
104 catch (Exception e) {
105 throw new ServletException(e);
106 }
107 }
108
109 private void updateEnvEntry(String name, String value) {
110 EnvEntry[] entries = this.environment.getEnvEntry();
111 for (int i = 0; i < entries.length; i++) {
112 if (name.equals(entries[i].getName())) {
113 entries[i].setReplacementValue(value);
114 }
115 }
116 }
117
118 }