View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common.callback;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 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  
27  import com.google.common.collect.Lists;
28  import com.google.common.collect.Maps;
29  import fr.ifremer.quadrige2.ui.swing.common.ApplicationUIContext;
30  import fr.ifremer.quadrige2.ui.swing.common.action.AbstractAction;
31  import fr.ifremer.quadrige2.ui.swing.common.model.ProgressionModel;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.nuiton.jaxx.application.ApplicationTechnicalException;
35  import org.nuiton.jaxx.application.swing.action.ApplicationActionException;
36  import org.nuiton.updater.ApplicationInfo;
37  import org.nuiton.updater.ApplicationUpdaterCallback;
38  
39  import java.util.List;
40  import java.util.Map;
41  import java.util.Map.Entry;
42  
43  import static org.nuiton.i18n.I18n.t;
44  
45  /**
46   * CallBack to update plugins.
47   *
48   * @since 1.0
49   */
50  public class PluginsUpdaterCallBack implements ApplicationUpdaterCallback {
51  
52      /**
53       * Logger.
54       */
55      private static final Log LOG = LogFactory.getLog(PluginsUpdaterCallBack.class);
56  
57      protected final ApplicationUIContext context;
58  
59      protected List<String> pluginNames;
60      
61      protected final ProgressionModel progressionModel;
62  
63      protected boolean applicationUpdated;
64  
65      protected final AbstractAction<?, ?, ?> action;
66  
67      /**
68       * <p>Constructor for PluginsUpdaterCallBack.</p>
69       *
70       * @param action a {@link AbstractAction} object.
71       * @param progressionModel a {@link ProgressionModel} object.
72       */
73      public PluginsUpdaterCallBack(AbstractAction<?, ?, ?> action, ProgressionModel progressionModel) {
74          this.action = action;
75          this.context = action.getContext();
76          this.progressionModel = progressionModel;
77      }
78  
79      /**
80       * <p>setTypes.</p>
81       *
82       * @param types a {@link String} object.
83       */
84      public void setTypes(String... types) {
85          this.pluginNames = Lists.newArrayList(types);
86      }
87      /**
88       * <p>isApplicationUpdated.</p>
89       *
90       * @return a boolean.
91       */
92      public boolean isApplicationUpdated() {
93          return applicationUpdated;
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public Map<String, ApplicationInfo> updateToDo(Map<String, ApplicationInfo> appToUpdate) {
99  
100         Map<String, ApplicationInfo> result = Maps.newHashMap();
101         for (String pluginName : pluginNames) {
102             ApplicationInfo info = appToUpdate.get(pluginName);
103             if (info != null) {
104                 result.put(info.name, info);
105             }
106         }
107         return result;
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     public void startUpdate(ApplicationInfo info) {
113         progressionModel.setMessage(t("quadrige2.pluginsUpdater.startUpdate", info.name, info.newVersion));
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public void updateDone(Map<String, ApplicationInfo> appToUpdate,
119         Map<String, Exception> appUpdateError) {
120 
121         boolean updateDone = false;
122 
123         // check errors
124         for (final Entry<String, ApplicationInfo> entry : appToUpdate.entrySet()) {
125             Exception error = appUpdateError.get(entry.getKey());
126             if (error != null) {
127                 throw ApplicationActionException.propagateError(
128                     action, new ApplicationTechnicalException(t("quadrige2.pluginsUpdater.error", entry.getKey()), error));
129             }
130             ApplicationInfo info = entry.getValue();
131             if (info != null) {
132 
133                 if (LOG.isInfoEnabled()) {
134                     LOG.info(String.format(
135                         "A '%s' plugin update was downloaded (oldVersion: %s, newVersion: %s), will restart application to use it", 
136                         entry.getKey(), info.oldVersion, info.newVersion));
137                 }
138                 updateDone = true;
139             }
140         }
141 
142         if (updateDone) {
143             applicationUpdated = true;
144         }
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     public void aborted(String propertiesURL, Exception eee) {
150         if (LOG.isErrorEnabled()) {
151             LOG.error("Could not update from " + propertiesURL, eee);
152         }
153         throw ApplicationActionException.propagateError(action, eee);
154     }
155 }