KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > SiteContext


1 /*--
2  Copyright (C) 2001-2003 Aetrion LLC.
3  All rights reserved.
4  Redistribution and use in source and binary forms, with or without
5  modification, are permitted provided that the following conditions
6  are met:
7  1. Redistributions of source code must retain the above copyright
8     notice, this list of conditions, and the following disclaimer.
9  2. Redistributions in binary form must reproduce the above copyright
10     notice, this list of conditions, and the disclaimer that follows
11     these conditions in the documentation and/or other materials
12     provided with the distribution.
13  3. The name "JPublish" must not be used to endorse or promote products
14     derived from this software without prior written permission. For
15     written permission, please contact info@aetrion.com.
16  4. Products derived from this software may not be called "JPublish", nor
17     may "JPublish" appear in their name, without prior written permission
18     from Aetrion LLC (info@aetrion.com).
19  In addition, the authors of this software request (but do not require)
20  that you include in the end-user documentation provided with the
21  redistribution and/or in the software itself an acknowledgement equivalent
22  to the following:
23      "This product includes software developed by
24       Aetrion LLC (http://www.aetrion.com/)."
25  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
26  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
29  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36  For more information on JPublish, please see <http://www.jpublish.org/>.
37  */

38 package org.jpublish;
39
40 import java.io.IOException JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.util.*;
43
44 import javax.servlet.ServletContext JavaDoc;
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 /**
83  * This class contains configuration information for a particular site. Pages are loaded and actions are executed within
84  * the context of the site and have access to all of the methods within this class. <p>
85  *
86  * Instances of the SiteContext class will also reload themselves automatically when the underlying configuration file
87  * changes.</p>
88  *
89  * @author Anthony Eden
90  */

