1 38 package org.jpublish; 39 40 import java.io.IOException ; 41 import java.io.InputStream ; 42 import java.util.*; 43 44 import javax.servlet.ServletContext ; 45 46 import com.anthonyeden.lib.config.Configuration; 47 import com.anthonyeden.lib.config.ConfigurationException; 48 import com.anthonyeden.lib.config.ConfigurationFactory; 49 import com.anthonyeden.lib.config.sax.SAXConfigurationFactory; 50 import com.anthonyeden.lib.util.ClassUtilities; 51 import com.anthonyeden.lib.util.IOUtilities; 52 import com.anthonyeden.lib.util.MessageUtilities; 53 import com.opensymphony.xwork.config.ConfigurationManager; 54 import org.apache.commons.logging.Log; 55 import org.apache.commons.logging.LogFactory; 56 import org.apache.commons.vfs.FileObject; 57 import org.apache.commons.vfs.FileSystemManager; 58 import org.apache.commons.vfs.impl.DefaultFileSystemManager; 59 import org.apache.commons.vfs.provider.FileProvider; 60 import org.jpublish.action.ActionManager; 61 import org.jpublish.action.VFSXmlConfigurationProvider; 62 import org.jpublish.component.ComponentManager; 63 import org.jpublish.module.Module; 64 import org.jpublish.page.PageManager; 65 import org.jpublish.repository.Repository; 66 import org.jpublish.repository.RepositoryContentSource; 67 import org.jpublish.resource.ResourceManager; 68 import org.jpublish.template.TemplateContentSource; 69 import org.jpublish.template.TemplateManager; 70 import org.jpublish.util.PathUtilities; 71 import org.jpublish.util.encoding.CharacterEncodingManager; 72 import org.jpublish.util.mime.MimeTypeMap; 73 import org.jpublish.util.uri.InternalURI; 74 import org.jpublish.util.uri.InternalURIParser; 75 import org.jpublish.util.uri.RepositoryURI; 76 import org.jpublish.util.uri.TemplateURI; 77 import org.jpublish.vfs.FileProviderFactory; 78 import org.jpublish.view.ContentSource; 79 import org.jpublish.view.ViewRenderer; 80 import org.jpublish.servlet.PathDispatcher; 81 82 91 public class SiteContext { 92 93 96 public final static String DEFAULT_ACTION_MANAGER = 97 "org.jpublish.action.DefaultActionManager"; 98 99 102 public final static String DEFAULT_PAGE_MANAGER = 103 "org.jpublish.page.DefaultPageManager"; 104 105 108 public final static String DEFAULT_TEMPLATE_MANAGER = 109 "org.jpublish.template.DefaultTemplateManager"; 110 111 114 public final static String DEFAULT_RAW_RESOURCE_MANAGER = 115 "org.jpublish.resource.DefaultResourceManager"; 116 117 120 public final static String DEFAULT_STATIC_RESOURCE_MANAGER = 121 "org.jpublish.resource.DefaultResourceManager"; 122 123 126 public final static String DEFAULT_COMPONENT_MANAGER = 127 "org.jpublish.component.DefaultComponentManager"; 128 129 132 public final static String DEFAULT_REPOSITORY = 133 "org.jpublish.repository.DefaultRepository"; 134 135 138 public final static String DEFAULT_VIEW_RENDERER = 139 "org.jpublish.view.velocity.VelocityViewRenderer"; 140 141 144 public final static String DEFAULT_ACTION_ROOT = "actions"; 145 146 149 public final static String DEFAULT_PAGE_ROOT = "pages"; 150 151 154 public final static String DEFAULT_TEMPLATE_ROOT = "templates"; 155 156 159 public final static String DEFAULT_STATIC_ROOT = "static"; 160 161 164 public final static String DEFAULT_RAW_ROOT = "raw"; 165 166 169 public final static String DEFAULT_ACTION_IDENTIFIER = "action"; 170 171 174 public final static String DEFAULT_PAGE = "index.html"; 175 176 179 public final static String DEFAULT_TEMPLATE = "basic"; 180 181 184 public final static String DEFAULT_CONFIGURATION_SUFFIX = ".xml"; 185 186 189 public final static String DEFAULT_SCHEME = "webapp"; 190 191 194 public final static String DEFAULT_JPUBLISH_CONFIGURATION = 195 "/WEB-INF/jpublish.xml"; 196 197 200 public final static String DEFAULT_XWORK_CONFIGURATION = 201 "/WEB-INF/xwork.xml"; 202 203 206 public final static Log syslog = LogFactory.getLog("syslog"); 207 208 private final static String ATTRIBUTE_NAME = "name"; 209 private final static String ATTRIBUTE_CLASSNAME = "classname"; 210 211 private static Log log = LogFactory.getLog(SiteContext.class); 212 213 private String configurationSuffix = DEFAULT_CONFIGURATION_SUFFIX; 214 private String configurationPath = null; 215 private String defaultScheme = DEFAULT_SCHEME; 216 private ServletContext servletContext = null; 217 private FileProvider defaultFileProvider = null; 218 private FileProviderFactory fileProviderFactory = null; 219 private FileSystemManager contextFileSystemManager = null; 220 private PathDispatcher pathDispatcher = null; 221 222 private String actionIdentifier = DEFAULT_ACTION_IDENTIFIER; 223 private String defaultPage = DEFAULT_PAGE; 224 private String defaultTemplate = DEFAULT_TEMPLATE; 225 226 private boolean protectReservedNames = false; 227 private boolean parameterActionsEnabled = false; 228 private boolean debug = false; 229 230 private List modules = new ArrayList(); 231 private List repositories = new ArrayList(); 232 233 private List defaultExceptionHandlers = new ArrayList(); 234 private Map exceptionHandlerMap = new HashMap(); 235 private Map cachedExceptionHandlers = new HashMap(); 236 237 private PageManager defaultPageManager = null; 238 private Map pageManagers = new HashMap(); 239 240 private ActionManager defaultActionManager = null; 241 private Map actionManagers = new HashMap(); 242 243 private TemplateManager defaultTemplateManager = null; 244 private Map templateManagers = new HashMap(); 245 246 private ResourceManager defaultStaticResourceManager = null; 247 private Map staticResourceManagers = new HashMap(); 248 249 private ResourceManager rawResourceManager = null; 250 private ComponentManager componentManager = null; 251 252 private ViewRenderer defaultViewRenderer = null; 253 private Map viewRenderers = new HashMap(); 254 255 private MimeTypeMap mimeTypeMap = new MimeTypeMap(); 256 private CharacterEncodingManager characterEncodingManager = 257 new CharacterEncodingManager(); 258 259 private Map attributes = new HashMap(); 260 261 267 public SiteContext(ServletContext servletContext) throws IOException { 268 this.servletContext = servletContext; 269 270 this.fileProviderFactory = new FileProviderFactory(this); 271 272 try { 273 this.defaultFileProvider = fileProviderFactory.createProvider(DEFAULT_SCHEME); 274 } catch (Exception e) { 275 throw new JPublishRuntimeException("Error loading default file provider", e); 276 } 277 278 DefaultFileSystemManager fileSystemManager = 279 new DefaultFileSystemManager(); 280 fileSystemManager.addProvider(DEFAULT_SCHEME, defaultFileProvider); 281 FileObject baseFile = fileSystemManager.resolveFile(defaultScheme + ":/"); 282 fileSystemManager.setBaseFile(baseFile); 283 this.contextFileSystemManager = fileSystemManager; 284 285 FileObject xworkConfigurationFile = fileSystemManager.resolveFile(DEFAULT_XWORK_CONFIGURATION); 287 if (log.isDebugEnabled()) { 288 log.debug("XWork Configuration file: " + xworkConfigurationFile); 289 } 290 ConfigurationManager.addConfigurationProvider(new VFSXmlConfigurationProvider(xworkConfigurationFile)); 291 } 292 293 298 public ServletContext getServletContext() { 299 return servletContext; 300 } 301 302 307 308 public FileSystemManager getContextFileSystemManager() { 309 return contextFileSystemManager; 310 } 311 312 317 318 public String getDefaultScheme() { 319 return defaultScheme; 320 } 321 322 327 328 public FileProvider getDefaultFileProvider() { 329 return defaultFileProvider; 330 } 331 332 337 338 public FileProviderFactory getFileProviderFactory() { 339 return fileProviderFactory; 340 } 341 342 347 348 public String getConfigurationSuffix() { 349 return configurationSuffix; 350 } 351 352 357 358 public String getConfigurationPath() { 359 return configurationPath; 360 } 361 362 367 368 public void setConfigurationSuffix(String configurationSuffix) { 369 this.configurationSuffix = configurationSuffix; 370 } 371 372 377 378 public String getActionIdentifier() { 379 return actionIdentifier; 380 } 381 382 388 389 public synchronized void setActionIdentifier(String actionIdentifier) { 390 if (actionIdentifier == null) { 391 this.actionIdentifier = DEFAULT_ACTION_IDENTIFIER; 392 } else { 393 this.actionIdentifier = actionIdentifier; 394 } 395 } 396 397 403 404 public boolean isParameterActionsEnabled() { 405 return parameterActionsEnabled; 406 } 407 408 415 416 public void setParameterActionsEnabled(boolean parameterActionsEnabled) { 417 this.parameterActionsEnabled = parameterActionsEnabled; 418 } 419 420 427 428 public void setParameterActionsEnabled(String parameterActionsEnabled) { 429 setParameterActionsEnabled("true".equals(parameterActionsEnabled)); 430 } 431 432 438 439 public String getDefaultPage() { 440 return defaultPage; 441 } 442 443 448 449 public void setDefaultPage(String defaultPage) { 450 this.defaultPage = defaultPage; 451 } 452 453 459 460 public String getDefaultTemplate() { 461 return defaultTemplate; 462 } 463 464 470 471 public void setDefaultTemplate(String defaultTemplate) { 472 this.defaultTemplate = defaultTemplate; 473 } 474 475 480 481 public String getDefaultMimeType() { 482 return getMimeTypeMap().getDefaultMimeType(); 483 } 484 485 490 491 public void setDefaultMimeType(String defaultMimeType) { 492 getMimeTypeMap().setDefaultMimeType(defaultMimeType); 493 } 494 495 500 501 public boolean isProtectReservedNames() { 502 return protectReservedNames; 503 } 504 505 510 511 public void setProtectReservedNames(boolean protectReservedNames) { 512 if (protectReservedNames) { 513 log.info("Protect reserved names enabled"); 514 } 515 this.protectReservedNames = protectReservedNames; 516 } 517 518 523 524 public void setProtectReservedNames(String protectReservedNames) { 525 setProtectReservedNames("true".equals(protectReservedNames)); 526 } 527 528 533 534 public boolean isDebug() { 535 return debug; 536 } 537 538 543 544 public void setDebug(boolean debug) { 545 if (debug) { 546 log.info("JPublish debugging enabled."); 547 } 548 this.debug = debug; 549 } 550 551 556 557 public void setDebug(String debug) { 558 setDebug("true".equals(debug)); 559 } 560 561 566 567 public List getModules() { 568 return modules; 569 } 570 571 577 578 public List getRepositories() { 579 return repositories; 580 } 581 582 590 591 public List getExceptionHandlers(String path) { 592 List exceptionHandlers = (List) cachedExceptionHandlers.get(path); 593 if (exceptionHandlers == null) { 594 Iterator keys = exceptionHandlerMap.keySet().iterator(); 595 while (keys.hasNext()) { 596 String key = (String ) keys.next(); 597 if (PathUtilities.match(path, key)) { 598 exceptionHandlers = (List) exceptionHandlerMap.get(key); 599 cachedExceptionHandlers.put(path, exceptionHandlers); 600 return exceptionHandlers; 601 } 602 } 603 return getDefaultExceptionHandlers(); 604 } else { 605 return exceptionHandlers; 606 } 607 } 608 609 615 616 public List getDefaultExceptionHandlers() { 617 return defaultExceptionHandlers; 618 } 619 620 627 628 public Repository getRepository(String name) { 629 Iterator iter = getRepositories().iterator(); 630 while (iter.hasNext()) { 631 Repository repository = (Repository) iter.next(); 632 if (repository.getName().equals(name)) { 633 return repository; 634 } 635 } 636 return null; 637 } 638 639 645 646 public ActionManager getActionManager() { 647 return defaultActionManager; 648 } 649 650 656 public ActionManager getActionManager(String name) { 657 return (ActionManager) actionManagers.get(name); 658 } 659 660 666 public void setActionManager(String name, ActionManager actionManager) { 667 actionManagers.put(name, actionManager); 668 } 669 670 675 public void setDefaultActionManager(ActionManager defaultActionManager) { 676 this.defaultActionManager = defaultActionManager; 677 } 678 679 685 public PageManager getPageManager() { 686 return defaultPageManager; 687 } 688 689 695 public PageManager getPageManager(String name) { 696 return (PageManager) pageManagers.get(name); 697 } 698 699 705 public void setPageManager(String name, PageManager pageManager) { 706 pageManagers.put(name, pageManager); 707 } 708 709 714 public void setDefaultPageManager(PageManager defaultPageManager) { 715 this.defaultPageManager = defaultPageManager; 716 } 717 718 724 public TemplateManager getTemplateManager() { 725 return defaultTemplateManager; 726 } 727 728 734 public TemplateManager getTemplateManager(String name) { 735 return (TemplateManager) templateManagers.get(name); 736 } 737 738 public void setTemplateManager(String name, TemplateManager templateManager) { 739 templateManagers.put(name, templateManager); 740 } 741 742 747 public void setDefaultTemplateManager(TemplateManager defaultTemplateManager) { 748 this.defaultTemplateManager = defaultTemplateManager; 749 } 750 751 756 public ResourceManager getStaticResourceManager() { 757 return defaultStaticResourceManager; 758 } 759 760 public ResourceManager getStaticResourceManager(String name) { 761 return (ResourceManager) staticResourceManagers.get(name); 762 } 763 764 public void setStaticResourceManager(String name, 765 ResourceManager resourceManager) { 766 staticResourceManagers.put(name, resourceManager); 767 } 768 769 public void setDefaultStaticResourceManager(ResourceManager defaultStaticResourceManager) { 770 this.defaultStaticResourceManager = defaultStaticResourceManager; 771 } 772 773 778 779 public ResourceManager getRawResourceManager() { 780 return rawResourceManager; 781 } 782 783 788 789 public ViewRenderer getViewRenderer() { 790 return defaultViewRenderer; 791 } 792 793 799 800 public ViewRenderer getViewRenderer(String name) { 801 return (ViewRenderer) viewRenderers.get(name); 802 } 803 804 810 811 public void setViewRenderer(String name, ViewRenderer viewRenderer) { 812 viewRenderers.put(name, viewRenderer); 813 } 814 815 820 821 public void setDefaultViewRenderer(ViewRenderer viewRenderer) { 822 this.defaultViewRenderer = viewRenderer; 823 } 824 825 830 831 public ComponentManager getComponentManager() { 832 return componentManager; 833 } 834 835 840 841 public MimeTypeMap getMimeTypeMap() { 842 return mimeTypeMap; 843 } 844 845 850 851 public CharacterEncodingManager getCharacterEncodingManager() { 852 return characterEncodingManager; 853 } 854 855 868 869 public ContentSource getContentSource(String name) { 870 try { 871 if (log.isDebugEnabled()) { 872 log.debug("getContent(" + name + ")"); 873 } 874 InternalURI uri = InternalURIParser.getInstance().parse(name); 875 String protocol = uri.getProtocol(); 876 if (protocol.equalsIgnoreCase(InternalURIParser.TEMPLATE_PROTOCOL)) { 877 String templateManagerName = 878 ((TemplateURI) uri).getTemplateManagerName(); 879 if (templateManagerName == null) { 880 log.debug("Using default template manager"); 881 return new TemplateContentSource(defaultTemplateManager, 882 uri.getPath()); 883 } else { 884 TemplateManager templateManager = getTemplateManager(templateManagerName); 885 String path = uri.getPath(); 886 if (log.isDebugEnabled()) { 887 log.debug("Looking for template: " + path); 888 } 889 return new TemplateContentSource(templateManager, path); 890 } 891 } else if (protocol.equalsIgnoreCase(InternalURIParser.REPOSITORY_PROTOCOL)) { 892 String repositoryName = 893 ((RepositoryURI) uri).getRepositoryName(); 894 Repository repository = getRepository(repositoryName); 895 String path = uri.getPath(); 896 if (log.isDebugEnabled()) { 897 log.debug("Looking for content: " + path); 898 } 899 return new RepositoryContentSource(repository, path); 900 } else { 901 log.warn("Protocol " + protocol + 902 " not supported as a content source"); 903 return null; 904 } 905 } catch (Throwable t) { 906 909 log.error("Error getting content: " + t.getMessage()); 910 if (log.isDebugEnabled()) { 911 t.printStackTrace(); 912 } 913 return null; 914 } 915 } 916 917 922 public PathDispatcher getPathDispatcher() { 923 return pathDispatcher; 924 } 925 926 928 934 public Object getAttribute(String name) { 935 return attributes.get(name); 936 } 937 938 944 public void setAttribute(String name, Object value) { 945 attributes.put(name, value); 946 } 947 948 953 public void removeAttribute(String name) { 954 attributes.remove(name); 955 } 956 957 962 public Iterator getAttributeNames() { 963 return attributes.keySet().iterator(); 964 } 965 966 968 975 public void loadConfiguration(String configurationPath) 976 throws IOException , ConfigurationException { 977 if (configurationPath == null) { 978 configurationPath = DEFAULT_JPUBLISH_CONFIGURATION; 979 } 980 981 FileObject configurationFile = contextFileSystemManager.resolveFile(configurationPath); 982 983 InputStream in = null; 984 try { 985 in = configurationFile.getContent().getInputStream(); 986 loadConfiguration(configurationPath, in); 987 this.configurationPath = configurationPath; 988 } finally { 989 IOUtilities.close(in); 990 } 991 } 992 993 1001 public void loadConfiguration(String configurationPath, InputStream in) 1002 throws IOException , ConfigurationException { 1003 ConfigurationFactory configFactory = 1004 SAXConfigurationFactory.getInstance(); 1005 loadConfiguration(configFactory.getConfiguration(configurationPath, in)); 1006 } 1007 1008 1014 public void loadConfiguration(Configuration configuration) 1015 throws ConfigurationException { 1016 try { 1017 Iterator iter = null; 1018 1019 log.debug("Loading configuration"); 1020 1021 setConfigurationSuffix(configuration.getChildValue("configuration-suffix", DEFAULT_CONFIGURATION_SUFFIX)); 1023 1024 log.debug("Creating default ActionManager"); 1026 loadDefaultActionManager(); 1027 1028 log.debug("Creating ActionManagers"); 1030 iter = configuration.getChildren("action-manager").iterator(); 1031 while (iter.hasNext()) { 1032 loadActionManager((Configuration) iter.next()); 1033 } 1034 1035 log.debug("Creating default PageManager"); 1037 loadDefaultPageManager(); 1038 1039 log.debug("Creating PageManagers"); 1041 iter = configuration.getChildren("page-manager").iterator(); 1042 while (iter.hasNext()) { 1043 loadPageManager((Configuration) iter.next()); 1044 } 1045 1046 log.debug("Creating default TemplateManager"); 1048 loadDefaultTemplateManager(); 1049 1050 log.debug("Creating TemplateManagers"); 1052 iter = configuration.getChildren("template-manager").iterator(); 1053 while (iter.hasNext()) { 1054 loadTemplateManager((Configuration) iter.next()); 1055 } 1056 1057 log.debug("Creating default static ResourceManager"); 1059 loadDefaultStaticResourceManager(); 1060 1061 log.debug("Creating static ResourceManagers"); 1063 iter = configuration.getChildren("static-resource-manager").iterator(); 1064 while (iter.hasNext()) { 1065 loadStaticResourceManager((Configuration) iter.next()); 1066 } 1067 1068 log.debug("Creating raw ResourceManager"); 1070 loadRawResourceManager(configuration.getChild("raw-resource-manager")); 1071 1072 log.debug("Creating default ViewRenderer"); 1074 loadDefaultViewRenderer(); 1075 1076 log.debug("Creating ViewRenderers"); 1078 iter = configuration.getChildren("view-renderer").iterator(); 1079 while (iter.hasNext()) { 1080 loadViewRenderer((Configuration) iter.next()); 1081 } 1082 1083 Iterator moduleElements = 1085 configuration.getChildren("module").iterator(); 1086 while (moduleElements.hasNext()) { 1087 Configuration moduleElement = 1088 (Configuration) moduleElements.next(); 1089 loadModule(moduleElement); 1090 } 1091 1092 Iterator repositoryElements = 1094 configuration.getChildren("repository").iterator(); 1095 while (repositoryElements.hasNext()) { 1096 Configuration repositoryElement = 1097 (Configuration) repositoryElements.next(); 1098 String className = repositoryElement.getAttribute(ATTRIBUTE_CLASSNAME, DEFAULT_REPOSITORY); 1099 log.debug("Repository class name: " + className); 1100 Repository repository = (Repository) ClassUtilities.loadClass(className).newInstance(); 1101 repository.setSiteContext(this); 1102 repository.loadConfiguration(repositoryElement); 1103 if (log.isDebugEnabled()) { 1104 log.debug("Adding repository: " + repository.getName()); 1105 } 1106 getRepositories().add(repository); 1107 } 1108 1109 log.debug("Creating ComponentManager"); 1111 loadComponentManager(configuration.getChild("component-manager")); 1112 1113 setActionIdentifier(configuration.getChildValue("action-identifier", 1115 DEFAULT_ACTION_IDENTIFIER)); 1116 1117 setDefaultPage(configuration.getChildValue("default-page", 1119 DEFAULT_PAGE)); 1120 setDefaultTemplate(configuration.getChildValue("default-template", 1121 DEFAULT_TEMPLATE)); 1122 setDefaultMimeType(configuration.getChildValue("default-mime-type")); 1123 1124 setProtectReservedNames(configuration.getChildValue("protect-reserved-names", "false")); 1126 1127 setParameterActionsEnabled(configuration.getChildValue("parameter-actions-enabled", "false")); 1129 1130 setDebug(configuration.getChildValue("debug", "false")); 1132 1133 log.debug("Loading CharacterEncodingMaps"); 1135 characterEncodingManager.loadConfiguration(configuration); 1136 1137 Iterator mimeTypeMapElements = 1139 configuration.getChildren("mime-mapping").iterator(); 1140 while (mimeTypeMapElements.hasNext()) { 1141 Configuration mimeTypeMapElement = 1142 (Configuration) mimeTypeMapElements.next(); 1143 String ext = mimeTypeMapElement.getAttribute("ext"); 1144 String mimeType = mimeTypeMapElement.getAttribute("mimetype"); 1145 mimeTypeMap.put(ext, mimeType); 1146 } 1147 1148 Configuration defaultExceptionHandlersElement = 1150 configuration.getChild("default-exception-handlers"); 1151 if (defaultExceptionHandlersElement != null) { 1152 1153 Iterator defaultExceptionHandlerElements = 1154 defaultExceptionHandlersElement.getChildren("exception-handler").iterator(); 1155 1156 while (defaultExceptionHandlerElements.hasNext()) { 1157 Configuration defaultExceptionHandlerElement = 1158 (Configuration) defaultExceptionHandlerElements.next(); 1159 String className = 1160 defaultExceptionHandlerElement.getAttribute(ATTRIBUTE_CLASSNAME); 1161 defaultExceptionHandlers.add(ClassUtilities.loadClass(className).newInstance()); 1162 } 1163 } 1164 1165 Iterator exceptionHandlerMapElements = 1167 configuration.getChildren("exception-handler-map").iterator(); 1168 while (exceptionHandlerMapElements.hasNext()) { 1169 Configuration exceptionHandlerMapElement = 1170 (Configuration) exceptionHandlerMapElements.next(); 1171 loadExceptionHandlerMap(exceptionHandlerMapElement); 1172 } 1173 1174 pathDispatcher = new PathDispatcher(this); 1176 pathDispatcher.loadConfiguration(configuration); 1177 } catch (ClassNotFoundException e) { 1178 Object [] args = {}; 1179 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1180 "classNotFound", args), e, configuration); 1181 } catch (InstantiationException e) { 1182 Object [] args = {}; 1183 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1184 "classInstantiationError", args), e, 1185 configuration); 1186 } catch (IllegalAccessException e) { 1187 Object [] args = {}; 1188 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1189 "classIllegalAccessError", args), e, 1190 configuration); 1191 } 1192 } 1193 1194 1200 1201 protected void loadDefaultActionManager() throws ConfigurationException { 1202 try { 1203 defaultActionManager = 1204 (ActionManager) ClassUtilities.loadClass(DEFAULT_ACTION_MANAGER).newInstance(); 1205 defaultActionManager.setSiteContext(this); 1206 } catch (ClassNotFoundException e) { 1207 Object [] args = {DEFAULT_ACTION_MANAGER}; 1208 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1209 "defaultActionManagerClassIllegalAccessError", args), e); 1210 } catch (InstantiationException e) { 1211 Object [] args = {DEFAULT_ACTION_MANAGER}; 1212 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1213 "defaultActionManagerClassIllegalAccessError", args), e); 1214 } catch (IllegalAccessException e) { 1215 Object [] args = {DEFAULT_ACTION_MANAGER}; 1216 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1217 "defaultActionManagerClassIllegalAccessError", args), e); 1218 } 1219 } 1220 1221 1227 1228 protected void loadActionManager(Configuration configuration) 1229 throws ConfigurationException { 1230 String actionManagerClass = null; 1231 try { 1232 if (configuration != null) { 1233 actionManagerClass = configuration.getAttribute(ATTRIBUTE_CLASSNAME, DEFAULT_ACTION_MANAGER); 1234 String name = configuration.getAttribute(ATTRIBUTE_NAME); 1235 ActionManager actionManager = (ActionManager) ClassUtilities.loadClass(actionManagerClass).newInstance(); 1236 actionManager.setSiteContext(this); 1237 actionManager.loadConfiguration(configuration); 1238 if (name == null) { 1239 setDefaultActionManager(actionManager); 1240 } else { 1241 setActionManager(name, actionManager); 1242 } 1243 } else { 1244 defaultActionManager = (ActionManager) ClassUtilities.loadClass(DEFAULT_ACTION_MANAGER).newInstance(); 1245 defaultActionManager.setSiteContext(this); 1246 } 1247 } catch (ClassNotFoundException e) { 1248 Object [] args = {actionManagerClass}; 1249 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1250 "actionManagerClassNotFound", args), e, configuration); 1251 } catch (InstantiationException e) { 1252 Object [] args = {actionManagerClass}; 1253 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1254 "actionManagerClassInstantiationError", args), e, 1255 configuration); 1256 } catch (IllegalAccessException e) { 1257 Object [] args = {actionManagerClass}; 1258 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1259 "actionManagerClassIllegalAccessError", args), e, 1260 configuration); 1261 } 1262 } 1263 1264 1270 1271 protected void loadDefaultPageManager() throws ConfigurationException { 1272 try { 1273 defaultPageManager = 1274 (PageManager) ClassUtilities.loadClass(DEFAULT_PAGE_MANAGER).newInstance(); 1275 defaultPageManager.setSiteContext(this); 1276 } catch (ClassNotFoundException e) { 1277 Object [] args = {DEFAULT_PAGE_MANAGER}; 1278 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1279 "defaultPageManagerClassIllegalAccessError", args), e); 1280 } catch (InstantiationException e) { 1281 Object [] args = {DEFAULT_PAGE_MANAGER}; 1282 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1283 "defaultPageManagerClassIllegalAccessError", args), e); 1284 } catch (IllegalAccessException e) { 1285 Object [] args = {DEFAULT_PAGE_MANAGER}; 1286 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1287 "defaultPageManagerClassIllegalAccessError", args), e); 1288 } 1289 } 1290 1291 1297 1298 protected void loadPageManager(Configuration configuration) 1299 throws ConfigurationException { 1300 String pageManagerClass = null; 1301 try { 1302 if (configuration != null) { 1303 pageManagerClass = configuration.getAttribute(ATTRIBUTE_CLASSNAME, DEFAULT_PAGE_MANAGER); 1304 String name = configuration.getAttribute(ATTRIBUTE_NAME); 1305 PageManager pageManager = (PageManager) ClassUtilities.loadClass(pageManagerClass).newInstance(); 1306 pageManager.setSiteContext(this); 1307 pageManager.loadConfiguration(configuration); 1308 1309 if (name == null) { 1310 setDefaultPageManager(pageManager); 1311 } else { 1312 setPageManager(name, pageManager); 1313 } 1314 } else { 1315 defaultPageManager = (PageManager) ClassUtilities.loadClass(DEFAULT_PAGE_MANAGER).newInstance(); 1316 defaultPageManager.setSiteContext(this); 1317 } 1318 } catch (ClassNotFoundException e) { 1319 Object [] args = {pageManagerClass}; 1320 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1321 "pageManagerClassNotFound", args), e, configuration); 1322 } catch (InstantiationException e) { 1323 Object [] args = {pageManagerClass}; 1324 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1325 "pageManagerClassInstantiationError", args), e, 1326 configuration); 1327 } catch (IllegalAccessException e) { 1328 Object [] args = {pageManagerClass}; 1329 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1330 "pageManagerClassIllegalAccessError", args), e, 1331 configuration); 1332 } 1333 } 1334 1335 1341 1342 protected void loadDefaultTemplateManager() throws ConfigurationException { 1343 try { 1344 defaultTemplateManager = 1345 (TemplateManager) ClassUtilities.loadClass(DEFAULT_TEMPLATE_MANAGER).newInstance(); 1346 defaultTemplateManager.setSiteContext(this); 1347 } catch (ClassNotFoundException e) { 1348 Object [] args = {DEFAULT_TEMPLATE_MANAGER}; 1349 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1350 "defaultTemplateManagerClassIllegalAccessError", args), e); 1351 } catch (InstantiationException e) { 1352 Object [] args = {DEFAULT_TEMPLATE_MANAGER}; 1353 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1354 "defaultTemplateManagerClassIllegalAccessError", args), e); 1355 } catch (IllegalAccessException e) { 1356 Object [] args = {DEFAULT_TEMPLATE_MANAGER}; 1357 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1358 "defaultTemplateManagerClassIllegalAccessError", args), e); 1359 } 1360 } 1361 1362 1368 1369 protected void loadTemplateManager(Configuration configuration) 1370 throws ConfigurationException { 1371 String templateManagerClass = null; 1372 try { 1373 if (configuration != null) { 1374 templateManagerClass = configuration.getAttribute(ATTRIBUTE_CLASSNAME, DEFAULT_TEMPLATE_MANAGER); 1375 String name = configuration.getAttribute(ATTRIBUTE_NAME); 1376 TemplateManager templateManager = 1377 (TemplateManager) ClassUtilities.loadClass(templateManagerClass).newInstance(); 1378 templateManager.setSiteContext(this); 1379 templateManager.loadConfiguration(configuration); 1380 1381 if (name == null) { 1382 setDefaultTemplateManager(templateManager); 1383 } else { 1384 setTemplateManager(name, templateManager); 1385 } 1386 } else { 1387 defaultTemplateManager = (TemplateManager) ClassUtilities.loadClass(DEFAULT_TEMPLATE_MANAGER).newInstance(); 1388 defaultTemplateManager.setSiteContext(this); 1389 } 1390 } catch (ClassNotFoundException e) { 1391 Object [] args = {templateManagerClass}; 1392 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1393 "templateManagerClassNotFound", args), e, configuration); 1394 } catch (InstantiationException e) { 1395 Object [] args = {templateManagerClass}; 1396 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1397 "templateManagerClassInstantiationError", args), e, 1398 configuration); 1399 } catch (IllegalAccessException e) { 1400 Object [] args = {templateManagerClass}; 1401 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1402 "templateManagerClassIllegalAccessError", args), e, 1403 configuration); 1404 } 1405 } 1406 1407 1413 1414 protected void loadDefaultStaticResourceManager() throws ConfigurationException { 1415 try { 1416 defaultStaticResourceManager = 1417 (ResourceManager) ClassUtilities.loadClass(DEFAULT_STATIC_RESOURCE_MANAGER).newInstance(); 1418 defaultStaticResourceManager.setSiteContext(this); 1419 } catch (ClassNotFoundException e) { 1420 Object [] args = {DEFAULT_STATIC_RESOURCE_MANAGER}; 1421 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1422 "defaultStaticResourceManagerClassIllegalAccessError", args), e); 1423 } catch (InstantiationException e) { 1424 Object [] args = {DEFAULT_STATIC_RESOURCE_MANAGER}; 1425 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1426 "defaultStaticResourceManagerClassIllegalAccessError", args), e); 1427 } catch (IllegalAccessException e) { 1428 Object [] args = {DEFAULT_TEMPLATE_MANAGER}; 1429 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1430 "defaultStaticResourceManagerClassIllegalAccessError", args), e); 1431 } 1432 } 1433 1434 1442 protected void loadStaticResourceManager(Configuration configuration) 1443 throws ConfigurationException { 1444 String staticResourceManagerClass = null; 1445 try { 1446 if (configuration != null) { 1447 staticResourceManagerClass = configuration.getAttribute(ATTRIBUTE_CLASSNAME, 1448 DEFAULT_STATIC_RESOURCE_MANAGER); 1449 log.debug("Static resource manager class: " + 1450 staticResourceManagerClass); 1451 String name = configuration.getAttribute(ATTRIBUTE_NAME); 1452 log.debug("Static resource manager name: " + name); 1453 ResourceManager staticResourceManager = 1454 (ResourceManager) ClassUtilities.loadClass(staticResourceManagerClass).newInstance(); 1455 staticResourceManager.setSiteContext(this); 1456 staticResourceManager.loadConfiguration(configuration); 1457 1458 if (name == null) { 1459 setDefaultStaticResourceManager(staticResourceManager); 1460 } else { 1461 setStaticResourceManager(name, staticResourceManager); 1462 } 1463 } else { 1464 defaultStaticResourceManager = 1465 (ResourceManager) ClassUtilities.loadClass(DEFAULT_STATIC_RESOURCE_MANAGER).newInstance(); 1466 defaultStaticResourceManager.setSiteContext(this); 1467 } 1468 } catch (ClassNotFoundException e) { 1469 Object [] args = {staticResourceManagerClass}; 1470 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1471 "staticResourceManagerClassNotFound", args), e, configuration); 1472 } catch (InstantiationException e) { 1473 Object [] args = {staticResourceManagerClass}; 1474 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1475 "staticResourceManagerClassInstantiationError", args), e, 1476 configuration); 1477 } catch (IllegalAccessException e) { 1478 Object [] args = {staticResourceManagerClass}; 1479 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1480 "staticResourceManagerClassIllegalAccessError", args), e, 1481 configuration); 1482 } 1483 } 1484 1485 1492 1493 protected void loadRawResourceManager(Configuration configuration) 1494 throws ConfigurationException { 1495 String rawResourceManagerClass = null; 1496 try { 1497 if (configuration != null) { 1498 rawResourceManagerClass = configuration.getAttribute(ATTRIBUTE_CLASSNAME, DEFAULT_RAW_RESOURCE_MANAGER); 1499 rawResourceManager = 1500 (ResourceManager) ClassUtilities.loadClass(rawResourceManagerClass).newInstance(); 1501 rawResourceManager.setSiteContext(this); 1502 rawResourceManager.loadConfiguration(configuration); 1503 } else { 1504 rawResourceManager = 1505 (ResourceManager) ClassUtilities.loadClass(DEFAULT_RAW_RESOURCE_MANAGER).newInstance(); 1506 rawResourceManager.setSiteContext(this); 1507 } 1508 } catch (ClassNotFoundException e) { 1509 Object [] args = {rawResourceManagerClass}; 1510 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1511 "rawResourceManagerClassNotFound", args), e, configuration); 1512 } catch (InstantiationException e) { 1513 Object [] args = {rawResourceManagerClass}; 1514 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1515 "rawResourceManagerClassInstantiationError", args), e, 1516 configuration); 1517 } catch (IllegalAccessException e) { 1518 Object [] args = {rawResourceManagerClass}; 1519 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1520 "rawResourceManagerClassIllegalAccessError", args), e, 1521 configuration); 1522 } 1523 } 1524 1525 1531 1532 protected void loadDefaultViewRenderer() throws ConfigurationException { 1533 try { 1534 defaultViewRenderer = 1535 (ViewRenderer) ClassUtilities.loadClass(DEFAULT_VIEW_RENDERER).newInstance(); 1536 defaultViewRenderer.setSiteContext(this); 1537 defaultViewRenderer.init(); 1538 } catch (ClassNotFoundException e) { 1539 Object [] args = {DEFAULT_VIEW_RENDERER}; 1540 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1541 "defaultViewRendererClassNotFound", args), e); 1542 } catch (InstantiationException e) { 1543 Object [] args = {DEFAULT_VIEW_RENDERER}; 1544 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1545 "defaultViewRendererClassInstantiationError", args), e); 1546 } catch (IllegalAccessException e) { 1547 Object [] args = {DEFAULT_VIEW_RENDERER}; 1548 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1549 "defaultViewRendererClassIllegalAccessError", args), e); 1550 } 1551 } 1552 1553 1559 1560 protected void loadViewRenderer(Configuration configuration) 1561 throws ConfigurationException { 1562 String name = null; 1563 String viewRendererClass = null; 1564 try { 1565 name = configuration.getAttribute(ATTRIBUTE_NAME); 1566 viewRendererClass = configuration.getAttribute(ATTRIBUTE_CLASSNAME, DEFAULT_VIEW_RENDERER); 1567 if (log.isDebugEnabled()) { 1568 log.debug("Loading view renderer [name=" + name + ",class=" + 1569 viewRendererClass + ")"); 1570 } 1571 1572 ViewRenderer viewRenderer = 1573 (ViewRenderer) ClassUtilities.loadClass(viewRendererClass).newInstance(); 1574 viewRenderer.setSiteContext(this); 1575 viewRenderer.loadConfiguration(configuration); 1576 viewRenderer.init(); 1577 if (name == null) { 1578 setDefaultViewRenderer(viewRenderer); 1579 } else { 1580 setViewRenderer(name, viewRenderer); 1581 } 1582 } catch (ClassNotFoundException e) { 1583 Object [] args = {viewRendererClass, name}; 1584 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1585 "viewRendererClassNotFound", args), e, configuration); 1586 } catch (InstantiationException e) { 1587 Object [] args = {viewRendererClass, name}; 1588 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1589 "viewRendererClassInstantiationError", args), e, 1590 configuration); 1591 } catch (IllegalAccessException e) { 1592 Object [] args = {viewRendererClass, name}; 1593 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1594 "viewRendererClassIllegalAccessError", args), e, 1595 configuration); 1596 } 1597 } 1598 1599 1605 1606 protected void loadComponentManager(Configuration configuration) 1607 throws ConfigurationException { 1608 String componentManagerClass = null; 1609 try { 1610 if (configuration != null) { 1611 componentManagerClass = configuration.getAttribute(ATTRIBUTE_CLASSNAME, DEFAULT_COMPONENT_MANAGER); 1612 componentManager = (ComponentManager) ClassUtilities.loadClass(componentManagerClass).newInstance(); 1613 componentManager.setSiteContext(this); 1614 componentManager.loadConfiguration(configuration); 1615 } else { 1616 componentManager = (ComponentManager) ClassUtilities.loadClass(DEFAULT_COMPONENT_MANAGER).newInstance(); 1617 componentManager.setSiteContext(this); 1618 } 1619 } catch (ClassNotFoundException e) { 1620 Object [] args = {componentManagerClass}; 1621 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1622 "componentManagerClassNotFound", args), e, configuration); 1623 } catch (InstantiationException e) { 1624 Object [] args = {componentManagerClass}; 1625 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1626 "componentManagerClassInstantiationError", args), e, 1627 configuration); 1628 } catch (IllegalAccessException e) { 1629 Object [] args = {componentManagerClass}; 1630 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1631 "componentManagerClassIllegalAccessError", args), e, 1632 configuration); 1633 } 1634 } 1635 1636 1641 1642 protected void loadModule(Configuration configuration) { 1643 String className = configuration.getAttribute("classname"); 1644 try { 1645 Module module = (Module) ClassUtilities.loadClass(className).newInstance(); 1646 module.init(this, configuration); 1647 modules.add(module); 1648 } catch (Exception e) { 1649 log.error("Unable to load module " + className); 1650 } 1651 } 1652 1653 1659 1660 protected void loadExceptionHandlerMap(Configuration configuration) 1661 throws ConfigurationException { 1662 String path = configuration.getAttribute("path"); 1663 if (path == null) { 1664 throw new ConfigurationException("Exception handler path must be defined"); 1665 } 1666 1667 Iterator exceptionHandlerElements = configuration.getChildren("exception-handler").iterator(); 1668 while (exceptionHandlerElements.hasNext()) { 1669 Configuration exceptionHandlerElement = 1670 (Configuration) exceptionHandlerElements.next(); 1671 String exceptionHandlerClass = exceptionHandlerElement.getAttribute(ATTRIBUTE_CLASSNAME); 1672 try { 1673 List exceptionHandlers = (List) exceptionHandlerMap.get(path); 1674 if (exceptionHandlers == null) { 1675 exceptionHandlers = new ArrayList(); 1676 exceptionHandlerMap.put(path, exceptionHandlers); 1677 } 1678 exceptionHandlers.add(ClassUtilities.loadClass(exceptionHandlerClass).newInstance()); 1679 } catch (ClassNotFoundException e) { 1680 Object [] args = {exceptionHandlerClass}; 1681 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1682 "exceptionHandlerClassNotFound", args), e, configuration); 1683 } catch (InstantiationException e) { 1684 Object [] args = {exceptionHandlerClass}; 1685 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1686 "exceptionHandlerClassInstantiationError", args), e, 1687 configuration); 1688 } catch (IllegalAccessException e) { 1689 Object [] args = {exceptionHandlerClass}; 1690 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 1691 "exceptionHandlerClassIllegalAccessError", args), e, 1692 configuration); 1693 } 1694 } 1695 } 1696 1697} 1698 1699 | Popular Tags |