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