91 public class SiteContext {
92
93     /**
94      * Default ActionManager implementation class name.
95      */

96     public final static String JavaDoc DEFAULT_ACTION_MANAGER =
97             "org.jpublish.action.DefaultActionManager";
98
99     /**
100      * Default PageManager implementation class name.
101      */

102     public final static String JavaDoc DEFAULT_PAGE_MANAGER =
103             "org.jpublish.page.DefaultPageManager";
104
105     /**
106      * Default TemplateManager implementation class name.
107      */

108     public final static String JavaDoc DEFAULT_TEMPLATE_MANAGER =
109             "org.jpublish.template.DefaultTemplateManager";
110
111     /**
112      * Default raw resource manager.
113      */

114     public final static String JavaDoc DEFAULT_RAW_RESOURCE_MANAGER =
115             "org.jpublish.resource.DefaultResourceManager";
116
117     /**
118      * Default StaticResourceManager implementation class name.
119      */

120     public final static String JavaDoc DEFAULT_STATIC_RESOURCE_MANAGER =
121             "org.jpublish.resource.DefaultResourceManager";
122
123     /**
124      * Default ComponentManager implementation class name.
125      */

126     public final static String JavaDoc DEFAULT_COMPONENT_MANAGER =
127             "org.jpublish.component.DefaultComponentManager";
128
129     /**
130      * Default Repository implementation class name.
131      */

132     public final static String JavaDoc DEFAULT_REPOSITORY =
133             "org.jpublish.repository.DefaultRepository";
134
135     /**
136      * Default ViewRenderer implementation class name.
137      */

138     public final static String JavaDoc DEFAULT_VIEW_RENDERER =
139             "org.jpublish.view.velocity.VelocityViewRenderer";
140
141     /**
142      * Default action root for action lookups in the VFS.
143      */

144     public final static String JavaDoc DEFAULT_ACTION_ROOT = "actions";
145
146     /**
147      * Default page root for page lookups in the VFS.
148      */

149     public final static String JavaDoc DEFAULT_PAGE_ROOT = "pages";
150
151     /**
152      * Default template root for template lookups in the VFS.
153      */

154     public final static String JavaDoc DEFAULT_TEMPLATE_ROOT = "templates";
155
156     /**
157      * Default static root for static resource lookups in the VFS.
158      */

159     public final static String JavaDoc DEFAULT_STATIC_ROOT = "static";
160
161     /**
162      * Default raw root for raw resource lookups in the VFS.
163      */

164     public final static String JavaDoc DEFAULT_RAW_ROOT = "raw";
165
166     /**
167      * Default action identifier for parameter defined actions
168      */

169     public final static String JavaDoc DEFAULT_ACTION_IDENTIFIER = "action";
170
171     /**
172      * Default page name for requests without a page (i.e. directory requests)
173      */

174     public final static String JavaDoc DEFAULT_PAGE = "index.html";
175
176     /**
177      * Default template name
178      */

179     public final static String JavaDoc DEFAULT_TEMPLATE = "basic";
180
181     /**
182      * Default configuration suffix
183      */

184     public final static String JavaDoc DEFAULT_CONFIGURATION_SUFFIX = ".xml";
185
186     /**
187      * Default VFS URI scheme (webapp)
188      */

189     public final static String JavaDoc DEFAULT_SCHEME = "webapp";
190
191     /**
192      * Default path for JPublish configuration file (/WEB-INF/jpublish.xml)
193      */

194     public final static String JavaDoc DEFAULT_JPUBLISH_CONFIGURATION =
195             "/WEB-INF/jpublish.xml";
196
197     /**
198      * Default path for XWork configuration file (/WEB-INF/xwork.xml)
199      */

200     public final static String JavaDoc DEFAULT_XWORK_CONFIGURATION =
201             "/WEB-INF/xwork.xml";
202
203     /**
204      * System log object for application-level logging
205      */

206     public final static Log syslog = LogFactory.getLog("syslog");
207
208     private final static String JavaDoc ATTRIBUTE_NAME = "name";
209     private final static String JavaDoc ATTRIBUTE_CLASSNAME = "classname";
210
211     private static Log log = LogFactory.getLog(SiteContext.class);
212
213     private String JavaDoc configurationSuffix = DEFAULT_CONFIGURATION_SUFFIX;
214     private String JavaDoc configurationPath = null;
215     private String JavaDoc defaultScheme = DEFAULT_SCHEME;
216     private ServletContext JavaDoc 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 JavaDoc actionIdentifier = DEFAULT_ACTION_IDENTIFIER;
223     private String JavaDoc defaultPage = DEFAULT_PAGE;
224     private String JavaDoc 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     /**
262      * Construct a new SiteContext.
263      *
264      * @param servletContext The ServletContext
265      * @throws IOException Description of the Exception
266      */

267     public SiteContext(ServletContext JavaDoc servletContext) throws IOException JavaDoc {
268         this.servletContext = servletContext;
269
270         this.fileProviderFactory = new FileProviderFactory(this);
271
272         try {
273             this.defaultFileProvider = fileProviderFactory.createProvider(DEFAULT_SCHEME);
274         } catch (Exception JavaDoc 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         // set the XWork configuration providers
286
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     /**
294      * Get the ServletContext.
295      *
296      * @return The ServletContext
297      */

298     public ServletContext JavaDoc getServletContext() {
299         return servletContext;
300     }
301
302     /**
303      * Get a virtual file system manager which provides access to the complete hiearchy of the web application.
304      *
305      * @return The context file system manager
306      */

307
308     public FileSystemManager getContextFileSystemManager() {
309         return contextFileSystemManager;
310     }
311
312     /**
313      * Get the default scheme which is used when constructing default file system managers.
314      *
315      * @return The default scheme
316      */

317
318     public String JavaDoc getDefaultScheme() {
319         return defaultScheme;
320     }
321
322     /**
323      * Get the default FileProvider which can be used if no FileProvider is specified for a manager.
324      *
325      * @return The default FileProvider
326      */

327
328     public FileProvider getDefaultFileProvider() {
329         return defaultFileProvider;
330     }
331
332     /**
333      * Get the FileProviderFactory.
334      *
335      * @return The FileProviderFactory
336      */

337
338     public FileProviderFactory getFileProviderFactory() {
339         return fileProviderFactory;
340     }
341
342     /**
343      * Get the configuration suffix.
344      *
345      * @return The configuration suffix
346      */

347
348     public String JavaDoc getConfigurationSuffix() {
349         return configurationSuffix;
350     }
351
352     /**
353      * Get the configuration path which is used to load the SiteContext configuration.
354      *
355      * @return The configuration path
356      */

357
358     public String JavaDoc getConfigurationPath() {
359         return configurationPath;
360     }
361
362     /**
363      * Set the configuration suffix.
364      *
365      * @param configurationSuffix The new configuration suffix
366      */

367
368     public void setConfigurationSuffix(String JavaDoc configurationSuffix) {
369         this.configurationSuffix = configurationSuffix;
370     }
371
372     /**
373      * Get the action identifier which is used to trigger parameter actions. The default value is 'action'.
374      *
375      * @return The action identifier
376      */

377
378     public String JavaDoc getActionIdentifier() {
379         return actionIdentifier;
380     }
381
382     /**
383      * Set the action identifier. If this method is invoked with a null argument then the action identifier will be
384      * reset to the default value.
385      *
386      * @param actionIdentifier The new action identifer or null to reset
387      */

388
389     public synchronized void setActionIdentifier(String JavaDoc actionIdentifier) {
390         if (actionIdentifier == null) {
391             this.actionIdentifier = DEFAULT_ACTION_IDENTIFIER;
392         } else {
393             this.actionIdentifier = actionIdentifier;
394         }
395     }
396
397     /**
398      * Return true if parameter actions are enabled.
399      *
400      * @return True if parameter actions are enabled
401      * @since 1.4.1
402      */

403
404     public boolean isParameterActionsEnabled() {
405         return parameterActionsEnabled;
406     }
407
408     /**
409      * Set to true to enable parameter actions. Parameter actions are disabled by default because of the inherent
410      * security risks involved in using them.
411      *
412      * @param parameterActionsEnabled True to enabled parameter actions
413      * @since 1.4.1
414      */

415
416     public void setParameterActionsEnabled(boolean parameterActionsEnabled) {
417         this.parameterActionsEnabled = parameterActionsEnabled;
418     }
419
420     /**
421      * Set to true to enable parameter actions. Parameter actions are disabled by default because of the inherent
422      * security risks involved in using them.
423      *
424      * @param parameterActionsEnabled True to enabled parameter actions
425      * @since 1.4.1
426      */

427
428     public void setParameterActionsEnabled(String JavaDoc parameterActionsEnabled) {
429         setParameterActionsEnabled("true".equals(parameterActionsEnabled));
430     }
431
432     /**
433      * Get the default page. This value will be used when directories are requested. By default this returns
434      * 'index.html'.
435      *
436      * @return The default page
437      */

438
439     public String JavaDoc getDefaultPage() {
440         return defaultPage;
441     }
442
443     /**
444      * Set the default page. This value will be used when directories are requested.
445      *
446      * @param defaultPage The new default page
447      */

448
449     public void setDefaultPage(String JavaDoc defaultPage) {
450         this.defaultPage = defaultPage;
451     }
452
453     /**
454      * Get the default template. This method returns the name of the template which will be used when no template is
455      * specified in a page's configuration.
456      *
457      * @return The default template
458      */

459
460     public String JavaDoc getDefaultTemplate() {
461         return defaultTemplate;
462     }
463
464     /**
465      * Set the default template. The default template will be used when no template is specified in a page's
466      * configuration.
467      *
468      * @param defaultTemplate The new default template
469      */

470
471     public void setDefaultTemplate(String JavaDoc defaultTemplate) {
472         this.defaultTemplate = defaultTemplate;
473     }
474
475     /**
476      * Get the default mime type. This method delegates to the current MimeTypeMap.
477      *
478      * @return The default mime type
479      */

480
481     public String JavaDoc getDefaultMimeType() {
482         return getMimeTypeMap().getDefaultMimeType();
483     }
484
485     /**
486      * Set the default mime type. This method delegates to the current MimeTypeMap.
487      *
488      * @param defaultMimeType The new default mime type
489      */

490
491     public void setDefaultMimeType(String JavaDoc defaultMimeType) {
492         getMimeTypeMap().setDefaultMimeType(defaultMimeType);
493     }
494
495     /**
496      * Returns true if reserved names should be protected in the JPublishContext. This method returns false by default.
497      *
498      * @return True if reserved names should be protected
499      */

500
501     public boolean isProtectReservedNames() {
502         return protectReservedNames;
503     }
504
505     /**
506      * Set to true to protect reserved names in the JPublishContext.
507      *
508      * @param protectReservedNames True to protect reserved names
509      */

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     /**
519      * Set to "true" to protect reserved names in the JPublishContext.
520      *
521      * @param protectReservedNames "true" to protect reserved names
522      */

523
524     public void setProtectReservedNames(String JavaDoc protectReservedNames) {
525         setProtectReservedNames("true".equals(protectReservedNames));
526     }
527
528     /**
529      * Return true if debugging is enabled.
530      *
531      * @return True if debugging is enabled
532      */

533
534     public boolean isDebug() {
535         return debug;
536     }
537
538     /**
539      * Set to true to enable debugging.
540      *
541      * @param debug True to enable debugging
542      */

543
544     public void setDebug(boolean debug) {
545         if (debug) {
546             log.info("JPublish debugging enabled.");
547         }
548         this.debug = debug;
549     }
550
551     /**
552      * Set to "true" to enable debugging.
553      *
554      * @param debug "true" to enable debugging
555      */

556
557     public void setDebug(String JavaDoc debug) {
558         setDebug("true".equals(debug));
559     }
560
561     /**
562      * Get a List of all loaded modules.
563      *
564      * @return List of loaded modules
565      */

566
567     public List getModules() {
568         return modules;
569     }
570
571     /**
572      * Get a list of all registered repositories. The list returned by this method is mutable so you can add and remove
573      * repositories at runtime.
574      *
575      * @return A list of all registered Repository objects
576      */

577
578     public List getRepositories() {
579         return repositories;
580     }
581
582     /**
583      * Get a List of all exception handlers for the given path. The path can include the '*' wildcard. If there are no
584      * exception handlers for the given path then this method will return the default handlers. If you do not want any
585      * handlers then define a exception handler mapping with no defined exception handlers.
586      *
587      * @param path The path
588      * @return A List of exception handlers
589      */

590
591     public List getExceptionHandlers(String JavaDoc path) {
592         List exceptionHandlers = (List) cachedExceptionHandlers.get(path);
593         if (exceptionHandlers == null) {
594             Iterator keys = exceptionHandlerMap.keySet().iterator();
595             while (keys.hasNext()) {
596                 String JavaDoc key = (String JavaDoc) 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     /**
610      * Get the List of default exception handlers. These exception handlers should be used whenever no exception
611      * handlers are defined.
612      *
613      * @return The default exception handlers
614      */

615
616     public List getDefaultExceptionHandlers() {
617         return defaultExceptionHandlers;
618     }
619
620     /**
621      * Get a Repository by name. If the repository was not registered then this method will return null. The Repository
622      * name returned from Repository.getName() is used as the test against the name parameter.
623      *
624      * @param name The name of the Repository
625      * @return The repository or null
626      */

627
628     public Repository getRepository(String JavaDoc 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     /**
640      * Get the site's ActionManager.
641      *
642      * @return The ActionManager
643      * @see org.jpublish.action.ActionManager
644      */

645
646     public ActionManager getActionManager() {
647         return defaultActionManager;
648     }
649
650     /**
651      * Gets the actionManager attribute of the SiteContext object
652      *
653      * @param name Description of the Parameter
654      * @return The actionManager value
655      */

656     public ActionManager getActionManager(String JavaDoc name) {
657         return (ActionManager) actionManagers.get(name);
658     }
659
660     /**
661      * Sets the actionManager attribute of the SiteContext object
662      *
663      * @param name The new actionManager value
664      * @param actionManager The new actionManager value
665      */

666     public void setActionManager(String JavaDoc name, ActionManager actionManager) {
667         actionManagers.put(name, actionManager);
668     }
669
670     /**
671      * Sets the defaultActionManager attribute of the SiteContext object
672      *
673      * @param defaultActionManager The new defaultActionManager value
674      */

675     public void setDefaultActionManager(ActionManager defaultActionManager) {
676         this.defaultActionManager = defaultActionManager;
677     }
678
679     /**
680      * Get the site's default PageManager.
681      *
682      * @return The default PageManager
683      * @see org.jpublish.page.PageManager
684      */

685     public PageManager getPageManager() {
686         return defaultPageManager;
687     }
688
689     /**
690      * Get the named PageManager.
691      *
692      * @param name Description of the Parameter
693      * @return The named page manager
694      */

695     public PageManager getPageManager(String JavaDoc name) {
696         return (PageManager) pageManagers.get(name);
697     }
698
699     /**
700      * Set the named PageManager.
701      *
702      * @param name The name
703      * @param pageManager The PageManager
704      */

705     public void setPageManager(String JavaDoc name, PageManager pageManager) {
706         pageManagers.put(name, pageManager);
707     }
708
709     /**
710      * Set the default PageManager.
711      *
712      * @param defaultPageManager The default PageManager
713      */

714     public void setDefaultPageManager(PageManager defaultPageManager) {
715         this.defaultPageManager = defaultPageManager;
716     }
717
718     /**
719      * Get the site's default TemplateManager.
720      *
721      * @return The TemplateManager
722      * @see org.jpublish.template.TemplateManager
723      */

724     public TemplateManager getTemplateManager() {
725         return defaultTemplateManager;
726     }
727
728     /**
729      * Gets the named TemplateManager
730      *
731      * @param name The TemplateManager name
732      * @return The named TemplateManager
733      */

734     public TemplateManager getTemplateManager(String JavaDoc name) {
735         return (TemplateManager) templateManagers.get(name);
736     }
737
738     public void setTemplateManager(String JavaDoc name, TemplateManager templateManager) {
739         templateManagers.put(name, templateManager);
740     }
741
742     /**
743      * Sets the defaultTemplateManager attribute of the SiteContext object
744      *
745      * @param defaultTemplateManager The new defaultTemplateManager value
746      */

747     public void setDefaultTemplateManager(TemplateManager defaultTemplateManager) {
748         this.defaultTemplateManager = defaultTemplateManager;
749     }
750
751     /**
752      * Get the site's static ResourceManager.
753      *
754      * @return The ResourceManager
755      */

756     public ResourceManager getStaticResourceManager() {
757         return defaultStaticResourceManager;
758     }
759
760     public ResourceManager getStaticResourceManager(String JavaDoc name) {
761         return (ResourceManager) staticResourceManagers.get(name);
762     }
763
764     public void setStaticResourceManager(String JavaDoc name,
765             ResourceManager resourceManager) {
766         staticResourceManagers.put(name, resourceManager);
767     }
768
769     public void setDefaultStaticResourceManager(ResourceManager defaultStaticResourceManager) {
770         this.defaultStaticResourceManager = defaultStaticResourceManager;
771     }
772
773     /**
774      * Get the site's raw ResourceManager.
775      *
776      * @return The ResourceManager
777      */

778
779     public ResourceManager getRawResourceManager() {
780         return rawResourceManager;
781     }
782
783     /**
784      * Get the site's default ViewRenderer which is used to render content.
785      *
786      * @return The ViewRenderer
787      */

788
789     public ViewRenderer getViewRenderer() {
790         return defaultViewRenderer;
791     }
792
793     /**
794      * Get the named ViewRenderer.
795      *
796      * @param name The view renderer name
797      * @return The ViewRenderer or null
798      */

799
800     public ViewRenderer getViewRenderer(String JavaDoc name) {
801         return (ViewRenderer) viewRenderers.get(name);
802     }
803
804     /**
805      * Set the named ViewRenderer.
806      *
807      * @param name The ViewRenderer name
808      * @param viewRenderer The ViewRenderer
809      */

810
811     public void setViewRenderer(String JavaDoc name, ViewRenderer viewRenderer) {
812         viewRenderers.put(name, viewRenderer);
813     }
814
815     /**
816      * Set the Site's default ViewRenderer.
817      *
818      * @param viewRenderer Thw default ViewRenderer
819      */

820
821     public void setDefaultViewRenderer(ViewRenderer viewRenderer) {
822         this.defaultViewRenderer = viewRenderer;
823     }
824
825     /**
826      * Get the site's ComponentManager.
827      *
828      * @return The ComponentManager
829      */

830
831     public ComponentManager getComponentManager() {
832         return componentManager;
833     }
834
835     /**
836      * Get the site's MimeType map. Mime types can be mapped to file suffixes.
837      *
838      * @return The MimeTypeMap
839      */

840
841     public MimeTypeMap getMimeTypeMap() {
842         return mimeTypeMap;
843     }
844
845     /**
846      * Return the CharacterEncodingManager.
847      *
848      * @return The CharacterEncodingManager
849      */

850
851     public CharacterEncodingManager getCharacterEncodingManager() {
852         return characterEncodingManager;
853     }
854
855     /**
856      * Retrieve a ContentSource object for the specified named content. The content name must include the origin prefix.
857      * In the case of content pulled from a repository this would be: <blockquote> <code>repository:repository_name://path/to/content</code>
858      * </blockquote> <p>
859      *
860      * In the case of templates, this would be:</p> <blockquote> <code>template:/path/to/template</code> </blockquote>
861      * <p>
862      *
863      * This method must return null if the named content can not be found.</p>
864      *
865      * @param name The content name
866      * @return The ContentSource object
867      */

868
869     public ContentSource getContentSource(String JavaDoc name) {
870         try {
871             if (log.isDebugEnabled()) {
872                 log.debug("getContent(" + name + ")");
873             }
874             InternalURI uri = InternalURIParser.getInstance().parse(name);
875             String JavaDoc protocol = uri.getProtocol();
876             if (protocol.equalsIgnoreCase(InternalURIParser.TEMPLATE_PROTOCOL)) {
877                 String JavaDoc 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 JavaDoc 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 JavaDoc repositoryName =
893                         ((RepositoryURI) uri).getRepositoryName();
894                 Repository repository = getRepository(repositoryName);
895                 String JavaDoc 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 JavaDoc t) {
906             // this is necessary to support FreeMarker for the moment.
907
// FreeMarker requires that this method return null
908

909             log.error("Error getting content: " + t.getMessage());
910             if (log.isDebugEnabled()) {
911                 t.printStackTrace();
912             }
913             return null;
914         }
915     }
916
917     /**
918      * Get the path dispatcher used when JPublish will dispatch a request to another servlet.
919      *
920      * @return The PathDispatcher
921      */

922     public PathDispatcher getPathDispatcher() {
923         return pathDispatcher;
924     }
925
926     // Start Attribute support
927

928     /**
929      * Get the named site attribute. Returns null if there is no site attribute for the specified name.
930      *
931      * @param name The attribute name
932      * @return The site attribute or null
933      */

934     public Object JavaDoc getAttribute(String JavaDoc name) {
935         return attributes.get(name);
936     }
937
938     /**
939      * Set the named site attribute.
940      *
941      * @param name The site attribute name
942      * @param value The site attribute value
943      */

944     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
945         attributes.put(name, value);
946     }
947
948     /**
949      * Remove the named site attribute.
950      *
951      * @param name The site attribute name
952      */

953     public void removeAttribute(String JavaDoc name) {
954         attributes.remove(name);
955     }
956
957     /**
958      * Get an Iterator of all names for site attributes.
959      *
960      * @return Iterator of attribute names
961      */

962     public Iterator getAttributeNames() {
963         return attributes.keySet().iterator();
964     }
965
966     // End Attribute support
967

968     /**
969      * Load the site configuration from the given path.
970      *
971      * @param configurationPath The configuration path
972      * @throws IOException
973      * @throws ConfigurationException
974      */

975     public void loadConfiguration(String JavaDoc configurationPath)
976             throws IOException JavaDoc, ConfigurationException {
977         if (configurationPath == null) {
978             configurationPath = DEFAULT_JPUBLISH_CONFIGURATION;
979         }
980
981         FileObject configurationFile = contextFileSystemManager.resolveFile(configurationPath);
982
983         InputStream JavaDoc 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     /**
994      * Load the site configuration from the given InputStream. The InputStream must be attached to an XML document.
995      *
996      * @param configurationPath The configuration path
997      * @param in The InputStream
998      * @throws IOException
999      * @throws ConfigurationException
1000     */

1001    public void loadConfiguration(String JavaDoc configurationPath, InputStream JavaDoc in)
1002            throws IOException JavaDoc, ConfigurationException {
1003        ConfigurationFactory configFactory =
1004                SAXConfigurationFactory.getInstance();
1005        loadConfiguration(configFactory.getConfiguration(configurationPath, in));
1006    }
1007
1008    /**
1009     * Load the site configuration from the given Configuration object.
1010     *
1011     * @param configuration The Configuration object
1012     * @throws ConfigurationException
1013     */

1014    public void loadConfiguration(Configuration configuration)
1015            throws ConfigurationException {
1016        try {
1017            Iterator iter = null;
1018
1019            log.debug("Loading configuration");
1020
1021            // set the configuration suffix
1022
setConfigurationSuffix(configuration.getChildValue("configuration-suffix", DEFAULT_CONFIGURATION_SUFFIX));
1023
1024            // load the default ActionManager
1025
log.debug("Creating default ActionManager");
1026            loadDefaultActionManager();
1027
1028            // load the ActionManagers
1029
log.debug("Creating ActionManagers");
1030            iter = configuration.getChildren("action-manager").iterator();
1031            while (iter.hasNext()) {
1032                loadActionManager((Configuration) iter.next());
1033            }
1034
1035            // load the default PageManager
1036
log.debug("Creating default PageManager");
1037            loadDefaultPageManager();
1038
1039            // load the PageManagers
1040
log.debug("Creating PageManagers");
1041            iter = configuration.getChildren("page-manager").iterator();
1042            while (iter.hasNext()) {
1043                loadPageManager((Configuration) iter.next());
1044            }
1045            
1046            // load the default TemplateManager
1047
log.debug("Creating default TemplateManager");
1048            loadDefaultTemplateManager();
1049
1050            // load the TemplateManagers
1051
log.debug("Creating TemplateManagers");
1052            iter = configuration.getChildren("template-manager").iterator();
1053            while (iter.hasNext()) {
1054                loadTemplateManager((Configuration) iter.next());
1055            }
1056            
1057            // load the default static ResourceManager
1058
log.debug("Creating default static ResourceManager");
1059            loadDefaultStaticResourceManager();
1060
1061            // load the static ResourceManagers
1062
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            // load the raw ResourceManager
1069
log.debug("Creating raw ResourceManager");
1070            loadRawResourceManager(configuration.getChild("raw-resource-manager"));
1071
1072            // load the default ViewRenderer
1073
log.debug("Creating default ViewRenderer");
1074            loadDefaultViewRenderer();
1075
1076            // load the ViewRenderers
1077
log.debug("Creating ViewRenderers");
1078            iter = configuration.getChildren("view-renderer").iterator();
1079            while (iter.hasNext()) {
1080                loadViewRenderer((Configuration) iter.next());
1081            }
1082
1083            // load modules
1084
Iterator moduleElements =
1085                    configuration.getChildren("module").iterator();
1086            while (moduleElements.hasNext()) {
1087                Configuration moduleElement =
1088                        (Configuration) moduleElements.next();
1089                loadModule(moduleElement);
1090            }
1091
1092            // load repository references
1093
Iterator repositoryElements =
1094                    configuration.getChildren("repository").iterator();
1095            while (repositoryElements.hasNext()) {
1096                Configuration repositoryElement =
1097                        (Configuration) repositoryElements.next();
1098                String JavaDoc 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            // load the ComponentManager
1110
log.debug("Creating ComponentManager");
1111            loadComponentManager(configuration.getChild("component-manager"));
1112
1113            // set the action identifier
1114
setActionIdentifier(configuration.getChildValue("action-identifier",
1115                    DEFAULT_ACTION_IDENTIFIER));
1116
1117            // Set defaults
1118
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            // protect reserved names in the context
1125
setProtectReservedNames(configuration.getChildValue("protect-reserved-names", "false"));
1126
1127            // enable or disable parameter actions
1128
setParameterActionsEnabled(configuration.getChildValue("parameter-actions-enabled", "false"));
1129
1130            // enable or disable debugging
1131
setDebug(configuration.getChildValue("debug", "false"));
1132
1133            // load character encoding maps
1134
log.debug("Loading CharacterEncodingMaps");
1135            characterEncodingManager.loadConfiguration(configuration);
1136
1137            // load the mime type map
1138
Iterator mimeTypeMapElements =
1139                    configuration.getChildren("mime-mapping").iterator();
1140            while (mimeTypeMapElements.hasNext()) {
1141                Configuration mimeTypeMapElement =
1142                        (Configuration) mimeTypeMapElements.next();
1143                String JavaDoc ext = mimeTypeMapElement.getAttribute("ext");
1144                String JavaDoc mimeType = mimeTypeMapElement.getAttribute("mimetype");
1145                mimeTypeMap.put(ext, mimeType);
1146            }
1147
1148            // load default exception handlers
1149
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 JavaDoc className =
1160                            defaultExceptionHandlerElement.getAttribute(ATTRIBUTE_CLASSNAME);
1161                    defaultExceptionHandlers.add(ClassUtilities.loadClass(className).newInstance());
1162                }
1163            }
1164
1165            // load exception handlers
1166
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            // configure the path dispatcher
1175
pathDispatcher = new PathDispatcher(this);
1176            pathDispatcher.loadConfiguration(configuration);
1177        } catch (ClassNotFoundException JavaDoc e) {
1178            Object JavaDoc[] args = {};
1179            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1180                    "classNotFound", args), e, configuration);
1181        } catch (InstantiationException JavaDoc e) {
1182            Object JavaDoc[] args = {};
1183            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1184                    "classInstantiationError", args), e,
1185                    configuration);
1186        } catch (IllegalAccessException JavaDoc e) {
1187            Object JavaDoc[] args = {};
1188            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1189                    "classIllegalAccessError", args), e,
1190                    configuration);
1191        }
1192    }
1193
1194    /**
1195     * Load the default action manager. This action manager can be replaced by specifying a action-manager element
1196     * without a name attribute.
1197     *
1198     * @throws ConfigurationException
1199     */

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 JavaDoc e) {
1207            Object JavaDoc[] args = {DEFAULT_ACTION_MANAGER};
1208            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1209                    "defaultActionManagerClassIllegalAccessError", args), e);
1210        } catch (InstantiationException JavaDoc e) {
1211            Object JavaDoc[] args = {DEFAULT_ACTION_MANAGER};
1212            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1213                    "defaultActionManagerClassIllegalAccessError", args), e);
1214        } catch (IllegalAccessException JavaDoc e) {
1215            Object JavaDoc[] args = {DEFAULT_ACTION_MANAGER};
1216            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1217                    "defaultActionManagerClassIllegalAccessError", args), e);
1218        }
1219    }
1220
1221    /**
1222     * Load the action manager.
1223     *
1224     * @param configuration The configuration
1225     * @throws ConfigurationException
1226     */

