KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > valves > ValveBase


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.valves;
20
21
22 import java.io.IOException JavaDoc;
23
24 import javax.management.MBeanRegistration JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.MalformedObjectNameException JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29
30 import org.apache.catalina.CometEvent;
31 import org.apache.catalina.Contained;
32 import org.apache.catalina.Container;
33 import org.apache.catalina.Context;
34 import org.apache.catalina.Engine;
35 import org.apache.catalina.Host;
36 import org.apache.catalina.Pipeline;
37 import org.apache.catalina.Valve;
38 import org.apache.catalina.Wrapper;
39 import org.apache.catalina.connector.Request;
40 import org.apache.catalina.connector.Response;
41 import org.apache.catalina.core.ContainerBase;
42 import org.apache.catalina.util.StringManager;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45
46
47 /**
48  * Convenience base class for implementations of the <b>Valve</b> interface.
49  * A subclass <strong>MUST</strong> implement an <code>invoke()</code>
50  * method to provide the required functionality, and <strong>MAY</strong>
51  * implement the <code>Lifecycle</code> interface to provide configuration
52  * management and lifecycle support.
53  *
54  * @author Craig R. McClanahan
55  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
56  */

57
58 public abstract class ValveBase
59     implements Contained, Valve, MBeanRegistration JavaDoc {
60     private static Log log = LogFactory.getLog(ValveBase.class);
61
62     //------------------------------------------------------ Instance Variables
63

64
65     /**
66      * The Container whose pipeline this Valve is a component of.
67      */

68     protected Container container = null;
69
70
71     /**
72      * Container log
73      */

74     protected Log containerLog = null;
75
76
77     /**
78      * Descriptive information about this Valve implementation. This value
79      * should be overridden by subclasses.
80      */

81     protected static String JavaDoc info =
82         "org.apache.catalina.core.ValveBase/1.0";
83
84
85     /**
86      * The next Valve in the pipeline this Valve is a component of.
87      */

88     protected Valve next = null;
89
90
91     /**
92      * The string manager for this package.
93      */

94     protected final static StringManager sm =
95         StringManager.getManager(Constants.Package);
96
97
98     //-------------------------------------------------------------- Properties
99

100
101     /**
102      * Return the Container with which this Valve is associated, if any.
103      */

104     public Container getContainer() {
105
106         return (container);
107
108     }
109
110
111     /**
112      * Set the Container with which this Valve is associated, if any.
113      *
114      * @param container The new associated container
115      */

116     public void setContainer(Container container) {
117
118         this.container = container;
119
120     }
121
122
123     /**
124      * Return descriptive information about this Valve implementation.
125      */

126     public String JavaDoc getInfo() {
127
128         return (info);
129
130     }
131
132
133     /**
134      * Return the next Valve in this pipeline, or <code>null</code> if this
135      * is the last Valve in the pipeline.
136      */

137     public Valve getNext() {
138
139         return (next);
140
141     }
142
143
144     /**
145      * Set the Valve that follows this one in the pipeline it is part of.
146      *
147      * @param valve The new next valve
148      */

149     public void setNext(Valve valve) {
150
151         this.next = valve;
152
153     }
154
155
156     //---------------------------------------------------------- Public Methods
157

158
159     /**
160      * Execute a periodic task, such as reloading, etc. This method will be
161      * invoked inside the classloading context of this container. Unexpected
162      * throwables will be caught and logged.
163      */

164     public void backgroundProcess() {
165     }
166
167
168     /**
169      * The implementation-specific logic represented by this Valve. See the
170      * Valve description for the normal design patterns for this method.
171      * <p>
172      * This method <strong>MUST</strong> be provided by a subclass.
173      *
174      * @param request The servlet request to be processed
175      * @param response The servlet response to be created
176      *
177      * @exception IOException if an input/output error occurs
178      * @exception ServletException if a servlet error occurs
179      */

180     public abstract void invoke(Request request, Response response)
181         throws IOException JavaDoc, ServletException JavaDoc;
182
183
184     /**
185      * Process a Comet event. This method will rarely need to be provided by
186      * a subclass, unless it needs to reassociate a particular object with
187      * the thread that is processing the request.
188      *
189      * @param request The servlet request to be processed
190      * @param response The servlet response to be created
191      *
192      * @exception IOException if an input/output error occurs, or is thrown
193      * by a subsequently invoked Valve, Filter, or Servlet
194      * @exception ServletException if a servlet error occurs, or is thrown
195      * by a subsequently invoked Valve, Filter, or Servlet
196      */

197     public void event(Request request, Response response, CometEvent event)
198         throws IOException JavaDoc, ServletException JavaDoc {
199         // Perform the request
200
getNext().event(request, response, event);
201     }
202
203
204     /**
205      * Return a String rendering of this object.
206      */

207     public String JavaDoc toString() {
208         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(this.getClass().getName());
209         sb.append("[");
210         if (container != null)
211             sb.append(container.getName());
212         sb.append("]");
213         return (sb.toString());
214     }
215
216
217     // -------------------- JMX and Registration --------------------
218
protected String JavaDoc domain;
219     protected ObjectName JavaDoc oname;
220     protected MBeanServer JavaDoc mserver;
221     protected ObjectName JavaDoc controller;
222
223     public ObjectName JavaDoc getObjectName() {
224         return oname;
225     }
226
227     public void setObjectName(ObjectName JavaDoc oname) {
228         this.oname = oname;
229     }
230
231     public String JavaDoc getDomain() {
232         return domain;
233     }
234
235     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server,
236                                   ObjectName JavaDoc name) throws Exception JavaDoc {
237         oname=name;
238         mserver=server;
239         domain=name.getDomain();
240
241
242         return name;
243     }
244
245     public void postRegister(Boolean JavaDoc registrationDone) {
246     }
247
248     public void preDeregister() throws Exception JavaDoc {
249     }
250
251     public void postDeregister() {
252     }
253
254     public ObjectName JavaDoc getController() {
255         return controller;
256     }
257
258     public void setController(ObjectName JavaDoc controller) {
259         this.controller = controller;
260     }
261
262     /** From the name, extract the parent object name
263      *
264      * @param valveName The valve name
265      * @return ObjectName The parent name
266      */

