001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.sys.web.servlet;
017    
018    import java.io.IOException;
019    import java.io.PrintWriter;
020    import java.io.StringWriter;
021    import java.util.ArrayList;
022    
023    import javax.servlet.http.HttpServlet;
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    
027    import org.kuali.kfs.coa.businessobject.Chart;
028    import org.kuali.kfs.sys.context.SpringContext;
029    import org.kuali.rice.kns.service.BusinessObjectService;
030    
031    public class KualiHeartbeatServlet extends HttpServlet {
032        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiHeartbeatServlet.class);
033            /**
034             * 
035             */
036            private static final long serialVersionUID = 4901222949286730892L;
037    
038            public void doGet(HttpServletRequest req, HttpServletResponse resp){
039                    this.doPost(req, resp);
040            }
041            public void doPost(HttpServletRequest req, HttpServletResponse resp){
042            StringBuilder sb = new StringBuilder(200);
043            sb.append("<html><head><title>heartbeat</title></head><body>");
044                    try {
045                    ArrayList<Chart> hbResult = (ArrayList<Chart>) SpringContext.getBean(BusinessObjectService.class).findAll(Chart.class);
046                        // force a call to KIM
047                    if ( hbResult.isEmpty() ) {
048                        sb.append( "NO CHARTS RETRIEVED");
049                    } else {
050                        // we don't care what it returns, only that the call to KIM does not bomb out
051                        hbResult.get(0).getFinCoaManager();
052                        sb.append( "LUB-DUB,LUB-DUB");
053                    }
054                    } catch ( Exception ex ){
055                        sb.append( "Exception running heartbeat monitor: " + ex.getClass() + ": " + ex.getMessage() );
056                        StringWriter sw = new StringWriter(1000);
057                        PrintWriter pw = new PrintWriter( sw );
058                        ex.printStackTrace( pw );
059                        sb.append( sw.toString() );
060                        LOG.fatal( "Failed to detect heartbeat.  Apply paddles now!", ex);
061                    } finally {
062                        sb.append( "</body></html>");
063                    try {
064                            resp.getWriter().println(sb.toString());
065                            resp.addHeader("Content-Type", "text/html");
066                    } catch (IOException ex) {
067                    LOG.error( "Failure writing to heartbeat servlet output stream:", ex );
068                    }
069                    }
070            }
071    }