1227
1228    protected void loadActionManager(Configuration configuration)
1229            throws ConfigurationException {
1230        String JavaDoc actionManagerClass = null;
1231        try {
1232            if (configuration != null) {
1233                actionManagerClass = configuration.getAttribute(ATTRIBUTE_CLASSNAME, DEFAULT_ACTION_MANAGER);
1234                String JavaDoc 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 JavaDoc e) {
1248            Object JavaDoc[] args = {actionManagerClass};
1249            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1250                    "actionManagerClassNotFound", args), e, configuration);
1251        } catch (InstantiationException JavaDoc e) {
1252            Object JavaDoc[] args = {actionManagerClass};
1253            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1254                    "actionManagerClassInstantiationError", args), e,
1255                    configuration);
1256        } catch (IllegalAccessException JavaDoc e) {
1257            Object JavaDoc[] args = {actionManagerClass};
1258            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1259                    "actionManagerClassIllegalAccessError", args), e,
1260                    configuration);
1261        }
1262    }
1263
1264    /**
1265     * Load the default page manager. This page manager can be replaced by specifying a page-manager element without a
1266     * name attribute.
1267     *
1268     * @throws ConfigurationException
1269     */

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 JavaDoc e) {
1277            Object JavaDoc[] args = {DEFAULT_PAGE_MANAGER};
1278            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1279                    "defaultPageManagerClassIllegalAccessError", args), e);
1280        } catch (InstantiationException JavaDoc e) {
1281            Object JavaDoc[] args = {DEFAULT_PAGE_MANAGER};
1282            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1283                    "defaultPageManagerClassIllegalAccessError", args), e);
1284        } catch (IllegalAccessException JavaDoc e) {
1285            Object JavaDoc[] args = {DEFAULT_PAGE_MANAGER};
1286            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1287                    "defaultPageManagerClassIllegalAccessError", args), e);
1288        }
1289    }
1290
1291    /**
1292     * Load the page manager.
1293     *
1294     * @param configuration The configuration object
1295     * @throws ConfigurationException
1296     */

