1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
37
38
39
40
41
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
71
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
87 if (_class==null && (_className==null || _className.equals("")))
88 throw new UnavailableException("No class in holder");
89
90
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
134
135 public ServletHandler getServletHandler()
136 {
137 return _servletHandler;
138 }
139
140
141
142
143
144
145 public void setServletHandler(ServletHandler servletHandler)
146 {
147 _servletHandler = servletHandler;
148 }
149
150
151
152
153
154 public void setClassName(String className)
155 {
156 _className = className;
157 _class=null;
158 }
159
160
161
162
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
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 }