001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardEngineMapper.java,v 1.5 2001/07/22 20:25:08 pier Exp $
003: * $Revision: 1.5 $
004: * $Date: 2001/07/22 20:25:08 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.core;
065:
066: import org.apache.catalina.Container;
067: import org.apache.catalina.Engine;
068: import org.apache.catalina.Host;
069: import org.apache.catalina.Mapper;
070: import org.apache.catalina.Request;
071: import org.apache.catalina.util.StringManager;
072:
073: /**
074: * Implementation of <code>Mapper</code> for an <code>Engine</code>,
075: * designed to process HTTP requests. This mapper selects an appropriate
076: * <code>Host</code> based on the server name included in the request.
077: * <p>
078: * <b>IMPLEMENTATION NOTE</b>: This Mapper only works with a
079: * <code>StandardEngine</code>, because it relies on internal APIs.
080: *
081: * @author Craig R. McClanahan
082: * @version $Revision: 1.5 $ $Date: 2001/07/22 20:25:08 $
083: */
084:
085: public class StandardEngineMapper implements Mapper {
086:
087: // ----------------------------------------------------- Instance Variables
088:
089: /**
090: * The Container with which this Mapper is associated.
091: */
092: private StandardEngine engine = null;
093:
094: /**
095: * The protocol with which this Mapper is associated.
096: */
097: private String protocol = null;
098:
099: /**
100: * The string manager for this package.
101: */
102: private static final StringManager sm = StringManager
103: .getManager(Constants.Package);
104:
105: // ------------------------------------------------------------- Properties
106:
107: /**
108: * Return the Container with which this Mapper is associated.
109: */
110: public Container getContainer() {
111:
112: return (engine);
113:
114: }
115:
116: /**
117: * Set the Container with which this Mapper is associated.
118: *
119: * @param container The newly associated Container
120: *
121: * @exception IllegalArgumentException if this Container is not
122: * acceptable to this Mapper
123: */
124: public void setContainer(Container container) {
125:
126: if (!(container instanceof StandardEngine))
127: throw new IllegalArgumentException(sm
128: .getString("httpEngineMapper.container"));
129: engine = (StandardEngine) container;
130:
131: }
132:
133: /**
134: * Return the protocol for which this Mapper is responsible.
135: */
136: public String getProtocol() {
137:
138: return (this .protocol);
139:
140: }
141:
142: /**
143: * Set the protocol for which this Mapper is responsible.
144: *
145: * @param protocol The newly associated protocol
146: */
147: public void setProtocol(String protocol) {
148:
149: this .protocol = protocol;
150:
151: }
152:
153: // --------------------------------------------------------- Public Methods
154:
155: /**
156: * Return the child Container that should be used to process this Request,
157: * based upon its characteristics. If no such child Container can be
158: * identified, return <code>null</code> instead.
159: *
160: * @param request Request being processed
161: * @param update Update the Request to reflect the mapping selection?
162: */
163: public Container map(Request request, boolean update) {
164:
165: int debug = engine.getDebug();
166:
167: // Extract the requested server name
168: String server = request.getRequest().getServerName();
169: if (server == null) {
170: server = engine.getDefaultHost();
171: if (update)
172: request.setServerName(server);
173: }
174: if (server == null)
175: return (null);
176: server = server.toLowerCase();
177: if (debug >= 1)
178: engine.log("Mapping server name '" + server + "'");
179:
180: // Find the matching child Host directly
181: if (debug >= 2)
182: engine.log(" Trying a direct match");
183: Host host = (Host) engine.findChild(server);
184:
185: // Find a matching Host by alias. FIXME - Optimize this!
186: if (host == null) {
187: if (debug >= 2)
188: engine.log(" Trying an alias match");
189: Container children[] = engine.findChildren();
190: for (int i = 0; i < children.length; i++) {
191: String aliases[] = ((Host) children[i]).findAliases();
192: for (int j = 0; j < aliases.length; j++) {
193: if (server.equals(aliases[j])) {
194: host = (Host) children[i];
195: break;
196: }
197: }
198: if (host != null)
199: break;
200: }
201: }
202:
203: // Trying the "default" host if any
204: if (host == null) {
205: if (debug >= 2)
206: engine.log(" Trying the default host");
207: host = (Host) engine.findChild(engine.getDefaultHost());
208: }
209:
210: // Update the Request if requested, and return the selected Host
211: ; // No update to the Request is required
212: return (host);
213:
214: }
215:
216: }
|