KickJava   Java API By Example, From Geeks To Geeks.

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


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 Netscape nsapi_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 NSConfig
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>NSConfig</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>objConfig</b> - path to use for writing Netscape obj.conf
50                             file. If not set, defaults to
51                             "conf/auto/obj.conf".</li>
52      <li><b>objectName</b> - Name of the Object to execute the requests.
53                              Defaults to "servlet".</li>
54      <li><b>workersConfig</b> - path to workers.properties file used by
55                                 nsapi_redirect. If not set, defaults to
56                                 "conf/jk/workers.properties".</li>
57      <li><b>nsapiJk</b> - path to Netscape mod_jk plugin file. If not set,
58                         defaults to "bin/nsapi_redirect.dll" on windows,
59                         "bin/nsapi_rd.nlm" on netware, and
60                         "bin/nsapi_redirector.so" everywhere else.</li>
61      <li><b>jkLog</b> - path to log file to be used by nsapi_redirect.</li>
62      <li><b>jkDebug</b> - Loglevel setting. May be debug, info, error, or emerg.
63                           If not set, defaults to emerg.</li>
64      <li><b>jkWorker</b> The desired worker. Must be set to one of the workers
65                          defined in the workers.properties file. "ajp12", "ajp13"
66                          or "inprocess" are the workers found in the default
67                          workers.properties file. If not specified, defaults
68                          to "ajp13" if an Ajp13Interceptor is in use, otherwise
69                          it defaults to "ajp12".</li>
70      <li><b>forwardAll</b> - If true, forward all requests to Tomcat. This helps
71                              insure that all the behavior configured in the web.xml
72                              file functions correctly. If false, let Netscape serve
73                              static resources assuming it has been configured
74                              to do so. The default is true.
75                              Warning: When false, some configuration in
76                              the web.xml may not be duplicated in Netscape.
77                              Review the uriworkermap file to see what
78                              configuration is actually being set in Netscape.</li>
79      <li><b>noRoot</b> - If true, the root context is not mapped to
80                          Tomcat. If false and forwardAll is true, all requests
81                          to the root context are mapped to Tomcat. If false and
82                          forwardAll is false, only JSP and servlets requests to
83                          the root context are mapped to Tomcat. When false,
84                          to correctly serve Tomcat's root context you must also
85                          modify the Home Directory setting in Netscape
86                          to point to Tomcat's root context directory.
87                          Otherwise some content, such as the root index.html,
88                          will be served by Netscape before nsapi_redirect gets a chance
89                          to claim the request and pass it to Tomcat.
90                          The default is true.</li>
91     </ul>
92   <p>
93     @author Costin Manolache
94     @author Larry Isaacs
95     @author Gal Shachor
96     @author Bill Barker
97  */

