1 package org.apache.velocity.runtime; 2 3 18 19 import org.apache.velocity.Template; 20 import org.apache.velocity.runtime.directive.Directive; 21 import org.apache.velocity.runtime.directive.VelocimacroProxy; 22 23 import java.util.Vector ; 24 import java.util.Map ; 25 import java.util.HashMap ; 26 27 35 public class VelocimacroFactory 36 { 37 40 private RuntimeServices rsvc = null; 41 42 46 private VelocimacroManager vmManager = null; 47 48 52 private boolean replaceAllowed = false; 53 54 60 private boolean addNewAllowed = true; 61 62 65 private boolean templateLocal = false; 66 67 70 private boolean blather = false; 71 72 76 private boolean autoReloadLibrary = false; 77 78 81 private Vector macroLibVec = null; 82 83 87 private Map libModMap; 88 89 93 public VelocimacroFactory( RuntimeServices rs ) 94 { 95 this.rsvc = rs; 96 97 101 libModMap = new HashMap (); 102 vmManager = new VelocimacroManager( rsvc ); 103 } 104 105 109 public void initVelocimacro() 110 { 111 114 synchronized( this ) 115 { 116 119 setReplacementPermission( true ); 120 setBlather( true ); 121 122 logVMMessageInfo("Velocimacro : initialization starting."); 123 124 127 128 vmManager.setNamespaceUsage( false ); 129 130 135 136 Object libfiles = rsvc.getProperty( RuntimeConstants.VM_LIBRARY ); 137 138 if( libfiles != null) 139 { 140 if (libfiles instanceof Vector ) 141 { 142 macroLibVec = (Vector ) libfiles; 143 } 144 else if (libfiles instanceof String ) 145 { 146 macroLibVec = new Vector (); 147 macroLibVec.addElement( libfiles ); 148 } 149 150 for( int i = 0; i < macroLibVec.size(); i++) 151 { 152 String lib = (String ) macroLibVec.elementAt(i); 153 154 157 158 if (lib != null && !lib.equals("")) 159 { 160 164 165 vmManager.setRegisterFromLib( true ); 166 167 logVMMessageInfo("Velocimacro : adding VMs from " + 168 "VM library template : " + lib ); 169 170 try 171 { 172 Template template = rsvc.getTemplate( lib ); 173 174 179 180 Twonk twonk = new Twonk(); 181 twonk.template = template; 182 twonk.modificationTime = template.getLastModified(); 183 libModMap.put( lib, twonk ); 184 } 185 catch (Exception e) 186 { 187 logVMMessageInfo("Velocimacro : error using VM " + 188 "library template " + lib + " : " + e ); 189 } 190 191 logVMMessageInfo("Velocimacro : VM library template " + 192 "macro registration complete." ); 193 194 vmManager.setRegisterFromLib( false ); 195 } 196 } 197 } 198 199 202 203 209 setAddMacroPermission( true ); 210 211 if ( !rsvc.getBoolean( RuntimeConstants.VM_PERM_ALLOW_INLINE, true) ) 212 { 213 setAddMacroPermission( false ); 214 215 logVMMessageInfo("Velocimacro : allowInline = false : VMs can not " + 216 "be defined inline in templates"); 217 } 218 else 219 { 220 logVMMessageInfo("Velocimacro : allowInline = true : VMs can be " + 221 "defined inline in templates"); 222 } 223 224 230 setReplacementPermission( false ); 231 232 if ( rsvc.getBoolean( 233 RuntimeConstants.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, false) ) 234 { 235 setReplacementPermission( true ); 236 237 logVMMessageInfo("Velocimacro : allowInlineToOverride = true : VMs " + 238 "defined inline may replace previous VM definitions"); 239 } 240 else 241 { 242 logVMMessageInfo("Velocimacro : allowInlineToOverride = false : VMs " + 243 "defined inline may NOT replace previous VM definitions"); 244 } 245 246 250 vmManager.setNamespaceUsage( true ); 251 252 255 setTemplateLocalInline( rsvc.getBoolean( 256 RuntimeConstants.VM_PERM_INLINE_LOCAL, false) ); 257 258 if ( getTemplateLocalInline() ) 259 { 260 logVMMessageInfo("Velocimacro : allowInlineLocal = true : VMs " + 261 "defined inline will be local to their defining template only."); 262 } 263 else 264 { 265 logVMMessageInfo("Velocimacro : allowInlineLocal = false : VMs " + 266 "defined inline will be global in scope if allowed."); 267 } 268 269 vmManager.setTemplateLocalInlineVM( getTemplateLocalInline() ); 270 271 274 setBlather( rsvc.getBoolean( RuntimeConstants.VM_MESSAGES_ON, true )); 275 276 if (getBlather()) 277 { 278 logVMMessageInfo("Velocimacro : messages on : VM system " + 279 "will output logging messages"); 280 } 281 else 282 { 283 logVMMessageInfo("Velocimacro : messages off : VM system will be quiet"); 284 } 285 286 289 setAutoload( rsvc.getBoolean( RuntimeConstants.VM_LIBRARY_AUTORELOAD, false )); 290 291 if (getAutoload()) 292 { 293 logVMMessageInfo("Velocimacro : autoload on : VM system " + 294 "will automatically reload global library macros"); 295 } 296 else 297 { 298 logVMMessageInfo("Velocimacro : autoload off : VM system " + 299 "will not automatically reload global library macros"); 300 } 301 302 rsvc.info("Velocimacro : initialization complete."); 303 } 304 305 return; 306 } 307 308 311 public boolean addVelocimacro( String name, String macroBody, 312 String argArray[], String sourceTemplate ) 313 { 314 320 if ( name == null || macroBody == null || argArray == null || 321 sourceTemplate == null ) 322 { 323 logVMMessageWarn("Velocimacro : VM addition rejected : " + 324 "programmer error : arg null" ); 325 326 return false; 327 } 328 329 332 333 if (!canAddVelocimacro( name, sourceTemplate )) 334 { 335 return false; 336 } 337 338 341 synchronized( this ) 342 { 343 vmManager.addVM( name, macroBody, argArray, sourceTemplate ); 344 } 345 346 349 if ( blather) 350 { 351 String s = "#" + argArray[0]; 352 s += "("; 353 354 for( int i=1; i < argArray.length; i++) 355 { 356 s += " "; 357 s += argArray[i]; 358 } 359 360 s += " ) : source = "; 361 s += sourceTemplate; 362 363 logVMMessageInfo( "Velocimacro : added new VM : " + s ); 364 } 365 366 return true; 367 } 368 369 377 private boolean canAddVelocimacro( String name, String sourceTemplate) 378 { 379 383 384 if ( getAutoload() ) 385 { 386 389 390 for( int i = 0; i < macroLibVec.size(); i++) 391 { 392 String lib = (String ) macroLibVec.elementAt(i); 393 394 if (lib.equals( sourceTemplate ) ) 395 { 396 return true; 397 } 398 } 399 } 400 401 402 407 if (!addNewAllowed) 408 { 409 logVMMessageWarn("Velocimacro : VM addition rejected : " + name + 410 " : inline VMs not allowed." ); 411 412 return false; 413 } 414 415 418 if (!templateLocal) 419 { 420 428 if ( isVelocimacro( name, sourceTemplate ) && !replaceAllowed ) 429 { 430 logVMMessageWarn("Velocimacro : VM addition rejected : " 431 + name + " : inline not allowed to replace existing VM" ); 432 return false; 433 } 434 } 435 436 return true; 437 } 438 439 442 private void logVMMessageInfo( String s ) 443 { 444 if (blather) 445 rsvc.info( s ); 446 } 447 448 451 private void logVMMessageWarn( String s ) 452 { 453 if (blather) 454 rsvc.warn( s ); 455 } 456 457 460 public boolean isVelocimacro( String vm , String sourceTemplate ) 461 { 462 synchronized(this) 463 { 464 468 if (vmManager.get( vm, sourceTemplate ) != null) 469 return true; 470 } 471 return false; 472 } 473 474 479 public Directive getVelocimacro( String vmName, String sourceTemplate ) 480 { 481 VelocimacroProxy vp = null; 482 483 synchronized( this ) 484 { 485 488 489 vp = vmManager.get( vmName, sourceTemplate); 490 491 495 496 if ( vp != null && getAutoload() ) 497 { 498 502 503 String lib = vmManager.getLibraryName( vmName, sourceTemplate ); 504 505 if (lib != null) 506 { 507 try 508 { 509 512 513 Twonk tw = (Twonk) libModMap.get( lib ); 514 515 if ( tw != null) 516 { 517 Template template = tw.template; 518 519 525 526 long tt = tw.modificationTime; 527 long ft = template.getResourceLoader().getLastModified( template ); 528 529 if ( ft > tt ) 530 { 531 logVMMessageInfo("Velocimacro : autoload reload for VMs from " + 532 "VM library template : " + lib ); 533 534 541 542 tw.modificationTime = ft; 543 544 template = rsvc.getTemplate( lib ); 545 546 549 550 tw.template = template; 551 tw.modificationTime = template.getLastModified(); 552 553 558 } 559 } 560 } 561 catch (Exception e) 562 { 563 logVMMessageInfo("Velocimacro : error using VM " + 564 "library template " + lib + " : " + e ); 565 } 566 567 570 571 vp = vmManager.get( vmName, sourceTemplate); 572 } 573 } 574 } 575 576 return vp; 577 } 578 579 582 public boolean dumpVMNamespace( String namespace ) 583 { 584 return vmManager.dumpNamespace( namespace ); 585 } 586 587 593 private void setTemplateLocalInline( boolean b ) 594 { 595 templateLocal = b; 596 } 597 598 private boolean getTemplateLocalInline() 599 { 600 return templateLocal; 601 } 602 603 606 private boolean setAddMacroPermission( boolean arg ) 607 { 608 boolean b = addNewAllowed; 609 610 addNewAllowed = arg; 611 return b; 612 } 613 614 618 private boolean setReplacementPermission( boolean arg ) 619 { 620 boolean b = replaceAllowed; 621 replaceAllowed = arg; 622 return b; 623 } 624 625 628 private void setBlather( boolean b ) 629 { 630 blather = b; 631 } 632 633 636 private boolean getBlather() 637 { 638 return blather; 639 } 640 641 645 private void setAutoload( boolean b) 646 { 647 autoReloadLibrary = b; 648 } 649 650 654 private boolean getAutoload() 655 { 656 return autoReloadLibrary; 657 } 658 659 667 private class Twonk 668 { 669 public Template template; 670 public long modificationTime; 671 } 672 } 673 674 675 676 677 678 679 680 | Popular Tags |