001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portlets.layout;
018:
019: import java.io.IOException;
020: import java.util.HashMap;
021: import java.util.Locale;
022: import java.util.Map;
023: import java.util.StringTokenizer;
024:
025: import javax.portlet.ActionRequest;
026: import javax.portlet.ActionResponse;
027: import javax.portlet.PortletConfig;
028: import javax.portlet.PortletException;
029: import javax.portlet.PortletPreferences;
030: import javax.portlet.RenderRequest;
031: import javax.portlet.RenderResponse;
032:
033: import org.apache.commons.configuration.Configuration;
034: import org.apache.commons.configuration.ConfigurationException;
035: import org.apache.commons.configuration.PropertiesConfiguration;
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.apache.jetspeed.CommonPortletServices;
039: import org.apache.jetspeed.JetspeedActions;
040: import org.apache.jetspeed.PortalReservedParameters;
041: import org.apache.jetspeed.capabilities.CapabilityMap;
042: import org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent;
043: import org.apache.jetspeed.components.portletregistry.PortletRegistry;
044: import org.apache.jetspeed.container.window.PortletWindowAccessor;
045: import org.apache.jetspeed.layout.JetspeedPowerTool;
046: import org.apache.jetspeed.locator.LocatorDescriptor;
047: import org.apache.jetspeed.locator.TemplateDescriptor;
048: import org.apache.jetspeed.locator.TemplateLocator;
049: import org.apache.jetspeed.locator.TemplateLocatorException;
050: import org.apache.jetspeed.om.page.Fragment;
051: import org.apache.jetspeed.om.page.Page;
052: import org.apache.jetspeed.page.PageManager;
053: import org.apache.jetspeed.request.RequestContext;
054: import org.apache.jetspeed.velocity.JetspeedPowerToolFactory;
055: import org.apache.pluto.om.window.PortletWindow;
056:
057: /**
058: */
059: public class LayoutPortlet extends
060: org.apache.portals.bridges.common.GenericServletPortlet {
061: public static final String GENERIC_TEMPLATE_TYPE = "generic";
062:
063: public static final String FRAGMENT_PROCESSING_ERROR_PREFIX = "fragment.processing.error.";
064:
065: public static final String FRAGMENT_ATTR = "fragment";
066:
067: public static final String LAYOUT_ATTR = "layout";
068:
069: public static final String HIDDEN = "hidden";
070:
071: public static final String LAYOUT_TEMPLATE_TYPE = "layout";
072:
073: public static final String DECORATOR_TYPE = "decorator";
074:
075: public static final String PARAM_SOLO_PAGE = "SoloPage";
076:
077: /** Commons logging */
078: protected final static Log log = LogFactory
079: .getLog(LayoutPortlet.class);
080:
081: protected PortletRegistry registry;
082: protected PageManager pageManager;
083: protected JetspeedPowerToolFactory jptFactory;
084: protected TemplateLocator templateLocator;
085: protected PortletEntityAccessComponent entityAccess;
086: protected PortletWindowAccessor windowAccess;
087: protected TemplateLocator decorationLocator;
088:
089: private Map layoutTemplatesCache = new HashMap();
090: public static final String DEFAULT_TEMPLATE_EXT = ".vm";
091: public static final String TEMPLATE_EXTENSION_KEY = "template.extension";
092: public static final String DEFAULT_TEMPLATE_TYPE = "velocity";
093: public static final String TEMPLATE_TYPE_KEY = "template.type";
094:
095: public void init(PortletConfig config) throws PortletException {
096: super .init(config);
097:
098: registry = (PortletRegistry) getPortletContext().getAttribute(
099: CommonPortletServices.CPS_REGISTRY_COMPONENT);
100: if (null == registry) {
101: throw new PortletException(
102: "Failed to find the Portlet Registry on portlet initialization");
103: }
104: pageManager = (PageManager) getPortletContext().getAttribute(
105: CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);
106: if (null == pageManager) {
107: throw new PortletException(
108: "Failed to find the Page Manager on portlet initialization");
109: }
110: jptFactory = (JetspeedPowerToolFactory) getPortletContext()
111: .getAttribute(
112: CommonPortletServices.CPS_JETSPEED_POWERTOOL_FACTORY);
113: if (null == jptFactory) {
114: throw new PortletException(
115: "Failed to find the JPT Factory on portlet initialization");
116: }
117:
118: entityAccess = (PortletEntityAccessComponent) getPortletContext()
119: .getAttribute(
120: CommonPortletServices.CPS_ENTITY_ACCESS_COMPONENT);
121: if (null == entityAccess) {
122: throw new PortletException(
123: "Failed to find the Entity Access on portlet initialization");
124: }
125:
126: windowAccess = (PortletWindowAccessor) getPortletContext()
127: .getAttribute(
128: CommonPortletServices.CPS_WINDOW_ACCESS_COMPONENT);
129: if (null == windowAccess) {
130: throw new PortletException(
131: "Failed to find the Window Access on portlet initialization");
132: }
133:
134: templateLocator = (TemplateLocator) getPortletContext()
135: .getAttribute("TemplateLocator");
136: decorationLocator = (TemplateLocator) getPortletContext()
137: .getAttribute("DecorationLocator");
138: }
139:
140: public void doHelp(RenderRequest request, RenderResponse response)
141: throws PortletException, IOException {
142: RequestContext context = getRequestContext(request);
143: response.setContentType(context.getMimeType());
144: JetspeedPowerTool jpt = getJetspeedPowerTool(request);
145:
146: String absHelpPage = "";
147:
148: // request.setAttribute(PortalReservedParameters.PAGE_ATTRIBUTE, getPage(request));
149: // request.setAttribute("fragment", getFragment(request, false));
150:
151: try {
152: String helpPage = (String) request.getPortletSession()
153: .getAttribute(
154: PortalReservedParameters.PAGE_LAYOUT_HELP);
155: if (helpPage == null) {
156: PortletPreferences prefs = request.getPreferences();
157: helpPage = prefs.getValue(PARAM_HELP_PAGE, null);
158: if (helpPage == null) {
159: helpPage = this .getInitParameter(PARAM_HELP_PAGE);
160: if (helpPage == null)
161: helpPage = "columns";
162: }
163: request.getPortletSession().setAttribute(
164: PortalReservedParameters.PAGE_LAYOUT_HELP,
165: helpPage);
166: }
167:
168: String templateKey = helpPage + "/"
169: + JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE + "-help";
170: CachedTemplate ct = (CachedTemplate) layoutTemplatesCache
171: .get(templateKey);
172: if (ct == null) {
173: TemplateDescriptor template = null;
174: Configuration props = getConfiguration(request,
175: helpPage);
176: String ext = (String) props
177: .getString(TEMPLATE_EXTENSION_KEY);
178: String path = helpPage + "/"
179: + JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE
180: + "-help" + ext;
181: template = jpt.getTemplate(path,
182: JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE);
183: if (template == null) {
184: String msg = "*** FAILED getTemplate:" + path;
185: throw new TemplateLocatorException(msg);
186: } else {
187: synchronized (layoutTemplatesCache) {
188: ct = new CachedTemplate(templateKey, template,
189: props);
190: layoutTemplatesCache.put(templateKey, ct);
191: }
192: }
193: }
194:
195: absHelpPage = ct.getTemplate().getAppRelativePath();
196: log.debug("Path to help page for LayoutPortlet "
197: + absHelpPage);
198: request.setAttribute(PARAM_VIEW_PAGE, absHelpPage);
199: } catch (TemplateLocatorException e) {
200: throw new PortletException("Unable to locate view page "
201: + absHelpPage, e);
202: }
203: super .doView(request, response);
204:
205: }
206:
207: /**
208: *
209: */
210: public void doView(RenderRequest request, RenderResponse response)
211: throws PortletException, IOException {
212: RequestContext context = getRequestContext(request);
213: response.setContentType(context.getMimeType());
214: PortletWindow window = context.getPortalURL()
215: .getNavigationalState().getMaximizedWindow();
216: boolean maximized = (window != null);
217: boolean solo = false;
218:
219: if (maximized) {
220: request.setAttribute("layout", getMaximizedLayout(request));
221: solo = JetspeedActions.SOLO_STATE.equals(context
222: .getPortalURL().getNavigationalState()
223: .getMappedState(window));
224: if (solo) {
225: maximized = false;
226: }
227: } else {
228: request.setAttribute("layout", getFragment(request, false));
229: }
230: String viewPage = null;
231: String absViewPage = null;
232: try {
233: JetspeedPowerTool jpt = getJetspeedPowerTool(request);
234: if (maximized) {
235: viewPage = (String) request
236: .getPortletSession()
237: .getAttribute(
238: PortalReservedParameters.PAGE_LAYOUT_MAX);
239: if (viewPage == null) {
240: PortletPreferences prefs = request.getPreferences();
241: viewPage = prefs.getValue(PARAM_MAX_PAGE, null);
242: if (viewPage == null) {
243: viewPage = this
244: .getInitParameter(PARAM_MAX_PAGE);
245: if (viewPage == null)
246: viewPage = "maximized";
247: }
248: request.getPortletSession().setAttribute(
249: PortalReservedParameters.PAGE_LAYOUT_MAX,
250: viewPage);
251: }
252: } else if (solo) {
253: viewPage = (String) request
254: .getPortletSession()
255: .getAttribute(
256: PortalReservedParameters.PAGE_LAYOUT_SOLO);
257: if (viewPage == null) {
258: PortletPreferences prefs = request.getPreferences();
259: viewPage = prefs.getValue(PARAM_SOLO_PAGE, null);
260: if (viewPage == null) {
261: viewPage = this
262: .getInitParameter(PARAM_SOLO_PAGE);
263: if (viewPage == null) {
264: viewPage = "solo";
265: }
266: }
267: request.getPortletSession().setAttribute(
268: PortalReservedParameters.PAGE_LAYOUT_SOLO,
269: viewPage);
270: }
271: } else {
272: viewPage = (String) request
273: .getPortletSession()
274: .getAttribute(
275: PortalReservedParameters.PAGE_LAYOUT_VIEW);
276: if (viewPage == null) {
277: PortletPreferences prefs = request.getPreferences();
278: viewPage = prefs.getValue(PARAM_VIEW_PAGE, null);
279: if (viewPage == null) {
280: viewPage = this
281: .getInitParameter(PARAM_VIEW_PAGE);
282: if (viewPage == null)
283: viewPage = "columns";
284: }
285: request.getPortletSession().setAttribute(
286: PortalReservedParameters.PAGE_LAYOUT_VIEW,
287: viewPage);
288: }
289: }
290:
291: String templateKey = viewPage + "/"
292: + JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE;
293: CachedTemplate ct = (CachedTemplate) layoutTemplatesCache
294: .get(templateKey);
295: if (ct == null) {
296: TemplateDescriptor template = null;
297: Configuration props = getConfiguration(request,
298: viewPage);
299: String ext = (String) props
300: .getString(TEMPLATE_EXTENSION_KEY);
301: String path = viewPage + "/"
302: + JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE + ext;
303:
304: template = jpt.getTemplate(path,
305: JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE);
306: if (template == null) {
307: String msg = "*** FAILED getTemplate:" + path;
308: throw new TemplateLocatorException(msg);
309: } else {
310: synchronized (layoutTemplatesCache) {
311: ct = new CachedTemplate(templateKey, template,
312: props);
313: layoutTemplatesCache.put(templateKey, ct);
314: }
315:
316: }
317: }
318: absViewPage = ct.getTemplate().getAppRelativePath();
319: log.debug("Path to view page for LayoutPortlet "
320: + absViewPage);
321: request.setAttribute(PARAM_VIEW_PAGE, absViewPage);
322: } catch (TemplateLocatorException e) {
323: throw new PortletException("Unable to locate view page "
324: + absViewPage, e);
325: }
326: super .doView(request, response);
327:
328: request
329: .removeAttribute(PortalReservedParameters.PAGE_ATTRIBUTE);
330: request.removeAttribute("fragment");
331: request.removeAttribute("layout");
332: request.removeAttribute("dispatcher");
333: }
334:
335: public void processAction(ActionRequest request,
336: ActionResponse response) throws PortletException,
337: IOException {
338: String page = request.getParameter("page");
339: String deleteFragmentId = request.getParameter("deleteId");
340: String portlets = request.getParameter("portlets");
341: if (deleteFragmentId != null && deleteFragmentId.length() > 0) {
342: removeFragment(page, deleteFragmentId);
343: } else if (portlets != null && portlets.length() > 0) {
344: int count = 0;
345: StringTokenizer tokenizer = new StringTokenizer(portlets,
346: ",");
347: while (tokenizer.hasMoreTokens()) {
348: String portlet = tokenizer.nextToken();
349: try {
350: if (portlet.startsWith("box_")) {
351: portlet = portlet.substring("box_".length());
352: addPortletToPage(page, portlet);
353: count++;
354: }
355: } catch (Exception e) {
356: log.error("failed to add portlet to page: "
357: + portlet);
358: }
359: }
360:
361: }
362: }
363:
364: protected void removeFragment(String pageId, String fragmentId) {
365: Page page = null;
366: try {
367: page = pageManager.getPage(pageId);
368:
369: } catch (Exception e) {
370: log.error("failed to remove portlet " + fragmentId
371: + " from page: " + pageId, e);
372: }
373: removeFragment(page, page.getRootFragment(), fragmentId);
374: }
375:
376: protected void removeFragment(Page page, Fragment root,
377: String fragmentId) {
378: try {
379: Fragment f = page.getFragmentById(fragmentId);
380: if (f == null) {
381: // ignore no longer existing fragment error
382: return;
383: }
384: root.getFragments().remove(f);
385: pageManager.updatePage(page);
386: } catch (Exception e) {
387: log.error("failed to remove portlet " + fragmentId
388: + " from page: " + page, e);
389: }
390: }
391:
392: protected void addPortletToPage(String pageId, String portletId) {
393: Page page = null;
394: try {
395: page = pageManager.getContentPage(pageId);
396: } catch (Exception e) {
397: log.error("failed to add portlet " + portletId
398: + " to page: " + pageId, e);
399: }
400: addPortletToPage(page, page.getRootFragment(), portletId);
401: }
402:
403: protected void addPortletToPage(Page page, Fragment root,
404: String portletId) {
405: try {
406: Fragment fragment = pageManager.newFragment();
407: fragment.setType(Fragment.PORTLET);
408: fragment.setName(portletId);
409:
410: root.getFragments().add(fragment);
411: pageManager.updatePage(page);
412: } catch (Exception e) {
413: log.error("failed to add portlet " + portletId
414: + " to page: " + page, e);
415: }
416: }
417:
418: /**
419: * <p>
420: * initJetspeedPowerTool
421: * </p>
422: *
423: * @param request
424: * @param response
425: * @return
426: * @throws PortletException
427: */
428: protected JetspeedPowerTool getJetspeedPowerTool(
429: RenderRequest request) throws PortletException {
430: JetspeedPowerTool tool = (JetspeedPowerTool) request
431: .getAttribute(PortalReservedParameters.JETSPEED_POWER_TOOL_REQ_ATTRIBUTE);
432: RequestContext requestContext = (RequestContext) request
433: .getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
434:
435: if (tool == null) {
436:
437: try {
438: if (requestContext == null) {
439: throw new IllegalStateException(
440: "LayoutPortlet unable to handle request because there is no RequestContext in "
441: + "the HttpServletRequest.");
442: }
443:
444: tool = this .jptFactory
445: .getJetspeedPowerTool(requestContext);
446: request
447: .setAttribute(
448: PortalReservedParameters.JETSPEED_POWER_TOOL_REQ_ATTRIBUTE,
449: tool);
450: }
451:
452: catch (Exception e1) {
453: throw new PortletException(
454: "Unable to init JetspeedPowerTool: "
455: + e1.toString(), e1);
456: }
457: }
458:
459: return tool;
460: }
461:
462: /**
463: *
464: * @param request
465: * @param maximized
466: * @return
467: */
468: protected Fragment getFragment(RenderRequest request,
469: boolean maximized) {
470: String attribute = (maximized) ? PortalReservedParameters.MAXIMIZED_FRAGMENT_ATTRIBUTE
471: : PortalReservedParameters.FRAGMENT_ATTRIBUTE;
472: return (Fragment) request.getAttribute(attribute);
473: }
474:
475: /**
476: *
477: * @param request
478: * @return
479: */
480: protected Fragment getMaximizedLayout(RenderRequest request) {
481: return (Fragment) request
482: .getAttribute(PortalReservedParameters.MAXIMIZED_LAYOUT_ATTRIBUTE);
483: }
484:
485: /**
486: *
487: * @param request
488: * @return
489: */
490: protected RequestContext getRequestContext(RenderRequest request) {
491: RequestContext requestContext = (RequestContext) request
492: .getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
493: if (requestContext != null) {
494: return requestContext;
495: } else {
496: throw new IllegalStateException(
497: "getRequestContext() failed as it appears that no RenderRequest is available within the RenderRequest");
498: }
499: }
500:
501: /**
502: * <p>
503: * doEdit
504: * </p>
505: *
506: * @see javax.portlet.GenericPortlet#doEdit(javax.portlet.RenderRequest,
507: * javax.portlet.RenderResponse)
508: * @param request
509: * @param response
510: * @throws PortletException
511: * @throws IOException
512: */
513: public void doEdit(RenderRequest request, RenderResponse response)
514: throws PortletException, IOException {
515: doView(request, response);
516: }
517:
518: /**
519: *
520: * @param request
521: * @return
522: * @throws TemplateLocatorException
523: */
524: protected LocatorDescriptor getTemplateLocatorDescriptor(
525: RenderRequest request) throws TemplateLocatorException {
526: RequestContext requestContext = getRequestContext(request);
527: CapabilityMap capabilityMap = requestContext.getCapabilityMap();
528: Locale locale = requestContext.getLocale();
529:
530: LocatorDescriptor templateLocatorDescriptor = templateLocator
531: .createLocatorDescriptor(null);
532: templateLocatorDescriptor.setMediaType(capabilityMap
533: .getPreferredMediaType().getName());
534: templateLocatorDescriptor.setCountry(locale.getCountry());
535: templateLocatorDescriptor.setLanguage(locale.getLanguage());
536: return templateLocatorDescriptor;
537: }
538:
539: /**
540: *
541: * @param request
542: * @return
543: * @throws TemplateLocatorException
544: */
545: protected LocatorDescriptor getDecoratorLocatorDescriptor(
546: RenderRequest request) throws TemplateLocatorException {
547: RequestContext requestContext = getRequestContext(request);
548: CapabilityMap capabilityMap = requestContext.getCapabilityMap();
549: Locale locale = requestContext.getLocale();
550:
551: LocatorDescriptor decorationLocatorDescriptor = decorationLocator
552: .createLocatorDescriptor(null);
553: decorationLocatorDescriptor.setMediaType(capabilityMap
554: .getPreferredMediaType().getName());
555: decorationLocatorDescriptor.setCountry(locale.getCountry());
556: decorationLocatorDescriptor.setLanguage(locale.getLanguage());
557:
558: return decorationLocatorDescriptor;
559: }
560:
561: /**
562: *
563: * @param request
564: * @param fragment
565: * @param page
566: * @return
567: * @throws TemplateLocatorException
568: * @throws ConfigurationException
569: */
570: public String decorateAndInclude(RenderRequest request,
571: Fragment fragment, Page page)
572: throws TemplateLocatorException, ConfigurationException {
573: String fragmentType = fragment.getType();
574: String decorator = fragment.getDecorator();
575: LocatorDescriptor decorationLocatorDescriptor = getDecoratorLocatorDescriptor(request);
576: if (decorator == null) {
577: decorator = page.getEffectiveDefaultDecorator(fragmentType);
578: }
579:
580: // get fragment properties for fragmentType or generic
581: TemplateDescriptor propsTemp = getTemplate(decorator + "/"
582: + DECORATOR_TYPE + ".properties", fragmentType,
583: decorationLocator, decorationLocatorDescriptor);
584: if (propsTemp == null) {
585: fragmentType = GENERIC_TEMPLATE_TYPE;
586: propsTemp = getTemplate(decorator + "/" + DECORATOR_TYPE
587: + ".properties", fragmentType, decorationLocator,
588: decorationLocatorDescriptor);
589: }
590:
591: // get decorator template
592: Configuration decoConf = new PropertiesConfiguration(propsTemp
593: .getAbsolutePath());
594: String ext = decoConf.getString("template.extension");
595: String decoratorPath = decorator + "/" + DECORATOR_TYPE + ext;
596: TemplateDescriptor template = null;
597: try {
598: template = getDecoration(request, decoratorPath,
599: fragmentType);
600: } catch (TemplateLocatorException e) {
601: String parent = decoConf.getString("extends");
602: if (parent != null) {
603: template = getDecoration(request, parent + "/"
604: + DECORATOR_TYPE + ext, fragmentType);
605: }
606: }
607:
608: return template.getAppRelativePath();
609: }
610:
611: /**
612: *
613: * @param request
614: * @param path
615: * @param templateType
616: * @return
617: * @throws TemplateLocatorException
618: */
619: protected TemplateDescriptor getDecoration(RenderRequest request,
620: String path, String templateType)
621: throws TemplateLocatorException {
622: return getTemplate(path, templateType, decorationLocator,
623: getDecoratorLocatorDescriptor(request));
624: }
625:
626: /**
627: *
628: * @param path
629: * @param templateType
630: * @param locator
631: * @param descriptor
632: * @return
633: * @throws TemplateLocatorException
634: */
635: protected TemplateDescriptor getTemplate(String path,
636: String templateType, TemplateLocator locator,
637: LocatorDescriptor descriptor)
638: throws TemplateLocatorException {
639:
640: if (templateType == null) {
641: templateType = GENERIC_TEMPLATE_TYPE;
642: }
643: try {
644:
645: descriptor.setName(path);
646: descriptor.setType(templateType);
647:
648: TemplateDescriptor template = locator
649: .locateTemplate(descriptor);
650: return template;
651: } catch (TemplateLocatorException e) {
652: log.error("Unable to locate template: " + path, e);
653: throw e;
654: }
655: }
656:
657: /**
658: * Gets the configuration (layout.properties) object for the decoration.
659: * @param name Name of the Decoration.
660: * @return <code>java.util.Properties</code> representing the configuration
661: * object.
662: */
663: protected Configuration getConfiguration(RenderRequest request,
664: String name) {
665: Configuration props = null;
666: JetspeedPowerTool jpt = null;
667: String templatePropertiesPath = null;
668: String key = name;
669: try {
670: jpt = getJetspeedPowerTool(request);
671: templatePropertiesPath = jpt.getTemplate(
672: name + "/" + JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE
673: + ".properties",
674: JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE)
675: .getAbsolutePath();
676: } catch (PortletException e) {
677: log.warn(
678: "Could not acquire JetspeedPowerTool from request",
679: e);
680: } catch (TemplateLocatorException e) {
681: log.warn("Could not find templatePorpertiesPath", e);
682: } catch (Exception e) {
683: log
684: .warn(
685: "Could not determine Layout template properties file",
686: e);
687: }
688: // if no path then set name to "default"
689: if (null == templatePropertiesPath) {
690: key = "default";
691: } else {
692: key = templatePropertiesPath;
693: }
694: if (log.isDebugEnabled()) {
695: log.debug("Template descriptor path:<"
696: + templatePropertiesPath + ">");
697: }
698:
699: // load Decoration.CONFIG_FILE_NAME (layout.properties)
700: try {
701: props = new PropertiesConfiguration(templatePropertiesPath);
702: if (log.isDebugEnabled())
703: log.debug("Successfully read in: <"
704: + templatePropertiesPath + "> ");
705: } catch (Exception e) {
706: props = new PropertiesConfiguration();
707: log.warn("Could not locate the " + templatePropertiesPath
708: + " file for layout template \"" + name
709: + "\". This layout template may not exist.", e);
710: props.setProperty("id", name);
711: props.setProperty(TEMPLATE_TYPE_KEY, DEFAULT_TEMPLATE_TYPE);
712: props.setProperty(TEMPLATE_EXTENSION_KEY,
713: DEFAULT_TEMPLATE_EXT);
714: } finally {
715: String templateIdPropVal = (String) props.getProperty("id");
716: String templateNamePropVal = (String) props
717: .getProperty(TEMPLATE_TYPE_KEY);
718: String templateExtPropVal = (String) props
719: .getProperty(TEMPLATE_EXTENSION_KEY);
720:
721: if (templateIdPropVal == null) {
722: templateIdPropVal = name;
723: props.setProperty("id", templateIdPropVal);
724: }
725:
726: if (templateNamePropVal == null) {
727: props.setProperty(TEMPLATE_TYPE_KEY,
728: DEFAULT_TEMPLATE_TYPE);
729: }
730: if (templateExtPropVal == null) {
731: props.setProperty(TEMPLATE_EXTENSION_KEY,
732: DEFAULT_TEMPLATE_EXT);
733: }
734: }
735:
736: if (log.isDebugEnabled()) {
737: log.debug("Template layout.properties extension is:<"
738: + props.getString(TEMPLATE_EXTENSION_KEY));
739: }
740: return props;
741: }
742:
743: class CachedTemplate {
744: private String key;
745: private TemplateDescriptor template;
746: private Configuration config;
747:
748: public CachedTemplate(String key, TemplateDescriptor template,
749: Configuration config) {
750: this .key = key;
751: this .template = template;
752: this .config = config;
753: }
754:
755: public Configuration getConfig() {
756: return config;
757: }
758:
759: public String getKey() {
760: return key;
761: }
762:
763: public TemplateDescriptor getTemplate() {
764: return template;
765: }
766: }
767: }
|