001: /*
002: * Copyright 1999-2001,2004 The Apache Software Foundation.
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: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.valves;
018:
019: import java.io.IOException;
020:
021: import javax.management.MBeanRegistration;
022: import javax.management.MBeanServer;
023: import javax.management.MalformedObjectNameException;
024: import javax.management.ObjectName;
025: import javax.servlet.ServletException;
026:
027: import org.apache.catalina.Contained;
028: import org.apache.catalina.Container;
029: import org.apache.catalina.Context;
030: import org.apache.catalina.Engine;
031: import org.apache.catalina.Host;
032: import org.apache.catalina.Pipeline;
033: import org.apache.catalina.Request;
034: import org.apache.catalina.Response;
035: import org.apache.catalina.Valve;
036: import org.apache.catalina.ValveContext;
037: import org.apache.catalina.Wrapper;
038: import org.apache.catalina.core.ContainerBase;
039: import org.apache.catalina.util.StringManager;
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042:
043: /**
044: * Convenience base class for implementations of the <b>Valve</b> interface.
045: * A subclass <strong>MUST</strong> implement an <code>invoke()</code>
046: * method to provide the required functionality, and <strong>MAY</strong>
047: * implement the <code>Lifecycle</code> interface to provide configuration
048: * management and lifecycle support.
049: *
050: * @author Craig R. McClanahan
051: * @version $Revision: 1.12 $ $Date: 2004/05/26 16:28:23 $
052: */
053:
054: public abstract class ValveBase implements Contained, Valve,
055: MBeanRegistration {
056: private static Log log = LogFactory.getLog(ValveBase.class);
057:
058: //------------------------------------------------------ Instance Variables
059:
060: /**
061: * The Container whose pipeline this Valve is a component of.
062: */
063: protected Container container = null;
064:
065: /**
066: * The debugging detail level for this component.
067: */
068: protected int debug = 0;
069:
070: /**
071: * Descriptive information about this Valve implementation. This value
072: * should be overridden by subclasses.
073: */
074: protected static String info = "org.apache.catalina.core.ValveBase/1.0";
075:
076: /**
077: * The string manager for this package.
078: */
079: protected final static StringManager sm = StringManager
080: .getManager(Constants.Package);
081:
082: //-------------------------------------------------------------- Properties
083:
084: /**
085: * Return the Container with which this Valve is associated, if any.
086: */
087: public Container getContainer() {
088:
089: return (container);
090:
091: }
092:
093: /**
094: * Set the Container with which this Valve is associated, if any.
095: *
096: * @param container The new associated container
097: */
098: public void setContainer(Container container) {
099:
100: this .container = container;
101:
102: }
103:
104: /**
105: * Return the debugging detail level for this component.
106: */
107: public int getDebug() {
108:
109: return (this .debug);
110:
111: }
112:
113: /**
114: * Set the debugging detail level for this component.
115: *
116: * @param debug The new debugging detail level
117: */
118: public void setDebug(int debug) {
119:
120: this .debug = debug;
121:
122: }
123:
124: /**
125: * Return descriptive information about this Valve implementation.
126: */
127: public String getInfo() {
128:
129: return (info);
130:
131: }
132:
133: //---------------------------------------------------------- Public Methods
134:
135: /**
136: * The implementation-specific logic represented by this Valve. See the
137: * Valve description for the normal design patterns for this method.
138: * <p>
139: * This method <strong>MUST</strong> be provided by a subclass.
140: *
141: * @param request The servlet request to be processed
142: * @param response The servlet response to be created
143: * @param context The valve context used to invoke the next valve
144: * in the current processing pipeline
145: *
146: * @exception IOException if an input/output error occurs
147: * @exception ServletException if a servlet error occurs
148: */
149: public abstract void invoke(Request request, Response response,
150: ValveContext context) throws IOException, ServletException;
151:
152: // -------------------- JMX and Registration --------------------
153: protected String domain;
154: protected ObjectName oname;
155: protected MBeanServer mserver;
156: protected ObjectName controller;
157:
158: public ObjectName getObjectName() {
159: return oname;
160: }
161:
162: public void setObjectName(ObjectName oname) {
163: this .oname = oname;
164: }
165:
166: public String getDomain() {
167: return domain;
168: }
169:
170: public ObjectName preRegister(MBeanServer server, ObjectName name)
171: throws Exception {
172: oname = name;
173: mserver = server;
174: domain = name.getDomain();
175:
176: return name;
177: }
178:
179: public void postRegister(Boolean registrationDone) {
180: }
181:
182: public void preDeregister() throws Exception {
183: }
184:
185: public void postDeregister() {
186: }
187:
188: public ObjectName getController() {
189: return controller;
190: }
191:
192: public void setController(ObjectName controller) {
193: this .controller = controller;
194: }
195:
196: /** From the name, extract the parent object name
197: *
198: * @param valveName The valve name
199: * @return ObjectName The parent name
200: */
201: public ObjectName getParentName(ObjectName valveName) {
202:
203: return null;
204: }
205:
206: public ObjectName createObjectName(String domain, ObjectName parent)
207: throws MalformedObjectNameException {
208: Container container = this .getContainer();
209: if (container == null || !(container instanceof ContainerBase))
210: return null;
211: ContainerBase containerBase = (ContainerBase) container;
212: Pipeline pipe = containerBase.getPipeline();
213: Valve valves[] = pipe.getValves();
214:
215: /* Compute the "parent name" part */
216: String parentName = "";
217: if (container instanceof Engine) {
218: } else if (container instanceof Host) {
219: parentName = ",host=" + container.getName();
220: } else if (container instanceof Context) {
221: String path = ((Context) container).getPath();
222: if (path.length() < 1) {
223: path = "/";
224: }
225: Host host = (Host) container.getParent();
226: parentName = ",path=" + path + ",host=" + host.getName();
227: } else if (container instanceof Wrapper) {
228: Context ctx = (Context) container.getParent();
229: String path = ctx.getPath();
230: if (path.length() < 1) {
231: path = "/";
232: }
233: Host host = (Host) ctx.getParent();
234: parentName = ",servlet=" + container.getName() + ",path="
235: + path + ",host=" + host.getName();
236: }
237: log.debug("valve parent=" + parentName + " " + parent);
238:
239: String className = this .getClass().getName();
240: int period = className.lastIndexOf('.');
241: if (period >= 0)
242: className = className.substring(period + 1);
243:
244: int seq = 0;
245: for (int i = 0; i < valves.length; i++) {
246: // Find other valves with the same name
247: if (valves[i] == this ) {
248: break;
249: }
250: if (valves[i] != null
251: && valves[i].getClass() == this .getClass()) {
252: log.debug("Duplicate " + valves[i] + " " + this + " "
253: + container);
254: seq++;
255: }
256: }
257: String ext = "";
258: if (seq > 0) {
259: ext = ",seq=" + seq;
260: }
261:
262: ObjectName objectName = new ObjectName(domain
263: + ":type=Valve,name=" + className + ext + parentName);
264: log.debug("valve objectname = " + objectName);
265: return objectName;
266: }
267:
268: // -------------------- JMX data --------------------
269:
270: public ObjectName getContainerName() {
271: if (container == null)
272: return null;
273: return ((ContainerBase) container).getJmxName();
274: }
275: }
|