267     public ObjectName JavaDoc getParentName( ObjectName JavaDoc valveName ) {
268
269         return null;
270     }
271
272     public ObjectName JavaDoc createObjectName(String JavaDoc domain, ObjectName JavaDoc parent)
273             throws MalformedObjectNameException JavaDoc
274     {
275         Container container=this.getContainer();
276         if( container == null || ! (container instanceof ContainerBase) )
277             return null;
278         this.containerLog = container.getLogger();
279         ContainerBase containerBase=(ContainerBase)container;
280         Pipeline pipe=containerBase.getPipeline();
281         Valve valves[]=pipe.getValves();
282
283         /* Compute the "parent name" part */
284         String JavaDoc parentName="";
285         if (container instanceof Engine) {
286         } else if (container instanceof Host) {
287             parentName=",host=" +container.getName();
288         } else if (container instanceof Context) {
289                     String JavaDoc path = ((Context)container).getPath();
290                     if (path.length() < 1) {
291                         path = "/";
292                     }
293                     Host host = (Host) container.getParent();
294                     parentName=",path=" + path + ",host=" +
295                             host.getName();
296         } else if (container instanceof Wrapper) {
297             Context ctx = (Context) container.getParent();
298             String JavaDoc path = ctx.getPath();
299             if (path.length() < 1) {
300                 path = "/";
301             }
302             Host host = (Host) ctx.getParent();
303             parentName=",servlet=" + container.getName() +
304                     ",path=" + path + ",host=" + host.getName();
305         }
306         log.debug("valve parent=" + parentName + " " + parent);
307
308         String JavaDoc className=this.getClass().getName();
309         int period = className.lastIndexOf('.');
310         if (period >= 0)
311             className = className.substring(period + 1);
312
313         int seq=0;
314         for( int i=0; i<valves.length; i++ ) {
315             // Find other valves with the same name
316
if (valves[i] == this) {
317                 break;
318             }
319             if( valves[i]!=null &&
320                     valves[i].getClass() == this.getClass() ) {
321                 log.debug("Duplicate " + valves[i] + " " + this + " " + container);
322                 seq++;
323             }
324         }
325         String JavaDoc ext="";
326         if( seq > 0 ) {
327             ext=",seq=" + seq;
328         }
329
330         ObjectName JavaDoc objectName =
331             new ObjectName JavaDoc( domain + ":type=Valve,name=" + className + ext + parentName);
332         log.debug("valve objectname = "+objectName);
333         return objectName;
334     }
335
336     // -------------------- JMX data --------------------
337

338     public ObjectName JavaDoc getContainerName() {
339         if( container== null) return null;
340         return ((ContainerBase)container).getJmxName();
341     }
342 }
343
Popular Tags