View Javadoc
1   /*
2    * Copyright 2010-2012 Capgemini
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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  	 * (non-Javadoc)
56  	 *
57  	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.
58  	 * HttpServletRequest , javax.servlet.http.HttpServletResponse)
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  	 * (non-Javadoc)
68  	 *
69  	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.
70  	 * HttpServletRequest , javax.servlet.http.HttpServletResponse)
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  	 * Init the AppStatus Web UI.
80  	 * <p>
81  	 * Read <b>bean</> init parameter. If defined, switch to Spring-enabled
82  	 * behavior.
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  			// Using Spring
99  			instantiation = new SpringObjectInstantiationListener(this.getServletContext());
100 
101 			// Status
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 		// Pages
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 		// Custom pages
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 		// Radiator at the end.
161 		addPage(pages, new RadiatorPage());
162 
163 		// Init
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 }