KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > ServerData


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

18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20
21 import org.apache.commons.lang.StringUtils;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.apache.turbine.util.uri.URIConstants;
27
28 /**
29  * Holds basic server information under which Turbine is running.
30  * This class is accessable via the RunData object within the Turbine
31  * system. You can also use it as a placeholder for this information
32  * if you are only emulating a servlet system.
33  *
34  * @author <a HREF="mailto:burton@apache.org">Kevin A. Burton</a>
35  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
36  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37  * @version $Id: ServerData.java,v 1.9.2.2 2004/05/20 03:16:38 seade Exp $
38  */

39 public class ServerData
40 {
41     /** Cached serverName, */
42     private String JavaDoc serverName = null;
43
44     /** Cached serverPort. */
45     private int serverPort = 0;
46
47     /** Cached serverScheme. */
48     private String JavaDoc serverScheme = null;
49
50     /** Cached script name. */
51     private String JavaDoc scriptName = null;
52
53     /** Cached context path. */
54     private String JavaDoc contextPath = null;
55
56     /** Logging */
57     private static Log log = LogFactory.getLog(ServerData.class);
58
59     /**
60      * Constructor.
61      *
62      * @param serverName The server name.
63      * @param serverPort The server port.
64      * @param serverScheme The server scheme.
65      * @param scriptName The script name.
66      * @param contextPath The context Path
67      */

68     public ServerData(String JavaDoc serverName,
69         int serverPort,
70         String JavaDoc serverScheme,
71         String JavaDoc scriptName,
72         String JavaDoc contextPath)
73     {
74         if (log.isDebugEnabled())
75         {
76             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
77             sb.append("Constructor(");
78             sb.append(serverName);
79             sb.append(", ");
80             sb.append(serverPort);
81             sb.append(", ");
82             sb.append(serverScheme);
83             sb.append(", ");
84             sb.append(scriptName);
85             sb.append(", ");
86             sb.append(contextPath);
87             sb.append(")");
88             log.debug(sb.toString());
89         }
90
91         setServerName(serverName);
92         setServerPort(serverPort);
93         setServerScheme(serverScheme);
94         setScriptName(scriptName);
95         setContextPath(contextPath);
96     }
97
98     /**
99      * Copy-Constructor
100      *
101      * @param serverData A ServerData Object
102      */

103     public ServerData(ServerData serverData)
104     {
105         log.debug("Copy Constructor(" + serverData + ")");
106
107         setServerName(serverData.getServerName());
108         setServerPort(serverData.getServerPort());
109         setServerScheme(serverData.getServerScheme());
110         setScriptName(serverData.getScriptName());
111         setContextPath(serverData.getContextPath());
112     }
113
114     /**
115      * A C'tor that takes a HTTP Request object and
116      * builds the server data from its contents
117      *
118      * @param req The HTTP Request
119      */

120     public ServerData(HttpServletRequest JavaDoc req)
121     {
122         setServerName(req.getServerName());
123         setServerPort(req.getServerPort());
124         setServerScheme(req.getScheme());
125         setScriptName(req.getServletPath());
126         setContextPath(req.getContextPath());
127     }
128
129     /**
130      * generates a new Object with the same values as this one.
131      *
132      * @return A cloned object.
133      */

134     public Object JavaDoc clone()
135     {
136         log.debug("clone()");
137         return new ServerData(this);
138     }
139
140     /**
141      * Get the name of the server.
142      *
143      * @return A String.
144      */

145     public String JavaDoc getServerName()
146     {
147         return StringUtils.isEmpty(serverName) ? "" : serverName;
148     }
149
150     /**
151      * Sets the cached serverName.
152      *
153      * @param serverName the server name.
154      */

155     public void setServerName(String JavaDoc serverName)
156     {
157         log.debug("setServerName(" + serverName + ")");
158         this.serverName = serverName;
159     }
160
161     /**
162      * Get the server port.
163      *
164      * @return the server port.
165      */

166     public int getServerPort()
167     {
168         return this.serverPort;
169     }
170
171     /**
172      * Sets the cached serverPort.
173      *
174      * @param serverPort the server port.
175      */

176     public void setServerPort(int serverPort)
177     {
178         log.debug("setServerPort(" + serverPort + ")");
179         this.serverPort = serverPort;
180     }
181
182     /**
183      * Get the server scheme.
184      *
185      * @return the server scheme.
186      */

187     public String JavaDoc getServerScheme()
188     {
189         return StringUtils.isEmpty(serverScheme) ? "" : serverScheme;
190     }
191
192     /**
193      * Sets the cached serverScheme.
194      *
195      * @param serverScheme the server scheme.
196      */

197     public void setServerScheme(String JavaDoc serverScheme)
198     {
199         log.debug("setServerScheme(" + serverScheme + ")");
200         this.serverScheme = serverScheme;
201     }
202
203     /**
204      * Get the script name
205      *
206      * @return the script name.
207      */

208     public String JavaDoc getScriptName()
209     {
210         return StringUtils.isEmpty(scriptName) ? "" : scriptName;
211     }
212
213     /**
214      * Set the script name.
215      *
216      * @param scriptName the script name.
217      */

218     public void setScriptName(String JavaDoc scriptName)
219     {
220         log.debug("setScriptName(" + scriptName + ")");
221         this.scriptName = scriptName;
222     }
223
224     /**
225      * Get the context path.
226      *
227      * @return the context path.
228      */

229     public String JavaDoc getContextPath()
230     {
231         return StringUtils.isEmpty(contextPath) ? "" : contextPath;
232     }
233
234     /**
235      * Set the context path.
236      *
237      * @param contextPath A String.
238      */

239     public void setContextPath(String JavaDoc contextPath)
240     {
241         log.debug("setContextPath(" + contextPath + ")");
242         this.contextPath = contextPath;
243     }
244
245     /**
246      * Appends the Host URL to the supplied StringBuffer.
247      *
248      * @param url A StringBuffer object
249      */

250     public void getHostUrl(StringBuffer JavaDoc url)
251     {
252         url.append(getServerScheme());
253         url.append("://");
254         url.append(getServerName());
255         if ((getServerScheme().equals(URIConstants.HTTP)
256                 && getServerPort() != URIConstants.HTTP_PORT)
257             ||
258             (getServerScheme().equals(URIConstants.HTTPS)
259                 && getServerPort() != URIConstants.HTTPS_PORT)
260             )
261         {
262             url.append(":");
263             url.append(getServerPort());
264         }
265     }
266
267     /**
268      * Returns this object as an URL.
269      *
270      * @return The contents of this object as a String
271      */

272     public String JavaDoc toString()
273     {
274         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
275
276         getHostUrl(url);
277
278         url.append(getContextPath());
279         url.append(getScriptName());
280         return url.toString();
281     }
282 }
283
Popular Tags