View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.servlet;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.ServletContext;
24  import javax.servlet.UnavailableException;
25  
26  import org.eclipse.jetty.server.handler.ContextHandler;
27  import org.eclipse.jetty.util.Loader;
28  import org.eclipse.jetty.util.annotation.ManagedAttribute;
29  import org.eclipse.jetty.util.component.AbstractLifeCycle;
30  import org.eclipse.jetty.util.component.ContainerLifeCycle;
31  import org.eclipse.jetty.util.component.Dumpable;
32  import org.eclipse.jetty.util.log.Log;
33  import org.eclipse.jetty.util.log.Logger;
34  
35  /**
36   * AbstractHolder
37   * 
38   * Base class for all servlet-related classes that may be lazily instantiated  (eg servlet, filter, 
39   * listener), and/or require metadata to be held regarding their origin 
40   * (web.xml, annotation, programmatic api etc).
41   * @param <T> the type of holder
42   */
43  public abstract class BaseHolder<T> extends AbstractLifeCycle implements Dumpable
44  {
45      private static final Logger LOG = Log.getLogger(BaseHolder.class);
46      
47      
48      public enum Source { EMBEDDED, JAVAX_API, DESCRIPTOR, ANNOTATION };
49      
50      final protected Source _source;
51      protected transient Class<? extends T> _class;
52      protected String _className;
53      protected boolean _extInstance;
54      protected ServletHandler _servletHandler;
55      
56      /* ---------------------------------------------------------------- */
57      protected BaseHolder(Source source)
58      {
59          _source=source;
60      }
61  
62      /* ------------------------------------------------------------ */
63      public Source getSource()
64      {
65          return _source;
66      }
67      
68      /* ------------------------------------------------------------ */
69      /**
70       * Do any setup necessary after starting
71       * @throws Exception if unable to initialize
72       */
73      public void initialize()
74      throws Exception
75      {
76          if (!isStarted())
77              throw new IllegalStateException("Not started: "+this);
78      }
79  
80      /* ------------------------------------------------------------ */
81      @SuppressWarnings("unchecked")
82      @Override
83      public void doStart()
84          throws Exception
85      {
86          //if no class already loaded and no classname, make permanently unavailable
87          if (_class==null && (_className==null || _className.equals("")))
88              throw new UnavailableException("No class in holder");
89          
90          //try to load class
91          if (_class==null)
92          {
93              try
94              {
95                  _class=Loader.loadClass(Holder.class, _className);
96                  if(LOG.isDebugEnabled())
97                      LOG.debug("Holding {} from {}",_class,_class.getClassLoader());
98              }
99              catch (Exception e)
100             {
101                 LOG.warn(e);
102                 throw new UnavailableException(e.getMessage());
103             }
104         }
105     }
106     
107     
108     /* ------------------------------------------------------------ */
109     @Override
110     public void doStop()
111         throws Exception
112     {
113         if (!_extInstance)
114             _class=null;
115     }
116 
117 
118     /* ------------------------------------------------------------ */
119     @ManagedAttribute(value="Class Name", readonly=true)
120     public String getClassName()
121     {
122         return _className;
123     }
124 
125     /* ------------------------------------------------------------ */
126     public Class<? extends T> getHeldClass()
127     {
128         return _class;
129     }
130 
131     /* ------------------------------------------------------------ */
132     /**
133      * @return Returns the servletHandler.
134      */
135     public ServletHandler getServletHandler()
136     {
137         return _servletHandler;
138     }
139     
140 
141     /* ------------------------------------------------------------ */
142     /**
143      * @param servletHandler The {@link ServletHandler} that will handle requests dispatched to this servlet.
144      */
145     public void setServletHandler(ServletHandler servletHandler)
146     {
147         _servletHandler = servletHandler;
148     }
149     
150     /* ------------------------------------------------------------ */
151     /**
152      * @param className The className to set.
153      */
154     public void setClassName(String className)
155     {
156         _className = className;
157         _class=null;
158     }
159 
160     /* ------------------------------------------------------------ */
161     /**
162      * @param held The class to hold
163      */
164     public void setHeldClass(Class<? extends T> held)
165     {
166         _class=held;
167         if (held!=null)
168         {
169             _className=held.getName();
170         }
171     }
172     
173 
174     /* ------------------------------------------------------------ */
175     protected void illegalStateIfContextStarted()
176     {
177         if (_servletHandler!=null)
178         {
179             ServletContext context=_servletHandler.getServletContext();
180             if ((context instanceof ContextHandler.Context) && ((ContextHandler.Context)context).getContextHandler().isStarted())
181                 throw new IllegalStateException("Started");
182         }
183     }
184     
185     /* ------------------------------------------------------------ */
186     /**
187      * @return True if this holder was created for a specific instance.
188      */
189     public boolean isInstance()
190     {
191         return _extInstance;
192     }
193     
194     
195     /* ------------------------------------------------------------ */
196     @Override
197     public void dump(Appendable out, String indent) throws IOException
198     {
199         out.append(toString())
200         .append(" - ").append(AbstractLifeCycle.getState(this)).append("\n");
201     }
202 
203     /* ------------------------------------------------------------ */
204     @Override
205     public String dump()
206     {
207         return ContainerLifeCycle.dump(this);
208     }
209 }