KickJava   Java API By Example, From Geeks To Geeks.

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


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.Date JavaDoc;
24
25 import org.apache.catalina.Context;
26
27
28 /**
29     Generates automatic IIS isapi_redirect configurations based on
30     the Tomcat server.xml settings and the war contexts
31     initialized during startup.
32     <p>
33     This config interceptor is enabled by inserting an IISConfig
34     element in the <b>&lt;ContextManager&gt;</b> tag body inside
35     the server.xml file like so:
36     <pre>
37     * < ContextManager ... >
38     * ...
39     * <<b>IISConfig</b> <i>options</i> />
40     * ...
41     * < /ContextManager >
42     </pre>
43     where <i>options</i> can include any of the following attributes:
44     <ul>
45      <li><b>configHome</b> - default parent directory for the following paths.
46                             If not set, this defaults to TOMCAT_HOME. Ignored
47                             whenever any of the following paths is absolute.
48                              </li>
49      <li><b>regConfig</b> - path to use for writing IIS isapi_redirect registry
50                             file. If not set, defaults to
51                             "conf/auto/iis_redirect.reg".</li>
52      <li><b>workersConfig</b> - path to workers.properties file used by
53                                 isapi_redirect. If not set, defaults to
54                                 "conf/jk/workers.properties".</li>
55      <li><b>uriConfig</b> - path to use for writing IIS isapi_redirect uriworkermap
56                             file. If not set, defaults to
57                             "conf/auto/uriworkermap.properties".</li>
58      <li><b>jkLog</b> - path to log file to be used by isapi_redirect.</li>
59      <li><b>jkDebug</b> - Loglevel setting. May be debug, info, error, or emerg.
60                           If not set, defaults to emerg.</li>
61      <li><b>jkWorker</b> The desired worker. Must be set to one of the workers
62                          defined in the workers.properties file. "ajp12", "ajp13"
63                          or "inprocess" are the workers found in the default
64                          workers.properties file. If not specified, defaults
65                          to "ajp13" if an Ajp13Interceptor is in use, otherwise
66                          it defaults to "ajp12".</li>
67      <li><b>forwardAll</b> - If true, forward all requests to Tomcat. This helps
68                              insure that all the behavior configured in the web.xml
69                              file functions correctly. If false, let IIS serve
70                              static resources assuming it has been configured
71                              to do so. The default is true.
72                              Warning: When false, some configuration in
73                              the web.xml may not be duplicated in IIS.
74                              Review the uriworkermap file to see what
75                              configuration is actually being set in IIS.</li>
76      <li><b>noRoot</b> - If true, the root context is not mapped to
77                          Tomcat. If false and forwardAll is true, all requests
78                          to the root context are mapped to Tomcat. If false and
79                          forwardAll is false, only JSP and servlets requests to
80                          the root context are mapped to Tomcat. When false,
81                          to correctly serve Tomcat's root context you must also
82                          modify the Home Directory setting in IIS
83                          to point to Tomcat's root context directory.
84                          Otherwise some content, such as the root index.html,
85                          will be served by IIS before isapi_redirect gets a chance
86                          to claim the request and pass it to Tomcat.
87                          The default is true.</li>
88     </ul>
89   <p>
90     @author Costin Manolache
91     @author Larry Isaacs
92     @author Gal Shachor
93     @author Bill Barker
94  */

