View Javadoc

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