1 package fr.ifremer.quadrige3.ui.swing.action;
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 import fr.ifremer.quadrige3.ui.swing.Application;
27 import fr.ifremer.quadrige3.ui.swing.callback.ApplicationUpdaterCallBack;
28 import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.nuiton.updater.ApplicationUpdater;
32
33 import java.io.File;
34
35 import static org.nuiton.i18n.I18n.t;
36
37
38
39
40
41
42 public class UpdateApplicationAction extends AbstractMainUIAction {
43
44 private static final Log LOG = LogFactory.getLog(UpdateApplicationAction.class);
45
46 public UpdateApplicationAction(AbstractMainUIHandler handler) {
47 super(handler, true);
48 setActionDescription(t("quadrige3.applicationUpdater.application.action"));
49 types = ApplicationUpdaterCallBack.UpdateType.values();
50 }
51
52 private ApplicationUpdaterCallBack.UpdateType[] types;
53
54 private boolean reload;
55 private boolean silent;
56
57
58 @Override
59 public boolean prepareAction() throws Exception {
60 boolean doAction = super.prepareAction();
61
62 if (doAction) {
63
64 doAction = getContext().checkUpdateReachable(getConfig().getUpdateApplicationUrl(), true);
65 }
66 return doAction;
67 }
68
69
70 @Override
71 public void releaseAction() {
72 super.releaseAction();
73 types = ApplicationUpdaterCallBack.UpdateType.values();
74 }
75
76
77 @Override
78 public void doAction() {
79
80 reload = false;
81
82 File current = getConfig().getBaseDirectory();
83 if (current == null || !current.exists()) {
84
85
86 if (LOG.isWarnEnabled()) {
87 LOG.warn("No application base directory defined, skip updates.");
88 }
89 } else {
90
91 String url = getConfig().getUpdateApplicationUrl();
92 File dest = new File(getConfig().getBaseDirectory(), "NEW");
93
94 if (LOG.isInfoEnabled()) {
95 LOG.info(String.format("Try to update jre, i18N, help or application (current application location: %s), using update url: %s", current, url));
96 }
97
98 createProgressionUIModel();
99 getProgressionUIModel().setMessage(t("quadrige3.applicationUpdater.checkUpdates"));
100
101 ApplicationUpdaterCallBack callback
102 = new ApplicationUpdaterCallBack(this, getProgressionUIModel());
103
104 callback.setTypes(types);
105
106 ApplicationUpdater up = new ApplicationUpdater();
107 up.update(url,
108 current,
109 dest,
110 false,
111 callback,
112 getProgressionUIModel());
113
114 if (callback.isApplicationUpdated()) {
115
116 reload = true;
117
118 } else {
119
120 sendMessage(t("quadrige3.applicationUpdater.application.noUpdate"));
121 }
122 }
123 }
124
125
126
127
128
129
130 public void setTypes(ApplicationUpdaterCallBack.UpdateType... types) {
131 this.types = types;
132 }
133
134 public void setSilent(boolean silent) {
135 this.silent = silent;
136 }
137
138
139 @Override
140 public void postSuccessAction() {
141 super.postSuccessAction();
142
143 if (silent) return;
144
145 if (reload) {
146
147 try {
148 Thread.sleep(1000);
149 } catch (InterruptedException e) {
150 if (LOG.isWarnEnabled()) {
151 LOG.warn("Could not wait 1 second...", e);
152 }
153 }
154
155
156 getHandler().showSuccessMessage(t("quadrige3.applicationUpdater.success.title"),
157 t("quadrige3.applicationUpdater.application.success.message"));
158
159 CloseApplicationAction action = getContext().getActionFactory().createLogicAction(
160 getHandler(), CloseApplicationAction.class);
161 action.setExitCode(Application.UPDATE_EXIT_CODE);
162 getActionEngine().runAction(action);
163 }
164 }
165
166
167
168
169
170
171 public boolean isReload() {
172 return reload;
173 }
174 }