Attached is a diff to the class that writes the Apache config file.
I added code to write JkMount directives to include the <servlet-mapping> 's
from the web.xml files for each context.

I like/desire the functionality, but the implementation is a little cheap.

I did this by reparsing the web.xml's.  This is inefficient, because the
files were already parsed by the time execution reaches here.  But alas, I
wanted this feature now and didn't know if there was access to the mappings
from this task.

Jim

Index: ./src/share/org/apache/tomcat/task/ApacheConfig.java
===================================================================
RCS file:
/home/cvspublic/jakarta-tomcat/src/share/org/apache/tomcat/task/ApacheConfig
.java,v
retrieving revision 1.22
diff -r1.22 ApacheConfig.java
66a67,68
> import org.w3c.dom.*;
> import javax.xml.parsers.*;
202a205,218
>   /* Get a factory and parser for reading
>   the web.xml files.  One builder will be used for all the
>   contexts as they are iterated below. */
>   DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
>   DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
>   try {
>    docBuilderFactory = DocumentBuilderFactory.newInstance();
>    docBuilder = docBuilderFactory.newDocumentBuilder();
>   }
>   catch (Exception x) {
>    docBuilderFactory = null;
>    docBuilder = null;
>   }
>
288a305,366
>
>             /* write servlet mappings */
>             try {
>                 File fileWebInf = new File( docBase + File.separator +
"WEB-INF" + File.separator + "web.xml");
>                 if( (null!=docBuilder) && (fileWebInf.exists()) ) {
>                     Node ndTest;
>                     Element elMap;
>                     int i, n;
>                     int j, m;
>
>                     /* Parse file and retrieve all
>                     <servlet-mapping> tags */
>                     Document doc = docBuilder.parse(fileWebInf);
>                     NodeList nlMaps = doc.getElementsByTagName(
"servlet-mapping" );
>
>                     /* If there are any, walk the list and write
>                     a JkMount directive for each url-pattern. */
>                     n = nlMaps.getLength();
>                     if( n > 0 ) {
>                         mod_jk.println("#" );
>                         mod_jk.println("# These lines create Apache
directives for the servlet mappings" );
>                         mod_jk.println("# contained in the file " +
fileWebInf.getAbsolutePath() );
>                         mod_jk.println("#" );
>                         i = 0;
>                         while( i<n ) {
>                             ndTest = nlMaps.item(i);
>                             if( ndTest instanceof Element ) {
>                                 elMap = (Element)ndTest;
>                                 NodeList nlUrls =
elMap.getElementsByTagName( "url-pattern" );
>                                 /* The DTD says there must only be one, so
even if more
>                                 than one was present, only the first is
processed */
>                                 Node ndPattern =
nlUrls.item(0).getFirstChild();
>                                 if( ndPattern.getNodeType() ==
Node.TEXT_NODE ) {
>                                     /* The standard line terminator
problem presents itself here.
>                                     The string from getNodeValue() may or
may not contain various
>                                     /r and /n characters depending on
platform and editor used to
>                                     create web.xml file.
>                                     It would have been nice if
ndPattern.normalize() would have stripped
>                                     off anything but the text, but it just
throws an exception.
>                                     So, remove /r and /n from ends of
text.
>                                     Also, ensure that the pattern starts
with a '/'.  Extension
>                                     mappings (*.xyz) will not contain one.
Because the line will be
>                                     a directive to Apache, the forward
slash will be appropriate
>                                     on all platforms. */
>                                     String s = ndPattern.getNodeValue();
>                                     s = s.trim();
>                                     if( s.charAt(0) != '/' ) {
>                                         s = "/" + s;
>                                     }
>                                     mod_jk.println( "JkMount "+ path + s
+" ajp12" );
>                                 }
>                             }
>                             i++;
>                         }
>                     }
>                 }
>             }
>             catch( Exception x ) {
>                 /* If anything goes wrong, just skip writing the mappings.
>                 The web.xml file was dealt with when the context was
created
>                 presumably errors where logged there. */
>             }



Reply via email to