Coverage Report - org.vostokframework.domain.loading.states.fileloader.adapters.DataLoaderFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
DataLoaderFactory
4%
3/64
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.adapters
 30  
 {
 31  
         import org.as3coreaddendum.errors.IllegalStateError;
 32  
         import org.vostokframework.domain.assets.AssetType;
 33  
         import org.vostokframework.domain.loading.settings.ApplicationDomainSetting;
 34  
         import org.vostokframework.domain.loading.settings.LoadingSecuritySettings;
 35  
         import org.vostokframework.domain.loading.settings.LoadingSettings;
 36  
         import org.vostokframework.domain.loading.settings.SecurityDomainSetting;
 37  
         import org.vostokframework.domain.loading.states.fileloader.IDataLoader;
 38  
         import org.vostokframework.domain.loading.states.fileloader.IDataLoaderFactory;
 39  
 
 40  
         import flash.display.Loader;
 41  
         import flash.media.Sound;
 42  
         import flash.media.SoundLoaderContext;
 43  
         import flash.net.NetConnection;
 44  
         import flash.net.NetStream;
 45  
         import flash.net.URLLoader;
 46  
         import flash.net.URLLoaderDataFormat;
 47  
         import flash.net.URLRequest;
 48  
         import flash.system.ApplicationDomain;
 49  
         import flash.system.LoaderContext;
 50  
         import flash.system.SecurityDomain;
 51  
 
 52  
         /**
 53  
          * description
 54  
          * 
 55  
          * @author Flávio Silva
 56  
          */
 57  
         public class DataLoaderFactory implements IDataLoaderFactory
 58  
         {
 59  
                 
 60  
                 /**
 61  
                  * description
 62  
                  * 
 63  
                  */
 64  
                 public function DataLoaderFactory()
 65  1
                 {
 66  
                         
 67  1
                 }
 68  
                 
 69  
                 public function create(type:AssetType, url:String, settings:LoadingSettings):IDataLoader
 70  
                 {
 71  
                         var dataLoader:IDataLoader;
 72  0
                         var urlRequest:URLRequest = new URLRequest(url);
 73  
                         
 74  0
                         switch(type)
 75  
                         {
 76  0
                                 case AssetType.IMAGE:
 77  0
                                 case AssetType.SWF:
 78  
                                 {
 79  0
                                         var loader:Loader = new Loader();
 80  0
                                         var loaderContext:LoaderContext = createLoaderContext(settings.security);
 81  
                                         
 82  0
                                         dataLoader = new NativeLoaderAdapter(loader, urlRequest, loaderContext);
 83  0
                                         break;
 84  
                                 }
 85  
                                 
 86  0
                                 case AssetType.CSS:
 87  0
                                 case AssetType.JSON:
 88  0
                                 case AssetType.TXT:
 89  0
                                 case AssetType.XML:
 90  
                                 {
 91  0
                                         var urlLoader:URLLoader = new URLLoader();
 92  0
                                         urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
 93  
                                         
 94  0
                                         dataLoader = new NativeURLLoaderAdapter(urlLoader, urlRequest);
 95  0
                                         break;
 96  
                                 }
 97  
                                 
 98  0
                                 case AssetType.AAC:
 99  0
                                 case AssetType.VIDEO:
 100  
                                 {
 101  0
                                         var netConnection:NetConnection = new NetConnection();
 102  0
                                         netConnection.connect(null);
 103  
                                         
 104  0
                                         var netStream:NetStream = new NetStream(netConnection);
 105  0
                                         netStream.bufferTime = settings.media.bufferTime;
 106  0
                                         netStream.checkPolicyFile = settings.security.checkPolicyFile;
 107  
                                         
 108  0
                                         dataLoader = new NativeNetStreamAdapter(netStream, netConnection, urlRequest);
 109  0
                                         dataLoader = new ProgressNetStream(dataLoader, netStream, settings.media.bufferPercent);
 110  
                                         
 111  0
                                         if (settings.media.autoStopStream) dataLoader = new AutoStopNetStream(dataLoader, netStream);
 112  
                                         
 113  0
                                         if (settings.media.autoCreateVideo)
 114  
                                         {
 115  0
                                                 dataLoader = new AutoCreateNetStreamVideo(dataLoader, netStream);
 116  0
                                                 if (settings.media.autoResizeVideo)
 117  
                                                 {
 118  0
                                                         dataLoader = new AutoResizeNetStreamVideo(dataLoader, netStream);
 119  
                                                 }
 120  
                                         }
 121  
                                         
 122  0
                                         break;
 123  
                                 }
 124  
                                 
 125  0
                                 case AssetType.MP3:
 126  
                                 {
 127  0
                                         var sound:Sound = new Sound();
 128  0
                                         var soundContext:SoundLoaderContext = createSoundLoaderContext(settings.security);
 129  
                                         
 130  0
                                         dataLoader = new NativeSoundAdapter(sound, urlRequest, soundContext);
 131  0
                                         break;
 132  
                                 }
 133  
                                 
 134  
                         }
 135  
                         
 136  0
                         if (!dataLoader)
 137  
                         {
 138  0
                                 var errorMessage:String = "It was not possible to create a NativeDataLoader object for the received type:\n";
 139  0
                                 errorMessage = "<type>: " + type + "\n";
 140  0
                                 errorMessage = "<url>: " + url + "\n";
 141  
                                 
 142  0
                                 throw new IllegalStateError(errorMessage);
 143  
                         }
 144  
                         
 145  
                         //TODO:settings.media.audioLinkage
 146  
                         
 147  0
                         return dataLoader;
 148  
                 }
 149  
                 
 150  
                 protected function createLoaderContext(security:LoadingSecuritySettings):LoaderContext
 151  
                 {
 152  0
                         var checkPolicyFile:Boolean = security.checkPolicyFile;
 153  0
                         var ignoreLocalSecurityDomain:Boolean = security.ignoreLocalSecurityDomain;
 154  
                         
 155  0
                         var applicationDomain:ApplicationDomain = getApplicationDomain(security.applicationDomain);
 156  0
                         var securityDomain:SecurityDomain = getSecurityDomain(security.securityDomain);
 157  
                         
 158  
                         //if (ignoreLocalSecurityDomain && isLocal) securityDomain = null;//TODO:implement it
 159  
                         
 160  0
                         return new LoaderContext(checkPolicyFile, applicationDomain, securityDomain);
 161  
                 }
 162  
                 
 163  
                 protected function createSoundLoaderContext(security:LoadingSecuritySettings):SoundLoaderContext
 164  
                 {
 165  0
                         var checkPolicyFile:Boolean = security.checkPolicyFile;
 166  0
                         return new SoundLoaderContext(1000, checkPolicyFile);
 167  
                 }
 168  
                 
 169  
                 protected function getApplicationDomain(setting:ApplicationDomainSetting):ApplicationDomain
 170  
                 {
 171  0
                         if (!setting) return null;
 172  
                         
 173  
                         var applicationDomain:ApplicationDomain;
 174  
                         
 175  0
                         if (setting.equals(ApplicationDomainSetting.APART))
 176  
                         {
 177  0
                                 applicationDomain = new ApplicationDomain();
 178  
                         }
 179  0
                         else if (setting.equals(ApplicationDomainSetting.CHILD))
 180  
                         {
 181  0
                                 applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
 182  
                         }
 183  0
                         else if (setting.equals(ApplicationDomainSetting.CURRENT))
 184  
                         {
 185  0
                                 applicationDomain = ApplicationDomain.currentDomain;
 186  
                         }
 187  
                         
 188  0
                         return applicationDomain;
 189  
                 }
 190  
                 
 191  
                 protected function getSecurityDomain(setting:SecurityDomainSetting):SecurityDomain
 192  
                 {
 193  0
                         if (!setting) return null;
 194  
                         
 195  
                         var securityDomain:SecurityDomain;
 196  
                         
 197  0
                         if (setting.equals(SecurityDomainSetting.CURRENT))
 198  
                         {
 199  0
                                 securityDomain = SecurityDomain.currentDomain;
 200  
                         }
 201  
                         
 202  0
                         return securityDomain;
 203  
                 }
 204  
 
 205  
         }
 206  
 
 207  
 }