KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > web > pub > cluster > ExtOCSPHealthCheck


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.ui.web.pub.cluster;
15
16 import java.sql.Connection JavaDoc;
17 import java.sql.Statement JavaDoc;
18
19 import javax.servlet.ServletConfig JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21
22 import org.apache.log4j.Logger;
23 import org.ejbca.core.ejb.JNDINames;
24 import org.ejbca.ui.web.protocol.IHealtChecker;
25 import org.ejbca.util.JDBCUtil;
26
27
28
29 /**
30  * Ext OCSP Health Checker.
31  *
32  * Does the following system checks.
33  *
34  * * Not about to run out if memory (configurable through web.xml with param "MinimumFreeMemory")
35  * * Database connection can be established.
36  * * All OCSPSignTokens are aktive if not set as offline.
37  *
38  * @author Philip Vendil
39  * @version $Id: ExtOCSPHealthCheck.java,v 1.3 2006/07/25 09:18:00 primelars Exp $
40  */

41
42 public class ExtOCSPHealthCheck implements IHealthCheck {
43     
44     private static Logger log = Logger.getLogger(ExtOCSPHealthCheck.class);
45     private static IHealtChecker healtChecker;
46
47     private int minfreememory = 0;
48     private String JavaDoc checkDBString = null;
49     static public void setHealtChecker(IHealtChecker hc) {
50         healtChecker = hc;
51     }
52     
53     public void init(ServletConfig JavaDoc config) {
54         minfreememory = Integer.parseInt(config.getInitParameter("MinimumFreeMemory")) * 1024 * 1024;
55         checkDBString = config.getInitParameter("checkDBString");
56     }
57
58     
59     public String JavaDoc checkHealth(HttpServletRequest JavaDoc request) {
60         log.debug("Starting HealthCheck health check requested by : " + request.getRemoteAddr());
61         String JavaDoc errormessage = "";
62         
63         errormessage += checkDB();
64         if(errormessage.equals("")){
65           errormessage += checkMemory();
66           errormessage += checkOCSPSignTokens();
67     
68         }
69         
70         if(errormessage.equals("")){
71             // everything seems ok.
72
errormessage = null;
73         }
74         
75         return errormessage;
76     }
77     
78     private String JavaDoc checkMemory(){
79         String JavaDoc retval = "";
80         if(minfreememory >= Runtime.getRuntime().freeMemory()){
81           retval = "\nError Virtual Memory is about to run out, currently free memory :" + Runtime.getRuntime().freeMemory();
82         }
83         
84         return retval;
85     }
86     
87     private String JavaDoc checkDB(){
88         String JavaDoc retval = "";
89         try{
90           Connection JavaDoc con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);
91           Statement JavaDoc statement = con.createStatement();
92           statement.execute(checkDBString);
93           JDBCUtil.close(con);
94         }catch(Exception JavaDoc e){
95             retval = "\nError creating connection to EJBCA Database.";
96             log.error("Error creating connection to EJBCA Database.",e);
97         }
98         return retval;
99     }
100     
101     private String JavaDoc checkOCSPSignTokens(){
102         if ( healtChecker!=null )
103             return healtChecker.healtCheck();
104         else
105             return "No OCSP servlet started";
106     }
107 }
108
Popular Tags