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