Coverage Report - org.jtheque.core.managers.update.actions.DownloadAction
 
Classes in this File Line Coverage Branch Coverage Complexity
DownloadAction
0 %
0/23
0 %
0/14
3.75
 
 1  
 package org.jtheque.core.managers.update.actions;
 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.Managers;
 20  
 import org.jtheque.core.managers.error.InternationalizedError;
 21  
 import org.jtheque.core.managers.log.ILoggingManager;
 22  
 import org.jtheque.core.managers.view.able.IViewManager;
 23  
 import org.jtheque.utils.OSUtils;
 24  
 import org.jtheque.utils.StringUtils;
 25  
 import org.jtheque.utils.io.FileException;
 26  
 import org.jtheque.utils.io.FileUtils;
 27  
 
 28  
 import java.io.File;
 29  
 import java.io.FileNotFoundException;
 30  
 
 31  
 /**
 32  
  * An update action that download a file.
 33  
  *
 34  
  * @author Baptiste Wicht
 35  
  */
 36  0
 public final class DownloadAction extends AbstractUpdateAction {
 37  
     private String url;
 38  
     private String os;
 39  
 
 40  
     @Override
 41  
     public void execute() {
 42  0
         if (canBeExecutedOnThisOS()) {
 43  
             try {
 44  0
                 if(!new File(getDestination()).delete()){
 45  0
                     setFile("dljt_" + getFile());
 46  
                 }
 47  
 
 48  0
                 FileUtils.downloadFile(url, getDestination());
 49  0
             } catch (FileException e) {
 50  0
                 Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 51  
 
 52  0
                 if(e.getCause() instanceof FileNotFoundException){
 53  0
                     Managers.getManager(IViewManager.class).displayError(
 54  
                             new InternationalizedError("error.update.download", e.getMessage()));
 55  
                 }
 56  0
             }
 57  
         }
 58  0
     }
 59  
 
 60  
     /**
 61  
      * Test if this action can be executed in this operating system or not.
 62  
      *
 63  
      * @return <code>true</code> if this action can be executed in this operating system else <code>false</code>.
 64  
      */
 65  
     private boolean canBeExecutedOnThisOS() {
 66  0
         if (StringUtils.isNotEmpty(os)) {
 67  0
             if ("linux".equalsIgnoreCase(os)) {
 68  0
                 return OSUtils.isLinux();
 69  0
             } else if ("mac".equalsIgnoreCase(os)) {
 70  0
                 return OSUtils.isMac();
 71  0
             } else if ("windows".equalsIgnoreCase(os)) {
 72  0
                 return OSUtils.isWindows();
 73  
             }
 74  
         }
 75  
 
 76  0
         return true;
 77  
     }
 78  
 
 79  
     /**
 80  
      * Set the URL of the file to download.
 81  
      *
 82  
      * @param url The URL.
 83  
      */
 84  
     public void setUrl(String url) {
 85  0
         this.url = url;
 86  0
     }
 87  
 
 88  
     /**
 89  
      * Set the OS constraint of the action.
 90  
      *
 91  
      * @param os The OS constraint of the action.
 92  
      */
 93  
     public void setOs(String os) {
 94  0
         this.os = os;
 95  0
     }
 96  
 }