View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.blossom.template;
35  
36  import info.magnolia.cms.beans.config.ContentRepository;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.HierarchyManager;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.module.blossom.dispatcher.BlossomDispatcher;
41  import info.magnolia.module.blossom.support.RepositoryUtils;
42  import org.apache.commons.lang.StringUtils;
43  import org.apache.jackrabbit.value.StringValue;
44  import org.springframework.beans.factory.InitializingBean;
45  
46  import javax.jcr.RepositoryException;
47  import java.util.Map;
48  import java.util.concurrent.ConcurrentHashMap;
49  
50  /**
51   * Default implementation of {@link info.magnolia.module.blossom.template.BlossomTemplateRegistry}.
52   */
53  public class DefaultBlossomTemplateRegistry implements BlossomTemplateRegistry, InitializingBean {
54  
55      private static final String TEMPLATES_PATH = "/modules/blossom/templates/autodetected";
56  
57      protected TemplateDescriptionBuilder descriptionBuilder;
58      private Map<String, BlossomTemplateDescription> templatesByName = new ConcurrentHashMap<String, BlossomTemplateDescription>();
59  
60      public void setDescriptionBuilder(TemplateDescriptionBuilder descriptionBuilder) {
61          this.descriptionBuilder = descriptionBuilder;
62      }
63  
64      public BlossomTemplateDescription getTemplate(String name) {
65          return templatesByName.get(name);
66      }
67  
68      public void registerTemplate(BlossomDispatcher dispatcher, Object handler, String handlerPath) throws RepositoryException {
69          addTemplate(descriptionBuilder.buildDescription(dispatcher, handler, handlerPath));
70      }
71  
72      protected void addTemplate(BlossomTemplateDescription description) throws RepositoryException {
73          templatesByName.put(description.getName(), description);
74          writeTemplateDefinition(description);
75      }
76  
77      protected void writeTemplateDefinition(BlossomTemplateDescription templateDescription) throws RepositoryException {
78          HierarchyManager hierarchyManager = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.CONFIG);
79          Content configNode = RepositoryUtils.createContentNode(hierarchyManager, TEMPLATES_PATH, templateDescription.getName());
80          configNode.createNodeData("name", new StringValue(templateDescription.getName()));
81          configNode.createNodeData("title", new StringValue(templateDescription.getTitle()));
82          configNode.createNodeData("description", new StringValue(templateDescription.getDescription()));
83          configNode.createNodeData("visible", templateDescription.isVisible());
84          if (StringUtils.isNotBlank(templateDescription.getI18nBasename()))
85              configNode.createNodeData("i18nBasename", new StringValue(templateDescription.getI18nBasename()));
86          configNode.createNodeData("type", new StringValue("blossom"));
87          configNode.createNodeData("class", BlossomTemplate.class.getName());
88          configNode.getParent().save();
89      }
90  
91      public void afterPropertiesSet() throws Exception {
92  
93          HierarchyManager hierarchyManager = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.CONFIG);
94          RepositoryUtils.deleteContent(hierarchyManager, TEMPLATES_PATH);
95  
96          if (descriptionBuilder == null)
97              descriptionBuilder = new TemplateDescriptionBuilder();
98      }
99  }