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;
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.cms.core.ItemType;
40  import info.magnolia.cms.util.ContentUtil;
41  import info.magnolia.context.MgnlContext;
42  import info.magnolia.module.model.ModuleDefinition;
43  
44  import javax.jcr.RepositoryException;
45  import java.util.ArrayList;
46  import java.util.LinkedHashMap;
47  import java.util.List;
48  import java.util.Map;
49  
50  /**
51   * Default implementation of {@link info.magnolia.module.InstallContext}; provided by the {@link info.magnolia.module.ModuleManager}.
52   *
53   * @author gjoseph
54   * @version $Revision: $ ($Author: $)
55   */
56  public class InstallContextImpl implements InstallContext {
57      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(InstallContextImpl.class);
58  
59      private static final String DEFAULT_KEY = "General messages";
60  
61      private ModuleDefinition currentModule;
62      private InstallStatus status;
63      private boolean restartNeeded;
64      private int executedTaskCount;
65      private int totalTaskCount;
66      // ensure we'll keep messages in the order they were added
67      private final Map<String, List<Message>> messages = new LinkedHashMap<String, List<Message>>();
68  
69      public void setCurrentModule(ModuleDefinition module) {
70          this.currentModule = module;
71      }
72  
73      public void info(String message) {
74          log.info("> " + message);
75          log(new Message(MessagePriority.info, message));
76      }
77  
78      public void warn(String message) {
79          log.warn("> " + message);
80          log(new Message(MessagePriority.warning, message));
81      }
82  
83      public void error(String message, Throwable th) {
84          log.error("> " + message, th);
85          log(new Message(MessagePriority.error, message, th));
86      }
87  
88      public void restartNeeded(String message) {
89          this.restartNeeded = true;
90          log.warn("> restartNeeded > " + message);
91          log(new Message(MessagePriority.restartNeeded, message));
92      }
93  
94      boolean isRestartNeeded() {
95          return restartNeeded;
96      }
97  
98      void incExecutedTaskCount() {
99          executedTaskCount++;
100     }
101 
102     public int getExecutedTaskCount() {
103         return executedTaskCount;
104     }
105 
106     public int getTotalTaskCount() {
107         return totalTaskCount;
108     }
109 
110     void setTotalTaskCount(int totalTaskCount) {
111         this.totalTaskCount = totalTaskCount;
112     }
113 
114     public InstallStatus getStatus() {
115         return status;
116     }
117 
118     void setStatus(InstallStatus status) {
119         this.status = status;
120     }
121 
122     public Map<String, List<Message>> getMessages() {
123         return messages;
124     }
125 
126     public ModuleDefinition getCurrentModuleDefinition() {
127         return currentModule;
128     }
129 
130     public boolean isModuleRegistered(String moduleName) {
131         return ModuleRegistry.Factory.getInstance().isModuleRegistered(moduleName);
132     }
133 
134     public HierarchyManager getHierarchyManager(String workspace) {
135         return MgnlContext.getSystemContext().getHierarchyManager(workspace);
136     }
137 
138     public HierarchyManager getConfigHierarchyManager() {
139         return getHierarchyManager(ContentRepository.CONFIG);
140     }
141 
142     public boolean hasModulesNode() {
143         final HierarchyManager hm = getConfigHierarchyManager();
144         return hm.isExist("/" + ModuleManagerImpl.MODULES_NODE);
145     }
146 
147     public Content getModulesNode() throws RepositoryException {
148         final HierarchyManager hm = getConfigHierarchyManager();
149         return hm.getContent(ModuleManagerImpl.MODULES_NODE);
150     }
151 
152     public Content getOrCreateCurrentModuleNode() throws RepositoryException {
153         final Content allModulesNode = getModulesNode();
154         return ContentUtil.getOrCreateContent(allModulesNode, currentModule.getName(), ItemType.CONTENT);
155     }
156 
157     public Content getOrCreateCurrentModuleConfigNode() throws RepositoryException {
158         final Content moduleNode = getOrCreateCurrentModuleNode();
159         return ContentUtil.getOrCreateContent(moduleNode, "config", ItemType.CONTENT);
160     }
161 
162     protected void log(final Message message) {
163         final String k = getModuleKey();
164         List<Message> messagesForKey = messages.get(k);
165         if (messagesForKey == null) {
166             messagesForKey = new ArrayList<Message>();
167             messages.put(k, messagesForKey);
168         }
169         messagesForKey.add(message);
170     }
171 
172     /**
173      * The key used in the map of messages. This is mostly because Maps keys in FreeMarker can only be Strings...
174      * We just need to make sure this is consistent across templates...
175      */
176     protected String getModuleKey() {
177         return currentModule != null ? currentModule.toString() : DEFAULT_KEY;
178     }
179 }