1 | |
package org.jtheque.core.managers.update; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import org.jtheque.core.managers.AbstractManager; |
20 | |
import org.jtheque.core.managers.Managers; |
21 | |
import org.jtheque.core.managers.module.IModuleManager; |
22 | |
import org.jtheque.core.managers.module.beans.ModuleContainer; |
23 | |
import org.jtheque.core.managers.patch.IPatchManager; |
24 | |
import org.jtheque.core.managers.patch.OnlinePatch; |
25 | |
import org.jtheque.core.managers.update.actions.UpdateAction; |
26 | |
import org.jtheque.core.managers.update.versions.IVersionsLoader; |
27 | |
import org.jtheque.core.managers.update.versions.InstallVersion; |
28 | |
import org.jtheque.core.managers.update.versions.OnlineVersion; |
29 | |
import org.jtheque.core.managers.view.able.IViewManager; |
30 | |
import org.jtheque.utils.bean.Version; |
31 | |
|
32 | |
import java.util.ArrayList; |
33 | |
import java.util.Collection; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public final class UpdateManager extends AbstractManager implements IUpdateManager { |
42 | |
private final Collection<Updatable> updatables; |
43 | |
|
44 | |
private UpdatableState state; |
45 | |
|
46 | |
private IVersionsLoader versionsLoader; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public UpdateManager() { |
52 | 0 | super(); |
53 | |
|
54 | 0 | updatables = new ArrayList<Updatable>(10); |
55 | 0 | } |
56 | |
|
57 | |
@Override |
58 | |
public void init() { |
59 | |
try { |
60 | 0 | state = getStates().getOrCreateState(UpdatableState.class); |
61 | 0 | } catch (Exception e) { |
62 | 0 | getLogger().error(e); |
63 | |
|
64 | 0 | state = new UpdatableState(); |
65 | 0 | } |
66 | |
|
67 | 0 | for (Updatable updatable : updatables) { |
68 | 0 | readUpdatableVersion(updatable); |
69 | |
} |
70 | 0 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private void readUpdatableVersion(Updatable updatable) { |
78 | 0 | Version version = state.getVersion(updatable.getName()); |
79 | |
|
80 | 0 | if (version == null) { |
81 | 0 | updatable.setVersion(updatable.getDefaultVersion()); |
82 | |
} else { |
83 | 0 | updatable.setVersion(version); |
84 | |
} |
85 | 0 | } |
86 | |
|
87 | |
@Override |
88 | |
public void update(Updatable updatable, Version versionToDownload) { |
89 | |
|
90 | 0 | for (OnlineVersion onlineVersion : versionsLoader.getOnlineVersions(updatable)) { |
91 | 0 | if (onlineVersion.getVersion().equals(versionToDownload)) { |
92 | 0 | applyOnlineVersion(onlineVersion); |
93 | |
|
94 | 0 | break; |
95 | |
} |
96 | |
} |
97 | |
|
98 | 0 | updatable.setVersion(new Version(versionToDownload.getVersion())); |
99 | 0 | state.setVersion(updatable.getName(), updatable.getVersion()); |
100 | 0 | updatable.setUpdated(); |
101 | 0 | } |
102 | |
|
103 | |
@Override |
104 | |
public void update(Version versionToDownload) { |
105 | 0 | for (OnlineVersion onlineVersion : versionsLoader.getOnlineVersions(Managers.getCore())) { |
106 | 0 | if (onlineVersion.getVersion().equals(versionToDownload)) { |
107 | 0 | applyOnlineVersion(onlineVersion); |
108 | |
|
109 | 0 | break; |
110 | |
} |
111 | |
} |
112 | |
|
113 | 0 | Managers.getManager(IPatchManager.class).setUpdated(true); |
114 | |
|
115 | 0 | Managers.getManager(IViewManager.class).displayI18nText("message.application.updated"); |
116 | |
|
117 | 0 | Managers.getCore().getLifeCycleManager().restart(); |
118 | 0 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
private static void applyOnlineVersion(OnlineVersion onlineVersion) { |
126 | 0 | for (UpdateAction action : onlineVersion.getActions()) { |
127 | 0 | action.execute(); |
128 | |
} |
129 | |
|
130 | 0 | for (OnlinePatch patch : onlineVersion.getPatches()) { |
131 | 0 | Managers.getManager(IPatchManager.class).registerOnlinePatch(patch); |
132 | |
} |
133 | 0 | } |
134 | |
|
135 | |
@Override |
136 | |
public InstallationResult install(String versionFileURL) { |
137 | 0 | InstallationResult result = new InstallationResult(); |
138 | |
|
139 | |
try { |
140 | 0 | InstallVersion version = versionsLoader.getInstallVersion(versionFileURL); |
141 | |
|
142 | 0 | for (UpdateAction action : version.getActions()) { |
143 | 0 | action.execute(); |
144 | |
} |
145 | |
|
146 | 0 | registerPatches(result, version); |
147 | |
|
148 | 0 | result.setJarFile(version.getJarFile()); |
149 | 0 | result.setName(version.getTitle()); |
150 | 0 | result.setInstalled(true); |
151 | 0 | } catch (Exception e) { |
152 | 0 | getLogger().error(e); |
153 | 0 | result.setInstalled(false); |
154 | 0 | } |
155 | |
|
156 | 0 | return result; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
private static void registerPatches(InstallationResult result, OnlineVersion version) { |
166 | 0 | if (!version.getPatches().isEmpty()) { |
167 | 0 | for (OnlinePatch patch : version.getPatches()) { |
168 | 0 | Managers.getManager(IPatchManager.class).registerOnlinePatch(patch); |
169 | |
} |
170 | |
|
171 | 0 | result.setMustRestart(); |
172 | |
} |
173 | 0 | } |
174 | |
|
175 | |
@Override |
176 | |
public void update(ModuleContainer module, Version version) { |
177 | 0 | OnlineVersion onlineVersion = versionsLoader.getOnlineVersion(version, module); |
178 | |
|
179 | 0 | if (onlineVersion.getCoreVersion().isGreaterThan(Managers.getCore().getCoreCurrentVersion())) { |
180 | 0 | Managers.getManager(IViewManager.class).displayI18nText("modules.message.versionproblem"); |
181 | |
} else { |
182 | |
|
183 | 0 | for (UpdateAction action : onlineVersion.getActions()) { |
184 | 0 | action.execute(); |
185 | |
} |
186 | |
|
187 | 0 | Managers.getManager(IViewManager.class).displayI18nText("message.application.updated"); |
188 | |
|
189 | 0 | Managers.getCore().getLifeCycleManager().restart(); |
190 | |
} |
191 | 0 | } |
192 | |
|
193 | |
@Override |
194 | |
public Collection<Version> getKernelVersions() { |
195 | 0 | return getVersions(Managers.getCore()); |
196 | |
} |
197 | |
|
198 | |
@Override |
199 | |
public void verifyingUpdate() { |
200 | 0 | if (Managers.getCore().getConfiguration().verifyUpdateOnStartup()) { |
201 | 0 | if (!isCurrentVersionUpToDate()) { |
202 | 0 | proposeUpdateToUser("dialogs.propose.update", "dialogs.propose.update.title"); |
203 | |
} |
204 | |
|
205 | 0 | if (!isAModuleNotUpToDate()) { |
206 | 0 | proposeUpdateToUser("dialogs.propose.module.update", "dialogs.propose.module.update.title"); |
207 | |
} |
208 | |
} |
209 | 0 | } |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
private static void proposeUpdateToUser(String dialogKey, String dialogKeyTitle) { |
218 | 0 | boolean yes = Managers.getManager(IViewManager.class).askI18nUserForConfirmation( |
219 | |
dialogKey, dialogKeyTitle); |
220 | |
|
221 | 0 | if (yes) { |
222 | 0 | Managers.getManager(IViewManager.class).getViews().getModuleView().display(); |
223 | |
} |
224 | 0 | } |
225 | |
|
226 | |
@Override |
227 | |
public boolean isCurrentVersionUpToDate() { |
228 | 0 | boolean upToDate = true; |
229 | |
|
230 | 0 | for (Version version : getKernelVersions()) { |
231 | 0 | if (!Managers.getCore().getApplication().getVersion().isGreaterThan(version)) { |
232 | 0 | upToDate = false; |
233 | 0 | break; |
234 | |
} |
235 | |
} |
236 | |
|
237 | 0 | return upToDate; |
238 | |
} |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
private boolean isAModuleNotUpToDate() { |
246 | 0 | boolean notUpToDate = false; |
247 | |
|
248 | 0 | for (ModuleContainer module : Managers.getManager(IModuleManager.class).getModules()) { |
249 | 0 | if (!isUpToDate(module)) { |
250 | 0 | notUpToDate = true; |
251 | 0 | break; |
252 | |
} |
253 | |
} |
254 | |
|
255 | 0 | return notUpToDate; |
256 | |
} |
257 | |
|
258 | |
@Override |
259 | |
public boolean isUpToDate(Object object) { |
260 | 0 | for (Version version : versionsLoader.getVersions(object)) { |
261 | 0 | if (!versionsLoader.getVersion(object).isGreaterThan(version)) { |
262 | 0 | return false; |
263 | |
} |
264 | |
} |
265 | |
|
266 | 0 | return true; |
267 | |
} |
268 | |
|
269 | |
@Override |
270 | |
public void updateToMostRecentVersion(ModuleContainer module) { |
271 | 0 | Version maxVersion = null; |
272 | |
|
273 | 0 | for (Version version : versionsLoader.getVersions(module)) { |
274 | 0 | if (maxVersion == null || version.isGreaterThan(maxVersion)) { |
275 | 0 | maxVersion = version; |
276 | |
} |
277 | |
} |
278 | |
|
279 | 0 | update(module, maxVersion); |
280 | 0 | } |
281 | |
|
282 | |
@Override |
283 | |
public Version getMostRecentVersion(Object object) { |
284 | 0 | return versionsLoader.getMostRecentVersion(object); |
285 | |
} |
286 | |
|
287 | |
@Override |
288 | |
public Collection<Version> getVersions(Object object) { |
289 | 0 | return versionsLoader.getVersions(object); |
290 | |
} |
291 | |
|
292 | |
@Override |
293 | |
public void registerUpdatable(Updatable updatable) { |
294 | 0 | updatables.add(updatable); |
295 | |
|
296 | 0 | fireUpdatableAdded(); |
297 | 0 | } |
298 | |
|
299 | |
@Override |
300 | |
public Collection<Updatable> getUpdatables() { |
301 | 0 | return updatables; |
302 | |
} |
303 | |
|
304 | |
@Override |
305 | |
public void addUpdatableListener(UpdatableListener listener) { |
306 | 0 | getListeners().add(UpdatableListener.class, listener); |
307 | 0 | } |
308 | |
|
309 | |
@Override |
310 | |
public void removeUpdatableListener(UpdatableListener listener) { |
311 | 0 | getListeners().remove(UpdatableListener.class, listener); |
312 | 0 | } |
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
private static void fireUpdatableAdded() { |
318 | 0 | UpdatableListener[] listeners = getListeners().getListeners(UpdatableListener.class); |
319 | |
|
320 | 0 | for (UpdatableListener l : listeners) { |
321 | 0 | l.updatableAdded(); |
322 | |
} |
323 | 0 | } |
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
public void setVersionsLoader(IVersionsLoader versionsLoader) { |
331 | 0 | this.versionsLoader = versionsLoader; |
332 | 0 | } |
333 | |
} |