95 public class IISConfig extends BaseJkConfig {
96     private static org.apache.commons.logging.Log log =
97         org.apache.commons.logging.LogFactory.getLog(IISConfig.class);
98
99     public static final String JavaDoc WORKERS_CONFIG = "/conf/jk/workers.properties";
100     public static final String JavaDoc URI_WORKERS_MAP_CONFIG = "/conf/auto/uriworkermap.properties";
101     public static final String JavaDoc ISAPI_LOG_LOCATION = "/logs/iis_redirect.log";
102     public static final String JavaDoc ISAPI_REG_FILE = "/conf/auto/iis_redirect.reg";
103
104     private File JavaDoc regConfig = null;
105     private File JavaDoc uriConfig = null;
106
107     public IISConfig()
108     {
109     }
110
111     //-------------------- Properties --------------------
112

113     /**
114         set the path to the output file for the auto-generated
115         isapi_redirect registry file. If this path is relative
116         then getRegConfig() will resolve it absolutely against
117         the getConfigHome() path.
118         <p>
119         @param path String path to a file
120     */

121     public void setRegConfig(String JavaDoc path){
122     regConfig= (path==null)?null:new File JavaDoc(path);
123     }
124
125     /**
126         set a path to the uriworkermap.properties file.
127         @param path String path to uriworkermap.properties file
128     */

129     public void setUriConfig(String JavaDoc path){
130         uriConfig= (path==null?null:new File JavaDoc(path));
131     }
132
133     // -------------------- Initialize/guess defaults --------------------
134

135     /** Initialize defaults for properties that are not set
136     explicitely
137     */

138     protected void initProperties() {
139         super.initProperties();
140
141     regConfig=getConfigFile( regConfig, configHome, ISAPI_REG_FILE);
142     workersConfig=getConfigFile( workersConfig, configHome, WORKERS_CONFIG);
143     uriConfig=getConfigFile( uriConfig, configHome, URI_WORKERS_MAP_CONFIG);
144     jkLog=getConfigFile( jkLog, configHome, ISAPI_LOG_LOCATION);
145     }
146
147     // -------------------- Generate config --------------------
148

149     protected PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
150     String JavaDoc abUriConfig = uriConfig.getAbsolutePath();
151     return new PrintWriter JavaDoc(new FileWriter JavaDoc(abUriConfig,append));
152     }
153     protected boolean generateJkHead(PrintWriter JavaDoc mod_jk) {
154     try {
155         PrintWriter JavaDoc regfile = new PrintWriter JavaDoc(new FileWriter JavaDoc(regConfig));
156         log.info("Generating IIS registry file = "+regConfig );
157         generateRegistrySettings(regfile);
158         regfile.close();
159     } catch(IOException JavaDoc iex) {
160         log.warn("Unable to generate registry file " +regConfig);
161         return false;
162     }
163     log.info("Generating IIS URI worker map file = "+uriConfig );
164     generateUriWorkerHeader(mod_jk);
165     return true;
166     }
167
168     // -------------------- Config sections --------------------
169

170     /** Writes the registry settings required by the IIS connector
171      */

172     private void generateRegistrySettings(PrintWriter JavaDoc regfile)
173     {
174         regfile.println("REGEDIT4");
175         regfile.println();
176         regfile.println("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Apache Software Foundation\\Jakarta Isapi Redirector\\1.0]");
177         regfile.println("\"extension_uri\"=\"/jakarta/isapi_redirect.dll\"");
178         regfile.println("\"log_file\"=\"" + dubleSlash(jkLog.toString()) +"\"");
179         regfile.println("\"log_level\"=\"" + jkDebug + "\"");
180         regfile.println("\"worker_file\"=\"" + dubleSlash(workersConfig.toString()) +"\"");
181         regfile.println("\"worker_mount_file\"=\"" + dubleSlash(uriConfig.toString()) +"\"");
182     }
183
184     /** Writes the header information to the uriworkermap file
185      */

186     private void generateUriWorkerHeader(PrintWriter JavaDoc uri_worker)
187     {
188         uri_worker.println("###################################################################");
189         uri_worker.println("# Auto generated configuration. Dated: " + new Date JavaDoc());
190         uri_worker.println("###################################################################");
191         uri_worker.println();
192
193         uri_worker.println("#");
194         uri_worker.println("# Default worker to be used through our mappings");
195         uri_worker.println("#");
196         uri_worker.println("default.worker=" + jkWorker);
197         uri_worker.println();
198     }
199
200     /** Forward all requests for a context to tomcat.
201     The default.
202      */

