001: /*
002: * $Id: LocalServletConfig.java,v 1.19 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.ByteArrayInputStream;
010: import java.io.File;
011: import java.io.InputStream;
012: import java.util.Enumeration;
013: import java.util.Properties;
014: import java.util.jar.JarEntry;
015: import java.util.jar.JarFile;
016: import javax.servlet.ServletConfig;
017: import javax.servlet.ServletContext;
018:
019: import org.xins.common.Log;
020: import org.xins.common.xml.SAXParserProvider;
021:
022: import org.xml.sax.Attributes;
023: import org.xml.sax.InputSource;
024: import org.xml.sax.SAXException;
025: import org.xml.sax.helpers.DefaultHandler;
026:
027: /**
028: * This class is an implementation of the ServletConfig that can be
029: * called locally.
030: *
031: * @version $Revision: 1.19 $ $Date: 2007/09/18 08:45:09 $
032: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
033: */
034: public class LocalServletConfig implements ServletConfig {
035:
036: /**
037: * The name of the servlet.
038: */
039: private String _servletName;
040:
041: /**
042: * The class of the servlet.
043: */
044: private String _servletClass;
045:
046: /**
047: * The properties of the servlet.
048: */
049: private Properties _initParameters;
050:
051: /**
052: * The servlet context.
053: */
054: private ServletContext _context;
055:
056: /**
057: * The WAR file.
058: */
059: private File _warFile;
060:
061: /**
062: * Creates a new Servlet configuration.
063: *
064: * @param warFileLocation
065: * the war file containing the servlet to deploy,
066: * cannot be <code>null</code>.
067: */
068: public LocalServletConfig(File warFileLocation) {
069: _warFile = warFileLocation;
070: _initParameters = new Properties();
071: _context = new XINSServletContext(this );
072:
073: try {
074: JarFile warFile = new JarFile(warFileLocation);
075: JarEntry webxmlEntry = warFile
076: .getJarEntry("WEB-INF/web.xml");
077: InputStream webxmlInputStream = warFile
078: .getInputStream(webxmlEntry);
079: parseWebXML(webxmlInputStream);
080: } catch (Exception ex) {
081:
082: Log.log_1512(ex);
083: }
084: }
085:
086: /**
087: * Parses the web.xml file.
088: *
089: * @param webxmlInputStream
090: * the web.xml file input stream.
091: *
092: * @throws Exception
093: * if the file cannot be parsed for any reason.
094: */
095: private void parseWebXML(InputStream webxmlInputStream)
096: throws Exception {
097: DefaultHandler handler = new WebInfoParser();
098: SAXParserProvider.get().parse(webxmlInputStream, handler);
099: webxmlInputStream.close();
100: }
101:
102: public String getInitParameter(String param) {
103: return _initParameters.getProperty(param);
104: }
105:
106: public String getServletName() {
107: return _servletName;
108: }
109:
110: /**
111: * Gets the class name of the Servlet.
112: *
113: * @return
114: * the class name of the servlet, cannot be <code>null</code>.
115: */
116: public String getServletClass() {
117: return _servletClass;
118: }
119:
120: public ServletContext getServletContext() {
121: return _context;
122: }
123:
124: public Enumeration getInitParameterNames() {
125: return _initParameters.keys();
126: }
127:
128: /**
129: * Gets the WAR file location.
130: *
131: * @return
132: * the WAR file, never <code>null</code>
133: */
134: File getWarFile() {
135: return _warFile;
136: }
137:
138: /**
139: * Parser for the web.xml containing the information about the Servlet.
140: */
141: private class WebInfoParser extends DefaultHandler {
142: /**
143: * The PCDATA element of the tag that is actually parsed.
144: */
145: private StringBuffer _pcdata;
146:
147: /**
148: * The name of the property that is currently parsed.
149: */
150: private String _paramName;
151:
152: public void startElement(String namespaceURI, String localName,
153: String qName, Attributes atts)
154: throws IllegalArgumentException, SAXException {
155: _pcdata = new StringBuffer(80);
156: }
157:
158: public void endElement(String namespaceURI, String localName,
159: String qName) throws IllegalArgumentException,
160: SAXException {
161: if (qName.equals("param-name")) {
162: _paramName = _pcdata.toString();
163: } else if (qName.equals("param-value")) {
164: _initParameters.setProperty(_paramName, _pcdata
165: .toString());
166: } else if (qName.equals("servlet-name")) {
167: _servletName = _pcdata.toString();
168: } else if (qName.equals("servlet-class")) {
169: _servletClass = _pcdata.toString();
170: }
171: _pcdata = null;
172: }
173:
174: public void characters(char[] ch, int start, int length)
175: throws IndexOutOfBoundsException {
176:
177: if (_pcdata != null) {
178: _pcdata.append(ch, start, length);
179: }
180: }
181:
182: public InputSource resolveEntity(String publicId,
183: String systemId) {
184: return new InputSource(
185: new ByteArrayInputStream(new byte[0]));
186: }
187: }
188: }
|