1297
1298    protected void loadPageManager(Configuration configuration)
1299            throws ConfigurationException {
1300        String JavaDoc pageManagerClass = null;
1301        try {
1302            if (configuration != null) {
1303                pageManagerClass = configuration.getAttribute(ATTRIBUTE_CLASSNAME, DEFAULT_PAGE_MANAGER);
1304                String JavaDoc 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 JavaDoc e) {
1319            Object JavaDoc[] args = {pageManagerClass};
1320            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1321                    "pageManagerClassNotFound", args), e, configuration);
1322        } catch (InstantiationException JavaDoc e) {
1323            Object JavaDoc[] args = {pageManagerClass};
1324            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1325                    "pageManagerClassInstantiationError", args), e,
1326                    configuration);
1327        } catch (IllegalAccessException JavaDoc e) {
1328            Object JavaDoc[] args = {pageManagerClass};
1329            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1330                    "pageManagerClassIllegalAccessError", args), e,
1331                    configuration);
1332        }
1333    }
1334
1335    /**
1336     * Load the default template manager. This template manager can be replaced by specifying a template-manager element
1337     * without a name attribute.
1338     *
1339     * @throws ConfigurationException
1340     */

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 JavaDoc e) {
1348            Object JavaDoc[] args = {DEFAULT_TEMPLATE_MANAGER};
1349            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1350                    "defaultTemplateManagerClassIllegalAccessError", args), e);
1351        } catch (InstantiationException JavaDoc e) {
1352            Object JavaDoc[] args = {DEFAULT_TEMPLATE_MANAGER};
1353            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1354                    "defaultTemplateManagerClassIllegalAccessError", args), e);
1355        } catch (IllegalAccessException JavaDoc e) {
1356            Object JavaDoc[] args = {DEFAULT_TEMPLATE_MANAGER};
1357            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1358                    "defaultTemplateManagerClassIllegalAccessError", args), e);
1359        }
1360    }
1361
1362    /**
1363     * Load the template manager.
1364     *
1365     * @param configuration The Configuration object
1366     * @throws ConfigurationException
1367     */