98 public class NSConfig extends BaseJkConfig {
99     private static org.apache.commons.logging.Log log =
100         org.apache.commons.logging.LogFactory.getLog(NSConfig.class);
101
102     public static final String JavaDoc WORKERS_CONFIG = "/conf/jk/workers.properties";
103     public static final String JavaDoc NS_CONFIG = "/conf/auto/obj.conf";
104     public static final String JavaDoc NSAPI_LOG_LOCATION = "/logs/nsapi_redirect.log";
105     /** default location of nsapi plug-in. */
106     public static final String JavaDoc NSAPI_REDIRECTOR;
107     
108     //set up some defaults based on OS type
109
static{
110         String JavaDoc os = System.getProperty("os.name").toLowerCase();
111         if(os.indexOf("windows")>=0){
112            NSAPI_REDIRECTOR = "bin/nsapi_redirect.dll";
113         }else if(os.indexOf("netware")>=0){
114            NSAPI_REDIRECTOR = "bin/nsapi_rd.nlm";
115         }else{
116            NSAPI_REDIRECTOR = "bin/nsapi_redirector.so";
117         }
118     }
119
120     private File JavaDoc objConfig = null;
121     private File JavaDoc nsapiJk = null;
122     private String JavaDoc objectName = "servlet";
123
124     public NSConfig()
125     {
126     }
127
128     //-------------------- Properties --------------------
129

130     /**
131         set the path to the output file for the auto-generated
132         isapi_redirect registry file. If this path is relative
133         then getRegConfig() will resolve it absolutely against
134         the getConfigHome() path.
135         <p>
136         @param path String path to a file
137     */

138     public void setObjConfig(String JavaDoc path) {
139     objConfig= (path==null)?null:new File JavaDoc(path);
140     }
141
142     /**
143         set the path to the nsapi plugin module
144         @param path String path to a file
145     */

146     public void setNsapiJk(String JavaDoc path) {
147         nsapiJk=( path==null?null:new File JavaDoc(path));
148     }
149
150     /**
151         Set the name for the Object that implements the
152         jk_service call.
153         @param name Name of the obj.conf Object
154     */

155     public void setObjectName(String JavaDoc name) {
156         objectName = name;
157     }
158
159     // -------------------- Initialize/guess defaults --------------------
160

161     /** Initialize defaults for properties that are not set
162     explicitely
163     */

164     protected void initProperties() {
165         super.initProperties();
166
167     objConfig=getConfigFile( objConfig, configHome, NS_CONFIG);
168     workersConfig=getConfigFile( workersConfig, configHome, WORKERS_CONFIG);
169
170     if( nsapiJk == null )
171         nsapiJk=new File JavaDoc(NSAPI_REDIRECTOR);
172     else
173         nsapiJk =getConfigFile( nsapiJk, configHome, NSAPI_REDIRECTOR );
174     jkLog=getConfigFile( jkLog, configHome, NSAPI_LOG_LOCATION);
175     }
176
177     // -------------------- Generate config --------------------
178
protected PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
179     String JavaDoc abObjConfig = objConfig.getAbsolutePath();
180     return new PrintWriter JavaDoc(new FileWriter JavaDoc(abObjConfig,append));
181     }
182     protected boolean generateJkHead(PrintWriter JavaDoc mod_jk) {
183     log.info("Generating netscape web server config = "+objConfig );
184     
185     generateNsapiHead( mod_jk );
186     
187     mod_jk.println("<Object name=default>");
188     return true;
189     }
190
191     private void generateNsapiHead(PrintWriter JavaDoc objfile)
192     {
193         objfile.println("###################################################################");
194         objfile.println("# Auto generated configuration. Dated: " + new Date JavaDoc());
195         objfile.println("###################################################################");
196         objfile.println();
197
198         objfile.println("#");
199         objfile.println("# You will need to merge the content of this file with your ");
200         objfile.println("# regular obj.conf and then restart (=stop + start) your Netscape server. ");
201         objfile.println("#");
202         objfile.println();
203             
204         objfile.println("#");
205         objfile.println("# Loading the redirector into your server");
206         objfile.println("#");
207         objfile.println();
208         objfile.println("Init fn=\"load-modules\" funcs=\"jk_init,jk_service\" shlib=\"<put full path to the redirector here>\"");
209         objfile.println("Init fn=\"jk_init\" worker_file=\"" +
210                         workersConfig.toString().replace('\\', '/') +
211                         "\" log_level=\"" + jkDebug + "\" log_file=\"" +
212                         jkLog.toString().replace('\\', '/') +
213                         "\"");
214         objfile.println();
215     }
216
217     protected void generateJkTail(PrintWriter JavaDoc objfile)
218     {
219         objfile.println();
220         objfile.println("#######################################################");
221         objfile.println("# Protecting the WEB-INF and META-INF directories.");
222         objfile.println("#######################################################");
223         objfile.println("PathCheck fn=\"deny-existence\" path=\"*/WEB-INF/*\"");
224         objfile.println("PathCheck fn=\"deny-existence\" path=\"*/META-INF/*\"");
225         objfile.println();
226
227         objfile.println("</Object>");
228         objfile.println();
229
230         objfile.println("#######################################################");
231         objfile.println("# New object to execute your servlet requests.");
232         objfile.println("#######################################################");
233         objfile.println("<Object name=" + objectName + ">");
234         objfile.println("ObjectType fn=force-type type=text/html");
235         objfile.println("Service fn=\"jk_service\" worker=\""+ jkWorker + "\" path=\"/*\"");
236         objfile.println("</Object>");
237         objfile.println();
238     }
239
240     // -------------------- Forward all mode --------------------
241

