Coverage Report - org.jtheque.core.managers.update.UpdateManager
 
Classes in this File Line Coverage Branch Coverage Complexity
UpdateManager
0 %
0/112
0 %
0/54
2.25
 
 1  
 package org.jtheque.core.managers.update;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 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  
  * Manage the update of the application. This class can go on internet to verify if a more recent
 37  
  * version of JTheque is available and download one new version if there is one.
 38  
  *
 39  
  * @author Baptiste Wicht
 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  
      * Private constructor, the instance of the class is accessible by getInstance() method.
 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  
      * Read the updatable version of the updatable.
 74  
      *
 75  
      * @param updatable The updatable to fill with the version.
 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  
         //Download all files
 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  
      * Apply the online version.
 122  
      *
 123  
      * @param onlineVersion The online version to apply.
 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  
      * Register the patches of the new installation.
 161  
      *
 162  
      * @param result  The new installation result.
 163  
      * @param version The installed version.
 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  
             //Download all files
 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  
      * Propose an update to the user.
 213  
      *
 214  
      * @param dialogKey      The i18n key of the message.
 215  
      * @param dialogKeyTitle The i18n key of the title.
 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  
      * Indicate if all modules are up to date.
 242  
      *
 243  
      * @return true if all modules are up to date else false.
 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  
      * Fire an updatable added event.
 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  
      * Set the versions loader. This is not for use, this is only for Spring injection.
 327  
      *
 328  
      * @param versionsLoader The versions loader implementation.
 329  
      */
 330  
     public void setVersionsLoader(IVersionsLoader versionsLoader) {
 331  0
         this.versionsLoader = versionsLoader;
 332  0
     }
 333  
 }