1 16 17 package org.apache.ajp.tomcat4.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 95 private static org.apache.commons.logging.Log log= 96 org.apache.commons.logging.LogFactory.getLog( BaseJkConfig.class ); 97 98 protected int debug=0; 99 protected File configHome = null; 100 protected File workersConfig = null; 101 102 protected File jkLog = null; 103 protected String jkDebug="emerg"; 104 protected String jkWorker = "ajp13"; 105 106 protected boolean noRoot=true; 107 protected boolean forwardAll=true; 108 109 protected String tomcatHome; 110 protected boolean regenerate=false; 111 protected boolean append=false; 112 113 115 116 119 122 public void lifecycleEvent(LifecycleEvent evt) 123 { 124 if(Lifecycle.START_EVENT.equals(evt.getType())) { 125 execute( evt ); 126 } 127 } 128 129 132 public void execute(LifecycleEvent evt) 133 { 134 initProperties(); 135 PrintWriter mod_jk = null; 136 try { 137 mod_jk = getWriter(); 138 } catch(IOException iex) { 139 log.warn("Unable to open config file", iex); 140 return; 141 } 142 Lifecycle who = evt.getLifecycle(); 143 if( who instanceof Server ) { 144 executeServer((Server)who, mod_jk); 145 } else if ( who instanceof Host ) { 146 executeHost((Host)who, mod_jk); 147 } else if( who instanceof Context ) { 148 executeContext((Context)who, mod_jk); 149 } 150 mod_jk.close(); 151 } 152 155 public void executeServer(Server svr, PrintWriter mod_jk) { 156 if(! append ) { 157 if( ! generateJkHead(mod_jk) ) 158 return; 159 generateSSLConfig(mod_jk); 160 generateJkTail(mod_jk); 161 } 162 } 163 164 166 protected void generateSSLConfig(PrintWriter mod_jk) 167 { 168 } 169 171 protected boolean generateJkHead(PrintWriter mod_jk) 172 { 173 return true; 174 } 175 177 protected void generateJkTail(PrintWriter mod_jk) 178 { 179 } 180 182 protected void generateVhostHead(Host host, PrintWriter mod_jk) { 183 } 184 186 protected void generateVhostTail(Host host, PrintWriter mod_jk) { 187 } 188 191 protected void executeEngine(Engine egn, PrintWriter mod_jk) { 192 Container [] children = egn.findChildren(); 193 for(int ii=0; ii < children.length; ii++) { 194 if( children[ii] instanceof Host ) { 195 executeHost((Host)children[ii], mod_jk); 196 } else if( children[ii] instanceof Context ) { 197 executeContext((Context)children[ii], mod_jk); 198 } 199 } 200 } 201 204 protected void executeHost(Host hst, PrintWriter mod_jk) { 205 generateVhostHead(hst, mod_jk); 206 Container [] children = hst.findChildren(); 207 for(int ii=0; ii < children.length; ii++) { 208 if(children[ii] instanceof Context) { 209 executeContext((Context)children[ii],mod_jk); 210 } 211 } 212 generateVhostTail(hst, mod_jk); 213 } 214 221 public void executeContext(Context context, PrintWriter mod_jk){ 222 223 if(context.getPath().length() > 0 || ! noRoot ) { 224 String docRoot = context.getServletContext().getRealPath("/"); 225 if( forwardAll || docRoot == null) 226 generateStupidMappings( context, mod_jk ); 227 else 228 generateContextMappings( context, mod_jk); 229 } 230 } 231 protected void generateStupidMappings(Context context, PrintWriter mod_jk){ 232 } 233 protected void generateContextMappings(Context context, PrintWriter mod_jk){ 234 } 235 238 protected PrintWriter getWriter() throws IOException { 239 return null; 240 } 241 243 protected Host getHost(Container child) { 244 while(child != null && ! (child instanceof Host) ) { 245 child = child.getParent(); 246 } 247 return (Host)child; 248 } 249 250 252 256 public void setAppend(boolean apnd) { 257 append = apnd; 258 } 259 264 public void setForwardAll( boolean b ) { 265 forwardAll=b; 266 } 267 268 277 public void setNoRoot( boolean b ) { 278 noRoot=b; 279 } 280 281 297 public void setConfigHome(String dir){ 298 if( dir==null ) return; 299 File f=new File (dir); 300 if(!f.isDirectory()){ 301 throw new IllegalArgumentException ( 302 "BaseConfig.setConfigHome(): "+ 303 "Configuration Home must be a directory! : "+dir); 304 } 305 configHome = f; 306 } 307 308 312 public void setWorkersConfig(String path){ 313 workersConfig= (path==null?null:new File (path)); 314 } 315 316 320 public void setJkLog(String path){ 321 jkLog= ( path==null?null:new File (path)); 322 } 323 324 327 public void setJkDebug( String level ) { 328 jkDebug=level; 329 } 330 331 335 public void setJkWorker(String worker){ 336 jkWorker = worker; 337 } 338 339 341 344 protected void initProperties() { 345 tomcatHome = System.getProperty("catalina.home"); 346 File tomcatDir = new File (tomcatHome); 347 if(configHome==null){ 348 configHome=tomcatDir; 349 } 350 } 351 352 354 355 358 protected boolean addExtensionMapping( String ctxPath, String ext, 359 PrintWriter pw ) 360 { 361 return true; 362 } 363 364 365 368 protected boolean addMapping( String fullPath, PrintWriter pw ) { 369 return true; 370 } 371 372 374 protected String getAbsoluteDocBase(Context context) 375 { 376 String docBase = context.getServletContext().getRealPath("/"); 378 docBase = docBase.substring(0,docBase.length()-1); 379 if (!isAbsolute(docBase)){ 380 docBase = tomcatHome + "/" + docBase; 381 } 382 docBase = patch(docBase); 383 return docBase; 384 } 385 386 public static File getConfigFile( File base, File configDir, String defaultF ) 388 { 389 if( base==null ) 390 base=new File ( defaultF ); 391 if( ! base.isAbsolute() ) { 392 if( configDir != null ) 393 base=new File ( configDir, base.getPath()); 394 else 395 base=new File ( base.getAbsolutePath()); } 397 File parent=new File (base.getParent()); 398 if(!parent.exists()){ 399 if(!parent.mkdirs()){ 400 throw new RuntimeException ( 401 "Unable to create path to config file :"+ 402 base.getAbsolutePath()); 403 } 404 } 405 return base; 406 } 407 public static String patch(String path) { 408 String patchPath = path; 409 410 if (patchPath.length() >= 3 && 412 patchPath.charAt(0) == '/' && 413 Character.isLetter(patchPath.charAt(1)) && 414 patchPath.charAt(2) == ':') { 415 patchPath=patchPath.substring(1,3)+"/"+patchPath.substring(3); 416 } 417 418 if (patchPath.length() >= 2 && 420 Character.isLetter(patchPath.charAt(0)) && 421 patchPath.charAt(1) == ':') { 422 char[] ca = patchPath.replace('/', '\\').toCharArray(); 423 char c; 424 StringBuffer sb = new StringBuffer (); 425 426 for (int i = 0; i < ca.length; i++) { 427 if ((ca[i] != '\\') || 428 (ca[i] == '\\' && 429 i > 0 && 430 ca[i - 1] != '\\')) { 431 if (i == 0 && 432 Character.isLetter(ca[i]) && 433 i < ca.length - 1 && 434 ca[i + 1] == ':') { 435 c = Character.toUpperCase(ca[i]); 436 } else { 437 c = ca[i]; 438 } 439 440 sb.append(c); 441 } 442 } 443 444 patchPath = sb.toString(); 445 } 446 447 if (System.getProperty("os.name").startsWith("NetWare") && 449 path.length() >=3 && 450 path.indexOf(':') > 0) { 451 char[] ca = patchPath.replace('/', '\\').toCharArray(); 452 StringBuffer sb = new StringBuffer (); 453 454 for (int i = 0; i < ca.length; i++) { 455 if ((ca[i] != '\\') || 456 (ca[i] == '\\' && i > 0 && ca[i - 1] != '\\')) { 457 sb.append(ca[i]); 458 } 459 } 460 patchPath = sb.toString(); 461 } 462 463 return patchPath; 464 } 465 466 public static boolean isAbsolute( String path ) { 467 if( path.startsWith("/" ) ) return true; 469 470 if( path.startsWith(File.separator ) ) return true; 471 472 if (path.length() >= 3 && 474 Character.isLetter(path.charAt(0)) && 475 path.charAt(1) == ':') 476 return true; 477 478 if (System.getProperty("os.name").startsWith("NetWare") && 480 path.length() >=3 && 481 path.indexOf(':') > 0) 482 return true; 483 484 return false; 485 } 486 487 protected void log(String msg) { 488 log.info(msg); 489 } 490 } 491 | Popular Tags |