CatServlet.java
2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.dianping.cat.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.unidal.initialization.DefaultModuleContext;
import org.unidal.initialization.ModuleContext;
import org.unidal.initialization.ModuleInitializer;
import org.unidal.web.AbstractContainerServlet;
import com.dianping.cat.Cat;
public class CatServlet extends AbstractContainerServlet {
private static final long serialVersionUID = 1L;
private Exception m_exception;
private File getConfigFile(ServletConfig config, String name, String defaultConfigValue) {
String configValue = config.getInitParameter(name);
if (configValue != null) {
if (configValue.startsWith("/")) {
return new File(configValue);
} else {
return new File(Cat.getCatHome(), configValue);
}
} else {
return new File(Cat.getCatHome(), defaultConfigValue);
}
}
@Override
protected void initComponents(ServletConfig servletConfig) throws ServletException {
try {
ModuleContext ctx = new DefaultModuleContext(getContainer());
ModuleInitializer initializer = ctx.lookup(ModuleInitializer.class);
File clientXmlFile = getConfigFile(servletConfig, "cat-client-xml", "client.xml");
File serverXmlFile = getConfigFile(servletConfig, "cat-server-xml", "server.xml");
ctx.setAttribute("cat-client-config-file", clientXmlFile);
ctx.setAttribute("cat-server-config-file", serverXmlFile);
initializer.execute(ctx);
} catch (Exception e) {
m_exception = e;
System.err.println(e);
throw new ServletException(e);
}
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setCharacterEncoding("utf-8");
res.setContentType("text/plain");
PrintWriter writer = res.getWriter();
if (m_exception != null) {
writer.write("Server has NOT been initialized successfully!\r\n\r\n");
m_exception.printStackTrace(writer);
} else {
writer.write("Not implemented yet!");
}
}
}