1 16 17 package org.apache.jk.config; 18 19 import java.io.File ; 20 import java.io.FileWriter ; 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import java.util.Date ; 24 import java.util.Hashtable ; 25 26 import org.apache.catalina.Context; 27 import org.apache.catalina.Host; 28 29 48 49 117 public class ApacheConfig extends BaseJkConfig { 118 119 private static org.apache.commons.logging.Log log = 120 org.apache.commons.logging.LogFactory.getLog(ApacheConfig.class); 121 122 123 public static final String MOD_JK_CONFIG = "conf/auto/mod_jk.conf"; 124 127 public static final String WORKERS_CONFIG = "conf/jk/workers.properties"; 128 129 public static final String JK_LOG_LOCATION = "logs/mod_jk.log"; 130 131 public static final String MOD_JK; 132 133 static{ 135 String os = System.getProperty("os.name").toLowerCase(); 136 if(os.indexOf("windows")>=0){ 137 MOD_JK = "modules/mod_jk.dll"; 138 }else if(os.indexOf("netware")>=0){ 139 MOD_JK = "modules/mod_jk.nlm"; 140 }else{ 141 MOD_JK = "libexec/mod_jk.so"; 142 } 143 } 144 145 private File jkConfig = null; 146 private File modJk = null; 147 148 private boolean sslExtract=true; 150 private String sslHttpsIndicator="HTTPS"; 151 private String sslSessionIndicator="SSL_SESSION_ID"; 152 private String sslCipherIndicator="SSL_CIPHER"; 153 private String sslCertsIndicator="SSL_CLIENT_CERT"; 154 155 Hashtable NamedVirtualHosts=null; 156 157 public ApacheConfig() { 158 } 159 160 162 170 public void setJkConfig(String path){ 171 jkConfig= (path==null)?null:new File (path); 172 } 173 174 178 public void setModJk(String path){ 179 modJk=( path==null?null:new File (path)); 180 } 181 182 192 public void setExtractSSL( boolean sslMode ) { 193 this.sslExtract=sslMode; 194 } 195 196 198 public void setHttpsIndicator( String s ) { 199 sslHttpsIndicator=s; 200 } 201 202 204 public void setSessionIndicator( String s ) { 205 sslSessionIndicator=s; 206 } 207 208 210 public void setCipherIndicator( String s ) { 211 sslCipherIndicator=s; 212 } 213 214 217 public void setCertsIndicator( String s ) { 218 sslCertsIndicator=s; 219 } 220 221 223 226 protected void initProperties() { 227 super.initProperties(); 228 229 jkConfig= getConfigFile( jkConfig, configHome, MOD_JK_CONFIG); 230 workersConfig=getConfigFile( workersConfig, configHome, 231 WORKERS_CONFIG); 232 if( modJk == null ) 233 modJk=new File (MOD_JK); 234 else 235 modJk=getConfigFile( modJk, configHome, MOD_JK ); 236 jkLog=getConfigFile( jkLog, configHome, JK_LOG_LOCATION); 237 } 238 240 protected PrintWriter getWriter() throws IOException { 241 String abJkConfig = jkConfig.getAbsolutePath(); 242 return new PrintWriter (new FileWriter (abJkConfig, append)); 243 } 244 245 246 248 250 protected boolean generateJkHead(PrintWriter mod_jk) 251 { 252 253 mod_jk.println("########## Auto generated on " + new Date () + 254 "##########" ); 255 mod_jk.println(); 256 257 if( ! modJk.exists() ) { 260 log.info( "mod_jk location: " + modJk ); 261 log.info( "Make sure it is installed corectly or " + 262 " set the config location" ); 263 log.info( "Using <Listener className=\""+getClass().getName()+"\" modJk=\"PATH_TO_MOD_JK.SO_OR_DLL\" />" ); 264 } 265 266 mod_jk.println("<IfModule !mod_jk.c>"); 268 mod_jk.println(" LoadModule jk_module \""+ 269 modJk.toString().replace('\\','/') + 270 "\""); 271 mod_jk.println("</IfModule>"); 272 mod_jk.println(); 273 274 275 if( ! workersConfig.exists() ) { 278 log.warn( "Can't find workers.properties at " + workersConfig ); 279 log.warn( "Please install it in the default location or " + 280 " set the config location" ); 281 log.warn( "Using <Listener className=\"" + getClass().getName() + "\" workersConfig=\"FULL_PATH\" />" ); 282 return false; 283 } 284 285 mod_jk.println("JkWorkersFile \"" 286 + workersConfig.toString().replace('\\', '/') 287 + "\""); 288 289 mod_jk.println("JkLogFile \"" 290 + jkLog.toString().replace('\\', '/') 291 + "\""); 292 mod_jk.println(); 293 294 if( jkDebug != null ) { 295 mod_jk.println("JkLogLevel " + jkDebug); 296 mod_jk.println(); 297 } 298 return true; 299 } 300 301 protected void generateVhostHead(Host host, PrintWriter mod_jk) { 302 303 mod_jk.println(); 304 String vhostip = host.getName(); 305 String vhost = vhostip; 306 int ppos = vhost.indexOf(":"); 307 if(ppos >= 0) 308 vhost = vhost.substring(0,ppos); 309 310 mod_jk.println("<VirtualHost "+ vhostip + ">"); 311 mod_jk.println(" ServerName " + vhost ); 312 String [] aliases=host.findAliases(); 313 if( aliases.length > 0 ) { 314 mod_jk.print(" ServerAlias " ); 315 for( int ii=0; ii < aliases.length ; ii++) { 316 mod_jk.print( aliases[ii] + " " ); 317 } 318 mod_jk.println(); 319 } 320 indent=" "; 321 } 322 323 protected void generateVhostTail(Host host, PrintWriter mod_jk) { 324 mod_jk.println("</VirtualHost>"); 325 indent=""; 326 } 327 328 protected void generateSSLConfig(PrintWriter mod_jk) { 329 if( ! sslExtract ) { 330 mod_jk.println("JkExtractSSL Off"); 331 } 332 if( ! "HTTPS".equalsIgnoreCase( sslHttpsIndicator ) ) { 333 mod_jk.println("JkHTTPSIndicator " + sslHttpsIndicator); 334 } 335 if( ! "SSL_SESSION_ID".equalsIgnoreCase( sslSessionIndicator )) { 336 mod_jk.println("JkSESSIONIndicator " + sslSessionIndicator); 337 } 338 if( ! "SSL_CIPHER".equalsIgnoreCase( sslCipherIndicator )) { 339 mod_jk.println("JkCIPHERIndicator " + sslCipherIndicator); 340 } 341 if( ! "SSL_CLIENT_CERT".equalsIgnoreCase( sslCertsIndicator )) { 342 mod_jk.println("JkCERTSIndicator " + sslCertsIndicator); 343 } 344 345 mod_jk.println(); 346 } 347 348 String indent=""; 350 351 354 protected void generateStupidMappings(Context context, 355 PrintWriter mod_jk ) 356 { 357 String ctxPath = context.getPath(); 358 if(ctxPath == null) 359 return; 360 361 String nPath=("".equals(ctxPath)) ? "/" : ctxPath; 362 363 mod_jk.println(); 364 mod_jk.println(indent + "JkMount " + nPath + " " + jkWorker ); 365 if( "".equals(ctxPath) ) { 366 mod_jk.println(indent + "JkMount " + nPath + "* " + jkWorker ); 367 if ( context.getParent() instanceof Host ) { 368 mod_jk.println(indent + "DocumentRoot \"" + 369 getApacheDocBase(context) + "\""); 370 } else { 371 mod_jk.println(indent + 372 "# To avoid Apache serving root welcome files from htdocs, update DocumentRoot"); 373 mod_jk.println(indent + 374 "# to point to: \"" + getApacheDocBase(context) + "\""); 375 } 376 377 } else { 378 mod_jk.println(indent + "JkMount " + nPath + "/* " + jkWorker ); 379 } 380 } 381 382 383 private void generateNameVirtualHost( PrintWriter mod_jk, String ip ) { 384 if( !NamedVirtualHosts.containsKey(ip) ) { 385 mod_jk.println("NameVirtualHost " + ip + ""); 386 NamedVirtualHosts.put(ip,ip); 387 } 388 } 389 390 393 protected void generateContextMappings(Context context, PrintWriter mod_jk ) 394 { 395 String ctxPath = context.getPath(); 396 Host vhost = getHost(context); 397 398 if( noRoot && "".equals(ctxPath) ) { 399 log.debug("Ignoring root context in non-forward-all mode "); 400 return; 401 } 402 403 mod_jk.println(); 404 mod_jk.println(indent + "#################### " + 405 ((vhost!=null ) ? vhost.getName() + ":" : "" ) + 406 (("".equals(ctxPath)) ? "/" : ctxPath ) + 407 " ####################" ); 408 mod_jk.println(); 409 411 generateStaticMappings( context, mod_jk ); 412 413 416 418 if(context.getLoginConfig() != null) { 419 String loginPage = context.getLoginConfig().getLoginPage(); 420 if(loginPage != null) { 421 int lpos = loginPage.lastIndexOf("/"); 422 String jscurl = loginPage.substring(0,lpos+1) + "j_security_check"; 423 addMapping( ctxPath, jscurl, mod_jk); 424 } 425 } 426 String [] servletMaps = context.findServletMappings(); 427 for(int ii=0; ii < servletMaps.length; ii++) { 428 addMapping( ctxPath, servletMaps[ii] , mod_jk ); 429 } 430 } 431 432 434 protected boolean addExtensionMapping( String ctxPath, String ext, 435 PrintWriter mod_jk ) 436 { 437 if( log.isDebugEnabled() ) 438 log.debug( "Adding extension map for " + ctxPath + "/*." + ext ); 439 mod_jk.println(indent + "JkMount " + ctxPath + "/*." + ext 440 + " " + jkWorker); 441 return true; 442 } 443 444 445 447 protected boolean addMapping( String fullPath, PrintWriter mod_jk ) { 448 if( log.isDebugEnabled() ) 449 log.debug( "Adding map for " + fullPath ); 450 mod_jk.println(indent + "JkMount " + fullPath + " " + jkWorker ); 451 return true; 452 } 453 455 protected boolean addMapping( String ctxP, String ext, PrintWriter mod_jk ) { 456 if( log.isDebugEnabled() ) 457 log.debug( "Adding map for " + ext ); 458 if(! ext.startsWith("/") ) 459 ext = "/" + ext; 460 if(ext.length() > 1) 461 mod_jk.println(indent + "JkMount " + ctxP + ext+ " " + jkWorker ); 462 return true; 463 } 464 465 private void generateWelcomeFiles(Context context, PrintWriter mod_jk ) { 466 String wf[]=context.findWelcomeFiles(); 467 if( wf==null || wf.length == 0 ) 468 return; 469 mod_jk.print(indent + " DirectoryIndex "); 470 for( int i=0; i<wf.length ; i++ ) { 471 mod_jk.print( wf[i] + " " ); 472 } 473 mod_jk.println(); 474 } 475 476 480 private void generateStaticMappings(Context context, PrintWriter mod_jk ) { 481 String ctxPath = context.getPath(); 482 483 String docBase = getApacheDocBase(context); 485 486 if( !"".equals(ctxPath) ) { 487 mod_jk.println(indent + "# Static files "); 489 mod_jk.println(indent + "Alias " + ctxPath + " \"" + docBase + "\""); 490 mod_jk.println(); 491 } else { 492 if ( getHost(context) != null ) { 493 mod_jk.println(indent + "DocumentRoot \"" + 494 getApacheDocBase(context) + "\""); 495 } else { 496 mod_jk.println(indent + 499 "# Be sure to update DocumentRoot"); 500 mod_jk.println(indent + 501 "# to point to: \"" + docBase + "\""); 502 } 503 } 504 mod_jk.println(indent + "<Directory \"" + docBase + "\">"); 505 mod_jk.println(indent + " Options Indexes FollowSymLinks"); 506 507 generateWelcomeFiles(context, mod_jk); 508 509 mod_jk.println(indent + "</Directory>"); 511 mod_jk.println(); 512 513 514 mod_jk.println(); 516 mod_jk.println(indent + 517 "# Deny direct access to WEB-INF and META-INF"); 518 mod_jk.println(indent + "#"); 519 mod_jk.println(indent + "<Location \"" + ctxPath + "/WEB-INF/*\">"); 520 mod_jk.println(indent + " AllowOverride None"); 521 mod_jk.println(indent + " deny from all"); 522 mod_jk.println(indent + "</Location>"); 523 mod_jk.println(); 525 mod_jk.println(indent + "<Location \"" + ctxPath + "/META-INF/*\">"); 526 mod_jk.println(indent + " AllowOverride None"); 527 mod_jk.println(indent + " deny from all"); 528 mod_jk.println(indent + "</Location>"); 529 if (File.separatorChar == '\\') { 530 mod_jk.println(indent + "#"); 531 mod_jk.println(indent + 532 "# Use Directory too. On Windows, Location doesn't" 533 + " work unless case matches"); 534 mod_jk.println(indent + "#"); 535 mod_jk.println(indent + 536 "<Directory \"" + docBase + "/WEB-INF/\">"); 537 mod_jk.println(indent + " AllowOverride None"); 538 mod_jk.println(indent + " deny from all"); 539 mod_jk.println(indent + "</Directory>"); 540 mod_jk.println(); 541 mod_jk.println(indent + 542 "<Directory \"" + docBase + "/META-INF/\">"); 543 mod_jk.println(indent + " AllowOverride None"); 544 mod_jk.println(indent + " deny from all"); 545 mod_jk.println(indent + "</Directory>"); 546 } 547 mod_jk.println(); 548 } 549 550 552 private String getApacheDocBase(Context context) 553 { 554 String docBase = getAbsoluteDocBase(context); 556 if (File.separatorChar == '\\') { 557 docBase = docBase.replace('\\','/'); 559 } 560 return docBase; 561 } 562 563 private String getVirtualHostAddress(String vhost, String vhostip) { 564 if( vhostip == null ) { 565 if ( vhost != null && vhost.length() > 0 && Character.isDigit(vhost.charAt(0)) ) 566 vhostip=vhost; 567 else 568 vhostip="*"; 569 } 570 return vhostip; 571 } 572 573 } 574 | Popular Tags |