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.pages;
17  
18  import static net.sf.appstatus.web.HtmlUtils.applyLayout;
19  
20  import java.io.IOException;
21  import java.io.UnsupportedEncodingException;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.commons.lang3.StringUtils;
30  
31  import net.sf.appstatus.core.batch.IBatchManager;
32  import net.sf.appstatus.core.check.ICheckResult;
33  import net.sf.appstatus.web.IPage;
34  import net.sf.appstatus.web.StatusWebHandler;
35  
36  /**
37   * This is a radiator page.
38   * <p>
39   * This page displays a quick overview of the application and reloads every
40   * minute.
41   *
42   * @author Nicolas Richeton
43   *
44   */
45  public class RadiatorPage implements IPage {
46  
47  	private static final int STATUS_ERROR = 2;
48  	private static final int STATUS_MAINTENANCE = 3;
49  	private static final int STATUS_OK = 0;
50  	private static final int STATUS_WARN = 1;
51  
52  	public void doGet(final StatusWebHandler webHandler, final HttpServletRequest req, final HttpServletResponse resp)
53  			throws UnsupportedEncodingException, IOException {
54  
55  		// Setup response
56  		resp.setContentType("text/html");
57  		resp.setCharacterEncoding("UTF-8");
58  
59  		// Get Health checks
60  		final List<ICheckResult> results = webHandler.getAppStatus().checkAll(req.getLocale());
61  		int status = webHandler.getAppStatus().isMaintenance() ? STATUS_MAINTENANCE : STATUS_OK;
62  		for (final ICheckResult r : results) {
63  
64  			if (r.getCode() != ICheckResult.OK && !r.isFatal() && status == STATUS_OK) {
65  				status = STATUS_WARN;
66  			}
67  
68  			if (r.getCode() != ICheckResult.OK && r.isFatal()) {
69  				status = STATUS_ERROR;
70  				break;
71  			}
72  		}
73  
74  		String btnClass;
75  
76  		switch (status) {
77  		case STATUS_WARN:
78  			btnClass = "btn-warning";
79  			break;
80  		case STATUS_ERROR:
81  			btnClass = "btn-danger";
82  			break;
83  		case STATUS_MAINTENANCE:
84  			btnClass = "btn-info";
85  			break;
86  		default:
87  			btnClass = "btn-success";
88  		}
89  
90  		// Get batchs status.
91  		final IBatchManager manager = webHandler.getAppStatus().getBatchManager();
92  
93  		String batchStatus = " progress-success ";
94  		String active = StringUtils.EMPTY;
95  		int width = 0;
96  
97  		if (manager != null) {
98  			batchStatus = manager.getErrorBatches().size() > 0 ? " progress-danger " : " progress-success ";
99  			active = manager.getRunningBatches().size() > 0 ? " progress-striped active " : "";
100 			width = manager.getRunningBatches().size() + manager.getFinishedBatches().size() > 0 ? 100 : 0;
101 		}
102 
103 		final Map<String, String> model = new HashMap<String, String>();
104 		model.put("applicationName", webHandler.getApplicationName());
105 		model.put("batchBtnClass", btnClass);
106 		model.put("batchStatus", batchStatus);
107 		model.put("batchActive", active);
108 		model.put("batchBarWidth", width + "%");
109 
110 		resp.getWriter().append(applyLayout(model, "radiatorLayout.html"));
111 	}
112 
113 	public void doPost(final StatusWebHandler webHandler, final HttpServletRequest req,
114 			final HttpServletResponse resp) {
115 		// Nothing to do
116 	}
117 
118 	public String getId() {
119 		return "radiator";
120 	}
121 
122 	public String getName() {
123 		return "Radiator";
124 	}
125 
126 }