View Javadoc

    1   // ========================================================================
    2   // Copyright 1996-2005 Mort Bay Consulting Pty. Ltd.
    3   // ------------------------------------------------------------------------
    4   // Licensed under the Apache License, Version 2.0 (the "License");
    5   // you may not use this file except in compliance with the License.
    6   // You may obtain a copy of the License at
    7   // http://www.apache.org/licenses/LICENSE-2.0
    8   // Unless required by applicable law or agreed to in writing, software
    9   // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  // ========================================================================
    14
    15  package org.mortbay.jetty.servlet;
    16
    17  import java.io.Serializable;
    18  import java.util.Collections;
    19  import java.util.Enumeration;
    20  import java.util.HashMap;
    21  import java.util.Map;
    22
    23  import javax.servlet.UnavailableException;
    24
    25  import org.mortbay.component.AbstractLifeCycle;
    26  import org.mortbay.log.Log;
    27  import org.mortbay.util.Loader;
    28
    29
    30  /* --------------------------------------------------------------------- */
    31  /**
    32   * @author Greg Wilkins
    33   */
    34  public class Holder extends AbstractLifeCycle implements Serializable
    35  {
    36      protected transient Class _class;
    37      protected String _className;
    38      protected String _displayName;
    39      protected Map _initParams;
    40      protected boolean _extInstance;
    41
    42      /* ---------------------------------------------------------------- */
    43      protected String _name;
    44      protected ServletHandler _servletHandler;
    45
    46      protected Holder()
    47      {}
    48
    49      /* ---------------------------------------------------------------- */
    50      protected Holder(Class held)
    51      {
    52          _class=held;
    53          if (held!=null)
    54          {
    55              _className=held.getName();
    56              _name=held.getName()+"-"+this.hashCode();
    57          }
    58      }
    59
    60      /* ------------------------------------------------------------ */
    61      public void doStart()
    62          throws Exception
    63      {
    64          //if no class already loaded and no classname, make servlet permanently unavailable
    65          if (_class==null && (_className==null || _className.equals("")))
    66              throw new UnavailableException("No class for Servlet or Filter", -1);
    67
    68          //try to load class
    69          if (_class==null)
    70          {
    71              try
    72              {
    73                  _class=Loader.loadClass(Holder.class, _className);
    74                  if(Log.isDebugEnabled())Log.debug("Holding {}",_class);
    75              }
    76              catch (Exception e)
    77              {
    78                  Log.warn(e);
    79                  throw new UnavailableException(e.getMessage(), -1);
    80              }
    81          }
    82      }
    83
    84      /* ------------------------------------------------------------ */
    85      public void doStop()
    86      {
    87          if (!_extInstance)
    88              _class=null;
    89      }
    90
    91      /* ------------------------------------------------------------ */
    92      public String getClassName()
    93      {
    94          return _className;
    95      }
    96
    97      /* ------------------------------------------------------------ */
    98      public Class getHeldClass()
    99      {
    100         return _class;
    101     }
    102
    103     /* ------------------------------------------------------------ */
    104     public String getDisplayName()
    105     {
    106         return _displayName;
    107     }
    108
    109     /* ---------------------------------------------------------------- */
    110     public String getInitParameter(String param)
    111     {
    112         if (_initParams==null)
    113             return null;
    114         return (String)_initParams.get(param);
    115     }
    116
    117     /* ------------------------------------------------------------ */
    118     public Enumeration getInitParameterNames()
    119     {
    120         if (_initParams==null)
    121             return Collections.enumeration(Collections.EMPTY_LIST);
    122         return Collections.enumeration(_initParams.keySet());
    123     }
    124
    125     /* ---------------------------------------------------------------- */
    126     public Map getInitParameters()
    127     {
    128         return _initParams;
    129     }
    130
    131     /* ------------------------------------------------------------ */
    132     public String getName()
    133     {
    134         return _name;
    135     }
    136
    137     /* ------------------------------------------------------------ */
    138     /**
    139      * @return Returns the servletHandler.
    140      */
    141     public ServletHandler getServletHandler()
    142     {
    143         return _servletHandler;
    144     }
    145
    146     /* ------------------------------------------------------------ */
    147     public synchronized Object newInstance()
    148         throws InstantiationException,
    149                IllegalAccessException
    150     {
    151         if (_class==null)
    152             throw new InstantiationException("!"+_className);
    153         return _class.newInstance();
    154     }
    155
    156     public void destroyInstance(Object instance)
    157     throws Exception
    158     {
    159     }
    160
    161     /* ------------------------------------------------------------ */
    162     /**
    163      * @param className The className to set.
    164      */
    165     public void setClassName(String className)
    166     {
    167         _className = className;
    168         _class=null;
    169     }
    170
    171     /* ------------------------------------------------------------ */
    172     /**
    173      * @param className The className to set.
    174      */
    175     public void setHeldClass(Class held)
    176     {
    177         _class=held;
    178         _className = held!=null?held.getName():null;
    179     }
    180
    181     /* ------------------------------------------------------------ */
    182     public void setDisplayName(String name)
    183     {
    184         _displayName=name;
    185     }
    186
    187     /* ------------------------------------------------------------ */
    188     public void setInitParameter(String param,String value)
    189     {
    190         if (_initParams==null)
    191             _initParams=new HashMap(3);
    192         _initParams.put(param,value);
    193     }
    194
    195     /* ---------------------------------------------------------------- */
    196     public void setInitParameters(Map map)
    197     {
    198         _initParams=map;
    199     }
    200
    201     /* ------------------------------------------------------------ */
    202     /**
    203      * The name is a primary key for the held object.
    204      * Ensure that the name is set BEFORE adding a Holder
    205      * (eg ServletHolder or FilterHolder) to a ServletHandler.
    206      * @param name The name to set.
    207      */
    208     public void setName(String name)
    209     {
    210         _name = name;
    211     }
    212
    213     /* ------------------------------------------------------------ */
    214     /**
    215      * @param servletHandler The {@link ServletHandler} that will handle requests dispatched to this servlet.
    216      */
    217     public void setServletHandler(ServletHandler servletHandler)
    218     {
    219         _servletHandler = servletHandler;
    220     }
    221
    222     /* ------------------------------------------------------------ */
    223     public String toString()
    224     {
    225         return _name;
    226     }
    227 }
    228
    229
    230
    231
    232