1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
209
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 }