1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jk.config;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Hashtable;
import org.apache.catalina.Context;
import org.apache.catalina.Host;
/* The idea is to keep all configuration in server.xml and
the normal apache config files. We don't want people to
touch apache ( or IIS, NES ) config files unless they
want to and know what they're doing ( better than we do :-).
One nice feature ( if someone sends it ) would be to
also edit httpd.conf to add the include.
We'll generate a number of configuration files - this one
is trying to generate a native apache config file.
Some web.xml mappings do not "map" to server configuration - in
this case we need to fallback to forward all requests to tomcat.
Ajp14 will add to that the posibility to have tomcat and
apache on different machines, and many other improvements -
but this should also work for Ajp12, Ajp13 and Jni.
*/
/**
Generates automatic apache mod_jk configurations based on
the Tomcat server.xml settings and the war contexts
initialized during startup.
<p>
This config interceptor is enabled by inserting an ApacheConfig
<code>Listener</code> in
the server.xml file like so:
<pre>
* < Server ... >
* ...
* <Listener className=<b>org.apache.ajp.tomcat4.config.ApacheConfig</b>
* <i>options</i> />
* ...
* < /Server >
</pre>
where <i>options</i> can include any of the following attributes:
<ul>
<li><b>configHome</b> - default parent directory for the following paths.
If not set, this defaults to TOMCAT_HOME. Ignored
whenever any of the following paths is absolute.
</li>
<li><b>jkConfig</b> - path to use for writing Apache mod_jk conf file. If
not set, defaults to
"conf/auto/mod_jk.conf".</li>
<li><b>workersConfig</b> - path to workers.properties file used by
mod_jk. If not set, defaults to
"conf/jk/workers.properties".</li>
<li><b>modJk</b> - path to Apache mod_jk plugin file. If not set,
defaults to "modules/mod_jk.dll" on windows,
"modules/mod_jk.nlm" on netware, and
"libexec/mod_jk.so" everywhere else.</li>
<li><b>jkLog</b> - path to log file to be used by mod_jk.</li>
<li><b>jkDebug</b> - JK Loglevel setting. May be debug, info, error, or emerg.
If not set, defaults to emerg.</li>
<li><b>jkWorker</b> The desired worker. Must be set to one of the workers
defined in the workers.properties file. "ajp12", "ajp13"
or "inprocess" are the workers found in the default
workers.properties file. If not specified, defaults
to "ajp13" if an Ajp13Interceptor is in use, otherwise
it defaults to "ajp12".</li>
<li><b>forwardAll</b> - If true, forward all requests to Tomcat. This helps
insure that all the behavior configured in the web.xml
file functions correctly. If false, let Apache serve
static resources. The default is true.
Warning: When false, some configuration in
the web.xml may not be duplicated in Apache.
Review the mod_jk conf file to see what
configuration is actually being set in Apache.</li>
<li><b>noRoot</b> - If true, the root context is not mapped to
Tomcat. If false and forwardAll is true, all requests
to the root context are mapped to Tomcat. If false and
forwardAll is false, only JSP and servlets requests to
the root context are mapped to Tomcat. When false,
to correctly serve Tomcat's root context you must also
modify the DocumentRoot setting in Apache's httpd.conf
file to point to Tomcat's root context directory.
Otherwise some content, such as Apache's index.html,
will be served by Apache before mod_jk gets a chance
to claim the request and pass it to Tomcat.
The default is true.</li>
</ul>
<p>
@author Costin Manolache
@author Larry Isaacs
@author Mel Martinez
@author Bill Barker
*/
public class ApacheConfig extends BaseJkConfig {
private static org.apache.juli.logging.Log log =
org.apache.juli.logging.LogFactory.getLog(ApacheConfig.class);
/** default path to mod_jk .conf location */
public static final String MOD_JK_CONFIG = "conf/auto/mod_jk.conf";
/** default path to workers.properties file
This should be also auto-generated from server.xml.
*/
public static final String WORKERS_CONFIG = "conf/jk/workers.properties";
/** default mod_jk log file location */
public static final String JK_LOG_LOCATION = "logs/mod_jk.log";
/** default location of mod_jk Apache plug-in. */
public static final String MOD_JK;
//set up some defaults based on OS type
static{
String os = System.getProperty("os.name").toLowerCase();
if(os.indexOf("windows")>=0){
MOD_JK = "modules/mod_jk.so";
}else if(os.indexOf("netware")>=0){
MOD_JK = "modules/mod_jk.nlm";
}else{
MOD_JK = "libexec/mod_jk.so";
}
}
private File jkConfig = null;
private File modJk = null;
// ssl settings
private boolean sslExtract=true;
private String sslHttpsIndicator="HTTPS";
private String sslSessionIndicator="SSL_SESSION_ID";
private String sslCipherIndicator="SSL_CIPHER";
private String sslCertsIndicator="SSL_CLIENT_CERT";
Hashtable NamedVirtualHosts=null;
public ApacheConfig() {
}
//-------------------- Properties --------------------
/**
set the path to the output file for the auto-generated
mod_jk configuration file. If this path is relative
then it will be resolved absolutely against
the getConfigHome() path.
<p>
@param path String path to a file
*/
public void setJkConfig(String path){
jkConfig= (path==null)?null:new File(path);
}
/**
set the path to the mod_jk Apache Module
@param path String path to a file
*/
public void setModJk(String path){
modJk=( path==null?null:new File(path));
}
/** By default mod_jk is configured to collect SSL information from
the apache environment and send it to the Tomcat workers. The
problem is that there are many SSL solutions for Apache and as
a result the environment variable names may change.
The following JK related SSL configureation
can be used to customize mod_jk's SSL behaviour.
Should mod_jk send SSL information to Tomact (default is On)
*/
public void setExtractSSL( boolean sslMode ) {
this.sslExtract=sslMode;
}
/** What is the indicator for SSL (default is HTTPS)
*/
public void setHttpsIndicator( String s ) {
sslHttpsIndicator=s;
}
/**What is the indicator for SSL session (default is SSL_SESSION_ID)
*/
public void setSessionIndicator( String s ) {
sslSessionIndicator=s;
}
/**What is the indicator for client SSL cipher suit (default is SSL_CIPHER)
*/
public void setCipherIndicator( String s ) {
sslCipherIndicator=s;
}
/** What is the indicator for the client SSL certificated(default
is SSL_CLIENT_CERT
*/
public void setCertsIndicator( String s ) {
sslCertsIndicator=s;
}
// -------------------- Initialize/guess defaults --------------------
/** Initialize defaults for properties that are not set
explicitely
*/
protected void initProperties() {
super.initProperties();
jkConfig= getConfigFile( jkConfig, configHome, MOD_JK_CONFIG);
workersConfig=getConfigFile( workersConfig, configHome,
WORKERS_CONFIG);
if( modJk == null )
modJk=new File(MOD_JK);
else
modJk=getConfigFile( modJk, configHome, MOD_JK );
jkLog=getConfigFile( jkLog, configHome, JK_LOG_LOCATION);
}
// -------------------- Generate config --------------------
protected PrintWriter getWriter() throws IOException {
String abJkConfig = jkConfig.getAbsolutePath();
return new PrintWriter(new FileWriter(abJkConfig, append));
}
// -------------------- Config sections --------------------
/** Generate the loadModule and general options
*/
protected boolean generateJkHead(PrintWriter mod_jk)
{
mod_jk.println("########## Auto generated on " + new Date() +
"##########" );
mod_jk.println();
// Fail if mod_jk not found, let the user know the problem
// instead of running into problems later.
if( ! modJk.exists() ) {
log.info( "mod_jk location: " + modJk );
log.info( "Make sure it is installed corectly or " +
" set the config location" );
log.info( "Using <Listener className=\""+getClass().getName()+"\" modJk=\"PATH_TO_MOD_JK.SO_OR_DLL\" />" );
}
// Verify the file exists !!
mod_jk.println("<IfModule !mod_jk.c>");
mod_jk.println(" LoadModule jk_module \""+
modJk.toString().replace('\\','/') +
"\"");
mod_jk.println("</IfModule>");
mod_jk.println();
// Fail if workers file not found, let the user know the problem
// instead of running into problems later.
if( ! workersConfig.exists() ) {
log.warn( "Can't find workers.properties at " + workersConfig );
log.warn( "Please install it in the default location or " +
" set the config location" );
log.warn( "Using <Listener className=\"" + getClass().getName() + "\" workersConfig=\"FULL_PATH\" />" );
return false;
}
mod_jk.println("JkWorkersFile \""
+ workersConfig.toString().replace('\\', '/')
+ "\"");
mod_jk.println("JkLogFile \""
+ jkLog.toString().replace('\\', '/')
+ "\"");
mod_jk.println();
if( jkDebug != null ) {
mod_jk.println("JkLogLevel " + jkDebug);
mod_jk.println();
}
return true;
}
protected void generateVhostHead(Host host, PrintWriter mod_jk) {
mod_jk.println();
String vhostip = host.getName();
String vhost = vhostip;
int ppos = vhost.indexOf(":");
if(ppos >= 0)
vhost = vhost.substring(0,ppos);
mod_jk.println("<VirtualHost "+ vhostip + ">");
mod_jk.println(" ServerName " + vhost );
String [] aliases=host.findAliases();
if( aliases.length > 0 ) {
mod_jk.print(" ServerAlias " );
for( int ii=0; ii < aliases.length ; ii++) {
mod_jk.print( aliases[ii] + " " );
}
mod_jk.println();
}
indent=" ";
}
protected void generateVhostTail(Host host, PrintWriter mod_jk) {
mod_jk.println("</VirtualHost>");
indent="";
}
protected void generateSSLConfig(PrintWriter mod_jk) {
if( ! sslExtract ) {
mod_jk.println("JkExtractSSL Off");
}
if( ! "HTTPS".equalsIgnoreCase( sslHttpsIndicator ) ) {
mod_jk.println("JkHTTPSIndicator " + sslHttpsIndicator);
}
if( ! "SSL_SESSION_ID".equalsIgnoreCase( sslSessionIndicator )) {
mod_jk.println("JkSESSIONIndicator " + sslSessionIndicator);
}
if( ! "SSL_CIPHER".equalsIgnoreCase( sslCipherIndicator )) {
mod_jk.println("JkCIPHERIndicator " + sslCipherIndicator);
}
if( ! "SSL_CLIENT_CERT".equalsIgnoreCase( sslCertsIndicator )) {
mod_jk.println("JkCERTSIndicator " + sslCertsIndicator);
}
mod_jk.println();
}
// -------------------- Forward all mode --------------------
String indent="";
/** Forward all requests for a context to tomcat.
The default.
*/
protected void generateStupidMappings(Context context,
PrintWriter mod_jk )
{
String ctxPath = context.getPath();
if(ctxPath == null)
return;
String nPath=("".equals(ctxPath)) ? "/" : ctxPath;
mod_jk.println();
mod_jk.println(indent + "JkMount " + nPath + " " + jkWorker );
if( "".equals(ctxPath) ) {
mod_jk.println(indent + "JkMount " + nPath + "* " + jkWorker );
if ( context.getParent() instanceof Host ) {
mod_jk.println(indent + "DocumentRoot \"" +
getApacheDocBase(context) + "\"");
} else {
mod_jk.println(indent +
"# To avoid Apache serving root welcome files from htdocs, update DocumentRoot");
mod_jk.println(indent +
"# to point to: \"" + getApacheDocBase(context) + "\"");
}
} else {
mod_jk.println(indent + "JkMount " + nPath + "/* " + jkWorker );
}
}
private void generateNameVirtualHost( PrintWriter mod_jk, String ip ) {
if( !NamedVirtualHosts.containsKey(ip) ) {
mod_jk.println("NameVirtualHost " + ip + "");
NamedVirtualHosts.put(ip,ip);
}
}
// -------------------- Apache serves static mode --------------------
// This is not going to work for all apps. We fall back to stupid mode.
protected void generateContextMappings(Context context, PrintWriter mod_jk )
{
String ctxPath = context.getPath();
Host vhost = getHost(context);
if( noRoot && "".equals(ctxPath) ) {
log.debug("Ignoring root context in non-forward-all mode ");
return;
}
mod_jk.println();
mod_jk.println(indent + "#################### " +
((vhost!=null ) ? vhost.getName() + ":" : "" ) +
(("".equals(ctxPath)) ? "/" : ctxPath ) +
" ####################" );
mod_jk.println();
// Dynamic /servet pages go to Tomcat
generateStaticMappings( context, mod_jk );
// InvokerInterceptor - it doesn't have a container,
// but it's implemented using a special module.
// XXX we need to better collect all mappings
if(context.getLoginConfig() != null) {
String loginPage = context.getLoginConfig().getLoginPage();
if(loginPage != null) {
int lpos = loginPage.lastIndexOf("/");
String jscurl = loginPage.substring(0,lpos+1) + "j_security_check";
addMapping( ctxPath, jscurl, mod_jk);
}
}
String [] servletMaps = context.findServletMappings();
for(int ii=0; ii < servletMaps.length; ii++) {
addMapping( ctxPath, servletMaps[ii] , mod_jk );
}
}
/** Add an Apache extension mapping.
*/
protected boolean addExtensionMapping( String ctxPath, String ext,
PrintWriter mod_jk )
{
if( log.isDebugEnabled() )
log.debug( "Adding extension map for " + ctxPath + "/*." + ext );
mod_jk.println(indent + "JkMount " + ctxPath + "/*." + ext
+ " " + jkWorker);
return true;
}
/** Add a fulling specified Appache mapping.
*/
protected boolean addMapping( String fullPath, PrintWriter mod_jk ) {
if( log.isDebugEnabled() )
log.debug( "Adding map for " + fullPath );
mod_jk.println(indent + "JkMount " + fullPath + " " + jkWorker );
return true;
}
/** Add a partially specified Appache mapping.
*/
protected boolean addMapping( String ctxP, String ext, PrintWriter mod_jk ) {
if( log.isDebugEnabled() )
log.debug( "Adding map for " + ext );
if(! ext.startsWith("/") )
ext = "/" + ext;
if(ext.length() > 1)
mod_jk.println(indent + "JkMount " + ctxP + ext+ " " + jkWorker );
return true;
}
private void generateWelcomeFiles(Context context, PrintWriter mod_jk ) {
String wf[]=context.findWelcomeFiles();
if( wf==null || wf.length == 0 )
return;
mod_jk.print(indent + " DirectoryIndex ");
for( int i=0; i<wf.length ; i++ ) {
mod_jk.print( wf[i] + " " );
}
mod_jk.println();
}
/** Mappings for static content. XXX need to add welcome files,
* mime mappings ( all will be handled by Mime and Static modules of
* apache ).
*/
private void generateStaticMappings(Context context, PrintWriter mod_jk ) {
String ctxPath = context.getPath();
// Calculate the absolute path of the document base
String docBase = getApacheDocBase(context);
if( !"".equals(ctxPath) ) {
// Static files will be served by Apache
mod_jk.println(indent + "# Static files ");
mod_jk.println(indent + "Alias " + ctxPath + " \"" + docBase + "\"");
mod_jk.println();
} else {
if ( getHost(context) != null ) {
mod_jk.println(indent + "DocumentRoot \"" +
getApacheDocBase(context) + "\"");
} else {
// For root context, ask user to update DocumentRoot setting.
// Using "Alias / " interferes with the Alias for other contexts.
mod_jk.println(indent +
"# Be sure to update DocumentRoot");
mod_jk.println(indent +
"# to point to: \"" + docBase + "\"");
}
}
mod_jk.println(indent + "<Directory \"" + docBase + "\">");
mod_jk.println(indent + " Options Indexes FollowSymLinks");
generateWelcomeFiles(context, mod_jk);
// XXX XXX Here goes the Mime types and welcome files !!!!!!!!
mod_jk.println(indent + "</Directory>");
mod_jk.println();
// Deny serving any files from WEB-INF
mod_jk.println();
mod_jk.println(indent +
"# Deny direct access to WEB-INF and META-INF");
mod_jk.println(indent + "#");
mod_jk.println(indent + "<Location \"" + ctxPath + "/WEB-INF/*\">");
mod_jk.println(indent + " AllowOverride None");
mod_jk.println(indent + " deny from all");
mod_jk.println(indent + "</Location>");
// Deny serving any files from META-INF
mod_jk.println();
mod_jk.println(indent + "<Location \"" + ctxPath + "/META-INF/*\">");
mod_jk.println(indent + " AllowOverride None");
mod_jk.println(indent + " deny from all");
mod_jk.println(indent + "</Location>");
if (File.separatorChar == '\\') {
mod_jk.println(indent + "#");
mod_jk.println(indent +
"# Use Directory too. On Windows, Location doesn't"
+ " work unless case matches");
mod_jk.println(indent + "#");
mod_jk.println(indent +
"<Directory \"" + docBase + "/WEB-INF/\">");
mod_jk.println(indent + " AllowOverride None");
mod_jk.println(indent + " deny from all");
mod_jk.println(indent + "</Directory>");
mod_jk.println();
mod_jk.println(indent +
"<Directory \"" + docBase + "/META-INF/\">");
mod_jk.println(indent + " AllowOverride None");
mod_jk.println(indent + " deny from all");
mod_jk.println(indent + "</Directory>");
}
mod_jk.println();
}
// -------------------- Utils --------------------
private String getApacheDocBase(Context context)
{
// Calculate the absolute path of the document base
String docBase = getAbsoluteDocBase(context);
if (File.separatorChar == '\\') {
// use separator preferred by Apache
docBase = docBase.replace('\\','/');
}
return docBase;
}
private String getVirtualHostAddress(String vhost, String vhostip) {
if( vhostip == null ) {
if ( vhost != null && vhost.length() > 0 && Character.isDigit(vhost.charAt(0)) )
vhostip=vhost;
else
vhostip="*";
}
return vhostip;
}
}
|