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
|
/*
* 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.IOException;
import java.io.PrintWriter;
import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.Host;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Server;
/**
Base class for automatic jk based configurations based on
the Tomcat server.xml settings and the war contexts
initialized during startup.
<p>
This config interceptor is enabled by inserting a Config
element in the <b><ContextManager></b> tag body inside
the server.xml file like so:
<pre>
* < ContextManager ... >
* ...
* <<b>???Config</b> <i>options</i> />
* ...
* < /ContextManager >
</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>workersConfig</b> - path to workers.properties file used by
jk connector. If not set, defaults to
"conf/jk/workers.properties".</li>
<li><b>jkLog</b> - path to log file to be used by jk connector.</li>
<li><b>jkDebug</b> - 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 may also
need to modify the web server to point it's home
directory to Tomcat's root context directory.
Otherwise some content, such as the root index.html,
may be served by the web server before the connector
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 Bill Barker
@version $Revision: 467222 $
*/
public class BaseJkConfig implements LifecycleListener {
private static org.apache.juli.logging.Log log =
org.apache.juli.logging.LogFactory.getLog(BaseJkConfig.class);
protected File configHome = null;
protected File workersConfig = null;
protected File jkLog = null;
protected String jkDebug="emerg";
protected String jkWorker = "ajp13";
protected boolean noRoot=true;
protected boolean forwardAll=true;
protected String tomcatHome;
protected boolean regenerate=false;
protected boolean append=false;
protected boolean legacy=true;
// -------------------- Tomcat callbacks --------------------
// Auto-config should be able to react to dynamic config changes,
// and regenerate the config.
/**
* Generate the configuration - only when the server is
* completely initialized ( before starting )
*/
public void lifecycleEvent(LifecycleEvent evt) {
if(Lifecycle.START_EVENT.equals(evt.getType())) {
execute( evt );
}
}
/**
* Generate configuration files. Override with method to generate
* web server specific configuration.
*/
public void execute(LifecycleEvent evt) {
initProperties();
PrintWriter mod_jk = null;
try {
mod_jk = getWriter();
} catch(IOException iex) {
log.warn("Unable to open config file");
return;
}
Lifecycle who = evt.getLifecycle();
if( who instanceof Server ) {
executeServer((Server)who, mod_jk);
} else if(who instanceof Engine) {
executeEngine((Engine)who, mod_jk);
} else if ( who instanceof Host ) {
executeHost((Host)who, mod_jk);
} else if( who instanceof Context ) {
executeContext((Context)who, mod_jk);
}
mod_jk.close();
}
/**
* Generate configuration files. Override with method to generate
* web server specific configuration.
*/
public void executeServer(Server svr, PrintWriter mod_jk) {
if(! append ) {
if( ! generateJkHead(mod_jk) )
return;
generateSSLConfig(mod_jk);
generateJkTail(mod_jk);
}
}
/**
* Generate SSL options
*/
protected void generateSSLConfig(PrintWriter mod_jk) {
}
/**
* Generate general options
*/
protected boolean generateJkHead(PrintWriter mod_jk) {
return true;
}
/**
* Generate general options
*/
protected void generateJkTail(PrintWriter mod_jk) {
}
/**
* Generate Virtual Host start
*/
protected void generateVhostHead(Host host, PrintWriter mod_jk) {
}
/**
* Generate Virtual Host end
*/
protected void generateVhostTail(Host host, PrintWriter mod_jk) {
}
/**
* Generate configuration files. Override with method to generate
* web server specific configuration.
*/
protected void executeEngine(Engine egn, PrintWriter mod_jk) {
if(egn.getJvmRoute() != null) {
jkWorker = egn.getJvmRoute();
}
executeServer(egn.getService().getServer(), mod_jk);
Container [] children = egn.findChildren();
for(int ii=0; ii < children.length; ii++) {
if( children[ii] instanceof Host ) {
executeHost((Host)children[ii], mod_jk);
} else if( children[ii] instanceof Context ) {
executeContext((Context)children[ii], mod_jk);
}
}
}
/**
* Generate configuration files. Override with method to generate
* web server specific configuration.
*/
protected void executeHost(Host hst, PrintWriter mod_jk) {
generateVhostHead(hst, mod_jk);
Container [] children = hst.findChildren();
for(int ii=0; ii < children.length; ii++) {
if(children[ii] instanceof Context) {
executeContext((Context)children[ii],mod_jk);
}
}
generateVhostTail(hst, mod_jk);
}
/**
* executes the ApacheConfig interceptor. This method generates apache
* configuration files for use with mod_jk.
* @param context a Context object.
* @param mod_jk Writer for output.
*/
public void executeContext(Context context, PrintWriter mod_jk){
if(context.getPath().length() > 0 || ! noRoot ) {
String docRoot = context.getServletContext().getRealPath("/");
if( forwardAll || docRoot == null)
generateStupidMappings( context, mod_jk );
else
generateContextMappings( context, mod_jk);
}
}
protected void generateStupidMappings(Context context, PrintWriter mod_jk){
}
protected void generateContextMappings(Context context, PrintWriter mod_jk){
}
/**
* Get the output Writer. Override with method to generate
* web server specific configuration.
*/
protected PrintWriter getWriter() throws IOException {
return null;
}
/**
* Get the host associated with this Container (if any).
*/
protected Host getHost(Container child) {
while(child != null && ! (child instanceof Host) ) {
child = child.getParent();
}
return (Host)child;
}
//-------------------- Properties --------------------
/**
* Append to config file.
* Set to <code>true</code> if the config information should be
* appended.
*/
public void setAppend(boolean apnd) {
append = apnd;
}
/**
* If false, we'll try to generate a config that will
* let apache serve static files.
* The default is true, forward all requests in a context
* to tomcat.
*/
public void setForwardAll( boolean b ) {
forwardAll=b;
}
/**
* Special option - do not generate mappings for the ROOT
* context. The default is true, and will not generate the mappings,
* not redirecting all pages to tomcat (since /* matches everything).
* This means that the web server's root remains intact but isn't
* completely servlet/JSP enabled. If the ROOT webapp can be configured
* with the web server serving static files, there's no problem setting
* this option to false. If not, then setting it true means the web
* server will be out of picture for all requests.
*/
public void setNoRoot( boolean b ) {
noRoot=b;
}
/**
* set a path to the parent directory of the
* conf folder. That is, the parent directory
* within which path setters would be resolved against,
* if relative. For example if ConfigHome is set to "/home/tomcat"
* and regConfig is set to "conf/mod_jk.conf" then the resulting
* path used would be:
* "/home/tomcat/conf/mod_jk.conf".</p>
* <p>
* However, if the path is set to an absolute path,
* this attribute is ignored.
* <p>
* If not set, execute() will set this to TOMCAT_HOME.
* @param dir - path to a directory
*/
public void setConfigHome(String dir){
if( dir==null ) return;
File f=new File(dir);
if(!f.isDirectory()){
throw new IllegalArgumentException(
"BaseConfig.setConfigHome(): "+
"Configuration Home must be a directory! : "+dir);
}
configHome = f;
}
/**
* set a path to the workers.properties file.
* @param path String path to workers.properties file
*/
public void setWorkersConfig(String path){
workersConfig= (path==null?null:new File(path));
}
/**
* set the path to the log file
* @param path String path to a file
*/
public void setJkLog(String path){
jkLog = ( path==null ? null : new File(path));
}
/**
* Set the verbosity level
* ( use debug, error, etc. ) If not set, no log is written.
*/
public void setJkDebug( String level ) {
jkDebug=level;
}
/**
* Sets the JK worker.
* @param worker The worker
*/
public void setJkWorker(String worker){
jkWorker = worker;
}
public void setLegacy(boolean legacy) {
this.legacy = legacy;
}
// -------------------- Initialize/guess defaults --------------------
/**
* Initialize defaults for properties that are not set
* explicitely
*/
protected void initProperties() {
tomcatHome = System.getProperty("catalina.home");
File tomcatDir = new File(tomcatHome);
if(configHome==null){
configHome=tomcatDir;
}
}
// -------------------- Config Utils --------------------
/**
* Add an extension mapping. Override with method to generate
* web server specific configuration
*/
protected boolean addExtensionMapping( String ctxPath, String ext,
PrintWriter pw ) {
return true;
}
/**
* Add a fulling specified mapping. Override with method to generate
* web server specific configuration
*/
protected boolean addMapping( String fullPath, PrintWriter pw ) {
return true;
}
// -------------------- General Utils --------------------
protected String getAbsoluteDocBase(Context context) {
// Calculate the absolute path of the document base
String docBase = context.getServletContext().getRealPath("/");
docBase = docBase.substring(0,docBase.length()-1);
if (!isAbsolute(docBase)){
docBase = tomcatHome + "/" + docBase;
}
docBase = patch(docBase);
return docBase;
}
// ------------------ Grabbed from FileUtil -----------------
public static File getConfigFile( File base, File configDir, String defaultF )
{
if( base==null )
base=new File( defaultF );
if( ! base.isAbsolute() ) {
if( configDir != null )
base=new File( configDir, base.getPath());
else
base=new File( base.getAbsolutePath()); //??
}
File parent=new File(base.getParent());
if(!parent.exists()){
if(!parent.mkdirs()){
throw new RuntimeException(
"Unable to create path to config file :"+
base.getAbsolutePath());
}
}
return base;
}
public static String patch(String path) {
String patchPath = path;
// Move drive spec to the front of the path
if (patchPath.length() >= 3 &&
patchPath.charAt(0) == '/' &&
Character.isLetter(patchPath.charAt(1)) &&
patchPath.charAt(2) == ':') {
patchPath=patchPath.substring(1,3)+"/"+patchPath.substring(3);
}
// Eliminate consecutive slashes after the drive spec
if (patchPath.length() >= 2 &&
Character.isLetter(patchPath.charAt(0)) &&
patchPath.charAt(1) == ':') {
char[] ca = patchPath.replace('/', '\\').toCharArray();
char c;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ca.length; i++) {
if ((ca[i] != '\\') ||
(ca[i] == '\\' &&
i > 0 &&
ca[i - 1] != '\\')) {
if (i == 0 &&
Character.isLetter(ca[i]) &&
i < ca.length - 1 &&
ca[i + 1] == ':') {
c = Character.toUpperCase(ca[i]);
} else {
c = ca[i];
}
sb.append(c);
}
}
patchPath = sb.toString();
}
// fix path on NetWare - all '/' become '\\' and remove duplicate '\\'
if (System.getProperty("os.name").startsWith("NetWare") &&
path.length() >=3 &&
path.indexOf(':') > 0) {
char[] ca = patchPath.replace('/', '\\').toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ca.length; i++) {
if ((ca[i] != '\\') ||
(ca[i] == '\\' && i > 0 && ca[i - 1] != '\\')) {
sb.append(ca[i]);
}
}
patchPath = sb.toString();
}
return patchPath;
}
public static boolean isAbsolute( String path ) {
// normal file
if( path.startsWith("/" ) ) return true;
if( path.startsWith(File.separator ) ) return true;
// win c:
if (path.length() >= 3 &&
Character.isLetter(path.charAt(0)) &&
path.charAt(1) == ':')
return true;
// NetWare volume:
if (System.getProperty("os.name").startsWith("NetWare") &&
path.length() >=3 &&
path.indexOf(':') > 0)
return true;
return false;
}
}
|