KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > jetty > servlet > Holder


1 // ========================================================================
2
// $Id: Holder.java,v 1.18 2005/08/13 00:01:27 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.jetty.servlet;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.AbstractMap JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.mortbay.log.LogFactory;
28 import org.mortbay.http.HttpContext;
29 import org.mortbay.http.HttpHandler;
30 import org.mortbay.util.LifeCycle;
31
32
33 /* --------------------------------------------------------------------- */
34 /**
35  * @version $Id: Holder.java,v 1.18 2005/08/13 00:01:27 gregwilkins Exp $
36  * @author Greg Wilkins
37  */

38 public class Holder
39     extends AbstractMap JavaDoc
40     implements LifeCycle,
41                Serializable JavaDoc
42 {
43     private static Log log = LogFactory.getLog(Holder.class);
44
45     /* ---------------------------------------------------------------- */
46     protected HttpHandler _httpHandler;
47     protected String JavaDoc _name;
48     protected String JavaDoc _displayName;
49     protected String JavaDoc _className;
50     protected Map JavaDoc _initParams;
51     
52     protected transient Class JavaDoc _class;
53
54     /* ---------------------------------------------------------------- */
55     /** Constructor for Serialization.
56      */

57     protected Holder()
58     {}
59     
60     /* ---------------------------------------------------------------- */
61     protected Holder(HttpHandler httpHandler,
62                      String JavaDoc name,
63                      String JavaDoc className)
64     {
65         if (name==null || name.length()==0)
66             throw new IllegalArgumentException JavaDoc("No name for "+className);
67         
68         if (className==null || className.length()==0)
69             throw new IllegalArgumentException JavaDoc("No classname");
70         
71         _httpHandler=httpHandler;
72         _className=className;
73         _name=name;
74         _displayName=name;
75     }
76
77     
78     /* ------------------------------------------------------------ */
79     public String JavaDoc getName()
80     {
81         return _name;
82     }
83     
84     /* ------------------------------------------------------------ */
85     public void setDisplayName(String JavaDoc name)
86     {
87         _name=name;
88     }
89     
90     /* ------------------------------------------------------------ */
91     public String JavaDoc getDisplayName()
92     {
93         return _name;
94     }
95     
96     /* ------------------------------------------------------------ */
97     public String JavaDoc getClassName()
98     {
99         return _className;
100     }
101     
102     /* ------------------------------------------------------------ */
103     public HttpHandler getHttpHandler()
104     {
105         return _httpHandler;
106     }
107
108     /* ------------------------------------------------------------ */
109     public HttpContext getHttpContext()
110     {
111         if (_httpHandler!=null)
112             return _httpHandler.getHttpContext();
113         return null;
114     }
115     
116     /* ------------------------------------------------------------ */
117     public void setInitParameter(String JavaDoc param,String JavaDoc value)
118     {
119         put(param,value);
120     }
121
122     /* ---------------------------------------------------------------- */
123     public String JavaDoc getInitParameter(String JavaDoc param)
124     {
125         if (_initParams==null)
126             return null;
127         return (String JavaDoc)_initParams.get(param);
128     }
129
130     /* ---------------------------------------------------------------- */
131     public Map JavaDoc getInitParameters()
132     {
133         return _initParams;
134     }
135     
136     /* ------------------------------------------------------------ */
137     public Enumeration JavaDoc getInitParameterNames()
138     {
139         if (_initParams==null)
140             return Collections.enumeration(Collections.EMPTY_LIST);
141         return Collections.enumeration(_initParams.keySet());
142     }
143
144     /* ------------------------------------------------------------ */
145     /** Map entrySet method.
146      * FilterHolder implements the Map interface as a
147      * configuration conveniance. The methods are mapped to the
148      * filter properties.
149      * @return The entrySet of the initParameter map
150      */

151     public synchronized Set JavaDoc entrySet()
152     {
153         if (_initParams==null)
154             _initParams=new HashMap JavaDoc(3);
155         return _initParams.entrySet();
156     }
157
158     /* ------------------------------------------------------------ */
159     /** Map put method.
160      * FilterHolder implements the Map interface as a
161      * configuration conveniance. The methods are mapped to the
162      * filter properties.
163      */

164     public synchronized Object JavaDoc put(Object JavaDoc name,Object JavaDoc value)
165     {
166         if (_initParams==null)
167             _initParams=new HashMap JavaDoc(3);
168         return _initParams.put(name,value);
169     }
170
171     /* ------------------------------------------------------------ */
172     /** Map get method.
173      * FilterHolder implements the Map interface as a
174      * configuration conveniance. The methods are mapped to the
175      * filter properties.
176      */

177     public synchronized Object JavaDoc get(Object JavaDoc name)
178     {
179         if (_initParams==null)
180             return null;
181         return _initParams.get(name);
182     }
183
184     /* ------------------------------------------------------------ */
185     public void start()
186         throws Exception JavaDoc
187     {
188         _class=_httpHandler.getHttpContext().loadClass(_className);
189         if(log.isDebugEnabled())log.debug("Started holder of "+_class);
190     }
191     
192     /* ------------------------------------------------------------ */
193     public synchronized Object JavaDoc newInstance()
194         throws InstantiationException JavaDoc,
195                IllegalAccessException JavaDoc
196     {
197         if (_class==null)
198             throw new InstantiationException JavaDoc("No class for "+this);
199         return _class.newInstance();
200     }
201
202     /* ------------------------------------------------------------ */
203     public boolean isStarted()
204     {
205         return _class!=null;
206     }
207     
208     /* ------------------------------------------------------------ */
209     public void stop()
210     {
211         _class=null;
212     }
213     
214     /* ------------------------------------------------------------ */
215     public String JavaDoc toString()
216     {
217         return _name;
218     }
219     
220 }
221
222
223
224
225
226
Popular Tags