001: //========================================================================
002: //$Id: $
003: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015: package org.jboss.jetty;
016:
017: import java.io.CharArrayWriter;
018:
019: import javax.xml.transform.Transformer;
020: import javax.xml.transform.TransformerConfigurationException;
021: import javax.xml.transform.TransformerException;
022: import javax.xml.transform.TransformerFactory;
023: import javax.xml.transform.dom.DOMSource;
024: import javax.xml.transform.stream.StreamResult;
025:
026: import org.jboss.logging.Logger;
027: import org.mortbay.xml.XmlConfiguration;
028: import org.w3c.dom.Element;
029:
030: /**
031: * Jetty
032: *
033: * Extends the jetty Server class.
034: *
035: * TODO remove this class and apply jboss-web.xml configuration in
036: * JettyService class instead.
037: *
038: * @author <a href="mailto:jules_gosnell@yahoo..com">Julian Gosnell </a>
039: * @author <a href="mailto:andreas@jboss.org">Andreas Schaefer </a>.
040: *
041: * <p>
042: * <b>20011201 andreas: </b>
043: * <ul>
044: * <li>Fixed fixURL() because it is to "Unix" centric. Right now the method looks for the last
045: * part of the JAR URL (file:/...) which should be the JAR file name and add a "/." before them.
046: * Now this should work for Windows as well (the problem with windows was that after "file:" came
047: * the DRIVE LETTER which created a wrong URL).
048: * </ul>
049: */
050: public class Jetty extends org.mortbay.jetty.Server {
051:
052: protected static final Logger _log = Logger
053: .getLogger("org.jboss.jetty");
054:
055: JettyService _service;
056:
057: /**
058: * the XML snippet
059: */
060: String _xmlConfigString = null;
061:
062: /**
063: * the XML snippet as a DOM element
064: */
065: Element _configElement = null;
066:
067: //TODO move these to JettyDeployer?
068: protected boolean _stopWebApplicationsGracefully = false;
069:
070: Jetty(JettyService service) {
071: super ();
072: _service = service;
073: }
074:
075: public Element getConfigurationElement() {
076: return _configElement;
077: }
078:
079: /**
080: * @param configElement XML fragment from jboss-service.xml
081: */
082: public void setConfigurationElement(Element configElement) {
083:
084: // convert to an xml string to pass into Jetty's normal
085: // configuration mechanism
086: _configElement = configElement;
087:
088: try {
089: DOMSource source = new DOMSource(configElement);
090:
091: CharArrayWriter writer = new CharArrayWriter();
092: StreamResult result = new StreamResult(writer);
093: TransformerFactory factory = TransformerFactory
094: .newInstance();
095: Transformer transformer = factory.newTransformer();
096: transformer.transform(source, result);
097: _xmlConfigString = writer.toString();
098:
099: // get rid of the first line, as this will be prepended by
100: // the XmlConfiguration
101: int index = _xmlConfigString.indexOf("?>");
102: if (index >= 0) {
103: index += 2;
104:
105: while ((_xmlConfigString.charAt(index) == '\n')
106: || (_xmlConfigString.charAt(index) == '\r'))
107: index++;
108: }
109:
110: _xmlConfigString = _xmlConfigString.substring(index);
111:
112: if (_log.isDebugEnabled())
113: _log.debug("Passing xml config to jetty:\n"
114: + _xmlConfigString);
115:
116: setXMLConfiguration(_xmlConfigString);
117:
118: } catch (TransformerConfigurationException tce) {
119: _log.error("Can't transform config Element -> xml:", tce);
120: } catch (TransformerException te) {
121: _log.error("Can't transform config Element -> xml:", te);
122: } catch (Exception e) {
123: _log
124: .error(
125: "Unexpected exception converting configuration Element -> xml",
126: e);
127: }
128: }
129:
130: /*
131: * Actually perform the configuration @param xmlString
132: */
133: private void setXMLConfiguration(String xmlString) {
134:
135: try {
136: XmlConfiguration xmlConfigurator = new XmlConfiguration(
137: xmlString);
138: xmlConfigurator.configure(this );
139: } catch (Exception e) {
140: _log.error("problem configuring Jetty:", e);
141: }
142: }
143:
144: public String[] getCompileClasspath(ClassLoader cl) {
145: return _service.getCompileClasspath(cl);
146: }
147:
148: public boolean getStopWebApplicationsGracefully() {
149: return _stopWebApplicationsGracefully;
150: }
151:
152: public void setStopWebApplicationsGracefully(boolean graceful) {
153: _stopWebApplicationsGracefully = graceful;
154: }
155: }
|