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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.servlet;
21 import java.io.IOException;
23 import javax.servlet.ServletContext;
24 import javax.servlet.UnavailableException;
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;
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).
43 public abstract class BaseHolder<T> extends AbstractLifeCycle implements Dumpable
45 private static final Logger LOG = Log.getLogger(BaseHolder.class);
48 public enum Source { EMBEDDED, JAVAX_API, DESCRIPTOR, ANNOTATION };
50 final protected Source _source;
51 protected transient Class<? extends T> _class;
52 protected String _className;
53 protected boolean _extInstance;
54 protected ServletHandler _servletHandler;
56 /* ---------------------------------------------------------------- */
57 protected BaseHolder(Source source)
62 /* ------------------------------------------------------------ */
63 public Source getSource()
68 /* ------------------------------------------------------------ */
70 * Do any setup necessary after starting
73 public void initialize()
77 throw new IllegalStateException("Not started: "+this);
80 /* ------------------------------------------------------------ */
81 @SuppressWarnings("unchecked")
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");
95 _class=Loader.loadClass(Holder.class, _className);
96 if(LOG.isDebugEnabled())
97 LOG.debug("Holding {} from {}",_class,_class.getClassLoader());
102 throw new UnavailableException(e.getMessage());
108 /* ------------------------------------------------------------ */
118 /* ------------------------------------------------------------ */
119 @ManagedAttribute(value="Class Name", readonly=true)
120 public String getClassName()
125 /* ------------------------------------------------------------ */
126 public Class<? extends T> getHeldClass()
131 /* ------------------------------------------------------------ */
133 * @return Returns the servletHandler.
135 public ServletHandler getServletHandler()
137 return _servletHandler;
141 /* ------------------------------------------------------------ */
143 * @param servletHandler The {@link ServletHandler} that will handle requests dispatched to this servlet.
145 public void setServletHandler(ServletHandler servletHandler)
147 _servletHandler = servletHandler;
150 /* ------------------------------------------------------------ */
152 * @param className The className to set.
154 public void setClassName(String className)
156 _className = className;
160 /* ------------------------------------------------------------ */
162 * @param held The class to hold
164 public void setHeldClass(Class<? extends T> held)
169 _className=held.getName();
174 /* ------------------------------------------------------------ */
175 protected void illegalStateIfContextStarted()
177 if (_servletHandler!=null)
179 ServletContext context=_servletHandler.getServletContext();
180 if ((context instanceof ContextHandler.Context) && ((ContextHandler.Context)context).getContextHandler().isStarted())
181 throw new IllegalStateException("Started");
185 /* ------------------------------------------------------------ */
187 * @return True if this holder was created for a specific instance.
189 public boolean isInstance()
195 /* ------------------------------------------------------------ */
197 public void dump(Appendable out, String indent) throws IOException
199 out.append(toString())
200 .append(" - ").append(AbstractLifeCycle.getState(this)).append("\n");
203 /* ------------------------------------------------------------ */
207 return ContainerLifeCycle.dump(this);