View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.action;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * To update jre / i18n or Dali using the {@link ApplicationUpdater} mecanism.
39   *
40   * @since 1.0
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      /** {@inheritDoc} */
58      @Override
59      public boolean prepareAction() throws Exception {
60          boolean doAction = super.prepareAction();
61  
62          if (doAction) {
63              // check application url is reachable
64              doAction = getContext().checkUpdateReachable(getConfig().getUpdateApplicationUrl(), true);
65          }
66          return doAction;
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public void releaseAction() {
72          super.releaseAction();
73          types = ApplicationUpdaterCallBack.UpdateType.values();
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public void doAction() {
79  
80          reload = false;
81  
82          File current = getConfig().getBaseDirectory();
83          if (current == null || !current.exists()) {
84  
85              // can not update application
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      * <p>Setter for the field <code>types</code>.</p>
127      *
128      * @param types a {@link ApplicationUpdaterCallBack.UpdateType} object.
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     /** {@inheritDoc} */
139     @Override
140     public void postSuccessAction() {
141         super.postSuccessAction();
142 
143         if (silent) return;
144 
145         if (reload) {
146             // wait 1 second to be sure action ui is up
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             // tell user restart will be done
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      * <p>isReload.</p>
168      *
169      * @return a boolean.
170      */
171     public boolean isReload() {
172         return reload;
173     }
174 }