Coverage report

  %line %branch
org.apache.jcs.auxiliary.remote.server.RemoteCacheStartupServlet
0% 
0% 

 1  
 package org.apache.jcs.auxiliary.remote.server;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.IOException;
 23  
 import java.io.OutputStream;
 24  
 import java.net.InetAddress;
 25  
 import java.net.UnknownHostException;
 26  
 import java.rmi.RemoteException;
 27  
 import java.rmi.registry.LocateRegistry;
 28  
 import java.util.Properties;
 29  
 
 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 org.apache.commons.logging.Log;
 36  
 import org.apache.commons.logging.LogFactory;
 37  
 import org.apache.jcs.engine.control.CompositeCacheManager;
 38  
 import org.apache.jcs.utils.props.PropertyLoader;
 39  
 
 40  
 /**
 41  
  * This servlet can be used to startup the JCS remote cache. It is easy to
 42  
  * deploy the remote server in a tomcat base. This give you an easy way to
 43  
  * monitor its activity.
 44  
  * <p>
 45  
  *
 46  
  * <code>
 47  
  *  <servlet>
 48  
         <servlet-name>JCSRemoteCacheStartupServlet</servlet-name>
 49  
         <servlet-class>
 50  
              org.apache.jcs.auxiliary.remote.server.RemoteCacheStartupServlet
 51  
         </servlet-class>
 52  
         <load-on-startup>1</load-on-startup>
 53  
     </servlet>
 54  
 
 55  
 
 56  
     <servlet-mapping>
 57  
         <servlet-name>JCSRemoteCacheStartupServlet</servlet-name>
 58  
         <url-pattern>/jcs</url-pattern>
 59  
     </servlet-mapping>
 60  
  * </code>
 61  
  *
 62  
  *
 63  
  * @author Aaron Smuts
 64  
  *
 65  
  */
 66  0
 public class RemoteCacheStartupServlet
 67  
     extends HttpServlet
 68  
 {
 69  
     private static final long serialVersionUID = 1L;
 70  
 
 71  0
     private final static Log log = LogFactory.getLog( RemoteCacheStartupServlet.class );
 72  
 
 73  
     private static final int DEFAULT_REGISTRY_PORT = 1101;
 74  
 
 75  
     private static final String DEFAULT_PROPS_FILE_NAME = "cache.ccf";
 76  
 
 77  
     /**
 78  
      * Starts the registry and then tries to bind to it.
 79  
      * <p>
 80  
      * Gets the port from a props file. Uses the local host name for the rgistry
 81  
      * host. Tries to start the registry, ignoreing failure. Starts the server.
 82  
      */
 83  
     public void init()
 84  
         throws ServletException
 85  
     {
 86  0
         super.init();
 87  
         // TODO load from props file or get as init param or get from jndi, or
 88  
         // all three
 89  0
         int registryPort = DEFAULT_REGISTRY_PORT;
 90  
 
 91  
         try
 92  
         {
 93  0
             Properties props = PropertyLoader.loadProperties( DEFAULT_PROPS_FILE_NAME );
 94  0
             if ( props != null )
 95  
             {
 96  0
                 String portS = props.getProperty( "registry.port", String.valueOf( DEFAULT_REGISTRY_PORT ) );
 97  
 
 98  
                 try
 99  
                 {
 100  0
                     registryPort = Integer.parseInt( portS );
 101  
                 }
 102  0
                 catch ( NumberFormatException e )
 103  
                 {
 104  0
                     log.error( "Problem converting port to an int.", e );
 105  0
                 }
 106  
             }
 107  
         }
 108  0
         catch ( Exception e )
 109  
         {
 110  0
             log.error( "Problem loading props.", e );
 111  
         }
 112  0
         catch ( Throwable t )
 113  
         {
 114  0
             log.error( "Problem loading props.", t );
 115  0
         }
 116  
 
 117  
         // we will always use the local machine for the registry
 118  
         String registryHost;
 119  
         try
 120  
         {
 121  0
             registryHost = InetAddress.getLocalHost().getHostAddress();
 122  
 
 123  0
             if ( log.isDebugEnabled() )
 124  
             {
 125  0
                 log.debug( "registryHost = [" + registryHost + "]" );
 126  
             }
 127  
 
 128  0
             if ( "localhost".equals( registryHost ) || "127.0.0.1".equals( registryHost ) )
 129  
             {
 130  0
                 log.warn( "The local address [" + registryHost
 131  
                     + "] is INVALID.  Other machines must be able to use the address to reach this server." );
 132  
             }
 133  
 
 134  
             try
 135  
             {
 136  0
                 LocateRegistry.createRegistry( registryPort );
 137  
             }
 138  0
             catch ( RemoteException e )
 139  
             {
 140  0
                 log.error( "Problem creating registry.  It may already be started. " + e.getMessage() );
 141  
             }
 142  0
             catch ( Throwable t )
 143  
             {
 144  0
                 log.error( "Problem creating registry.", t );
 145  0
             }
 146  
 
 147  
             try
 148  
             {
 149  0
                 RemoteCacheServerFactory.startup( registryHost, registryPort, "/" + DEFAULT_PROPS_FILE_NAME );
 150  
             }
 151  0
             catch ( IOException e )
 152  
             {
 153  0
                 log.error( "Problem starting remote cache server.", e );
 154  
             }
 155  
 
 156  0
             catch ( Throwable t )
 157  
             {
 158  0
                 log.error( "Problem starting remote cache server.", t );
 159  0
             }
 160  
         }
 161  0
         catch ( UnknownHostException e )
 162  
         {
 163  0
             log.error( "Could not get local address to use for the registry!", e );
 164  0
         }
 165  0
     }
 166  
 
 167  
     /**
 168  
      * It just dumps the stats.
 169  
      */
 170  
     protected void service( HttpServletRequest request, HttpServletResponse response )
 171  
         throws ServletException, IOException
 172  
     {
 173  
 
 174  0
         String stats = CompositeCacheManager.getInstance().getStats();
 175  0
         if ( log.isInfoEnabled() )
 176  
         {
 177  0
             log.info( stats );
 178  
         }
 179  
 
 180  
         try
 181  
         {
 182  0
             OutputStream os = response.getOutputStream();
 183  0
             os.write( stats.getBytes() );
 184  0
             os.close();
 185  
         }
 186  0
         catch ( IOException e )
 187  
         {
 188  0
             log.error( "Problem writing response.", e );
 189  0
         }
 190  0
     }
 191  
 
 192  
     /*
 193  
      * (non-Javadoc)
 194  
      *
 195  
      * @see javax.servlet.Servlet#destroy()
 196  
      */
 197  
     public void destroy()
 198  
     {
 199  0
         super.destroy();
 200  
 
 201  0
         log.info( "Shutting down remote cache " );
 202  
 
 203  0
         CompositeCacheManager.getInstance().shutDown();
 204  0
     }
 205  
 }

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