1 13 14 package org.ejbca.ui.web.pub.cluster; 15 16 import java.sql.Connection ; 17 import java.sql.Statement ; 18 import java.util.Iterator ; 19 20 import javax.ejb.EJBException ; 21 import javax.naming.Context ; 22 import javax.naming.InitialContext ; 23 import javax.servlet.ServletConfig ; 24 import javax.servlet.http.HttpServletRequest ; 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 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 checkDBString = null; 65 private boolean checkPublishers = false; 66 67 public void init(ServletConfig 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 checkHealth(HttpServletRequest request) { 77 log.debug("Starting HealthCheck health check requested by : " + request.getRemoteAddr()); 78 String 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 errormessage = null; 93 } 94 95 return errormessage; 96 } 97 98 private String checkMemory(){ 99 String 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 checkDB(){ 108 String retval = ""; 109 try{ 110 Connection con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE); 111 Statement statement = con.createStatement(); 112 statement.execute(checkDBString); 113 JDBCUtil.close(con); 114 }catch(Exception 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 checkCAs(){ 122 String retval = ""; 123 Iterator iter = getCAAdminSession().getAvailableCAs(admin).iterator(); 124 while(iter.hasNext()){ 125 CAInfo cainfo = getCAAdminSession().getCAInfo(admin,((Integer ) 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 checkPublishers(){ 138 String retval = ""; 139 Iterator iter = getPublisherSession().getAuthorizedPublisherIds(admin).iterator(); 140 while(iter.hasNext()){ 141 Integer publisherId = (Integer ) iter.next(); 142 try { 143 getPublisherSession().testConnection(admin,publisherId.intValue()); 144 } catch (PublisherConnectionException e) { 145 String 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 context = new InitialContext (); 159 publishersession = ((IPublisherSessionLocalHome) javax.rmi.PortableRemoteObject.narrow(context.lookup( 160 "PublisherSessionLocal"), IPublisherSessionLocalHome.class)).create(); 161 } catch (Exception e) { 162 throw new EJBException (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 context = new InitialContext (); 176 caadminsession = ((ICAAdminSessionLocalHome) javax.rmi.PortableRemoteObject.narrow(context.lookup( 177 "CAAdminSessionLocal"), ICAAdminSessionLocalHome.class)).create(); 178 } catch (Exception e) { 179 throw new EJBException (e); 180 } 181 182 } 183 184 return caadminsession; 185 } 186 187 188 189 } 190 | Popular Tags |