KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Iterator JavaDoc;
19
20 import javax.ejb.EJBException JavaDoc;
21 import javax.naming.Context JavaDoc;
22 import javax.naming.InitialContext JavaDoc;
23 import javax.servlet.ServletConfig JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import org.apache.log4j.Logger;
27 import org.ejbca.core.ejb.JNDINames;
28 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocal;
29 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocalHome;
30 import org.ejbca.core.ejb.ca.publisher.IPublisherSessionLocal;
31 import org.ejbca.core.ejb.ca.publisher.IPublisherSessionLocalHome;
32 import org.ejbca.core.model.SecConst;
33 import org.ejbca.core.model.ca.caadmin.CAInfo;
34 import org.ejbca.core.model.ca.catoken.CATokenInfo;
35 import org.ejbca.core.model.ca.catoken.HardCATokenInfo;
36 import org.ejbca.core.model.ca.catoken.IHardCAToken;
37 import org.ejbca.core.model.ca.publisher.PublisherConnectionException;
38 import org.ejbca.core.model.log.Admin;
39 import org.ejbca.util.JDBCUtil;
40
41
42
43 /**
44  * EJBCA Health Checker.
45  *
46  * Does the following system checks.
47  *
48  * * Not about to run out if memory (configurable through web.xml with param "MinimumFreeMemory")
49  * * Database connection can be established.
50  * * All CATokens are aktive if not set as offline.
51  * * All Publishers can establish connection
52  *
53  * @author Philip Vendil
54  * @version $Id: EJBCAHealthCheck.java,v 1.4 2006/05/05 14:19:51 herrvendil Exp $
55  */

56
57 public class EJBCAHealthCheck implements IHealthCheck {
58     
59     private static Logger log = Logger.getLogger(EJBCAHealthCheck.class);
60
61     private Admin admin = new Admin(Admin.TYPE_INTERNALUSER);
62     
63     private int minfreememory = 0;
64     private String JavaDoc checkDBString = null;
65     private boolean checkPublishers = false;
66     
67     public void init(ServletConfig JavaDoc config) {
68         minfreememory = Integer.parseInt(config.getInitParameter("MinimumFreeMemory")) * 1024 * 1024;
69         checkDBString = config.getInitParameter("checkDBString");
70         if(config.getInitParameter("CheckPublishers") != null){
71             checkPublishers = config.getInitParameter("CheckPublishers").equalsIgnoreCase("TRUE");
72         }
73     }
74
75     
76     public String JavaDoc checkHealth(HttpServletRequest JavaDoc request) {
77         log.debug("Starting HealthCheck health check requested by : " + request.getRemoteAddr());
78         String JavaDoc errormessage = "";
79         
80         errormessage += checkDB();
81         if(errormessage.equals("")){
82           errormessage += checkMemory();
83           errormessage += checkCAs();
84         
85           if(checkPublishers){
86             errormessage += checkPublishers();
87           }
88         }
89         
90         if(errormessage.equals("")){
91             // everything seems ok.
92
errormessage = null;
93         }
94         
95         return errormessage;
96     }
97     
98     private String JavaDoc checkMemory(){
99         String JavaDoc retval = "";
100         if(minfreememory >= Runtime.getRuntime().freeMemory()){
101           retval = "\nError Virtual Memory is about to run out, currently free memory :" + Runtime.getRuntime().freeMemory();
102         }
103         
104         return retval;
105     }
106     
107     private String JavaDoc checkDB(){
108         String JavaDoc retval = "";
109         try{
110           Connection JavaDoc con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);
111           Statement JavaDoc statement = con.createStatement();
112           statement.execute(checkDBString);
113           JDBCUtil.close(con);
114         }catch(Exception JavaDoc e){
115             retval = "\nError creating connection to EJBCA Database.";
116             log.error("Error creating connection to EJBCA Database.",e);
117         }
118         return retval;
119     }
120     
121     private String JavaDoc checkCAs(){
122         String JavaDoc retval = "";
123         Iterator JavaDoc iter = getCAAdminSession().getAvailableCAs(admin).iterator();
124         while(iter.hasNext()){
125             CAInfo cainfo = getCAAdminSession().getCAInfo(admin,((Integer JavaDoc) iter.next()).intValue());
126             CATokenInfo tokeninfo = cainfo.getCATokenInfo();
127             if(cainfo.getStatus() == SecConst.CA_ACTIVE){
128               if(tokeninfo instanceof HardCATokenInfo && ((HardCATokenInfo) tokeninfo).getCATokenStatus() == IHardCAToken.STATUS_OFFLINE){
129                 retval +="\n Error CA Token is disconnected, CA Name : " + cainfo.getName();
130                 log.error("Error CA Token is disconnected, CA Name : " + cainfo.getName());
131               }
132             }
133         }
134         return retval;
135     }
136     
137     private String JavaDoc checkPublishers(){
138         String JavaDoc retval = "";
139         Iterator JavaDoc iter = getPublisherSession().getAuthorizedPublisherIds(admin).iterator();
140         while(iter.hasNext()){
141             Integer JavaDoc publisherId = (Integer JavaDoc) iter.next();
142             try {
143                 getPublisherSession().testConnection(admin,publisherId.intValue());
144             } catch (PublisherConnectionException e) {
145                 String JavaDoc publishername = getPublisherSession().getPublisherName(admin,publisherId.intValue());
146                 retval +="\n Cannot connect to publisher " + publishername;
147                 log.error("Cannot connect to publisher " + publishername);
148             }
149         }
150         return retval;
151     }
152     
153     private IPublisherSessionLocal publishersession = null;
154     private IPublisherSessionLocal getPublisherSession(){
155         if(publishersession == null){
156
157             try {
158                 Context JavaDoc context = new InitialContext JavaDoc();
159                 publishersession = ((IPublisherSessionLocalHome) javax.rmi.PortableRemoteObject.narrow(context.lookup(
160                   "PublisherSessionLocal"), IPublisherSessionLocalHome.class)).create();
161             } catch (Exception JavaDoc e) {
162                 throw new EJBException JavaDoc(e);
163             }
164             
165         }
166         
167         return publishersession;
168     }
169     
170     private ICAAdminSessionLocal caadminsession = null;
171     private ICAAdminSessionLocal getCAAdminSession(){
172         if(caadminsession == null){
173
174             try {
175                 Context JavaDoc context = new InitialContext JavaDoc();
176                 caadminsession = ((ICAAdminSessionLocalHome) javax.rmi.PortableRemoteObject.narrow(context.lookup(
177                   "CAAdminSessionLocal"), ICAAdminSessionLocalHome.class)).create();
178             } catch (Exception JavaDoc e) {
179                 throw new EJBException JavaDoc(e);
180             }
181             
182         }
183         
184         return caadminsession;
185     }
186     
187     
188
189 }
190
Popular Tags