1368
1369    protected void loadTemplateManager(Configuration configuration)
1370            throws ConfigurationException {
1371        String JavaDoc templateManagerClass = null;
1372        try {
1373            if (configuration != null) {
1374                templateManagerClass = configuration.getAttribute(ATTRIBUTE_CLASSNAME, DEFAULT_TEMPLATE_MANAGER);
1375                String JavaDoc 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 JavaDoc e) {
1391            Object JavaDoc[] args = {templateManagerClass};
1392            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1393                    "templateManagerClassNotFound", args), e, configuration);
1394        } catch (InstantiationException JavaDoc e) {
1395            Object JavaDoc[] args = {templateManagerClass};
1396            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1397                    "templateManagerClassInstantiationError", args), e,
1398                    configuration);
1399        } catch (IllegalAccessException JavaDoc e) {
1400            Object JavaDoc[] args = {templateManagerClass};
1401            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1402                    "templateManagerClassIllegalAccessError", args), e,
1403                    configuration);
1404        }
1405    }
1406
1407    /**
1408     * Load the default static resource manager. This resource manager can be replaced by specifying a resource-manager
1409     * element without a name attribute.
1410     *
1411     * @throws ConfigurationException
1412     */

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 JavaDoc e) {
1420            Object JavaDoc[] args = {DEFAULT_STATIC_RESOURCE_MANAGER};
1421            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1422                    "defaultStaticResourceManagerClassIllegalAccessError", args), e);
1423        } catch (InstantiationException JavaDoc e) {
1424            Object JavaDoc[] args = {DEFAULT_STATIC_RESOURCE_MANAGER};
1425            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1426                    "defaultStaticResourceManagerClassIllegalAccessError", args), e);
1427        } catch (IllegalAccessException JavaDoc e) {
1428            Object JavaDoc[] args = {DEFAULT_TEMPLATE_MANAGER};
1429            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1430                    "defaultStaticResourceManagerClassIllegalAccessError", args), e);
1431        }
1432    }
1433
1434    /**
1435     * Load the ResourceManager for static data. The static ResourceLoader is used for loading binary data. Actions are
1436     * executed prior to returning the static data to the client, which is what differentiates this ResourceManager from
1437     * the raw data ResourceManager.
1438     *
1439     * @param configuration The configuration
1440     * @throws ConfigurationException
1441     */