203     protected void generateStupidMappings(Context context, PrintWriter JavaDoc uri_worker )
204     {
205         String JavaDoc ctxPath = context.getPath();
206     String JavaDoc nPath=("".equals(ctxPath)) ? "/" : ctxPath;
207
208         if( noRoot && "".equals(ctxPath) ) {
209             log.debug("Ignoring root context in forward-all mode ");
210             return;
211         }
212
213         // map all requests for this context to Tomcat
214
uri_worker.println(nPath +"=$(default.worker)");
215         if( "".equals(ctxPath) ) {
216             uri_worker.println(nPath +"*=$(default.worker)");
217             uri_worker.println(
218                     "# Note: To correctly serve the Tomcat's root context, IIS's Home Directory must");
219             uri_worker.println(
220                     "# must be set to: \"" + getAbsoluteDocBase(context) + "\"");
221         }
222         else
223             uri_worker.println(nPath +"/*=$(default.worker)");
224     }
225
226     protected void generateContextMappings(Context context, PrintWriter JavaDoc uri_worker )
227     {
228         String JavaDoc ctxPath = context.getPath();
229     String JavaDoc nPath=("".equals(ctxPath)) ? "/" : ctxPath;
230
231         if( noRoot && "".equals(ctxPath) ) {
232             log.debug("Ignoring root context in forward-all mode ");
233             return;
234         }
235
236         // Static files will be served by IIS
237
uri_worker.println();
238         uri_worker.println("#########################################################");
239         uri_worker.println("# Auto configuration for the " + nPath + " context.");
240         uri_worker.println("#########################################################");
241         uri_worker.println();
242
243         // Static mappings are not set in uriworkermap, but must be set with IIS admin.
244

245     // InvokerInterceptor - it doesn't have a container,
246
// but it's implemented using a special module.
247

248     // XXX we need to better collect all mappings
249

250     if(context.getLoginConfig() != null) {
251         String JavaDoc loginPage = context.getLoginConfig().getLoginPage();
252         if(loginPage != null) {
253         int lpos = loginPage.lastIndexOf("/");
254         String JavaDoc jscurl = loginPage.substring(0,lpos+1) + "j_security_check";
255         addMapping( ctxPath, jscurl, uri_worker);
256         }
257     }
258         String JavaDoc [] servletMaps=context.findServletMappings();
259     for( int ii=0; ii < servletMaps.length ; ii++) {
260         addMapping( ctxPath , servletMaps[ii] , uri_worker );
261     }
262     }
263
264     /** Add an IIS extension mapping.
265      */

266     protected boolean addMapping( String JavaDoc ctxPath, String JavaDoc ext,
267                      PrintWriter JavaDoc uri_worker )
268     {
269         if( log.isDebugEnabled() )
270             log.debug( "Adding extension map for " + ctxPath + "/*." + ext );
271     if(! ext.startsWith("/") )
272         ext = "/" + ext;
273     if(ext.length() > 1)
274         uri_worker.println(ctxPath + "/*." + ext + "=$(default.worker)");
275         return true;
276     }
277
278     /** Add a fulling specified IIS mapping.
279      */

280     protected boolean addMapping( String JavaDoc fullPath, PrintWriter JavaDoc uri_worker ) {
281         if( log.isDebugEnabled() )
282             log.debug( "Adding map for " + fullPath );
283         uri_worker.println(fullPath + "=$(default.worker)" );
284         return true;
285     }
286
287     // -------------------- Utils --------------------
288

289     private String JavaDoc dubleSlash(String JavaDoc in)
290     {
291         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
292         
293         for(int i = 0 ; i < in.length() ; i++) {
294             char ch = in.charAt(i);
295             if('\\' == ch) {
296                 sb.append("\\\\");
297             } else {
298                 sb.append(ch);
299             }
300         }
301         
302         return sb.toString();
303     }
304
305 }
306
Popular Tags