1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.appstatus.web;
17
18 import static org.apache.commons.lang3.StringUtils.defaultString;
19
20 import java.io.IOException;
21 import java.util.LinkedHashMap;
22 import java.util.Map;
23
24 import javax.servlet.ServletConfig;
25 import javax.servlet.ServletContext;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServlet;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.apache.commons.lang3.StringUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import net.sf.appstatus.core.AppStatus;
36 import net.sf.appstatus.core.AppStatusStatic;
37 import net.sf.appstatus.core.IServletContextProvider;
38 import net.sf.appstatus.web.pages.BatchPage;
39 import net.sf.appstatus.web.pages.LoggersPage;
40 import net.sf.appstatus.web.pages.RadiatorPage;
41 import net.sf.appstatus.web.pages.ServicesPage;
42 import net.sf.appstatus.web.pages.StatusPage;
43
44 public class StatusServlet extends HttpServlet {
45
46 private static Logger logger = LoggerFactory.getLogger(AppStatus.class);
47 private static final long serialVersionUID = 3912325072098291029L;
48 private static StatusWebHandler statusWeb = null;
49
50 private void addPage(final Map<String, IPage> map, final IPage page) {
51 map.put(page.getId(), page);
52 }
53
54
55
56
57
58
59
60 @Override
61 protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
62 throws ServletException, IOException {
63 statusWeb.doGet(req, resp);
64 }
65
66
67
68
69
70
71
72 @Override
73 protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
74 throws ServletException, IOException {
75 statusWeb.doPost(req, resp);
76 }
77
78
79
80
81
82
83
84 @Override
85 public void init(final ServletConfig config) throws ServletException {
86 super.init(config);
87
88 super.init();
89
90 SpringObjectInstantiationListener instantiation = null;
91 final String beanName = getInitParameter("bean");
92 final String[] pagesBeanNames = StringUtils.split(getInitParameter("custom-pages"), ", ");
93 final Map<String, IPage> pages = new LinkedHashMap<String, IPage>();
94 ;
95
96 AppStatus status;
97 if (beanName != null) {
98
99 instantiation = new SpringObjectInstantiationListener(this.getServletContext());
100
101
102 status = (AppStatus) instantiation.getInstance(beanName);
103
104 } else {
105 status = AppStatusStatic.getInstance();
106
107 }
108
109 status.setServletContextProvider(new IServletContextProvider() {
110 public ServletContext getServletContext() {
111 return StatusServlet.this.getServletContext();
112 }
113 });
114
115
116
117 addPage(pages, new StatusPage());
118
119 if (status.getServiceManager() != null) {
120 addPage(pages, new ServicesPage());
121 }
122
123 if (status.getBatchManager() != null) {
124 addPage(pages, new BatchPage());
125
126 }
127
128 if (status.getLoggersManager() != null) {
129 addPage(pages, new LoggersPage());
130
131 }
132
133
134 if (pagesBeanNames != null) {
135 for (final String pageBean : pagesBeanNames) {
136
137 IPage newPage = null;
138
139 if (instantiation != null) {
140 newPage = (IPage) instantiation.getInstance(pageBean);
141 if (newPage == null) {
142 try {
143 newPage = (IPage) Thread.currentThread().getContextClassLoader().loadClass(pageBean)
144 .newInstance();
145 } catch (final ClassNotFoundException e) {
146 logger.warn("Class {} not found ", pageBean, e);
147 } catch (final InstantiationException e) {
148 logger.warn("Cannot instanciate {} ", pageBean, e);
149 } catch (final IllegalAccessException e) {
150 logger.warn("Cannot access class {} for instantiation ", pageBean, e);
151 }
152 }
153 }
154
155 addPage(pages, newPage);
156
157 }
158 }
159
160
161 addPage(pages, new RadiatorPage());
162
163
164 statusWeb = new StatusWebHandler();
165 statusWeb.setAppStatus(status);
166 statusWeb.setApplicationName(defaultString(config.getServletContext().getServletContextName(), "No name"));
167 statusWeb.setPages(pages);
168 statusWeb.init();
169 }
170 }