1 16 17 package org.apache.jk.config; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.PrintWriter ; 22 23 import org.apache.catalina.Container; 24 import org.apache.catalina.Context; 25 import org.apache.catalina.Engine; 26 import org.apache.catalina.Host; 27 import org.apache.catalina.Lifecycle; 28 import org.apache.catalina.LifecycleEvent; 29 import org.apache.catalina.LifecycleListener; 30 import org.apache.catalina.Server; 31 32 33 93 public class BaseJkConfig implements LifecycleListener { 94 private static org.apache.commons.logging.Log log = 95 org.apache.commons.logging.LogFactory.getLog(BaseJkConfig.class); 96 97 protected File configHome = null; 98 protected File workersConfig = null; 99 100 protected File jkLog = null; 101 protected String jkDebug="emerg"; 102 protected String jkWorker = "ajp13"; 103 104 protected boolean noRoot=true; 105 protected boolean forwardAll=true; 106 107 protected String tomcatHome; 108 protected boolean regenerate=false; 109 protected boolean append=false; 110 protected boolean legacy=true; 111 112 114 115 118 122 public void lifecycleEvent(LifecycleEvent evt) { 123 if(Lifecycle.START_EVENT.equals(evt.getType())) { 124 execute( evt ); 125 } 126 } 127 128 132 public void execute(LifecycleEvent evt) { 133 initProperties(); 134 PrintWriter mod_jk = null; 135 try { 136 mod_jk = getWriter(); 137 } catch(IOException iex) { 138 log.warn("Unable to open config file"); 139 return; 140 } 141 Lifecycle who = evt.getLifecycle(); 142 if( who instanceof Server ) { 143 executeServer((Server)who, mod_jk); 144 } else if(who instanceof Engine) { 145 executeEngine((Engine)who, mod_jk); 146 } else if ( who instanceof Host ) { 147 executeHost((Host)who, mod_jk); 148 } else if( who instanceof Context ) { 149 executeContext((Context)who, mod_jk); 150 } 151 mod_jk.close(); 152 } 153 157 public void executeServer(Server svr, PrintWriter mod_jk) { 158 if(! append ) { 159 if( ! generateJkHead(mod_jk) ) 160 return; 161 generateSSLConfig(mod_jk); 162 generateJkTail(mod_jk); 163 } 164 } 165 166 169 protected void generateSSLConfig(PrintWriter mod_jk) { 170 } 171 172 175 protected boolean generateJkHead(PrintWriter mod_jk) { 176 return true; 177 } 178 179 182 protected void generateJkTail(PrintWriter mod_jk) { 183 } 184 185 188 protected void generateVhostHead(Host host, PrintWriter mod_jk) { 189 } 190 191 194 protected void generateVhostTail(Host host, PrintWriter mod_jk) { 195 } 196 197 201 protected void executeEngine(Engine egn, PrintWriter mod_jk) { 202 if(egn.getJvmRoute() != null) { 203 jkWorker = egn.getJvmRoute(); 204 } 205 executeServer(egn.getService().getServer(), mod_jk); 206 Container [] children = egn.findChildren(); 207 for(int ii=0; ii < children.length; ii++) { 208 if( children[ii] instanceof Host ) { 209 executeHost((Host)children[ii], mod_jk); 210 } else if( children[ii] instanceof Context ) { 211 executeContext((Context)children[ii], mod_jk); 212 } 213 } 214 } 215 219 protected void executeHost(Host hst, PrintWriter mod_jk) { 220 generateVhostHead(hst, mod_jk); 221 Container [] children = hst.findChildren(); 222 for(int ii=0; ii < children.length; ii++) { 223 if(children[ii] instanceof Context) { 224 executeContext((Context)children[ii],mod_jk); 225 } 226 } 227 generateVhostTail(hst, mod_jk); 228 } 229 235 public void executeContext(Context context, PrintWriter mod_jk){ 236 237 if(context.getPath().length() > 0 || ! noRoot ) { 238 String docRoot = context.getServletContext().getRealPath("/"); 239 if( forwardAll || docRoot == null) 240 generateStupidMappings( context, mod_jk ); 241 else 242 generateContextMappings( context, mod_jk); 243 } 244 } 245 246 protected void generateStupidMappings(Context context, PrintWriter mod_jk){ 247 } 248 protected void generateContextMappings(Context context, PrintWriter mod_jk){ 249 } 250 251 255 protected PrintWriter getWriter() throws IOException { 256 return null; 257 } 258 259 262 protected Host getHost(Container child) { 263 while(child != null && ! (child instanceof Host) ) { 264 child = child.getParent(); 265 } 266 return (Host)child; 267 } 268 269 271 276 public void setAppend(boolean apnd) { 277 append = apnd; 278 } 279 280 286 public void setForwardAll( boolean b ) { 287 forwardAll=b; 288 } 289 290 300 public void setNoRoot( boolean b ) { 301 noRoot=b; 302 } 303 304 319 public void setConfigHome(String dir){ 320 if( dir==null ) return; 321 File f=new File (dir); 322 if(!f.isDirectory()){ 323 throw new IllegalArgumentException ( 324 "BaseConfig.setConfigHome(): "+ 325 "Configuration Home must be a directory! : "+dir); 326 } 327 configHome = f; 328 } 329 330 334 public void setWorkersConfig(String path){ 335 workersConfig= (path==null?null:new File (path)); 336 } 337 338 342 public void setJkLog(String path){ 343 jkLog = ( path==null ? null : new File (path)); 344 } 345 346 350 public void setJkDebug( String level ) { 351 jkDebug=level; 352 } 353 354 358 public void setJkWorker(String worker){ 359 jkWorker = worker; 360 } 361 362 public void setLegacy(boolean legacy) { 363 this.legacy = legacy; 364 } 365 366 368 372 protected void initProperties() { 373 tomcatHome = System.getProperty("catalina.home"); 374 File tomcatDir = new File (tomcatHome); 375 if(configHome==null){ 376 configHome=tomcatDir; 377 } 378 } 379 380 382 383 387 protected boolean addExtensionMapping( String ctxPath, String ext, 388 PrintWriter pw ) { 389 return true; 390 } 391 392 393 397 protected boolean addMapping( String fullPath, PrintWriter pw ) { 398 return true; 399 } 400 401 403 protected String getAbsoluteDocBase(Context context) { 404 String docBase = context.getServletContext().getRealPath("/"); 406 docBase = docBase.substring(0,docBase.length()-1); 407 if (!isAbsolute(docBase)){ 408 docBase = tomcatHome + "/" + docBase; 409 } 410 docBase = patch(docBase); 411 return docBase; 412 } 413 414 public static File getConfigFile( File base, File configDir, String defaultF ) 416 { 417 if( base==null ) 418 base=new File ( defaultF ); 419 if( ! base.isAbsolute() ) { 420 if( configDir != null ) 421 base=new File ( configDir, base.getPath()); 422 else 423 base=new File ( base.getAbsolutePath()); } 425 File parent=new File (base.getParent()); 426 if(!parent.exists()){ 427 if(!parent.mkdirs()){ 428 throw new RuntimeException ( 429 "Unable to create path to config file :"+ 430 base.getAbsolutePath()); 431 } 432 } 433 return base; 434 } 435 436 public static String patch(String path) { 437 String patchPath = path; 438 439 if (patchPath.length() >= 3 && 441 patchPath.charAt(0) == '/' && 442 Character.isLetter(patchPath.charAt(1)) && 443 patchPath.charAt(2) == ':') { 444 patchPath=patchPath.substring(1,3)+"/"+patchPath.substring(3); 445 } 446 447 if (patchPath.length() >= 2 && 449 Character.isLetter(patchPath.charAt(0)) && 450 patchPath.charAt(1) == ':') { 451 char[] ca = patchPath.replace('/', '\\').toCharArray(); 452 char c; 453 StringBuffer sb = new StringBuffer (); 454 455 for (int i = 0; i < ca.length; i++) { 456 if ((ca[i] != '\\') || 457 (ca[i] == '\\' && 458 i > 0 && 459 ca[i - 1] != '\\')) { 460 if (i == 0 && 461 Character.isLetter(ca[i]) && 462 i < ca.length - 1 && 463 ca[i + 1] == ':') { 464 c = Character.toUpperCase(ca[i]); 465 } else { 466 c = ca[i]; 467 } 468 469 sb.append(c); 470 } 471 } 472 473 patchPath = sb.toString(); 474 } 475 476 if (System.getProperty("os.name").startsWith("NetWare") && 478 path.length() >=3 && 479 path.indexOf(':') > 0) { 480 char[] ca = patchPath.replace('/', '\\').toCharArray(); 481 StringBuffer sb = new StringBuffer (); 482 483 for (int i = 0; i < ca.length; i++) { 484 if ((ca[i] != '\\') || 485 (ca[i] == '\\' && i > 0 && ca[i - 1] != '\\')) { 486 sb.append(ca[i]); 487 } 488 } 489 patchPath = sb.toString(); 490 } 491 492 return patchPath; 493 } 494 495 public static boolean isAbsolute( String path ) { 496 if( path.startsWith("/" ) ) return true; 498 499 if( path.startsWith(File.separator ) ) return true; 500 501 if (path.length() >= 3 && 503 Character.isLetter(path.charAt(0)) && 504 path.charAt(1) == ':') 505 return true; 506 507 if (System.getProperty("os.name").startsWith("NetWare") && 509 path.length() >=3 && 510 path.indexOf(':') > 0) 511 return true; 512 513 return false; 514 } 515 } 516 | Popular Tags |