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.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
53
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
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
173
174
175 protected String getModuleKey() {
176 return currentModule != null ? currentModule.toString() : DEFAULT_KEY;
177 }
178 }