1 16 package org.apache.cocoon.portlet; 17 18 import org.apache.avalon.excalibur.logger.Log4JLoggerManager; 19 import org.apache.avalon.excalibur.logger.LogKitLoggerManager; 20 import org.apache.avalon.excalibur.logger.LoggerManager; 21 import org.apache.avalon.framework.activity.Disposable; 22 import org.apache.avalon.framework.activity.Initializable; 23 import org.apache.avalon.framework.component.ComponentManager; 24 import org.apache.avalon.framework.configuration.Configurable; 25 import org.apache.avalon.framework.configuration.Configuration; 26 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 27 import org.apache.avalon.framework.container.ContainerUtil; 28 import org.apache.avalon.framework.context.Contextualizable; 29 import org.apache.avalon.framework.context.DefaultContext; 30 import org.apache.avalon.framework.logger.LogEnabled; 31 import org.apache.avalon.framework.logger.LogKitLogger; 32 import org.apache.avalon.framework.logger.Logger; 33 34 import org.apache.cocoon.Cocoon; 35 import org.apache.cocoon.ConnectionResetException; 36 import org.apache.cocoon.Constants; 37 import org.apache.cocoon.ResourceNotFoundException; 38 import org.apache.cocoon.components.notification.DefaultNotifyingBuilder; 39 import org.apache.cocoon.components.notification.Notifier; 40 import org.apache.cocoon.components.notification.Notifying; 41 import org.apache.cocoon.environment.Environment; 42 import org.apache.cocoon.environment.portlet.PortletContext; 43 import org.apache.cocoon.environment.portlet.PortletEnvironment; 44 import org.apache.cocoon.portlet.multipart.MultipartActionRequest; 45 import org.apache.cocoon.portlet.multipart.RequestFactory; 46 import org.apache.cocoon.util.ClassUtils; 47 import org.apache.cocoon.util.IOUtils; 48 import org.apache.cocoon.util.StringUtils; 49 import org.apache.cocoon.util.log.CocoonLogFormatter; 50 import org.apache.cocoon.util.log.Log4JConfigurator; 51 52 import org.apache.commons.lang.BooleanUtils; 53 import org.apache.commons.lang.SystemUtils; 54 import org.apache.excalibur.instrument.InstrumentManager; 55 import org.apache.excalibur.instrument.manager.impl.DefaultInstrumentManagerImpl; 56 import org.apache.log.ContextMap; 57 import org.apache.log.ErrorHandler; 58 import org.apache.log.Hierarchy; 59 import org.apache.log.Priority; 60 import org.apache.log.util.DefaultErrorHandler; 61 import org.apache.log4j.LogManager; 62 63 import javax.portlet.ActionRequest; 64 import javax.portlet.ActionResponse; 65 import javax.portlet.GenericPortlet; 66 import javax.portlet.PortletConfig; 67 import javax.portlet.PortletException; 68 import javax.portlet.PortletSession; 69 import javax.portlet.RenderRequest; 70 import javax.portlet.RenderResponse; 71 import javax.portlet.PortletRequest; 72 import java.io.File ; 73 import java.io.FileInputStream ; 74 import java.io.FileOutputStream ; 75 import java.io.IOException ; 76 import java.io.InputStream ; 77 import java.io.OutputStream ; 78 import java.io.PrintStream ; 79 import java.lang.reflect.Constructor ; 80 import java.net.MalformedURLException ; 81 import java.net.SocketException ; 82 import java.net.URL ; 83 import java.util.ArrayList ; 84 import java.util.Arrays ; 85 import java.util.HashMap ; 86 import java.util.Iterator ; 87 import java.util.List ; 88 import java.util.StringTokenizer ; 89 import java.util.jar.Attributes ; 90 import java.util.jar.Manifest ; 91 92 97 public class CocoonPortlet extends GenericPortlet { 98 99 103 public static final String CONTEXT_PORTLET_CONFIG = "portlet-config"; 104 105 protected static final String PROCESSED_BY = "Processed by " 107 + Constants.COMPLETE_NAME + " in "; 108 109 static final float SECOND = 1000; 111 static final float MINUTE = 60 * SECOND; 112 static final float HOUR = 60 * MINUTE; 113 114 private Logger log; 115 private LoggerManager loggerManager; 116 117 120 protected long creationTime = 0; 121 122 125 protected Cocoon cocoon; 126 127 130 protected Exception exception; 131 132 135 protected DefaultContext appContext = new DefaultContext(); 136 137 138 141 protected static final boolean ALLOW_RELOAD = false; 142 143 146 protected boolean allowReload; 147 148 149 152 protected boolean showTime; 153 154 157 protected boolean hiddenShowTime; 158 159 160 163 private static final boolean ENABLE_UPLOADS = false; 164 private static final boolean SAVE_UPLOADS_TO_DISK = true; 165 private static final int MAX_UPLOAD_SIZE = 10000000; 167 170 private boolean enableUploads; 171 private boolean autoSaveUploads; 172 private boolean allowOverwrite; 173 private boolean silentlyRename; 174 private int maxUploadSize; 175 176 private File uploadDir; 177 private File workDir; 178 private File cacheDir; 179 private String containerEncoding; 180 private String defaultFormEncoding; 181 182 protected javax.portlet.PortletContext portletContext; 183 184 185 protected ClassLoader classLoader = this.getClass().getClassLoader(); 186 protected boolean initClassLoader = false; 187 188 private String parentComponentManagerClass; 189 private String parentComponentManagerInitParam; 190 191 192 private ComponentManager parentComponentManager; 193 194 protected String forceLoadParameter; 195 protected String forceSystemProperty; 196 197 201 private boolean manageExceptions; 202 203 206 private boolean enableInstrumentation; 207 208 211 private InstrumentManager instrumentManager; 212 213 218 protected String portletContextPath; 219 220 223 protected String portletContextURL; 224 225 229 protected RequestFactory requestFactory; 230 231 236 protected String servletPath; 237 238 247 protected int defaultSessionScope; 248 249 252 protected boolean storeSessionPath; 253 254 272 public void init(PortletConfig conf) throws PortletException { 273 274 super.init(conf); 275 276 if (!this.initClassLoader) { 280 this.initClassLoader = getInitParameterAsBoolean("init-classloader", false); 281 } 282 283 if (this.initClassLoader) { 284 try { 287 Thread.currentThread().setContextClassLoader(this.classLoader); 288 } catch (Exception e) { 289 } 290 } 291 292 try { 293 String value = System.getProperty("javax.xml.parsers.SAXParserFactory"); 297 if (value != null && value.startsWith("weblogic")) { 298 System.setProperty("javax.xml.parsers.SAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl"); 299 System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); 300 } 301 } catch (SecurityException e) { 302 System.out.println("CocoonPortlet: Could not check system properties, got: " + e); 304 } 305 306 this.portletContext = conf.getPortletContext(); 307 this.appContext.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, new PortletContext(this.portletContext)); 308 this.portletContextPath = this.portletContext.getRealPath("/"); 309 310 final String workDirParam = getInitParameter("work-directory"); 313 if (workDirParam != null) { 314 if (this.portletContextPath == null) { 315 this.workDir = new File (workDirParam); 317 } else { 318 File workDirParamFile = new File (workDirParam); 320 if (workDirParamFile.isAbsolute()) { 321 this.workDir = workDirParamFile; 323 } else { 324 this.workDir = new File (portletContextPath, workDirParam); 326 } 327 } 328 } else { 329 this.workDir = (File ) this.portletContext.getAttribute("javax.servlet.context.tempdir"); 331 if (this.workDir == null) { 332 this.workDir = new File (this.portletContext.getRealPath("/WEB-INF/work")); 333 } 334 this.workDir = new File (workDir, "cocoon-files"); 335 } 336 this.workDir.mkdirs(); 337 this.appContext.put(Constants.CONTEXT_WORK_DIR, workDir); 338 339 String path = this.portletContextPath; 340 String debugPathOne = null, debugPathTwo = null; 343 if (path == null) { 344 try { 346 path = this.portletContext.getResource("/WEB-INF").toString(); 347 } catch (MalformedURLException me) { 348 throw new PortletException("Unable to get resource 'WEB-INF'.", me); 349 } 350 debugPathOne = path; 351 path = path.substring(0, path.length() - "WEB-INF".length()); 352 debugPathTwo = path; 353 } 354 355 try { 356 if (path.indexOf(':') > 1) { 357 this.portletContextURL = path; 358 } else { 359 this.portletContextURL = new File (path).toURL().toExternalForm(); 360 } 361 } catch (MalformedURLException me) { 362 try { 366 this.portletContextURL = new File (path).toURL().toExternalForm(); 367 } catch (MalformedURLException ignored) { 368 throw new PortletException("Unable to determine portlet context URL.", me); 369 } 370 } 371 try { 372 this.appContext.put("context-root", new URL (this.portletContextURL)); 373 } catch (MalformedURLException ignore) { 374 } 376 377 initLogger(); 379 380 if (getLogger().isDebugEnabled()) { 381 getLogger().debug("getRealPath for /: " + this.portletContextPath); 382 if (this.portletContextPath == null) { 383 getLogger().debug("getResource for /WEB-INF: " + debugPathOne); 384 getLogger().debug("Path for Root: " + debugPathTwo); 385 } 386 } 387 388 this.forceLoadParameter = getInitParameter("load-class", null); 389 this.forceSystemProperty = getInitParameter("force-property", null); 390 391 if (getLogger().isDebugEnabled()) { 393 getLogger().debug("Portlet Context URL: " + this.portletContextURL); 394 if (workDirParam != null) { 395 getLogger().debug("Using work-directory " + this.workDir); 396 } else { 397 getLogger().debug("Using default work-directory " + this.workDir); 398 } 399 } 400 401 final String uploadDirParam = conf.getInitParameter("upload-directory"); 402 if (uploadDirParam != null) { 403 if (this.portletContextPath == null) { 404 this.uploadDir = new File (uploadDirParam); 405 } else { 406 File uploadDirParamFile = new File (uploadDirParam); 408 if (uploadDirParamFile.isAbsolute()) { 409 this.uploadDir = uploadDirParamFile; 411 } else { 412 this.uploadDir = new File (portletContextPath, uploadDirParam); 414 } 415 } 416 if (getLogger().isDebugEnabled()) { 417 getLogger().debug("Using upload-directory " + this.uploadDir); 418 } 419 } else { 420 this.uploadDir = new File (workDir, "upload-dir" + File.separator); 421 if (getLogger().isDebugEnabled()) { 422 getLogger().debug("Using default upload-directory " + this.uploadDir); 423 } 424 } 425 this.uploadDir.mkdirs(); 426 this.appContext.put(Constants.CONTEXT_UPLOAD_DIR, this.uploadDir); 427 428 this.enableUploads = getInitParameterAsBoolean("enable-uploads", ENABLE_UPLOADS); 429 430 this.autoSaveUploads = getInitParameterAsBoolean("autosave-uploads", SAVE_UPLOADS_TO_DISK); 431 432 String overwriteParam = getInitParameter("overwrite-uploads", "rename"); 433 if ("deny".equalsIgnoreCase(overwriteParam)) { 435 this.allowOverwrite = false; 436 this.silentlyRename = false; 437 } else if ("allow".equalsIgnoreCase(overwriteParam)) { 438 this.allowOverwrite = true; 439 this.silentlyRename = false; } else { 441 this.allowOverwrite = false; 443 this.silentlyRename = true; 444 } 445 446 this.maxUploadSize = getInitParameterAsInteger("upload-max-size", MAX_UPLOAD_SIZE); 447 448 String cacheDirParam = conf.getInitParameter("cache-directory"); 449 if (cacheDirParam != null) { 450 if (this.portletContextPath == null) { 451 this.cacheDir = new File (cacheDirParam); 452 } else { 453 File cacheDirParamFile = new File (cacheDirParam); 455 if (cacheDirParamFile.isAbsolute()) { 456 this.cacheDir = cacheDirParamFile; 458 } else { 459 this.cacheDir = new File (portletContextPath, cacheDirParam); 461 } 462 } 463 if (getLogger().isDebugEnabled()) { 464 getLogger().debug("Using cache-directory " + this.cacheDir); 465 } 466 } else { 467 this.cacheDir = IOUtils.createFile(workDir, "cache-dir" + File.separator); 468 if (getLogger().isDebugEnabled()) { 469 getLogger().debug("cache-directory was not set - defaulting to " + this.cacheDir); 470 } 471 } 472 this.cacheDir.mkdirs(); 473 this.appContext.put(Constants.CONTEXT_CACHE_DIR, this.cacheDir); 474 475 this.appContext.put(Constants.CONTEXT_CONFIG_URL, 476 getConfigFile(conf.getInitParameter("configurations"))); 477 if (conf.getInitParameter("configurations") == null) { 478 if (getLogger().isDebugEnabled()) { 479 getLogger().debug("configurations was not set - defaulting to... ?"); 480 } 481 } 482 483 this.allowReload = getInitParameterAsBoolean("allow-reload", ALLOW_RELOAD); 485 486 String value = conf.getInitParameter("show-time"); 487 this.showTime = BooleanUtils.toBoolean(value) || (this.hiddenShowTime = "hide".equals(value)); 488 if (value == null) { 489 if (getLogger().isDebugEnabled()) { 490 getLogger().debug("show-time was not set - defaulting to false"); 491 } 492 } 493 494 parentComponentManagerClass = getInitParameter("parent-component-manager", null); 495 if (parentComponentManagerClass != null) { 496 int dividerPos = parentComponentManagerClass.indexOf('/'); 497 if (dividerPos != -1) { 498 parentComponentManagerInitParam = parentComponentManagerClass.substring(dividerPos + 1); 499 parentComponentManagerClass = parentComponentManagerClass.substring(0, dividerPos); 500 } 501 } 502 503 this.containerEncoding = getInitParameter("container-encoding", "ISO-8859-1"); 504 this.defaultFormEncoding = getInitParameter("form-encoding", "ISO-8859-1"); 505 506 this.appContext.put(Constants.CONTEXT_DEFAULT_ENCODING, this.defaultFormEncoding); 507 this.manageExceptions = getInitParameterAsBoolean("manage-exceptions", true); 508 509 this.enableInstrumentation = getInitParameterAsBoolean("enable-instrumentation", false); 510 511 this.requestFactory = new RequestFactory(this.autoSaveUploads, 512 this.uploadDir, 513 this.allowOverwrite, 514 this.silentlyRename, 515 this.maxUploadSize, 516 this.defaultFormEncoding); 517 518 this.servletPath = getInitParameter("servlet-path", null); 519 if (this.servletPath != null) { 520 if (this.servletPath.startsWith("/")) { 521 this.servletPath = this.servletPath.substring(1); 522 } 523 if (this.servletPath.endsWith("/")) { 524 this.servletPath = servletPath.substring(0, servletPath.length() - 1); 525 } 526 } 527 528 final String sessionScopeParam = getInitParameter("default-session-scope", "portlet"); 529 if ("application".equalsIgnoreCase(sessionScopeParam)) { 530 this.defaultSessionScope = javax.portlet.PortletSession.APPLICATION_SCOPE; 531 } else { 532 this.defaultSessionScope = javax.portlet.PortletSession.PORTLET_SCOPE; 533 } 534 535 this.appContext.put(CONTEXT_PORTLET_CONFIG, conf); 537 this.createCocoon(); 538 } 539 540 543 public void destroy() { 544 if (this.initClassLoader) { 545 try { 546 Thread.currentThread().setContextClassLoader(this.classLoader); 547 } catch (Exception e) { 548 } 549 } 550 551 if (this.cocoon != null) { 552 if (getLogger().isDebugEnabled()) { 553 getLogger().debug("Portlet destroyed - disposing Cocoon"); 554 } 555 this.disposeCocoon(); 556 } 557 558 if (this.instrumentManager instanceof Disposable) { 559 ((Disposable) this.instrumentManager).dispose(); 560 } 561 562 if (this.parentComponentManager != null && this.parentComponentManager instanceof Disposable) { 563 ((Disposable) this.parentComponentManager).dispose(); 564 } 565 } 566 567 571 protected void addClassLoaderURL(URL URL) { 572 } 574 575 579 protected void addClassLoaderDirectory(String dir) { 580 } 582 583 599 protected String getClassPath() throws PortletException { 600 StringBuffer buildClassPath = new StringBuffer (); 601 602 File root = null; 603 if (portletContextPath != null) { 604 606 String classDir = this.portletContext.getRealPath("/WEB-INF/classes"); 607 String libDir = this.portletContext.getRealPath("/WEB-INF/lib"); 608 609 if (libDir != null) { 610 root = new File (libDir); 611 } 612 613 if (classDir != null) { 614 buildClassPath.append(classDir); 615 616 addClassLoaderDirectory(classDir); 617 } 618 } else { 619 URL classDirURL = null; 621 URL libDirURL = null; 622 623 try { 624 classDirURL = this.portletContext.getResource("/WEB-INF/classes"); 625 } catch (MalformedURLException me) { 626 if (getLogger().isWarnEnabled()) { 627 this.getLogger().warn("Unable to add WEB-INF/classes to the classpath", me); 628 } 629 } 630 631 try { 632 libDirURL = this.portletContext.getResource("/WEB-INF/lib"); 633 } catch (MalformedURLException me) { 634 if (getLogger().isWarnEnabled()) { 635 this.getLogger().warn("Unable to add WEB-INF/lib to the classpath", me); 636 } 637 } 638 639 if (libDirURL != null && libDirURL.toExternalForm().startsWith("file:")) { 640 root = new File (libDirURL.toExternalForm().substring("file:".length())); 641 } 642 643 if (classDirURL != null) { 644 buildClassPath.append(classDirURL.toExternalForm()); 645 646 addClassLoaderURL(classDirURL); 647 } 648 } 649 650 if (root == null) { 652 root = extractLibraries(); 653 } 654 655 if (root != null && root.isDirectory()) { 656 File [] libraries = root.listFiles(); 657 Arrays.sort(libraries); 658 for (int i = 0; i < libraries.length; i++) { 659 String fullName = IOUtils.getFullFilename(libraries[i]); 660 buildClassPath.append(File.pathSeparatorChar).append(fullName); 661 662 addClassLoaderDirectory(fullName); 663 } 664 } 665 666 buildClassPath.append(File.pathSeparatorChar) 667 .append(SystemUtils.JAVA_CLASS_PATH); 668 669 buildClassPath.append(File.pathSeparatorChar) 670 .append(getExtraClassPath()); 671 return buildClassPath.toString(); 672 } 673 674 private File extractLibraries() { 675 try { 676 URL manifestURL = this.portletContext.getResource("/META-INF/MANIFEST.MF"); 677 if (manifestURL == null) { 678 this.getLogger().fatalError("Unable to get Manifest"); 679 return null; 680 } 681 682 Manifest mf = new Manifest (manifestURL.openStream()); 683 Attributes attr = mf.getMainAttributes(); 684 String libValue = attr.getValue("Cocoon-Libs"); 685 if (libValue == null) { 686 this.getLogger().fatalError("Unable to get 'Cocoon-Libs' attribute from the Manifest"); 687 return null; 688 } 689 690 List libList = new ArrayList (); 691 for (StringTokenizer st = new StringTokenizer (libValue, " "); st.hasMoreTokens();) { 692 libList.add(st.nextToken()); 693 } 694 695 File root = new File (this.workDir, "lib"); 696 root.mkdirs(); 697 698 File [] oldLibs = root.listFiles(); 699 for (int i = 0; i < oldLibs.length; i++) { 700 String oldLib = oldLibs[i].getName(); 701 if (!libList.contains(oldLib)) { 702 this.getLogger().debug("Removing old library " + oldLibs[i]); 703 oldLibs[i].delete(); 704 } 705 } 706 707 this.getLogger().warn("Extracting libraries into " + root); 708 byte[] buffer = new byte[65536]; 709 for (Iterator i = libList.iterator(); i.hasNext();) { 710 String libName = (String ) i.next(); 711 712 long lastModified = -1; 713 try { 714 lastModified = Long.parseLong(attr.getValue("Cocoon-Lib-" + libName.replace('.', '_'))); 715 } catch (Exception e) { 716 this.getLogger().debug("Failed to parse lastModified: " + attr.getValue("Cocoon-Lib-" + libName.replace('.', '_'))); 717 } 718 719 File lib = new File (root, libName); 720 if (lib.exists() && lib.lastModified() != lastModified) { 721 this.getLogger().debug("Removing modified library " + lib); 722 lib.delete(); 723 } 724 725 InputStream is = this.portletContext.getResourceAsStream("/WEB-INF/lib/" + libName); 726 if (is == null) { 727 this.getLogger().warn("Skipping " + libName); 728 } else { 729 this.getLogger().debug("Extracting " + libName); 730 OutputStream os = null; 731 try { 732 os = new FileOutputStream (lib); 733 int count; 734 while ((count = is.read(buffer)) > 0) { 735 os.write(buffer, 0, count); 736 } 737 } finally { 738 if (is != null) is.close(); 739 if (os != null) os.close(); 740 } 741 } 742 743 if (lastModified != -1) { 744 lib.setLastModified(lastModified); 745 } 746 } 747 748 return root; 749 } catch (IOException e) { 750 this.getLogger().fatalError("Exception while processing Manifest file", e); 751 return null; 752 } 753 } 754 755 756 762 protected String getExtraClassPath() throws PortletException { 763 String extraClassPath = this.getInitParameter("extra-classpath"); 764 if (extraClassPath != null) { 765 StringBuffer sb = new StringBuffer (); 766 StringTokenizer st = new StringTokenizer (extraClassPath, SystemUtils.PATH_SEPARATOR, false); 767 int i = 0; 768 while (st.hasMoreTokens()) { 769 String s = st.nextToken(); 770 if (i++ > 0) { 771 sb.append(File.pathSeparatorChar); 772 } 773 if ((s.charAt(0) == File.separatorChar) || 774 (s.charAt(1) == ':')) { 775 if (getLogger().isDebugEnabled()) { 776 getLogger().debug("extraClassPath is absolute: " + s); 777 } 778 sb.append(s); 779 780 addClassLoaderDirectory(s); 781 } else { 782 if (s.indexOf("${") != -1) { 783 String path = StringUtils.replaceToken(s); 784 sb.append(path); 785 if (getLogger().isDebugEnabled()) { 786 getLogger().debug("extraClassPath is not absolute replacing using token: [" + s + "] : " + path); 787 } 788 addClassLoaderDirectory(path); 789 } else { 790 String path = null; 791 if (this.portletContextPath != null) { 792 path = this.portletContextPath + s; 793 if (getLogger().isDebugEnabled()) { 794 getLogger().debug("extraClassPath is not absolute pre-pending context path: " + path); 795 } 796 } else { 797 path = this.workDir.toString() + s; 798 if (getLogger().isDebugEnabled()) { 799 getLogger().debug("extraClassPath is not absolute pre-pending work-directory: " + path); 800 } 801 } 802 sb.append(path); 803 addClassLoaderDirectory(path); 804 } 805 } 806 } 807 return sb.toString(); 808 } 809 return ""; 810 } 811 812 822 protected void initLogger() { 823 final String logLevel = getInitParameter("log-level", "INFO"); 824 825 final String accesslogger = getInitParameter("portlet-logger", "cocoon"); 826 827 final Priority logPriority = Priority.getPriorityForName(logLevel); 828 829 final CocoonLogFormatter formatter = new CocoonLogFormatter(); 830 formatter.setFormat("%7.7{priority} %{time} [%8.8{category}] " + 831 "(%{uri}) %{thread}/%{class:short}: %{message}\\n%{throwable}"); 832 final PortletOutputLogTarget servTarget = new PortletOutputLogTarget(this.portletContext, formatter); 833 834 final Hierarchy defaultHierarchy = Hierarchy.getDefaultHierarchy(); 835 final ErrorHandler errorHandler = new DefaultErrorHandler(); 836 defaultHierarchy.setErrorHandler(errorHandler); 837 defaultHierarchy.setDefaultLogTarget(servTarget); 838 defaultHierarchy.setDefaultPriority(logPriority); 839 final Logger logger = new LogKitLogger(Hierarchy.getDefaultHierarchy().getLoggerFor("")); 840 final String loggerManagerClass = 841 this.getInitParameter("logger-class", LogKitLoggerManager.class.getName()); 842 843 845 final LoggerManager loggerManager = 846 newLoggerManager(loggerManagerClass, defaultHierarchy); 847 ContainerUtil.enableLogging(loggerManager, logger); 848 849 final DefaultContext subcontext = new DefaultContext(this.appContext); 850 subcontext.put("portlet-context", this.portletContext); 851 if (this.portletContextPath == null) { 852 File logSCDir = new File (this.workDir, "log"); 853 logSCDir.mkdirs(); 854 if (getLogger().isWarnEnabled()) { 855 getLogger().warn("Setting context-root for LogKit to " + logSCDir); 856 } 857 subcontext.put("context-root", logSCDir.toString()); 858 } else { 859 subcontext.put("context-root", this.portletContextPath); 860 } 861 862 try { 863 ContainerUtil.contextualize(loggerManager, subcontext); 864 this.loggerManager = loggerManager; 865 866 if (loggerManager instanceof Configurable) { 867 String logkitConfig = getInitParameter("logkit-config", "/WEB-INF/logkit.xconf"); 869 870 InputStream is = null; 872 if (logkitConfig.indexOf(':') == -1) { 873 is = this.portletContext.getResourceAsStream(logkitConfig); 874 if (is == null) is = new FileInputStream (logkitConfig); 875 } else { 876 URL logkitURL = new URL (logkitConfig); 877 is = logkitURL.openStream(); 878 } 879 final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 880 final Configuration conf = builder.build(is); 881 ContainerUtil.configure(loggerManager, conf); 882 } 883 884 final String log4jConfig = getInitParameter("log4j-config", null); 886 if ( log4jConfig != null ) { 887 final Log4JConfigurator configurator = new Log4JConfigurator(subcontext); 888 889 InputStream is = null; 891 if ( log4jConfig.indexOf(':') == -1) { 892 is = this.portletContext.getResourceAsStream(log4jConfig); 893 if (is == null) is = new FileInputStream (log4jConfig); 894 } else { 895 final URL log4jURL = new URL (log4jConfig); 896 is = log4jURL.openStream(); 897 } 898 configurator.doConfigure(is, LogManager.getLoggerRepository()); 899 } 900 901 ContainerUtil.initialize(loggerManager); 902 } catch (Exception e) { 903 errorHandler.error("Could not set up Cocoon Logger, will use screen instead", e, null); 904 } 905 906 this.log = this.loggerManager.getLoggerForCategory(accesslogger); 907 } 908 909 private LoggerManager newLoggerManager(String loggerManagerClass, Hierarchy hierarchy) { 910 if (loggerManagerClass.equals(LogKitLoggerManager.class.getName())) { 911 return new LogKitLoggerManager(hierarchy); 912 } else if (loggerManagerClass.equals(Log4JLoggerManager.class.getName()) || 913 loggerManagerClass.equalsIgnoreCase("LOG4J")) { 914 return new Log4JLoggerManager(); 915 } else { 916 try { 917 Class clazz = Class.forName(loggerManagerClass); 918 return (LoggerManager)clazz.newInstance(); 919 } catch (Exception e) { 920 return new LogKitLoggerManager(hierarchy); 921 } 922 } 923 } 924 925 932 private URL getConfigFile(final String configFileName) 933 throws PortletException { 934 final String usedFileName; 935 936 if (configFileName == null) { 937 if (getLogger().isWarnEnabled()) { 938 getLogger().warn("Portlet initialization argument 'configurations' not specified, attempting to use '/WEB-INF/cocoon.xconf'"); 939 } 940 usedFileName = "/WEB-INF/cocoon.xconf"; 941 } else { 942 usedFileName = configFileName; 943 } 944 945 if (getLogger().isDebugEnabled()) { 946 getLogger().debug("Using configuration file: " + usedFileName); 947 } 948 949 URL result; 950 try { 951 if (usedFileName.indexOf(':') == -1) { 953 result = this.portletContext.getResource(usedFileName); 954 } else { 955 result = new URL (usedFileName); 956 } 957 } catch (Exception mue) { 958 String msg = "Init parameter 'configurations' is invalid : " + usedFileName; 959 getLogger().error(msg, mue); 960 throw new PortletException(msg, mue); 961 } 962 963 if (result == null) { 964 File resultFile = new File (usedFileName); 965 if (resultFile.isFile()) { 966 try { 967 result = resultFile.getCanonicalFile().toURL(); 968 } catch (Exception e) { 969 String msg = "Init parameter 'configurations' is invalid : " + usedFileName; 970 getLogger().error(msg, e); 971 throw new PortletException(msg, e); 972 } 973 } 974 } 975 976 if (result == null) { 977 String msg = "Init parameter 'configuration' doesn't name an existing resource : " + usedFileName; 978 getLogger().error(msg); 979 throw new PortletException(msg); 980 } 981 return result; 982 } 983 984 996 private void forceLoad() { 997 if (this.forceLoadParameter != null) { 998 StringTokenizer fqcnTokenizer = new StringTokenizer (forceLoadParameter, " \t\r\n\f;,", false); 999 1000 while (fqcnTokenizer.hasMoreTokens()) { 1001 final String fqcn = fqcnTokenizer.nextToken().trim(); 1002 1003 try { 1004 if (getLogger().isDebugEnabled()) { 1005 getLogger().debug("Trying to load class: " + fqcn); 1006 } 1007 ClassUtils.loadClass(fqcn).newInstance(); 1008 } catch (Exception e) { 1009 if (getLogger().isWarnEnabled()) { 1010 getLogger().warn("Could not force-load class: " + fqcn, e); 1011 } 1012 } 1014 } 1015 } 1016 } 1017 1018 1025 private void forceProperty() { 1026 if (this.forceSystemProperty != null) { 1027 StringTokenizer tokenizer = new StringTokenizer (forceSystemProperty, " \t\r\n\f;,", false); 1028 1029 while (tokenizer.hasMoreTokens()) { 1030 final String property = tokenizer.nextToken().trim(); 1031 if (property.indexOf('=') == -1) { 1032 continue; 1033 } 1034 try { 1035 String key = property.substring(0, property.indexOf('=')); 1036 String value = property.substring(property.indexOf('=') + 1); 1037 if (value.indexOf("${") != -1) { 1038 value = StringUtils.replaceToken(value); 1039 } 1040 if (getLogger().isDebugEnabled()) { 1041 getLogger().debug("setting " + key + "=" + value); 1042 } 1043 System.setProperty(key, value); 1044 } catch (Exception e) { 1045 if (getLogger().isWarnEnabled()) { 1046 getLogger().warn("Could not set property: " + property, e); 1047 } 1048 } 1050 } 1051 } 1052 } 1053 1054 1058 public void processAction(ActionRequest req, ActionResponse res) 1059 throws PortletException, IOException { 1060 1061 1062 1063 if (this.initClassLoader) { 1064 try { 1065 Thread.currentThread().setContextClassLoader(this.classLoader); 1066 } catch (Exception e) { 1067 } 1068 } 1069 1070 long start = System.currentTimeMillis(); 1072 1073 res.setProperty("X-Cocoon-Version", Constants.VERSION); 1075 1076 ActionRequest request; 1078 try { 1079 if (this.enableUploads) { 1080 request = requestFactory.getServletRequest(req); 1081 } else { 1082 request = req; 1083 } 1084 } catch (Exception e) { 1085 if (getLogger().isErrorEnabled()) { 1086 getLogger().error("Problem with Cocoon portlet", e); 1087 } 1088 1089 manageException(req, res, null, null, 1090 "Problem in creating the Request", null, null, e); 1091 return; 1092 } 1093 1094 getCocoon(request.getParameter(Constants.RELOAD_PARAM)); 1096 1097 if (this.cocoon == null) { 1099 manageException(request, res, null, null, 1100 "Initialization Problem", 1101 null , 1102 null , 1103 this.exception); 1104 return; 1105 } 1106 1107 String servletPath = this.servletPath; 1109 if (servletPath == null) { 1110 servletPath = "portlets/" + getPortletConfig().getPortletName(); 1111 } 1112 String pathInfo = getPathInfo(request); 1113 1114 String uri = servletPath; 1115 if (pathInfo != null) { 1116 uri += pathInfo; 1117 } 1118 1119 ContextMap ctxMap = null; 1120 1121 Environment env; 1122 try { 1123 if (uri.charAt(0) == '/') { 1124 uri = uri.substring(1); 1125 } 1126 env = getEnvironment(servletPath, pathInfo, uri, request, res); 1127 } catch (Exception e) { 1128 if (getLogger().isErrorEnabled()) { 1129 getLogger().error("Problem with Cocoon portlet", e); 1130 } 1131 1132 manageException(request, res, null, uri, 1133 "Problem in creating the Environment", null, null, e); 1134 return; 1135 } 1136 1137 try { 1138 try { 1139 ctxMap = ContextMap.getCurrentContext(); 1142 String threadName = Thread.currentThread().getName(); 1144 ctxMap.set("threadName", threadName); 1145 ctxMap.set("objectModel", env.getObjectModel()); 1147 ctxMap.set("request-id", threadName + System.currentTimeMillis()); 1149 1150 if (this.cocoon.process(env)) { 1151 } else { 1152 getLogger().fatalError("The Cocoon engine failed to process the request."); 1155 manageException(request, res, env, uri, 1156 "Request Processing Failed", 1157 "Cocoon engine failed in process the request", 1158 "The processing engine failed to process the request. This could be due to lack of matching or bugs in the pipeline engine.", 1159 null); 1160 return; 1161 } 1162 } catch (ResourceNotFoundException e) { 1163 if (getLogger().isDebugEnabled()) { 1164 getLogger().warn(e.getMessage(), e); 1165 } else if (getLogger().isWarnEnabled()) { 1166 getLogger().warn(e.getMessage()); 1167 } 1168 1169 manageException(request, res, env, uri, 1170 "Resource Not Found", 1171 "Resource Not Found", 1172 "The requested portlet could not be found", 1173 e); 1174 return; 1175 1176 } catch (ConnectionResetException e) { 1177 if (getLogger().isDebugEnabled()) { 1178 getLogger().debug(e.getMessage(), e); 1179 } else if (getLogger().isWarnEnabled()) { 1180 getLogger().warn(e.getMessage()); 1181 } 1182 1183 } catch (IOException e) { 1184 if (getLogger().isDebugEnabled()) { 1186 getLogger().debug(e.getMessage(), e); 1187 } else if (getLogger().isWarnEnabled()) { 1188 getLogger().warn(e.getMessage()); 1189 } 1190 1191 } catch (Exception e) { 1192 if (getLogger().isErrorEnabled()) { 1193 getLogger().error("Internal Cocoon Problem", e); 1194 } 1195 1196 manageException(request, res, env, uri, 1197 "Internal Server Error", null, null, e); 1198 return; 1199 } 1200 1201 long end = System.currentTimeMillis(); 1202 String timeString = processTime(end - start); 1203 if (getLogger().isInfoEnabled()) { 1204 getLogger().info("'" + uri + "' " + timeString); 1205 } 1206 res.setProperty("X-Cocoon-Time", timeString); 1207 } finally { 1208 if (ctxMap != null) { 1209 ctxMap.clear(); 1210 } 1211 1212 try { 1213 if (request instanceof MultipartActionRequest) { 1214 if (getLogger().isDebugEnabled()) { 1215 getLogger().debug("Deleting uploaded file(s)."); 1216 } 1217 ((MultipartActionRequest) request).cleanup(); 1218 } 1219 } catch (IOException e) { 1220 getLogger().error("Cocoon got an Exception while trying to cleanup the uploaded files.", e); 1221 } 1222 } 1223 } 1224 1225 1229 public void render(RenderRequest req, RenderResponse res) 1230 throws PortletException, IOException { 1231 1232 1233 1234 if (this.initClassLoader) { 1235 try { 1236 Thread.currentThread().setContextClassLoader(this.classLoader); 1237 } catch (Exception e) { 1238 } 1239 } 1240 1241 long start = System.currentTimeMillis(); 1243 1244 res.setProperty("X-Cocoon-Version", Constants.VERSION); 1246 1247 RenderRequest request = req; 1249 1250 getCocoon(request.getParameter(Constants.RELOAD_PARAM)); 1252 1253 if (this.cocoon == null) { 1255 manageException(request, res, null, null, 1256 "Initialization Problem", 1257 null , 1258 null , 1259 this.exception); 1260 return; 1261 } 1262 1263 String servletPath = this.servletPath; 1265 if (servletPath == null) { 1266 servletPath = "portlets/" + getPortletConfig().getPortletName(); 1267 } 1268 String pathInfo = getPathInfo(request); 1269 1270 String uri = servletPath; 1271 if (pathInfo != null) { 1272 uri += pathInfo; 1273 } 1274 1275 String contentType = null; 1276 ContextMap ctxMap = null; 1277 1278 Environment env; 1279 try { 1280 if (uri.charAt(0) == '/') { 1281 uri = uri.substring(1); 1282 } 1283 env = getEnvironment(servletPath, pathInfo, uri, request, res); 1284 } catch (Exception e) { 1285 if (getLogger().isErrorEnabled()) { 1286 getLogger().error("Problem with Cocoon portlet", e); 1287 } 1288 1289 manageException(request, res, null, uri, 1290 "Problem in creating the Environment", null, null, e); 1291 return; 1292 } 1293 1294 try { 1295 try { 1296 ctxMap = ContextMap.getCurrentContext(); 1299 String threadName = Thread.currentThread().getName(); 1301 ctxMap.set("threadName", threadName); 1302 ctxMap.set("objectModel", env.getObjectModel()); 1304 ctxMap.set("request-id", threadName + System.currentTimeMillis()); 1306 1307 if (this.cocoon.process(env)) { 1308 contentType = env.getContentType(); 1309 } else { 1310 getLogger().fatalError("The Cocoon engine failed to process the request."); 1313 manageException(request, res, env, uri, 1314 "Request Processing Failed", 1315 "Cocoon engine failed in process the request", 1316 "The processing engine failed to process the request. This could be due to lack of matching or bugs in the pipeline engine.", 1317 null); 1318 return; 1319 } 1320 } catch (ResourceNotFoundException rse) { 1321 if (getLogger().isWarnEnabled()) { 1322 getLogger().warn("The resource was not found", rse); 1323 } 1324 1325 manageException(request, res, env, uri, 1326 "Resource Not Found", 1327 "Resource Not Found", 1328 "The requested portlet could not be found", 1329 rse); 1330 return; 1331 1332 } catch (ConnectionResetException e) { 1333 if (getLogger().isDebugEnabled()) { 1334 getLogger().debug(e.getMessage(), e); 1335 } else if (getLogger().isWarnEnabled()) { 1336 getLogger().warn(e.getMessage()); 1337 } 1338 1339 } catch (IOException e) { 1340 if (getLogger().isDebugEnabled()) { 1342 getLogger().debug(e.getMessage(), e); 1343 } else if (getLogger().isWarnEnabled()) { 1344 getLogger().warn(e.getMessage()); 1345 } 1346 1347 } catch (Exception e) { 1348 if (getLogger().isErrorEnabled()) { 1349 getLogger().error("Internal Cocoon Problem", e); 1350 } 1351 1352 manageException(request, res, env, uri, 1353 "Internal Server Error", null, null, e); 1354 return; 1355 } 1356 1357 long end = System.currentTimeMillis(); 1358 String timeString = processTime(end - start); 1359 if (getLogger().isInfoEnabled()) { 1360 getLogger().info("'" + uri + "' " + timeString); 1361 } 1362 res.setProperty("X-Cocoon-Time", timeString); 1363 1364 if (contentType != null && contentType.equals("text/html")) { 1365 String showTime = request.getParameter(Constants.SHOWTIME_PARAM); 1366 boolean show = this.showTime; 1367 if (showTime != null) { 1368 show = !showTime.equalsIgnoreCase("no"); 1369 } 1370 if (show) { 1371 boolean hide = this.hiddenShowTime; 1372 if (showTime != null) { 1373 hide = showTime.equalsIgnoreCase("hide"); 1374 } 1375 PrintStream out = new PrintStream (res.getPortletOutputStream()); 1376 out.print((hide) ? "<!-- " : "<p>"); 1377 out.print(timeString); 1378 out.println((hide) ? " -->" : "</p>\n"); 1379 } 1380 } 1381 } finally { 1382 if (ctxMap != null) { 1383 ctxMap.clear(); 1384 } 1385 1386 try { 1387 if (request instanceof MultipartActionRequest) { 1388 if (getLogger().isDebugEnabled()) { 1389 getLogger().debug("Deleting uploaded file(s)."); 1390 } 1391 ((MultipartActionRequest) request).cleanup(); 1392 } 1393 } catch (IOException e) { 1394 getLogger().error("Cocoon got an Exception while trying to cleanup the uploaded files.", e); 1395 } 1396 1397 1405 } 1406 } 1407 1408 private String getPathInfo(PortletRequest request) { 1409 PortletSession session = null; 1410 1411 String pathInfo = request.getParameter(PortletEnvironment.PARAMETER_PATH_INFO); 1412 if (storeSessionPath) { 1413 session = request.getPortletSession(true); 1414 if (pathInfo == null) { 1415 pathInfo = (String )session.getAttribute(PortletEnvironment.PARAMETER_PATH_INFO); 1416 } 1417 } 1418 1419 if (pathInfo == null) { 1421 pathInfo = "/"; 1422 } else if (!pathInfo.startsWith("/")) { 1423 pathInfo = '/' + pathInfo; 1424 } 1425 1426 if (storeSessionPath) { 1427 session.setAttribute(PortletEnvironment.PARAMETER_PATH_INFO, pathInfo); 1428 } 1429 return pathInfo; 1430 } 1431 1432 protected void manageException(ActionRequest req, ActionResponse res, Environment env, 1433 String uri, String title, String message, String description, 1434 Exception e) 1435 throws PortletException { 1436 throw new PortletException("Exception in CocoonPortlet", e); 1437 } 1438 1439 protected void manageException(RenderRequest req, RenderResponse res, Environment env, 1440 String uri, String title, String message, String description, 1441 Exception e) 1442 throws IOException , PortletException { 1443 if (this.manageExceptions) { 1444 if (env != null) { 1445 env.tryResetResponse(); 1446 } else { 1447 res.reset(); 1448 } 1449 1450 String type = Notifying.FATAL_NOTIFICATION; 1451 HashMap extraDescriptions = null; 1452 1453 extraDescriptions = new HashMap (2); 1454 extraDescriptions.put(Notifying.EXTRA_REQUESTURI, getPortletConfig().getPortletName()); 1455 if (uri != null) { 1456 extraDescriptions.put("Request URI", uri); 1457 } 1458 1459 if (!getLogger().isInfoEnabled()) { 1461 Throwable t = DefaultNotifyingBuilder.getRootCause(e); 1462 if (t != null) extraDescriptions.put(Notifying.EXTRA_CAUSE, t.getMessage()); 1463 e = null; 1464 } 1465 1466 Notifying n = new DefaultNotifyingBuilder().build(this, 1467 e, 1468 type, 1469 title, 1470 "Cocoon Portlet", 1471 message, 1472 description, 1473 extraDescriptions); 1474 1475 res.setContentType("text/html"); 1476 Notifier.notify(n, res.getPortletOutputStream(), "text/html"); 1477 } else { 1478 res.flushBuffer(); 1479 throw new PortletException("Exception in CocoonPortlet", e); 1480 } 1481 } 1482 1483 1486 protected Environment getEnvironment(String servletPath, 1487 String pathInfo, 1488 String uri, 1489 ActionRequest req, 1490 ActionResponse res) 1491 throws Exception { 1492 PortletEnvironment env; 1493 1494 String formEncoding = req.getParameter("cocoon-form-encoding"); 1495 if (formEncoding == null) { 1496 formEncoding = this.defaultFormEncoding; 1497 } 1498 env = new PortletEnvironment(servletPath, 1499 pathInfo, 1500 uri, 1501 this.portletContextURL, 1502 req, 1503 res, 1504 this.portletContext, 1505 (PortletContext) this.appContext.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT), 1506 this.containerEncoding, 1507 formEncoding, 1508 this.defaultSessionScope); 1509 env.enableLogging(getLogger()); 1510 return env; 1511 } 1512 1513 1516 protected Environment getEnvironment(String servletPath, 1517 String pathInfo, 1518 String uri, 1519 RenderRequest req, 1520 RenderResponse res) 1521 throws Exception { 1522 PortletEnvironment env; 1523 1524 String formEncoding = req.getParameter("cocoon-form-encoding"); 1525 if (formEncoding == null) { 1526 formEncoding = this.defaultFormEncoding; 1527 } 1528 env = new PortletEnvironment(servletPath, 1529 pathInfo, 1530 uri, 1531 this.portletContextURL, 1532 req, 1533 res, 1534 this.portletContext, 1535 (PortletContext) this.appContext.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT), 1536 this.containerEncoding, 1537 formEncoding, 1538 this.defaultSessionScope); 1539 env.enableLogging(getLogger()); 1540 return env; 1541 } 1542 1543 1551 protected synchronized ComponentManager getParentComponentManager() { 1552 if (parentComponentManager != null && parentComponentManager instanceof Disposable) { 1553 ((Disposable) parentComponentManager).dispose(); 1554 } 1555 1556 parentComponentManager = null; 1557 if (parentComponentManagerClass != null) { 1558 try { 1559 Class pcm = ClassUtils.loadClass(parentComponentManagerClass); 1560 Constructor pcmc = pcm.getConstructor(new Class []{String .class}); 1561 parentComponentManager = (ComponentManager) pcmc.newInstance(new Object []{parentComponentManagerInitParam}); 1562 1563 if (parentComponentManager instanceof LogEnabled) { 1564 ((LogEnabled) parentComponentManager).enableLogging(getLogger()); 1565 } 1566 if (parentComponentManager instanceof Contextualizable) { 1567 ((Contextualizable) parentComponentManager).contextualize(this.appContext); 1568 } 1569 if (parentComponentManager instanceof Initializable) { 1570 ((Initializable) parentComponentManager).initialize(); 1571 } 1572 } catch (Exception e) { 1573 if (getLogger().isErrorEnabled()) { 1574 getLogger().error("Could not initialize parent component manager.", e); 1575 } 1576 } 1577 } 1578 return parentComponentManager; 1579 } 1580 1581 1584 private synchronized void createCocoon() 1585 throws PortletException { 1586 1587 1588 1589 if (this.initClassLoader) { 1590 try { 1591 Thread.currentThread().setContextClassLoader(this.classLoader); 1592 } catch (Exception e) { 1593 } 1594 } 1595 1596 updateEnvironment(); 1597 forceLoad(); 1598 forceProperty(); 1599 1600 try { 1601 URL configFile = (URL ) this.appContext.get(Constants.CONTEXT_CONFIG_URL); 1602 if (getLogger().isInfoEnabled()) { 1603 getLogger().info("Reloading from: " + configFile.toExternalForm()); 1604 } 1605 Cocoon c = (Cocoon) ClassUtils.newInstance("org.apache.cocoon.Cocoon"); 1606 ContainerUtil.enableLogging(c, getCocoonLogger()); 1607 c.setLoggerManager(getLoggerManager()); 1608 ContainerUtil.contextualize(c, this.appContext); 1609 final ComponentManager parent = this.getParentComponentManager(); 1610 if (parent != null) { 1611 ContainerUtil.compose(c, parent); 1612 } 1613 if (this.enableInstrumentation) { 1614 c.setInstrumentManager(getInstrumentManager()); 1615 } 1616 ContainerUtil.initialize(c); 1617 this.creationTime = System.currentTimeMillis(); 1618 1619 disposeCocoon(); 1620 this.cocoon = c; 1621 } catch (Exception e) { 1622 if (getLogger().isErrorEnabled()) { 1623 getLogger().error("Exception reloading", e); 1624 } 1625 this.exception = e; 1626 disposeCocoon(); 1627 } 1628 } 1629 1630 private Logger getCocoonLogger() { 1631 final String rootlogger = getInitParameter("cocoon-logger"); 1632 if (rootlogger != null) { 1633 return this.getLoggerManager().getLoggerForCategory(rootlogger); 1634 } else { 1635 return getLogger(); 1636 } 1637 } 1638 1639 1646 protected void updateEnvironment() throws PortletException { 1647 this.appContext.put(Constants.CONTEXT_CLASS_LOADER, classLoader); 1648 this.appContext.put(Constants.CONTEXT_CLASSPATH, getClassPath()); 1649 } 1650 1651 1656 private InstrumentManager getInstrumentManager() 1657 throws Exception { 1658 String imConfig = getInitParameter("instrumentation-config"); 1659 if (imConfig == null) { 1660 throw new PortletException("Please define the init-param 'instrumentation-config' in your web.xml"); 1661 } 1662 1663 final InputStream is = this.portletContext.getResourceAsStream(imConfig); 1664 final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 1665 final Configuration conf = builder.build(is); 1666 1667 final String imLoggerCategory = conf.getAttribute("logger", "core.instrument"); 1669 Logger imLogger = this.loggerManager.getLoggerForCategory(imLoggerCategory); 1670 1671 DefaultInstrumentManagerImpl instrumentManager = new DefaultInstrumentManagerImpl(); 1673 instrumentManager.enableLogging(imLogger); 1674 instrumentManager.configure(conf); 1675 instrumentManager.initialize(); 1676 1677 if (getLogger().isDebugEnabled()) { 1678 getLogger().debug("Instrument manager created " + instrumentManager); 1679 } 1680 1681 this.instrumentManager = instrumentManager; 1682 return instrumentManager; 1683 } 1684 1685 private String processTime(long time) { 1686 StringBuffer out = new StringBuffer (PROCESSED_BY); 1687 if (time <= SECOND) { 1688 out.append(time); 1689 out.append(" milliseconds."); 1690 } else if (time <= MINUTE) { 1691 out.append(time / SECOND); 1692 out.append(" seconds."); 1693 } else if (time <= HOUR) { 1694 out.append(time / MINUTE); 1695 out.append(" minutes."); 1696 } else { 1697 out.append(time / HOUR); 1698 out.append(" hours."); 1699 } 1700 return out.toString(); 1701 } 1702 1703 1707 private void getCocoon(final String reloadParam) 1708 throws PortletException { 1709 if (this.allowReload) { 1710 boolean reload = false; 1711 1712 if (this.cocoon != null) { 1713 if (this.cocoon.modifiedSince(this.creationTime)) { 1714 if (getLogger().isInfoEnabled()) { 1715 getLogger().info("Configuration changed reload attempt"); 1716 } 1717 reload = true; 1718 } else if (reloadParam != null) { 1719 if (getLogger().isInfoEnabled()) { 1720 getLogger().info("Forced reload attempt"); 1721 } 1722 reload = true; 1723 } 1724 } else if (reloadParam != null) { 1725 if (getLogger().isInfoEnabled()) { 1726 getLogger().info("Invalid configurations reload"); 1727 } 1728 reload = true; 1729 } 1730 1731 if (reload) { 1732 initLogger(); 1733 createCocoon(); 1734 } 1735 } 1736 } 1737 1738 1741 private final void disposeCocoon() { 1742 if (this.cocoon != null) { 1743 ContainerUtil.dispose(this.cocoon); 1744 this.cocoon = null; 1745 } 1746 } 1747 1748 1752 public String getInitParameter(String name) { 1753 String result = super.getInitParameter(name); 1754 if (result != null) { 1755 result = result.trim(); 1756 if (result.length() == 0) { 1757 result = null; 1758 } 1759 } 1760 1761 return result; 1762 } 1763 1764 1765 protected String getInitParameter(String name, String defaultValue) { 1766 String result = getInitParameter(name); 1767 if (result == null) { 1768 if (getLogger() != null && getLogger().isDebugEnabled()) { 1769 getLogger().debug(name + " was not set - defaulting to '" + defaultValue + "'"); 1770 } 1771 return defaultValue; 1772 } else { 1773 return result; 1774 } 1775 } 1776 1777 1778 protected boolean getInitParameterAsBoolean(String name, boolean defaultValue) { 1779 String value = getInitParameter(name); 1780 if (value == null) { 1781 if (getLogger() != null && getLogger().isDebugEnabled()) { 1782 getLogger().debug(name + " was not set - defaulting to '" + defaultValue + "'"); 1783 } 1784 return defaultValue; 1785 } 1786 1787 return BooleanUtils.toBoolean(value); 1788 } 1789 1790 protected int getInitParameterAsInteger(String name, int defaultValue) { 1791 String value = getInitParameter(name); 1792 if (value == null) { 1793 if (getLogger() != null && getLogger().isDebugEnabled()) { 1794 getLogger().debug(name + " was not set - defaulting to '" + defaultValue + "'"); 1795 } 1796 return defaultValue; 1797 } else { 1798 return Integer.parseInt(value); 1799 } 1800 } 1801 1802 protected Logger getLogger() { 1803 return this.log; 1804 } 1805 1806 protected LoggerManager getLoggerManager() { 1807 return this.loggerManager; 1808 } 1809} 1810 | Popular Tags |