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