Coverage report

  %line %branch
net.sf.infrared.web.InfraRedListenerServlet$1
0% 
0% 

 1  
 /*
 2  
  *
 3  
  * Copyright 2005 Tavant Technologies and Contributors
 4  
  * 
 5  
  * Licensed under the Apache License, Version 2.0 (the "License")
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  *
 17  
  *
 18  
  *
 19  
  * Original Author:  kamal.govindraj (Tavant Technologies)
 20  
  * Contributor(s):   prashant.nair;
 21  
  *
 22  
  */
 23  
 package net.sf.infrared.web;
 24  
 
 25  
 import java.io.IOException;
 26  
 import java.net.BindException;
 27  
 
 28  
 import javax.servlet.ServletConfig;
 29  
 import javax.servlet.ServletContext;
 30  
 import javax.servlet.ServletException;
 31  
 import javax.servlet.http.HttpServlet;
 32  
 import javax.servlet.http.HttpServletRequest;
 33  
 import javax.servlet.http.HttpServletResponse;
 34  
 
 35  
 import net.sf.infrared.base.util.LoggingFactory;
 36  
 import net.sf.infrared.collector.Collector;
 37  
 import net.sf.infrared.collector.CollectorConfig;
 38  
 import net.sf.infrared.collector.impl.CollectorImpl;
 39  
 import net.sf.infrared.web.util.DataFetchUtil;
 40  
 import net.sf.infrared.web.util.WebConfig;
 41  
 
 42  
 import org.apache.log4j.Logger;
 43  
 
 44  
 /**
 45  
  *
 46  
  * @kamal.govindraj
 47  
  * @author prashant.nair
 48  
  */
 49  
 public class InfraRedListenerServlet extends HttpServlet {
 50  
     public static final String PORT_KEY = "port";
 51  
 
 52  
     public static final int DEFAULT_PORT = 9001;
 53  
 
 54  
     private static final Logger logger = 
 55  
                             LoggingFactory.getLogger(InfraRedListenerServlet.class.getName());
 56  
 
 57  
     private int port = DEFAULT_PORT;
 58  
     
 59  
     private Collector collector;
 60  
     
 61  
     public void init(ServletConfig config) throws ServletException {
 62  
         super.init(config);
 63  
         initCollector(config);
 64  
     }
 65  
 
 66  
     /**
 67  
      * Shuts down the listener. Called by the container when the application is 
 68  
      * stopped/undeployed
 69  
      */
 70  
     public void destroy() {
 71  
         if (collector != null) {
 72  
             collector.shutdown();
 73  
         }
 74  
     }
 75  
 
 76  
     /**
 77  
      * Produces an HTML page which lists the agents which are currently connected to the
 78  
      * central collector. This can be used as a deployment aid.
 79  
      */
 80  
     public void doGet(HttpServletRequest req, HttpServletResponse res) 
 81  
                                 throws ServletException,IOException {
 82  
         //@TODO provide an implementation that lists the agent that are 
 83  
         // connected to it.
 84  
     }
 85  
 
 86  
     void initCollector(ServletConfig config) throws ServletException {
 87  
         // The value of the port is read from the servlet config 
 88  
         // which is set in web.xml
 89  
         String portVal = config.getInitParameter(PORT_KEY);
 90  
         try {
 91  
             if (portVal != null) {
 92  
                 port = Integer.parseInt(portVal);
 93  
             }
 94  
         }
 95  
         catch (NumberFormatException numex) {
 96  
             String msg = "The value of 'port' init parameter, '" + portVal
 97  
                     + "', is not valid one. " + "Legal values are non-zero positive integers";
 98  
             logger.error(msg, numex);
 99  
             throw new ServletException(msg, numex); // stop the deployment
 100  
         }
 101  
         if (port <= 0) {
 102  
             String msg = "The value of 'port' init parameter, '" + portVal
 103  
                     + "', is not valid one. " + "Legal values are non-zero positive integers";
 104  
             logger.error(msg);
 105  
             throw new ServletException(msg);
 106  
         }
 107  
         try {
 108  
             startListening();
 109  
             if (logger.isDebugEnabled()) {
 110  
                 logger.debug("AgentListener thread spawned to listen on port " + port);
 111  
             }
 112  
         }
 113  
         catch (BindException bindex) {
 114  
             String msg = "Error while attempting to start listening; port specified is " + port
 115  
                     + ". " + "Check if this port is in use by another application";
 116  
             logger.error(msg, bindex);
 117  
             throw new ServletException(msg, bindex);
 118  
         }
 119  
         catch (IOException e) {
 120  
             String msg = "Error while attempting to start listening";
 121  
             logger.error(msg, e);
 122  
             throw new ServletException(msg, e);
 123  
         }
 124  
 
 125  
     }
 126  
 
 127  
     void startListening() throws IOException {
 128  
         Collector collector = new CollectorImpl();
 129  
         collector.start(new CollectorConfig(){
 130  
             public int getAgentListenPort(){
 131  0
                 return port;
 132  
             }            
 133  0
             public long getPersistInterval(){
 134  0
                 return WebConfig.getPersistInterval();
 135  
             }
 136  
         });
 137  
         DataFetchUtil.setCollector(collector);
 138  
     }
 139  
 
 140  
     int getPort() {
 141  
         return this.port;
 142  
     }
 143  
 
 144  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.