001: /*
002: * $Id: XINSServletContext.java,v 1.23 2007/09/18 08:45:09 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.common.servlet.container;
008:
009: import java.io.IOException;
010: import java.io.InputStream;
011: import java.net.MalformedURLException;
012: import java.net.URL;
013: import java.util.Enumeration;
014: import java.util.Set;
015: import java.util.jar.JarEntry;
016: import java.util.jar.JarFile;
017: import javax.servlet.RequestDispatcher;
018: import javax.servlet.Servlet;
019: import javax.servlet.ServletContext;
020:
021: import org.xins.common.Log;
022: import org.xins.common.Utils;
023:
024: /**
025: * This class is an implementation of the ServletContext that can be
026: * called locally.
027: *
028: * @version $Revision: 1.23 $ $Date: 2007/09/18 08:45:09 $
029: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
030: */
031: public class XINSServletContext implements ServletContext {
032:
033: /**
034: * The configuration of the servlet.
035: */
036: private LocalServletConfig _config;
037:
038: /**
039: * The root URL for the servlet.
040: */
041: private String _rootURL;
042:
043: /**
044: * Creates a new <code>XINSServletContext</code> instance.
045: */
046: public XINSServletContext() {
047: // empty
048: }
049:
050: /**
051: * Creates a new <code>XINSServletContext</code> with the specified
052: * configuration.
053: *
054: * @param config
055: * the config of the servlet, can be <code>null</code>.
056: */
057: XINSServletContext(LocalServletConfig config) {
058: _config = config;
059: if (Utils.getJavaVersion() < 1.4) {
060: try {
061: _rootURL = "jar:"
062: + config.getWarFile().toURL().toString() + "!";
063: } catch (MalformedURLException muex) {
064: Log.log_1513(config.getWarFile().toString());
065: }
066: } else {
067: _rootURL = "jar:" + config.getWarFile().toURI().toString()
068: + "!";
069: }
070: }
071:
072: public void removeAttribute(String str) {
073: throw new UnsupportedOperationException();
074: }
075:
076: public Servlet getServlet(String str) {
077: throw new UnsupportedOperationException();
078: }
079:
080: public Set getResourcePaths(String str) {
081: throw new UnsupportedOperationException();
082: }
083:
084: public Object getAttribute(String str) {
085: throw new UnsupportedOperationException();
086: }
087:
088: public ServletContext getContext(String str) {
089: throw new UnsupportedOperationException();
090: }
091:
092: public String getInitParameter(String str) {
093: throw new UnsupportedOperationException();
094: }
095:
096: public String getMimeType(String str) {
097: throw new UnsupportedOperationException();
098: }
099:
100: public RequestDispatcher getNamedDispatcher(String str) {
101: throw new UnsupportedOperationException();
102: }
103:
104: public String getRealPath(String str) {
105:
106: // The WAR file is not unpacked
107: return null;
108: }
109:
110: public RequestDispatcher getRequestDispatcher(String str) {
111: throw new UnsupportedOperationException();
112: }
113:
114: public URL getResource(String str) {
115: if (!str.startsWith("/")) {
116: str = "/" + str;
117: }
118: try {
119: return new URL(_rootURL + str);
120: } catch (MalformedURLException muex) {
121: Log.log_1513(_rootURL + str);
122: return null;
123: }
124: }
125:
126: public InputStream getResourceAsStream(String str) {
127: try {
128: JarFile warFile = new JarFile(_config.getWarFile());
129: JarEntry entry = warFile.getJarEntry(str);
130: if (entry == null) {
131: Log.log_1514(str, "No entry.");
132: return null;
133: } else {
134: return warFile.getInputStream(entry);
135: }
136: } catch (IOException ioe) {
137: Log.log_1514(str, ioe.getMessage());
138: return null;
139: }
140: }
141:
142: public void log(Exception exception, String msg) {
143: log(msg, exception);
144: }
145:
146: public void log(String msg) {
147: Log.log_1510(msg);
148: }
149:
150: public void log(String msg, Throwable throwable) {
151: Log.log_1511(throwable, msg);
152: }
153:
154: public void setAttribute(String str, Object obj) {
155: throw new UnsupportedOperationException();
156: }
157:
158: public Enumeration getServlets() {
159: throw new UnsupportedOperationException();
160: }
161:
162: public Enumeration getServletNames() {
163: throw new UnsupportedOperationException();
164: }
165:
166: public String getServletContextName() {
167: throw new UnsupportedOperationException();
168: }
169:
170: public String getServerInfo() {
171: String osName = System.getProperty("os.name");
172: String osVersion = System.getProperty("os.version");
173: String osArch = System.getProperty("os.arch");
174: String os = osName + " " + osVersion + "/" + osArch;
175: return "XINS Servlet Test Container (" + os + ')';
176: }
177:
178: public Enumeration getAttributeNames() {
179: throw new UnsupportedOperationException();
180: }
181:
182: public Enumeration getInitParameterNames() {
183: throw new UnsupportedOperationException();
184: }
185:
186: public int getMajorVersion() {
187: return 2;
188: }
189:
190: public int getMinorVersion() {
191: return 3;
192: }
193: }
|