package org.xins.common.servlet.container;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import org.xins.common.Log;
import org.xins.common.xml.SAXParserProvider;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class LocalServletConfig implements ServletConfig {
private String _servletName;
private String _servletClass;
private Properties _initParameters;
private ServletContext _context;
private File _warFile;
public LocalServletConfig(File warFileLocation) {
_warFile = warFileLocation;
_initParameters = new Properties();
_context = new XINSServletContext(this);
try {
JarFile warFile = new JarFile(warFileLocation);
JarEntry webxmlEntry = warFile.getJarEntry("WEB-INF/web.xml");
InputStream webxmlInputStream = warFile.getInputStream(webxmlEntry);
parseWebXML(webxmlInputStream);
} catch (Exception ex) {
Log.log_1512(ex);
}
}
private void parseWebXML(InputStream webxmlInputStream) throws Exception {
DefaultHandler handler = new WebInfoParser();
SAXParserProvider.get().parse(webxmlInputStream, handler);
webxmlInputStream.close();
}
public String getInitParameter(String param) {
return _initParameters.getProperty(param);
}
public String getServletName() {
return _servletName;
}
public String getServletClass() {
return _servletClass;
}
public ServletContext getServletContext() {
return _context;
}
public Enumeration getInitParameterNames() {
return _initParameters.keys();
}
File getWarFile() {
return _warFile;
}
private class WebInfoParser extends DefaultHandler {
private StringBuffer _pcdata;
private String _paramName;
public void startElement(String namespaceURI,
String localName,
String qName,
Attributes atts)
throws IllegalArgumentException, SAXException {
_pcdata = new StringBuffer(80);
}
public void endElement(String namespaceURI,
String localName,
String qName)
throws IllegalArgumentException, SAXException {
if (qName.equals("param-name")) {
_paramName = _pcdata.toString();
} else if (qName.equals("param-value")) {
_initParameters.setProperty(_paramName, _pcdata.toString());
} else if (qName.equals("servlet-name")) {
_servletName = _pcdata.toString();
} else if (qName.equals("servlet-class")) {
_servletClass = _pcdata.toString();
}
_pcdata = null;
}
public void characters(char[] ch, int start, int length)
throws IndexOutOfBoundsException {
if (_pcdata != null) {
_pcdata.append(ch, start, length);
}
}
public InputSource resolveEntity(String publicId, String systemId) {
return new InputSource(new ByteArrayInputStream(new byte[0]));
}
}
}