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