Coverage Report - org.vostokframework.domain.loading.states.fileloader.algorithms.MaxAttemptsFileLoadingAlgorithm
 
Classes in this File Line Coverage Branch Coverage Complexity
MaxAttemptsFileLoadingAlgorithm
83%
49/59
N/A
0
 
 1  
 /*
 2  
  * Licensed under the MIT License
 3  
  * 
 4  
  * Copyright 2011 (c) Flávio Silva, flsilva.com
 5  
  *
 6  
  * Permission is hereby granted, free of charge, to any person
 7  
  * obtaining a copy of this software and associated documentation
 8  
  * files (the "Software"), to deal in the Software without
 9  
  * restriction, including without limitation the rights to use,
 10  
  * copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  
  * copies of the Software, and to permit persons to whom the
 12  
  * Software is furnished to do so, subject to the following
 13  
  * conditions:
 14  
  *
 15  
  * The above copyright notice and this permission notice shall be
 16  
  * included in all copies or substantial portions of the Software.
 17  
  *
 18  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 19  
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 20  
  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 21  
  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 22  
  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 23  
  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 24  
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 25  
  * OTHER DEALINGS IN THE SOFTWARE.
 26  
  * 
 27  
  * http://www.opensource.org/licenses/mit-license.php
 28  
  */
 29  1
 package org.vostokframework.domain.loading.states.fileloader.algorithms
 30  
 {
 31  
         import org.as3collections.IIterator;
 32  
         import org.as3collections.IList;
 33  
         import org.as3collections.lists.ArrayList;
 34  
         import org.as3collections.lists.TypedList;
 35  
         import org.vostokframework.domain.loading.LoadError;
 36  
         import org.vostokframework.domain.loading.LoadErrorType;
 37  
         import org.vostokframework.domain.loading.states.fileloader.IFileLoadingAlgorithm;
 38  
         import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmErrorEvent;
 39  
 
 40  
         import flash.events.Event;
 41  
         import flash.events.EventDispatcher;
 42  
         import flash.events.IEventDispatcher;
 43  
 
 44  
         /**
 45  
          * description
 46  
          * 
 47  
          * @author Flávio Silva
 48  
          */
 49  
         public class MaxAttemptsFileLoadingAlgorithm extends FileLoadingAlgorithmBehavior
 50  
         {
 51  
                 /**
 52  
                  * @private
 53  
                   */
 54  
                 private var _dispatcher:IEventDispatcher;
 55  
                 private var _errors:IList;//<LoadError>
 56  
                 private var _maxAttempts:int;
 57  
                 private var _performedAttempts:int;
 58  
                 
 59  
                 /**
 60  
                  * description
 61  
                  * 
 62  
                  * @param loader
 63  
                  * @param request
 64  
                  * @param context
 65  
                  */
 66  
                 public function MaxAttemptsFileLoadingAlgorithm(wrapAlgorithm:IFileLoadingAlgorithm, maxAttempts:int = 1)
 67  
                 {
 68  1
                         super(wrapAlgorithm);
 69  
                         
 70  1
                         if (maxAttempts < 1) throw new ArgumentError("Argument <maxAttempts> must be greater than zero. Received: <" + maxAttempts + ">");
 71  
                         
 72  1
                         _maxAttempts = maxAttempts;
 73  1
                         _dispatcher = new EventDispatcher(this);
 74  1
                         _errors = new TypedList(new ArrayList(), LoadError);
 75  
                         
 76  1
                         addWrappedAlgorithmListeners();
 77  1
                 }
 78  
                 
 79  
                 override public function addEventListener(type : String, listener : Function, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = false) : void
 80  
                 {
 81  1
                         validateDisposal();
 82  
                         
 83  1
                         if (type == FileLoadingAlgorithmErrorEvent.FAILED)
 84  
                         {
 85  1
                                 _dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
 86  
                         }
 87  
                         else
 88  
                         {
 89  1
                                 super.addEventListener(type, listener, useCapture, priority, useWeakReference);
 90  
                         }
 91  1
                 }
 92  
                 
 93  
                 override public function dispatchEvent(event : Event) : Boolean
 94  
                 {
 95  1
                         validateDisposal();
 96  
                         
 97  1
                         if (event.type == FileLoadingAlgorithmErrorEvent.FAILED)
 98  
                         {
 99  1
                                 return _dispatcher.dispatchEvent(event);
 100  
                         }
 101  
                         else
 102  
                         {
 103  0
                                 return super.dispatchEvent(event);
 104  
                         }
 105  
                 }
 106  
                 
 107  
                 override public function hasEventListener(type : String) : Boolean
 108  
                 {
 109  0
                         validateDisposal();
 110  
                         
 111  0
                         if (type == FileLoadingAlgorithmErrorEvent.FAILED)
 112  
                         {
 113  0
                                 return _dispatcher.hasEventListener(type);
 114  
                         }
 115  
                         else
 116  
                         {
 117  0
                                 return super.hasEventListener(type);
 118  
                         }
 119  
                 }
 120  
                 
 121  
                 /**
 122  
                  * description
 123  
                  * 
 124  
                  * @return
 125  
                   */
 126  
                 override public function load(): void
 127  
                 {
 128  1
                         validateDisposal();
 129  
                         
 130  1
                         if (isExaustedAttempts() || hasSomeFatalError())
 131  
                         {
 132  1
                                 failed();
 133  1
                                 return;
 134  
                         }
 135  
                         
 136  1
                         wrappedAlgorithm.load();
 137  1
                 }
 138  
 
 139  
                 override public function removeEventListener(type : String, listener : Function, useCapture : Boolean = false) : void
 140  
                 {
 141  1
                         validateDisposal();
 142  
                         
 143  1
                         if (type == FileLoadingAlgorithmErrorEvent.FAILED)
 144  
                         {
 145  1
                                 _dispatcher.removeEventListener(type, listener, useCapture);
 146  
                         }
 147  
                         else
 148  
                         {
 149  1
                                 super.removeEventListener(type, listener, useCapture);
 150  
                         }
 151  1
                 }
 152  
                 
 153  
                 override public function willTrigger(type : String) : Boolean
 154  
                 {
 155  0
                         validateDisposal();
 156  
                         
 157  0
                         if (type == FileLoadingAlgorithmErrorEvent.FAILED)
 158  
                         {
 159  0
                                 return _dispatcher.willTrigger(type);
 160  
                         }
 161  
                         else
 162  
                         {
 163  0
                                 return super.willTrigger(type);
 164  
                         }
 165  
                 }
 166  
                 
 167  
                 override protected function doDispose():void
 168  
                 {
 169  1
                         removeWrappedAlgorithmListeners();
 170  1
                 }
 171  
                 
 172  
                 private function addWrappedAlgorithmListeners():void
 173  
                 {
 174  1
                         wrappedAlgorithm.addEventListener(FileLoadingAlgorithmErrorEvent.FAILED, failedHandler, false, 0, true);
 175  1
                 }
 176  
                 
 177  
                 private function failed():void
 178  
                 {
 179  1
                         validateDisposal();
 180  1
                         removeWrappedAlgorithmListeners();
 181  
                         
 182  1
                         dispatchEvent(new FileLoadingAlgorithmErrorEvent(FileLoadingAlgorithmErrorEvent.FAILED, _errors));
 183  1
                 }
 184  
                 
 185  
                 private function failedHandler(event:FileLoadingAlgorithmErrorEvent):void
 186  
                 {
 187  1
                         validateDisposal();
 188  
                         
 189  1
                         _errors.addAll(event.errors);
 190  1
                         _performedAttempts++;
 191  
                         /*
 192  
                         // IF IT'S A SECURITY ERROR
 193  
                         // IT DOES NOT USE ATTEMPTS TO TRY AGAIN
 194  
                         // IT JUST FAIL
 195  
                         var lastError:LoadError = _errors.getAt(_errors.size() - 1);
 196  
                         if (lastError.type.equals(LoadErrorType.SECURITY_ERROR))
 197  
                         {
 198  
                                 failed();
 199  
                                 return;
 200  
                         }
 201  
                         else
 202  
                         {
 203  
                                 load();
 204  
                         }
 205  
                         */
 206  1
                         load();
 207  1
                 }
 208  
                 
 209  
                 private function hasSomeFatalError():Boolean
 210  
                 {
 211  1
                         if (!_errors || _errors.isEmpty()) return false;
 212  
                         
 213  1
                         var it:IIterator = _errors.iterator();
 214  
                         var error:LoadError;
 215  
                         
 216  
                         // IF A SECURITY ERROR OR IO ERROR OCCURRED
 217  
                         // IT DOES NOT USE ATTEMPTS TO TRY AGAIN
 218  
                         // IT JUST FAIL
 219  
                         
 220  1
                         while (it.hasNext())
 221  
                         {
 222  1
                                 error = it.next();
 223  1
                                 if (error.type.equals(LoadErrorType.SECURITY_ERROR)
 224  1
                                         || error.type.equals(LoadErrorType.IO_ERROR)) return true;
 225  
                         }
 226  
                         
 227  0
                         return false;
 228  
                 }
 229  
                 
 230  
                 private function isExaustedAttempts():Boolean
 231  
                 {
 232  1
                         return _performedAttempts >= _maxAttempts;
 233  
                 }
 234  
                 
 235  
                 private function removeWrappedAlgorithmListeners():void
 236  
                 {
 237  1
                         wrappedAlgorithm.removeEventListener(FileLoadingAlgorithmErrorEvent.FAILED, failedHandler, false);
 238  1
                 }
 239  
                 
 240  
         }
 241  
 
 242  
 }