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.module.model.ModuleDefinition;
37 import info.magnolia.module.model.Version;
38 import info.magnolia.module.ui.ModuleManagerUI;
39 import info.magnolia.module.delta.Delta;
40 import info.magnolia.module.delta.DeltaType;
41 import info.magnolia.objectfactory.Components;
42
43 import java.util.ArrayList;
44 import java.util.EnumSet;
45 import java.util.HashSet;
46 import java.util.List;
47 import java.util.Set;
48
49
50
51
52
53
54
55
56 public interface ModuleManager {
57
58
59
60
61
62
63
64 List<ModuleDefinition> loadDefinitions() throws ModuleManagementException;
65
66
67
68
69 void checkForInstallOrUpdates() throws ModuleManagementException;
70
71
72
73
74
75 ModuleManagementState getStatus();
76
77 ModuleManagerUI getUI();
78
79 void performInstallOrUpdate();
80
81 InstallContext getInstallContext();
82
83 void startModules();
84
85 void stopModules();
86
87
88
89
90
91 public class Factory {
92
93
94
95 public static ModuleManager getInstance() {
96 return Components.getSingleton(ModuleManager.class);
97 }
98 }
99
100
101
102
103 public final static class ModuleManagementState {
104 private final List<ModuleAndDeltas> list;
105 private EnumSet<DeltaType> cachedDeltaTypes;
106
107 public ModuleManagementState() {
108 this.list = new ArrayList<ModuleAndDeltas>();
109 }
110
111 public boolean needsUpdateOrInstall() {
112 return !(list.isEmpty());
113 }
114
115 void addModule(ModuleDefinition module, Version currentVersion, List<Delta> deltas) {
116 list.add(new ModuleAndDeltas(module, currentVersion, deltas));
117 }
118
119 public List<ModuleAndDeltas> getList() {
120 return list;
121 }
122
123
124
125
126
127
128
129
130
131
132 public String getDeltaTypesDescription(String[] texts) {
133 if (texts == null || texts.length != 3) {
134 throw new IllegalStateException("Please pass an array of 3 strings.");
135 }
136
137
138 if (cachedDeltaTypes == null) {
139 cachedDeltaTypes = getDeltaTypes();
140 }
141
142 if (cachedDeltaTypes.size() == 1) {
143 if (cachedDeltaTypes.contains(DeltaType.install)) {
144 return texts[0];
145 } else if (cachedDeltaTypes.contains(DeltaType.update)) {
146 return texts[1];
147 }
148 } else if (cachedDeltaTypes.size() == 2) {
149 if (cachedDeltaTypes.containsAll(EnumSet.<DeltaType>of(DeltaType.install, DeltaType.update))) {
150 return texts[2];
151 }
152 }
153 throw new IllegalStateException("Unhandled delta types combination: " + cachedDeltaTypes);
154 }
155
156 protected EnumSet<DeltaType> getDeltaTypes() {
157 if (list.isEmpty()) {
158 throw new IllegalStateException("No registered deltas");
159 }
160 final Set<DeltaType> types = new HashSet<DeltaType>();
161 for (ModuleAndDeltas moduleAndDeltas : list) {
162 for (Delta delta : moduleAndDeltas.getDeltas()) {
163 types.add(delta.getType());
164 }
165 }
166 return EnumSet.copyOf(types);
167 }
168 }
169
170
171
172
173 public final static class ModuleAndDeltas {
174 private final ModuleDefinition module;
175 private final Version currentVersion;
176 private final List<Delta> deltas;
177
178 public ModuleAndDeltas(ModuleDefinition module, Version currentVersion, List<Delta> deltas) {
179 this.module = module;
180 this.currentVersion = currentVersion;
181 this.deltas = deltas;
182 }
183
184 public ModuleDefinition getModule() {
185 return module;
186 }
187
188 public Version getCurrentVersion() {
189 return currentVersion;
190 }
191
192 public List<Delta> getDeltas() {
193 return deltas;
194 }
195
196 @Override
197 public String toString() {
198 final StringBuffer sb = new StringBuffer("ModuleAndDeltas for ");
199 sb.append(module.getName());
200 if (currentVersion != null) {
201 sb.append(": current version is ");
202 sb.append(currentVersion);
203 sb.append(", updating to ");
204 } else {
205 sb.append(": installing version ");
206 }
207 sb.append(module.getVersion());
208 sb.append(" with ");
209 sb.append(deltas.size());
210 sb.append(" deltas.");
211 return sb.toString();
212 }
213 }
214
215 }