001: // ========================================================================
002: // Copyright 1996-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.jetty.servlet;
016:
017: import java.io.Serializable;
018: import java.util.Collections;
019: import java.util.Enumeration;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import javax.servlet.UnavailableException;
024:
025: import org.mortbay.component.AbstractLifeCycle;
026: import org.mortbay.log.Log;
027: import org.mortbay.util.Loader;
028:
029: /* --------------------------------------------------------------------- */
030: /**
031: * @author Greg Wilkins
032: */
033: public class Holder extends AbstractLifeCycle implements Serializable {
034: protected transient Class _class;
035: protected String _className;
036: protected String _displayName;
037: protected Map _initParams;
038: protected boolean _extInstance;
039:
040: /* ---------------------------------------------------------------- */
041: protected String _name;
042: protected ServletHandler _servletHandler;
043:
044: protected Holder() {
045: }
046:
047: /* ---------------------------------------------------------------- */
048: protected Holder(Class held) {
049: _class = held;
050: if (held != null) {
051: _className = held.getName();
052: _name = held.getName();
053: }
054: }
055:
056: /* ------------------------------------------------------------ */
057: public void doStart() throws Exception {
058: //if no class already loaded and no classname, make servlet permanently unavailable
059: if (_class == null
060: && (_className == null || _className.equals("")))
061: throw new UnavailableException(
062: "No class for Servlet or Filter", -1);
063:
064: //try to load class
065: if (_class == null) {
066: try {
067: _class = Loader.loadClass(Holder.class, _className);
068: if (Log.isDebugEnabled())
069: Log.debug("Holding {}", _class);
070: } catch (Exception e) {
071: throw new UnavailableException(e.getMessage(), -1);
072: }
073: }
074: }
075:
076: /* ------------------------------------------------------------ */
077: public void doStop() {
078: if (!_extInstance)
079: _class = null;
080: }
081:
082: /* ------------------------------------------------------------ */
083: public String getClassName() {
084: return _className;
085: }
086:
087: /* ------------------------------------------------------------ */
088: public Class getHeldClass() {
089: return _class;
090: }
091:
092: /* ------------------------------------------------------------ */
093: public String getDisplayName() {
094: return _displayName;
095: }
096:
097: /* ---------------------------------------------------------------- */
098: public String getInitParameter(String param) {
099: if (_initParams == null)
100: return null;
101: return (String) _initParams.get(param);
102: }
103:
104: /* ------------------------------------------------------------ */
105: public Enumeration getInitParameterNames() {
106: if (_initParams == null)
107: return Collections.enumeration(Collections.EMPTY_LIST);
108: return Collections.enumeration(_initParams.keySet());
109: }
110:
111: /* ---------------------------------------------------------------- */
112: public Map getInitParameters() {
113: return _initParams;
114: }
115:
116: /* ------------------------------------------------------------ */
117: public String getName() {
118: return _name;
119: }
120:
121: /* ------------------------------------------------------------ */
122: /**
123: * @return Returns the servletHandler.
124: */
125: public ServletHandler getServletHandler() {
126: return _servletHandler;
127: }
128:
129: /* ------------------------------------------------------------ */
130: public synchronized Object newInstance()
131: throws InstantiationException, IllegalAccessException {
132: if (_class == null)
133: throw new InstantiationException("!" + _className);
134: return _class.newInstance();
135: }
136:
137: /* ------------------------------------------------------------ */
138: /**
139: * @param className The className to set.
140: */
141: public void setClassName(String className) {
142: _className = className;
143: _class = null;
144: }
145:
146: /* ------------------------------------------------------------ */
147: /**
148: * @param className The className to set.
149: */
150: public void setHeldClass(Class held) {
151: _class = held;
152: _className = held != null ? held.getName() : null;
153: }
154:
155: /* ------------------------------------------------------------ */
156: public void setDisplayName(String name) {
157: _displayName = name;
158: }
159:
160: /* ------------------------------------------------------------ */
161: public void setInitParameter(String param, String value) {
162: if (_initParams == null)
163: _initParams = new HashMap(3);
164: _initParams.put(param, value);
165: }
166:
167: /* ---------------------------------------------------------------- */
168: public void setInitParameters(Map map) {
169: _initParams = map;
170: }
171:
172: /* ------------------------------------------------------------ */
173: /**
174: * @param name The name to set.
175: */
176: public void setName(String name) {
177: _name = name;
178: }
179:
180: /* ------------------------------------------------------------ */
181: /**
182: * @param servletHandler The {@link ServletHandler} that will handle requests dispatched to this servlet.
183: */
184: public void setServletHandler(ServletHandler servletHandler) {
185: _servletHandler = servletHandler;
186: }
187:
188: /* ------------------------------------------------------------ */
189: public String toString() {
190: return _name;
191: }
192: }
|