1442    protected void loadStaticResourceManager(Configuration configuration)
1443            throws ConfigurationException {
1444        String JavaDoc 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 JavaDoc 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 JavaDoc e) {
1469            Object JavaDoc[] args = {staticResourceManagerClass};
1470            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1471                    "staticResourceManagerClassNotFound", args), e, configuration);
1472        } catch (InstantiationException JavaDoc e) {
1473            Object JavaDoc[] args = {staticResourceManagerClass};
1474            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1475                    "staticResourceManagerClassInstantiationError", args), e,
1476                    configuration);
1477        } catch (IllegalAccessException JavaDoc e) {
1478            Object JavaDoc[] args = {staticResourceManagerClass};
1479            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1480                    "staticResourceManagerClassIllegalAccessError", args), e,
1481                    configuration);
1482        }
1483    }
1484
1485    /**
1486     * Load the ResourceManager for raw data. Resource files which are accessible through this manager are returned
1487     * immediately - no actions are executed.
1488     *
1489     * @param configuration The configuration
1490     * @throws ConfigurationException
1491     */

1492
1493    protected void loadRawResourceManager(Configuration configuration)
1494            throws ConfigurationException {
1495        String JavaDoc 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 JavaDoc e) {
1509            Object JavaDoc[] args = {rawResourceManagerClass};
1510            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1511                    "rawResourceManagerClassNotFound", args), e, configuration);
1512        } catch (InstantiationException JavaDoc e) {
1513            Object JavaDoc[] args = {rawResourceManagerClass};
1514            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1515                    "rawResourceManagerClassInstantiationError", args), e,
1516                    configuration);
1517        } catch (IllegalAccessException JavaDoc e) {
1518            Object JavaDoc[] args = {rawResourceManagerClass};
1519            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1520                    "rawResourceManagerClassIllegalAccessError", args), e,
1521                    configuration);
1522        }
1523    }
1524
1525    /**
1526     * Load the default view renderer. This view renderer can be replaced by specifying a view-renderer element without
1527     * a name attribute.
1528     *
1529     * @throws ConfigurationException
1530     */

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 JavaDoc e) {
1539            Object JavaDoc[] args = {DEFAULT_VIEW_RENDERER};
1540            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1541                    "defaultViewRendererClassNotFound", args), e);
1542        } catch (InstantiationException JavaDoc e) {
1543            Object JavaDoc[] args = {DEFAULT_VIEW_RENDERER};
1544            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1545                    "defaultViewRendererClassInstantiationError", args), e);
1546        } catch (IllegalAccessException JavaDoc e) {
1547            Object JavaDoc[] args = {DEFAULT_VIEW_RENDERER};
1548            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1549                    "defaultViewRendererClassIllegalAccessError", args), e);
1550        }
1551    }
1552
1553    /**
1554     * Load the view renderer. The configuration must not be null.
1555     *
1556     * @param configuration The configuration object
1557     * @throws ConfigurationException
1558     */

