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