242     /** Forward all requests for a context to tomcat.
243     The default.
244      */

245     protected void generateStupidMappings(Context context, PrintWriter JavaDoc objfile )
246     {
247         String JavaDoc ctxPath = context.getPath();
248     String JavaDoc nPath=("".equals(ctxPath)) ? "/" : ctxPath;
249
250         if( noRoot && "".equals(ctxPath) ) {
251             log.debug("Ignoring root context in forward-all mode ");
252             return;
253         }
254     objfile.println("<Object name=" + context.getName() + ">");
255
256         objfile.println("NameTrans fn=\"assign-name\" from=\"" + ctxPath + "\" name=\"" + objectName + "\"");
257         objfile.println("NameTrans fn=\"assign-name\" from=\"" + ctxPath + "/*\" name=\"" + objectName + "\"");
258     objfile.println("</Object>");
259     }
260
261
262     // -------------------- Netscape serves static mode --------------------
263
// This is not going to work for all apps. We fall back to stupid mode.
264

265     protected void generateContextMappings(Context context, PrintWriter JavaDoc objfile )
266     {
267         String JavaDoc ctxPath = context.getPath();
268     String JavaDoc nPath=("".equals(ctxPath)) ? "/" : ctxPath;
269
270         if( noRoot && "".equals(ctxPath) ) {
271             log.debug("Ignoring root context in non-forward-all mode ");
272             return;
273         }
274     objfile.println("<Object name=" + context.getName() + ">");
275         // Static files will be served by Netscape
276
objfile.println("#########################################################");
277         objfile.println("# Auto configuration for the " + nPath + " context starts.");
278         objfile.println("#########################################################");
279         objfile.println();
280
281         // XXX Need to determine what if/how static mappings are done
282

283     // InvokerInterceptor - it doesn't have a container,
284
// but it's implemented using a special module.
285

286     // XXX we need to better collect all mappings
287
if(context.getLoginConfig() != null) {
288         String JavaDoc loginPage = context.getLoginConfig().getLoginPage();
289         if(loginPage != null) {
290         int lpos = loginPage.lastIndexOf("/");
291         String JavaDoc jscurl = loginPage.substring(0,lpos+1) + "j_security_check";
292         addMapping( ctxPath, jscurl, objfile);
293         }
294     }
295     
296     String JavaDoc [] servletMaps=context.findServletMappings();
297     for(int ii=0; ii < servletMaps.length; ii++) {
298         addMapping( ctxPath , servletMaps[ii] , objfile );
299     }
300     objfile.println("</Object>");
301     }
302
303     /** Add a Netscape extension mapping.
304      */

305     protected boolean addMapping( String JavaDoc ctxPath, String JavaDoc ext,
306                      PrintWriter JavaDoc objfile )
307     {
308         if( log.isDebugEnabled() )
309             log.debug( "Adding extension map for " + ctxPath + "/*." + ext );
310     if(! ext.startsWith("/") )
311         ext = "/" + ext;
312     if(ext.length() > 1)
313         objfile.println("NameTrans fn=\"assign-name\" from=\"" +
314                     ctxPath + ext + "\" name=\"" + objectName + "\"");
315     return true;
316     }
317
318     /** Add a fulling specified Netscape mapping.
319      */

320     protected boolean addMapping( String JavaDoc fullPath, PrintWriter JavaDoc objfile ) {
321         if( log.isDebugEnabled() )
322             log.debug( "Adding map for " + fullPath );
323         objfile.println("NameTrans fn=\"assign-name\" from=\"" +
324                         fullPath + "\" name=\"" + objectName + "\"");
325     return true;
326     }
327
328 }
329
Popular Tags