1559
1560    protected void loadViewRenderer(Configuration configuration)
1561            throws ConfigurationException {
1562        String JavaDoc name = null;
1563        String JavaDoc 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 JavaDoc e) {
1583            Object JavaDoc[] args = {viewRendererClass, name};
1584            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1585                    "viewRendererClassNotFound", args), e, configuration);
1586        } catch (InstantiationException JavaDoc e) {
1587            Object JavaDoc[] args = {viewRendererClass, name};
1588            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1589                    "viewRendererClassInstantiationError", args), e,
1590                    configuration);
1591        } catch (IllegalAccessException JavaDoc e) {
1592            Object JavaDoc[] args = {viewRendererClass, name};
1593            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1594                    "viewRendererClassIllegalAccessError", args), e,
1595                    configuration);
1596        }
1597    }
1598
1599    /**
1600     * Load the component manager.
1601     *
1602     * @param configuration The configuration
1603     * @throws ConfigurationException
1604     */

1605
1606    protected void loadComponentManager(Configuration configuration)
1607            throws ConfigurationException {
1608        String JavaDoc 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 JavaDoc e) {
1620            Object JavaDoc[] args = {componentManagerClass};
1621            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1622                    "componentManagerClassNotFound", args), e, configuration);
1623        } catch (InstantiationException JavaDoc e) {
1624            Object JavaDoc[] args = {componentManagerClass};
1625            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1626                    "componentManagerClassInstantiationError", args), e,
1627                    configuration);
1628        } catch (IllegalAccessException JavaDoc e) {
1629            Object JavaDoc[] args = {componentManagerClass};
1630            throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1631                    "componentManagerClassIllegalAccessError", args), e,
1632                    configuration);
1633        }
1634    }
1635
1636    /**
1637     * Load the module specified in the given configuration object
1638     *
1639     * @param configuration The module configuration object
1640     */

1641
1642    protected void loadModule(Configuration configuration) {
1643        String JavaDoc 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 JavaDoc e) {
1649            log.error("Unable to load module " + className);
1650        }
1651    }
1652
1653    /**
1654     * Load the exception handler.
1655     *
1656     * @param configuration The exception handler configuration object
1657     * @throws ConfigurationException Description of the Exception
1658     */

1659
1660    protected void loadExceptionHandlerMap(Configuration configuration)
1661            throws ConfigurationException {
1662        String JavaDoc 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 JavaDoc 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 JavaDoc e) {
1680                Object JavaDoc[] args = {exceptionHandlerClass};
1681                throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1682                        "exceptionHandlerClassNotFound", args), e, configuration);
1683            } catch (InstantiationException JavaDoc e) {
1684                Object JavaDoc[] args = {exceptionHandlerClass};
1685                throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE,
1686                        "exceptionHandlerClassInstantiationError", args), e,
1687                        configuration);
1688            } catch (IllegalAccessException JavaDoc e) {
1689                Object JavaDoc[] 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