1 package org.apache.turbine.services.pull; 2 3 56 57 import java.util.ArrayList ; 58 import java.util.Iterator ; 59 import java.util.List ; 60 61 import org.apache.commons.configuration.Configuration; 62 import org.apache.fulcrum.BaseService; 63 import org.apache.fulcrum.InitializationException; 64 import org.apache.fulcrum.TurbineServices; 65 import org.apache.fulcrum.factory.FactoryService; 66 import org.apache.fulcrum.pool.PoolService; 67 import org.apache.fulcrum.security.entity.User; 68 import org.apache.turbine.RunData; 69 import org.apache.turbine.TemplateContext; 70 import org.apache.turbine.modules.DefaultTemplateContext; 71 import org.apache.turbine.services.yaaficomponent.YaafiComponentService; 72 73 147 public class TurbinePullService 148 extends BaseService 149 implements PullService 150 { 151 159 private TemplateContext globalContext; 160 161 165 protected static class ToolData 166 { 167 protected String toolName; 168 protected String toolClassName; 169 170 public ToolData(String toolName, String toolClassName) 171 { 172 this.toolName = toolName; 173 this.toolClassName = toolClassName; 174 } 175 } 176 177 182 private List globalTools; 183 private List requestTools; 184 private List sessionTools; 185 private List persistentTools; 186 187 192 private static final String GLOBAL_TOOL = "tool.global"; 193 private static final String REQUEST_TOOL = "tool.request"; 194 private static final String SESSION_TOOL = "tool.session"; 195 private static final String PERSISTENT_TOOL = "tool.persistent"; 196 197 200 private static String resourcesDirectory; 201 202 206 private static String absolutePathToResourcesDirectory; 207 208 211 private static final String TOOL_RESOURCES_DIR 212 = "tools.resources.dir"; 213 214 219 private static final String TOOL_RESOURCES_DIR_DEFAULT 220 = "/resources"; 221 222 226 private static final String TOOLS_PER_REQUEST_REFRESH = 227 "toolsPerRequestRefresh"; 228 229 233 private static boolean refreshToolsPerRequest; 234 235 238 public void init() 239 throws InitializationException 240 { 241 try 242 { 243 resourcesDirectory = getConfiguration().getString( 246 TOOL_RESOURCES_DIR, 247 TOOL_RESOURCES_DIR_DEFAULT); 248 249 absolutePathToResourcesDirectory = getRealPath(resourcesDirectory); 255 256 refreshToolsPerRequest = getConfiguration() 259 .getBoolean(TOOLS_PER_REQUEST_REFRESH); 260 261 if (refreshToolsPerRequest) 264 { 265 getCategory().info("Pull Model tools will " 266 + "be refreshed on a per request basis."); 267 } 268 269 setInit(true); 273 274 globalTools = getTools(GLOBAL_TOOL); 277 requestTools = getTools(REQUEST_TOOL); 278 sessionTools = getTools(SESSION_TOOL); 279 persistentTools = getTools(PERSISTENT_TOOL); 280 281 globalContext = new DefaultTemplateContext(); 283 populateWithGlobalTools(globalContext); 284 } 285 catch (Exception e) 286 { 287 throw new InitializationException( 288 "TurbinePullService failed to initialize", e); 289 } 290 } 291 292 298 protected List getTools(String keyPrefix) 299 { 300 List classes = new ArrayList (); 301 302 Configuration toolResources = getConfiguration().subset(keyPrefix); 303 304 if (toolResources == null) 307 { 308 return classes; 309 } 310 311 Iterator it = toolResources.getKeys(); 312 while (it.hasNext()) 313 { 314 String toolName = (String ) it.next(); 315 String toolClassName = toolResources.getString(toolName); 316 317 try 318 { 319 classes.add(new ToolData(toolName, toolClassName)); 321 322 getCategory().info("Setup tool class " + toolClassName 323 + " to add to the context as '$" + toolName + "'"); 324 } 325 catch (Exception e) 326 { 327 getCategory().error("Cannot find tool class " + toolClassName 328 + ", please check the name of the class."); 329 } 330 } 331 332 return classes; 333 } 334 335 340 public TemplateContext getGlobalContext() 341 { 342 return globalContext; 343 } 344 345 352 public TemplateContext getRuntimeContext(RunData data) 353 { 354 TemplateContext tc = new DefaultTemplateContext(); 355 populateContext(tc, data); 356 357 Iterator i = getGlobalContext().keySet().iterator(); 362 while (i.hasNext()) 363 { 364 String key = (String )i.next(); 365 tc.put (key, getGlobalContext().get(key)); 366 } 367 return tc; 368 } 369 370 379 public void populateContext(TemplateContext context, RunData data) 380 { 381 populateWithRequestTools(context, data); 382 383 User user = data.getUser(); 388 if (user != null) 389 { 390 populateWithSessionTools(sessionTools, context, user, false); 391 392 if (user.hasLoggedIn()) 393 { 394 populateWithSessionTools(persistentTools, context, user, true); 395 } 396 } 397 } 398 399 406 private void populateWithGlobalTools(TemplateContext context) 407 { 408 FactoryService factoryService = getFactoryService(); 410 Iterator it = globalTools.iterator(); 411 while (it.hasNext()) 412 { 413 ToolData toolData = (ToolData)it.next(); 414 try 415 { 416 Object tool = 417 factoryService.getInstance(toolData.toolClassName); 418 if (tool instanceof ApplicationTool) 419 { 420 ((ApplicationTool)tool).init(null); 421 } 422 context.put(toolData.toolName, tool); 424 } 425 catch (Exception e) 426 { 427 getCategory().error( 428 "Could not instantiate tool " + toolData.toolClassName 429 + " to add to the context" , e ); 430 } 431 } 432 } 433 434 440 private void populateWithRequestTools(TemplateContext context, RunData data) 441 { 442 PoolService pool = getPoolService(); 444 445 Iterator it = requestTools.iterator(); 447 while (it.hasNext()) 448 { 449 ToolData toolData = (ToolData)it.next(); 450 try 451 { 452 Object tool = pool.getInstance(Class.forName(toolData.toolClassName)); 453 if (tool instanceof ApplicationTool) 454 { 455 ((ApplicationTool)tool).init(data); 457 } 458 context.put(toolData.toolName, tool); 460 } 461 catch (Exception e) 462 { 463 getCategory().error( 464 "Could not instantiate tool " + toolData.toolClassName 465 + " to add to the context",e); 466 } 467 } 468 } 469 470 481 private void populateWithSessionTools(List tools, 482 TemplateContext context, 483 User user, 484 boolean usePerm) 485 { 486 PoolService pool = getPoolService(); 488 489 Iterator it = tools.iterator(); 491 while (it.hasNext()) 492 { 493 ToolData toolData = (ToolData)it.next(); 494 try 495 { 496 synchronized (user) 499 { 500 Object tool = usePerm 503 ? user.getPerm(toolData.toolClassName) 504 : user.getTemp(toolData.toolClassName); 505 506 if (tool == null) 507 { 508 tool = pool.getInstance(Class.forName(toolData.toolClassName)); 511 if (tool instanceof ApplicationTool) 512 { 513 ((ApplicationTool)tool).init(user); 515 } 516 if (usePerm) 518 { 519 user.setPerm(toolData.toolClassName, tool); 520 } 521 else 522 { 523 user.setTemp(toolData.toolClassName, tool); 524 } 525 } 526 else if (refreshToolsPerRequest 527 && tool instanceof ApplicationTool) 528 { 529 ((ApplicationTool)tool).refresh(); 530 } 531 context.put(toolData.toolName, tool); 533 } 534 } 535 catch (Exception e) 536 { 537 getCategory().error("Could not instantiate tool " + 538 toolData.toolClassName + " to add to the context"); 539 } 540 } 541 } 542 543 547 public String getAbsolutePathToResourcesDirectory() 548 { 549 return absolutePathToResourcesDirectory; 550 } 551 552 556 public String getResourcesDirectory() 557 { 558 return resourcesDirectory; 559 } 560 561 568 public void refreshGlobalTools() 569 { 570 Iterator i = globalTools.iterator(); 571 while (i.hasNext()) 572 { 573 ToolData toolData = (ToolData)i.next(); 574 Object tool = globalContext.get(toolData.toolName); 575 if (tool instanceof ApplicationTool) 576 { 577 ((ApplicationTool)tool).refresh(); 578 } 579 } 580 } 581 582 586 public boolean refreshToolsPerRequest() 587 { 588 return refreshToolsPerRequest; 589 } 590 591 597 public void releaseTools(TemplateContext context) 598 { 599 PoolService pool = getPoolService(); 601 602 releaseTools(context, pool, requestTools); 605 } 606 607 615 private void releaseTools(TemplateContext context, PoolService pool, 616 List tools) 617 { 618 Iterator it = tools.iterator(); 619 while (it.hasNext()) 620 { 621 ToolData toolData = (ToolData)it.next(); 622 Object tool = context.get(toolData.toolName); 629 context.remove(toolData.toolName); 630 if (tool != null) 631 { 632 pool.putInstance(tool); 633 } 634 } 635 } 636 637 638 643 protected static PoolService getPoolService() { 644 try { 645 YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService( 646 YaafiComponentService.SERVICE_NAME); 647 return (PoolService) yaafi.lookup(PoolService.class.getName()); 648 } catch (Exception e) { 649 throw new RuntimeException ("Problem looking up pool service", e); 650 } 651 } 652 653 658 protected static FactoryService getFactoryService() { 659 try { 660 YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService( 661 YaafiComponentService.SERVICE_NAME); 662 return (FactoryService) yaafi.lookup(FactoryService.class.getName()); 663 } catch (Exception e) { 664 throw new RuntimeException ("Problem looking up Factory service", e); 665 } 666 } 667 } 668 | Popular Tags |