View Javadoc
1   package fr.ifremer.tutti.ui.swing.update.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import fr.ifremer.tutti.TuttiConfiguration;
26  import fr.ifremer.tutti.persistence.ProgressionModel;
27  import fr.ifremer.tutti.ui.swing.RunTutti;
28  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
29  import fr.ifremer.tutti.ui.swing.content.actions.AbstractMainUITuttiAction;
30  import fr.ifremer.tutti.ui.swing.content.actions.CloseApplicationAction;
31  import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
32  import fr.ifremer.tutti.ui.swing.update.TuttiApplicationUpdaterCallBack;
33  import fr.ifremer.tutti.ui.swing.update.Updates;
34  import fr.ifremer.tutti.ui.swing.updater.UpdateModule;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.nuiton.updater.ApplicationUpdater;
38  
39  import java.io.File;
40  
41  import static org.nuiton.i18n.I18n.t;
42  
43  /**
44   * To update jre / i18n or tutti using the {@link ApplicationUpdater} mecanism.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   * @since 1.0
48   */
49  public class UpdateApplicationAction extends AbstractMainUITuttiAction {
50  
51      /** Logger. */
52      private static final Log log = LogFactory.getLog(UpdateApplicationAction.class);
53  
54      public UpdateApplicationAction(MainUIHandler handler) {
55          super(handler, true);
56          setActionDescription(t("tutti.main.action.updateApplication.tip"));
57          modulesToUpdate = Updates.getApplicationModules();
58      }
59  
60      protected UpdateModule[] modulesToUpdate;
61  
62      protected boolean reload;
63  
64      @Override
65      public boolean prepareAction() throws Exception {
66          boolean doAction = super.prepareAction();
67  
68          if (doAction) {
69              // check application url is reachable
70              TuttiUIContext context = getContext();
71              doAction = context.checkUpdateApplicationReachable(true);
72          }
73          return doAction;
74      }
75  
76      @Override
77      public void releaseAction() {
78          super.releaseAction();
79          modulesToUpdate = Updates.getApplicationModules();
80      }
81  
82      @Override
83      public void doAction() throws Exception {
84  
85          reload = false;
86  
87          TuttiUIContext context = getContext();
88          TuttiConfiguration config = getConfig();
89  
90          File current = config.getBasedir();
91          if (current == null || !current.exists()) {
92  
93              // can not update application
94              if (log.isWarnEnabled()) {
95                  log.warn("No application base directory defined, skip updates.");
96              }
97          } else {
98  
99              String url = config.getUpdateApplicationUrl();
100 
101             if (log.isInfoEnabled()) {
102                 log.info(String.format("Try to update jre, i18N, help or tutti (current application location: %s), using update url: %s", current, url));
103             }
104 
105             ProgressionModel progressionModel = new ProgressionModel();
106             context.getActionUI().getModel().setProgressionModel(progressionModel);
107             progressionModel.setMessage(t("tutti.updateApplication.checkUpdates"));
108 
109             TuttiApplicationUpdaterCallBack callback = new TuttiApplicationUpdaterCallBack(url, this, progressionModel);
110 
111             callback.setModulesToUpdate(modulesToUpdate);
112 
113             Updates.doUpdate(config, callback, current);
114 
115             if (callback.isApplicationUpdated()) {
116 
117                 reload = true;
118 
119             } else {
120 
121                 sendMessage(t("tutti.updateApplication.noUpdate"));
122             }
123         }
124     }
125 
126     public void setModulesToUpdate(UpdateModule... modulesToUpdate) {
127         this.modulesToUpdate = modulesToUpdate;
128     }
129 
130     @Override
131     public void postSuccessAction() {
132         super.postSuccessAction();
133 
134         if (reload) {
135             // wait 1 second to be sure action ui is up
136             try {
137                 Thread.sleep(1000);
138             } catch (InterruptedException e) {
139                 if (log.isWarnEnabled()) {
140                     log.warn("Could not wait 1 second...", e);
141                 }
142             }
143 
144             // tell user restart will be done
145 
146             getHandler().showSuccessMessage(t("tutti.updateApplication.title.success"),
147                                             t("tutti.updateApplication.message.success"));
148 
149             CloseApplicationAction action = getContext().getActionFactory().createLogicAction(
150                     getHandler(), CloseApplicationAction.class);
151             action.setExitCode(RunTutti.RESTART_EXIT_CODE);
152             getActionEngine().runAction(action);
153         }
154     }
155 
156     public boolean isReload() {
157         return reload;
158     }
159 }