View Javadoc
1   /*
2    * Copyright 2010 Capgemini Licensed under the Apache License, Version 2.0 (the
3    * "License"); you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    * 
6    * http://www.apache.org/licenses/LICENSE-2.0
7    * 
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package net.sf.appstatus.jmx;
15  
16  import java.util.ArrayList;
17  import java.util.HashMap;
18  import java.util.List;
19  import java.util.Map;
20  
21  import javax.servlet.ServletContext;
22  
23  import net.sf.appstatus.core.IServletContextProvider;
24  import net.sf.appstatus.core.AppStatus;
25  import net.sf.appstatus.core.check.ICheckResult;
26  
27  import org.springframework.beans.BeansException;
28  import org.springframework.context.ApplicationContext;
29  import org.springframework.context.ApplicationContextAware;
30  import org.springframework.jmx.export.annotation.ManagedAttribute;
31  import org.springframework.jmx.export.annotation.ManagedResource;
32  import org.springframework.web.context.ServletContextAware;
33  
34  /**
35   * JMX exposure of StatusChecker beans.
36   * 
37   * @author LABEMONT
38   * 
39   */
40  @ManagedResource(objectName = "AppStatus:bean=ServicesStatusChecker")
41  public class StatusJmx implements ApplicationContextAware, ServletContextAware {
42  
43    private AppStatus statusService = null;
44  
45    private ApplicationContext applicationContext;
46  
47    private ServletContext servletContext;
48  
49    public void setServletContext(ServletContext servletContext) {
50      this.servletContext = servletContext;
51    }
52  
53    @ManagedAttribute(description = "Status list", currencyTimeLimit = 15)
54    public Map<String, String> getStatus() {
55      Map<String, String> statusChecker = new HashMap<String, String>();
56      for (ICheckResult result : statusService.checkAll(null)) {
57        statusChecker.put(result.getProbeName(), formatCodeDisplay(result.getCode()));
58      }
59      return statusChecker;
60    }
61  
62    @ManagedAttribute(description = "Full status list : display return code, description and resolution steps.", currencyTimeLimit = 15)
63    public Map<String, List<String>> getFullStatus() {
64      Map<String, List<String>> statusChecker = new HashMap<String, List<String>>();
65      List<String> statusAttributs = null;
66      for (ICheckResult result : statusService.checkAll(null)) {
67        statusAttributs = new ArrayList<String>();
68        statusAttributs.add(formatCodeDisplay(result.getCode()));
69        statusAttributs.add(result.getDescription());
70        statusAttributs.add(result.getResolutionSteps());
71        statusChecker.put(result.getProbeName(), statusAttributs);
72      }
73      return statusChecker;
74    }
75  
76    @ManagedAttribute(description = "The services properties")
77    public Map<String, Map<String, String>> getServicesProperties() {
78      return this.statusService.getProperties();
79    }
80  
81    /**
82     * Human readable code format.
83     * 
84     * @param code
85     * @return the code from {@link ICheckResult} or the int code if not found
86     */
87    protected String formatCodeDisplay(int code) {
88      String codeDisplay = "";
89      switch (code) {
90      case (ICheckResult.ERROR):
91        codeDisplay = "ERROR";
92        break;
93      case (ICheckResult.OK):
94        codeDisplay = "OK";
95        break;
96      default:
97        codeDisplay = Integer.toString(code);
98      }
99      return codeDisplay;
100   }
101 
102   /**
103    * Load configuration from /status-jmx-conf.properties file.<br/>
104    * If not found look for Spring beans.
105    */
106   public void init() {
107     statusService = new AppStatus();
108 
109     statusService.setObjectInstanciationListener(new SpringBeanInstantiationListener(this.applicationContext));
110 
111     this.statusService.setServletContextProvider(new IServletContextProvider() {
112 
113       public ServletContext getServletContext() {
114         return StatusJmx.this.servletContext;
115       }
116     });
117 
118     statusService.init();
119   }
120 
121   public void setApplicationContext(ApplicationContext springApplicationContext) throws BeansException {
122     this.applicationContext = springApplicationContext;
123   }
124 }