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