1 /* 2 * Copyright the original author or authors. 3 * 4 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.mozilla.org/MPL/MPL-1.1.html 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import org.as2lib.app.exec.AbstractProcess; 18 import org.as2lib.io.file.FileLoader; 19 import org.as2lib.io.file.LoadStartListener; 20 import org.as2lib.io.file.LoadCompleteListener; 21 import org.as2lib.io.file.LoadProgressListener; 22 import org.as2lib.io.file.LoadErrorListener; 23 import org.as2lib.data.holder.Map; 24 import org.as2lib.app.exec.Executable; 25 26 /** 27 * {@code FileLoaderProcess} is a mediator to handle loading of files as 28 * a {@code Process}. 29 * 30 * <p>If you need to handle a {@link FileLoader} with a {@link Process} context 31 * you can use {@code FileLoaderProcess} as mediator between both APIs. 32 * 33 * <p>In contrast to {@code FileLoader} you have to call it like: 34 * <code> 35 * import org.as2lib.io.file.TextFileLoader; 36 * import org.as2lib.io.file.FileLoaderProcess; 37 * 38 * var fileLoader:TextFileLoader = new TextFileLoader(); 39 * var loader:FileLoaderProcess = new FileLoaderProcess(fileLoader); 40 * loader.setUri("test.txt"); 41 * loader.start(); 42 * </code> 43 * 44 * @author Martin Heidegger 45 * @version 1.0 46 */ 47 class org.as2lib.io.file.FileLoaderProcess extends AbstractProcess 48 implements LoadStartListener, 49 LoadCompleteListener, 50 LoadProgressListener, 51 LoadErrorListener { 52 53 /** Resource loader to be mediated. */ 54 private var resourceLoader:FileLoader; 55 56 /** URI to load. */ 57 private var uri:String; 58 59 /** Parameter submit method for the request. */ 60 private var method:String; 61 62 /** Parameter to be used for the request. */ 63 private var parameter:Map; 64 65 /** {@code Executable} to be called on finish of the process. */ 66 private var callBack:Executable; 67 68 /** 69 * Constructs a new {@code FileLoaderProcess}. 70 * 71 * @param resourceLoader {@code FileLoader} to be mediated 72 */ 73 public function FileLoaderProcess(resourceLoader:FileLoader) { 74 this.resourceLoader = resourceLoader; 75 resourceLoader.addListener(this); 76 } 77 78 /** 79 * Prepares the instance for loading events. 80 * 81 * <p>All passed-in parameters will be used to {@code .load} the certain 82 * resource. 83 * 84 * @param uri location of the resource to load 85 * @param parameters (optional) parameters for loading the resource 86 * @param method (optional) POST/GET as method for submitting the parameters, 87 * default method used if {@code method} was not passed-in is POST. 88 * @param callBack (optional) {@link Executable} to be executed after the 89 * the resource was loaded. 90 */ 91 public function setUri(uri:String, method:String, parameter:Map, callBack:Executable):Void { 92 this.uri = uri; 93 this.method = method; 94 this.parameter = parameter; 95 this.callBack = callBack; 96 } 97 98 /** 99 * Returns the {@code FileLoader} that was mediated. 100 * 101 * @return {@code FileLoader} that was mediated. 102 */ 103 public function getFileLoader(Void):FileLoader { 104 return resourceLoader; 105 } 106 107 /** 108 * Implementation of {@code AbstractProcess#run} to handle loading 109 * of the process. 110 */ 111 public function run() { 112 var uri:String = (arguments[0] instanceof String) ? arguments[0] : this.uri; 113 var method:String = (arguments[1] instanceof String) ? arguments[1] : this.method; 114 var parameter:Map = (arguments[2] instanceof Map) ? arguments[2] : this.parameter; 115 var callBack:Executable = (arguments[3] instanceof Executable) ? arguments[3] : this.callBack; 116 resourceLoader.load(uri, method, parameter, callBack); 117 working = !hasFinished(); 118 } 119 120 /** 121 * Forwards {@code getPercentage} to the mediated {@code FileLoader.getPercentage}. 122 * 123 * @return percentage of the resource 124 */ 125 public function getPercentage(Void):Number { 126 return resourceLoader.getPercentage(); 127 } 128 129 /** 130 * Handles the {@code FileLoader}s start event. 131 * 132 * @param resourceLoader {@code FileLoader} that sent the event 133 */ 134 public function onLoadStart(resourceLoader:FileLoader):Void { 135 sendStartEvent(); 136 } 137 138 /** 139 * Handles the {@code FileLoader}s complete event. 140 * 141 * @param resourceLoader {@code FileLoader} that sent the event 142 */ 143 public function onLoadComplete(resourceLoader:FileLoader):Void { 144 finish(); 145 } 146 147 /** 148 * Handles the {@code FileLoader}s progress event. 149 * 150 * @param resourceLoader {@code FileLoader} that sent the event 151 */ 152 public function onLoadProgress(resourceLoader:FileLoader):Void { 153 sendUpdateEvent(); 154 } 155 156 /** 157 * Handles the {@code FileLoader}s error event. 158 * 159 * @param resourceLoader {@code FileLoader} that sent the event 160 */ 161 public function onLoadError(resourceLoader:FileLoader, errorCode:String, error):Boolean { 162 interrupt(error); 163 return true; 164 } 165 }