Coverage Report - org.webmacro.servlet.ServletBroker
 
Classes in this File Line Coverage Branch Coverage Complexity
ServletBroker
53%
15/28
33%
4/12
3
ServletBroker$PropertiesPair
0%
0/13
0%
0/8
3
 
 1  
 /*
 2  
  * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
 3  
  *
 4  
  * Redistribution and use in source and binary forms, with or without
 5  
  * modification, are permitted under the terms of either of the following
 6  
  * Open Source licenses:
 7  
  *
 8  
  * The GNU General Public License, version 2, or any later version, as
 9  
  * published by the Free Software Foundation
 10  
  * (http://www.fsf.org/copyleft/gpl.html);
 11  
  *
 12  
  *  or
 13  
  *
 14  
  * The Semiotek Public License (http://webmacro.org/LICENSE.)
 15  
  *
 16  
  * This software is provided "as is", with NO WARRANTY, not even the
 17  
  * implied warranties of fitness to purpose, or merchantability. You
 18  
  * assume all risks and liabilities associated with its use.
 19  
  *
 20  
  * See www.webmacro.org for more information on the WebMacro project.
 21  
  */
 22  
 
 23  
 
 24  
 /**
 25  
  * Base class for servlet brokers.
 26  
  * @see Servlet20Broker
 27  
  * @see Servlet22Broker
 28  
  * @see Broker
 29  
  * @author Brian Goetz
 30  
  * @since 0.96
 31  
  */
 32  
 package org.webmacro.servlet;
 33  
 
 34  
 import org.slf4j.Logger;
 35  
 import org.slf4j.LoggerFactory;
 36  
 import org.webmacro.Broker;
 37  
 import org.webmacro.InitException;
 38  
 
 39  
 import javax.servlet.Servlet;
 40  
 import javax.servlet.ServletContext;
 41  
 import java.util.Map;
 42  
 import java.util.Properties;
 43  
 import java.util.WeakHashMap;
 44  
 
 45  
 /**
 46  
  * The base broker for Servlets and web applications. getBroker will
 47  
  * instantiate the correct ServletXXBroker for the version of the servlet
 48  
  * API that is available.
 49  
  *
 50  
  * @see #getBroker
 51  
  * @since Dawn of time
 52  
  */
 53  
 abstract public class ServletBroker extends Broker
 54  
 {
 55  
 
 56  
     protected ServletContext _servletContext;
 57  4
     protected Logger _log =  LoggerFactory.getLogger(ServletBroker.class);
 58  
     
 59  
 
 60  
     /**
 61  
      * Tracks ServletContexts we have been instantiated for, to prevent
 62  
      * duplicate log targets where multiple ServletBroker instances are
 63  
      * created. We use WeakHashMap instead of Set because it does the
 64  
      * key polled removal for us.
 65  
      */
 66  2
     private static Map servletContextsWithLogTargets = new WeakHashMap();
 67  
 
 68  
     protected ServletBroker (ServletContext sc) throws InitException
 69  
     {
 70  4
         super((Broker) null, sc.toString());
 71  4
         _servletContext = sc;
 72  4
     }
 73  
 
 74  
 
 75  
     public static Broker getBroker (Servlet s, Properties additionalProperties) 
 76  
         throws InitException
 77  
     {
 78  
         int minorVersion, majorVersion;
 79  
 
 80  32
         ServletContext sc = s.getServletConfig().getServletContext();
 81  
         try
 82  
         {
 83  32
             majorVersion = sc.getMajorVersion();
 84  32
             minorVersion = sc.getMinorVersion();
 85  
         }
 86  0
         catch (NoSuchMethodError e)
 87  
         {
 88  0
             majorVersion = 2;
 89  0
             minorVersion = 0;
 90  32
         }
 91  
 
 92  
         Broker b;
 93  32
         if (majorVersion > 2
 94  
                 || (majorVersion == 2 && minorVersion >= 2))
 95  22
             b = Servlet22Broker.getBroker(s, additionalProperties);
 96  
         else
 97  10
             b = Servlet20Broker.getBroker(s, additionalProperties);
 98  32
         return b;
 99  
     }
 100  
 
 101  
     /**
 102  
      * Get a Broker according to Servlet API version, when no Servlet reference
 103  
      * is available.
 104  
      * @param sc The ServletContext of the web application
 105  
      * @param cl A class loader, hopefully that of the web application, to
 106  
      * use when loading resources or classes.
 107  
      * @param additionalProperties
 108  
      * @return The broker for the servlet context.
 109  
      * @throws InitException
 110  
      * @since 2.1 JSDK
 111  
      */
 112  
     public static Broker getBroker (ServletContext sc, ClassLoader cl,
 113  
         Properties additionalProperties) throws InitException
 114  
     {
 115  
         int minorVersion, majorVersion;
 116  
 
 117  
         try
 118  
         {
 119  0
             majorVersion = sc.getMajorVersion();
 120  0
             minorVersion = sc.getMinorVersion();
 121  
         }
 122  0
         catch (NoSuchMethodError e)
 123  
         {
 124  0
             majorVersion = 2;
 125  0
             minorVersion = 0;
 126  0
         }
 127  
 
 128  
         Broker b;
 129  0
         if (majorVersion > 2
 130  
                 || (majorVersion == 2 && minorVersion >= 2))
 131  0
             b = Servlet22Broker.getBroker(sc, cl, additionalProperties);
 132  
         else
 133  0
             b = Servlet20Broker.getBroker(sc, cl, additionalProperties);
 134  0
         return b;
 135  
     }
 136  
 
 137  
     public static Broker getBroker (Servlet s) throws InitException
 138  
     {
 139  32
         return getBroker(s, null);
 140  
     }
 141  
 
 142  
     public ServletContext getServletContext ()
 143  
     {
 144  2
         return _servletContext;
 145  
     }
 146  
 
 147  
     protected static final class PropertiesPair
 148  
     {
 149  
         private final Object obj;
 150  
         private final Properties p;
 151  
 
 152  
         public PropertiesPair (Object s, Properties p)
 153  0
         {
 154  0
             this.obj = s;
 155  0
             this.p = p;
 156  0
         }
 157  
 
 158  
         public boolean equals (Object o)
 159  
         {
 160  0
             if (this == o) return true;
 161  0
             if (!(o instanceof PropertiesPair)) return false;
 162  
 
 163  0
             final PropertiesPair servletPropertiesPair = (PropertiesPair) o;
 164  
 
 165  0
             if (!p.equals(servletPropertiesPair.p)) return false;
 166  0
             if (!obj.equals(servletPropertiesPair.obj)) return false;
 167  
 
 168  0
             return true;
 169  
         }
 170  
 
 171  
         public int hashCode ()
 172  
         {
 173  
             int result;
 174  0
             result = obj.hashCode();
 175  0
             result = 29 * result + p.hashCode();
 176  0
             return result;
 177  
         }
 178  
     }
 179  
 }