KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jk > config > GeneratorApache2


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jk.config;
18
19 import java.io.File JavaDoc;
20 import java.io.FileWriter JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.w3c.dom.Node JavaDoc;
26
27
28 /* Naming conventions:
29
30 JK_CONF_DIR == serverRoot/work ( XXX /jkConfig ? )
31
32 - Each vhost has a sub-dir named after the canonycal name
33
34 - For each webapp in a vhost, there is a separate WEBAPP_NAME.jkmap
35
36 - In httpd.conf ( or equivalent servers ), in each virtual host you
37 should "Include JK_CONF_DIR/VHOST/jk_apache.conf". The config
38 file will contain the Alias declarations and other rules required
39 for apache operation. Same for other servers.
40
41 - WebXml2Jk will be invoked by a config tool or automatically for each
42 webapp - it'll generate the WEBAPP.jkmap files and config fragments.
43
44 WebXml2Jk will _not_ generate anything else but mappings.
45 It should _not_ try to guess locations or anything else - that's
46 another components' job.
47
48 */

49
50 /**
51  *
52  * @author Costin Manolache
53  */

54 public class GeneratorApache2 implements WebXml2Jk.MappingGenerator {
55     WebXml2Jk wxml;
56     String JavaDoc vhost;
57     String JavaDoc cpath;
58     String JavaDoc worker;
59     PrintWriter JavaDoc out;
60     
61     public void setWebXmlReader(WebXml2Jk wxml ) {
62         this.wxml=wxml;
63         vhost=wxml.vhost;
64         cpath=wxml.cpath;
65         worker=wxml.worker;
66     }
67
68     public void generateStart() throws IOException JavaDoc {
69         File JavaDoc base=wxml.getJkDir();
70         File JavaDoc outF=new File JavaDoc(base, "jk2.conf");
71         out=new PrintWriter JavaDoc( new FileWriter JavaDoc( outF ));
72
73         out.println("# Must be included in a virtual host context for " + vhost );
74
75         out.println("Alias " + cpath + " \"" + wxml.docBase + "\"");
76         out.println("<Directory \"" + wxml.docBase + "\" >");
77         out.println(" Options Indexes FollowSymLinks");
78         generateMimeMapping( out );
79         generateWelcomeFiles( out);
80
81         // If we use this instead of extension mapping for jsp we can avoid most
82
// jsp security problems.
83
out.println(" AddHandler jakarta-servlet2 .jsp");
84         out.println("</Directory>");
85         out.println();
86         
87         out.println("<Location \"" + cpath + "/WEB-INF\" >");
88         out.println(" AllowOverride None");
89         out.println(" Deny from all");
90         out.println("</Location>");
91         out.println();
92         out.println("<Location \"" + cpath + "/META-INF\" >");
93         out.println(" AllowOverride None");
94         out.println(" Deny from all");
95         out.println("</Location>");
96         out.println();
97     }
98
99     private void generateWelcomeFiles( PrintWriter JavaDoc out ) {
100         Vector JavaDoc wf= wxml.getWellcomeFiles();
101         out.print(" DirectoryIndex ");
102         for( int i=0; i<wf.size(); i++ ) {
103             out.print( " " + (String JavaDoc)wf.elementAt(i));
104         }
105         out.println();
106     }
107     
108     private void generateMimeMapping( PrintWriter JavaDoc out ) {
109         Node JavaDoc webN=wxml.getWebXmlNode();
110         for( Node JavaDoc mapN=WebXml2Jk.getChild( webN, "mime-mapping" );
111              mapN != null; mapN = WebXml2Jk.getNext( mapN ) ) {
112             String JavaDoc ext=WebXml2Jk.getChildContent( mapN, "extension" );
113             String JavaDoc type=WebXml2Jk.getChildContent( mapN, "mime-type" );
114
115             out.println(" AddType " + type + " " + ext );
116         }
117         
118
119     }
120
121     public void generateEnd() {
122         out.close();
123     }
124     
125     public void generateServletMapping( String JavaDoc servlet, String JavaDoc url ) {
126         out.println( "<Location \"" + cpath + url + "\" >");
127         out.println( " SetHandler jakarta-servlet2" );
128         out.println( " JkUriSet group " + worker );
129         out.println( " JkUriSet servlet " + servlet);
130         out.println( " JkUriSet host " + vhost );
131         out.println( " JkUriSet context " + cpath );
132         out.println( "</Location>");
133         out.println();
134     }
135
136     public void generateFilterMapping( String JavaDoc servlet, String JavaDoc url ) {
137         out.println( "<Location \"" + cpath + url + "\" >");
138         out.println( " SetHandler jakarta-servlet2" );
139         out.println( " JkUriSet group " + worker );
140         out.println( " JkUriSet servlet " + servlet);
141         out.println( " JkUriSet host " + vhost );
142         out.println( " JkUriSet context " + cpath );
143         out.println( "</Location>");
144         out.println();
145     }
146
147     public void generateLoginConfig( String JavaDoc loginPage,
148                                      String JavaDoc errPage, String JavaDoc authM ) {
149         out.println( "<Location \"" + cpath + loginPage + "\" >");
150         out.println( " SetHandler jakarta-servlet2" );
151         out.println( " JkUriSet group " + worker );
152         out.println( " JkUriSet host " + vhost );
153         out.println( " JkUriSet context " + cpath );
154         out.println( "</Location>");
155         out.println();
156     }
157
158     public void generateErrorPage( int err, String JavaDoc location ) {
159         
160     }
161
162     // XXX Only if BASIC/DIGEST and 'integrated auth'
163
public void generateConstraints( Vector JavaDoc urls, Vector JavaDoc methods, Vector JavaDoc roles, boolean isSSL ) {
164         for( int i=0; i<urls.size(); i++ ) {
165             String JavaDoc url=(String JavaDoc)urls.elementAt(i);
166
167             out.println( "<Location \"" + cpath + url + "\" >");
168
169             if( methods.size() > 0 ) {
170                 out.print(" <Limit ");
171                 for( int j=0; j<methods.size(); j++ ) {
172                     String JavaDoc m=(String JavaDoc)methods.elementAt(j);
173                     out.print( " " + m);
174                 }
175                 out.println( " >" );
176             }
177
178             out.println( " AuthType basic" );
179             out.print( " Require group " );
180             for( int j=0; j<roles.size(); j++ ) {
181                 String JavaDoc role=(String JavaDoc)roles.elementAt(j);
182                 out.print( " " + role);
183             }
184             out.println();
185
186             if( methods.size() > 0 ) {
187                 out.println(" </Limit>");
188             }
189             
190             out.println( "</Location>");
191         }
192     }
193 }
194
Popular Tags