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