1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 1 | package org.vostokframework.application.services |
30 | |
{ |
31 | |
import org.as3collections.ICollection; |
32 | |
import org.as3collections.IIterator; |
33 | |
import org.as3collections.IList; |
34 | |
import org.as3collections.IListMap; |
35 | |
import org.as3collections.lists.ArrayList; |
36 | |
import org.as3collections.lists.TypedList; |
37 | |
import org.as3collections.maps.ArrayListMap; |
38 | |
import org.as3collections.utils.CollectionUtil; |
39 | |
import org.as3utils.StringUtil; |
40 | |
import org.vostokframework.VostokFramework; |
41 | |
import org.vostokframework.VostokIdentification; |
42 | |
import org.vostokframework.application.LoadingContext; |
43 | |
import org.vostokframework.application.cache.CachedAssetData; |
44 | |
import org.vostokframework.application.cache.CachedAssetDataRepository; |
45 | |
import org.vostokframework.application.cache.errors.AssetDataAlreadyCachedError; |
46 | |
import org.vostokframework.application.cache.errors.CachedAssetDataNotFoundError; |
47 | |
import org.vostokframework.application.monitoring.ILoadingMonitor; |
48 | |
import org.vostokframework.application.monitoring.monitors.CompositeLoadingMonitor; |
49 | |
import org.vostokframework.application.monitoring.monitors.LoadingMonitor; |
50 | |
import org.vostokframework.application.monitoring.monitors.LoadingMonitorDispatcher; |
51 | |
import org.vostokframework.application.monitoring.monitors.dispatchers.AssetLoadingMonitorDispatcher; |
52 | |
import org.vostokframework.application.monitoring.monitors.dispatchers.QueueLoadingMonitorDispatcher; |
53 | |
import org.vostokframework.domain.assets.Asset; |
54 | |
import org.vostokframework.domain.loading.GlobalLoadingSettings; |
55 | |
import org.vostokframework.domain.loading.ILoader; |
56 | |
import org.vostokframework.domain.loading.ILoaderFactory; |
57 | |
import org.vostokframework.domain.loading.LoadPriority; |
58 | |
import org.vostokframework.domain.loading.LoaderRepository; |
59 | |
import org.vostokframework.domain.loading.errors.DuplicateLoaderError; |
60 | |
import org.vostokframework.domain.loading.errors.DuplicateLoadingMonitorError; |
61 | |
import org.vostokframework.domain.loading.errors.LoaderNotFoundError; |
62 | |
import org.vostokframework.domain.loading.errors.LoadingMonitorNotFoundError; |
63 | |
import org.vostokframework.domain.loading.settings.LoadingSettings; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public class LoadingService |
71 | |
{ |
72 | |
|
73 | |
|
74 | |
|
75 | |
private var _context:LoadingContext; |
76 | |
|
77 | |
private function get globalMonitor():ILoadingMonitor { return _context.globalQueueLoadingMonitor; } |
78 | |
|
79 | |
private function get globalQueueLoader():ILoader { return _context.globalQueueLoader; } |
80 | |
|
81 | |
private function get cachedAssetDataRepository():CachedAssetDataRepository { return _context.cachedAssetDataRepository; } |
82 | |
|
83 | |
private function get loaderRepository():LoaderRepository { return _context.loaderRepository; } |
84 | |
|
85 | |
|
86 | |
|
87 | |
private function get loaderFactory():ILoaderFactory { return _context.loaderFactory; } |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public function LoadingService() |
93 | 1 | { |
94 | 1 | _context = LoadingContext.getInstance(); |
95 | 1 | } |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public function cancel(loaderId:String, locale:String = null): void |
103 | |
{ |
104 | 1 | if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String."); |
105 | |
|
106 | 1 | if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; |
107 | 1 | var identification:VostokIdentification = new VostokIdentification(loaderId, locale); |
108 | |
|
109 | 1 | if (!exists(loaderId, locale)) |
110 | |
{ |
111 | 1 | var message:String = "There is no ILoader object stored with specified arguments:\n"; |
112 | 1 | message += "<loaderId>: <" + loaderId + ">\n"; |
113 | 1 | message += "<locale>: <" + locale + ">\n"; |
114 | 1 | message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n"; |
115 | |
|
116 | 1 | throw new LoaderNotFoundError(identification, message); |
117 | |
} |
118 | |
|
119 | 1 | globalQueueLoader.cancelChild(identification); |
120 | 1 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public function changePriority(loaderId:String, priority:LoadPriority, locale:String = null): void |
133 | |
{ |
134 | 1 | if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <assetId> must not be null nor an empty String."); |
135 | 1 | if (!priority) throw new ArgumentError("Argument <priority> must not be null."); |
136 | |
|
137 | 1 | if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; |
138 | 1 | var identification:VostokIdentification = new VostokIdentification(loaderId, locale); |
139 | |
|
140 | 1 | var assetService:AssetService = new AssetService(); |
141 | |
|
142 | 1 | if (!exists(loaderId, locale) && !assetService.assetExists(loaderId, locale)) |
143 | |
{ |
144 | 1 | var message:String = "There is no ILoader object NOR Asset object stored with specified arguments:\n"; |
145 | 1 | message += "<loaderId>: <" + loaderId + ">\n"; |
146 | 1 | message += "<locale>: <" + locale + ">\n"; |
147 | 1 | message += "If you are trying to change the priority of an ongoing loading, is a queue or an asset, use method <LoadingService().exists()> to check if a ILoader object exists.\n"; |
148 | 1 | message += "But if you are trying to change the priority of a previously created Asset object that is not loading, use method <AssetService().assetExists()> to check if an Asset object exists.\n"; |
149 | |
|
150 | 1 | throw new LoaderNotFoundError(identification, message); |
151 | |
} |
152 | |
|
153 | 1 | if (exists(loaderId, locale)) |
154 | |
{ |
155 | 1 | var loader:ILoader = globalQueueLoader.getChild(identification); |
156 | 1 | loader.priority = priority.ordinal; |
157 | |
} |
158 | |
|
159 | 1 | if (assetService.assetExists(loaderId, locale)) |
160 | |
{ |
161 | 1 | var asset:Asset = assetService.getAsset(loaderId, locale); |
162 | |
|
163 | 1 | var settings:LoadingSettings = _context.loadingSettingsRepository.find(asset); |
164 | 1 | settings.policy.priority = priority; |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | 1 | } |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public function containsAssetData(assetId:String, locale:String = null): Boolean |
188 | |
{ |
189 | 1 | if (StringUtil.isBlank(assetId)) throw new ArgumentError("Argument <assetId> must not be null nor an empty String."); |
190 | 1 | if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; |
191 | |
|
192 | 1 | var identification:VostokIdentification = new VostokIdentification(assetId, locale); |
193 | 1 | return cachedAssetDataRepository.exists(identification); |
194 | |
} |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
public function exists(loaderId:String, locale:String = null): Boolean |
203 | |
{ |
204 | 1 | if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String."); |
205 | |
|
206 | 1 | if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; |
207 | 1 | var identification:VostokIdentification = new VostokIdentification(loaderId, locale); |
208 | |
|
209 | |
|
210 | 1 | return globalQueueLoader.containsChild(identification); |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
public function getAssetData(assetId:String, locale:String = null): * |
220 | |
{ |
221 | 1 | if (StringUtil.isBlank(assetId)) throw new ArgumentError("Argument <assetId> must not be null nor an empty String."); |
222 | 1 | if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; |
223 | |
|
224 | 1 | var identification:VostokIdentification = new VostokIdentification(assetId, locale); |
225 | 1 | if(!cachedAssetDataRepository.exists(identification)) |
226 | |
{ |
227 | 1 | var message:String = "There is no data cached for an Asset object with identification:\n"; |
228 | 1 | message += "<" + identification + ">\n"; |
229 | 1 | message += "Use method <LoadingService().containsAssetData()> to check if an Asset object was loaded and cached.\n"; |
230 | |
|
231 | 1 | throw new CachedAssetDataNotFoundError(identification, message); |
232 | |
} |
233 | |
|
234 | 1 | var cachedAssetData:CachedAssetData = cachedAssetDataRepository.find(identification); |
235 | 1 | return cachedAssetData.data; |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
public function getMonitor(loaderId:String, locale:String = null): ILoadingMonitor |
245 | |
{ |
246 | 1 | if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String."); |
247 | |
|
248 | 1 | if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; |
249 | 1 | var identification:VostokIdentification = new VostokIdentification(loaderId, locale); |
250 | |
|
251 | 1 | if (!globalMonitor.containsChild(identification)) |
252 | |
{ |
253 | 1 | var message:String = "There is no ILoadingMonitor object stored with specified arguments:\n"; |
254 | 1 | message += "<loaderId>: <" + loaderId + ">\n"; |
255 | 1 | message += "<locale>: <" + locale + ">\n"; |
256 | 1 | message += "Use method <LoadingService().exists()> to check if an ILoadingMonitor object is mapped for a ILoader object.\n"; |
257 | |
|
258 | 1 | throw new LoadingMonitorNotFoundError(message); |
259 | |
} |
260 | |
|
261 | 1 | return globalMonitor.getChild(identification);; |
262 | |
} |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
public function isLoading(loaderId:String, locale:String = null): Boolean |
271 | |
{ |
272 | 1 | if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String."); |
273 | |
|
274 | 1 | if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; |
275 | 1 | var identification:VostokIdentification = new VostokIdentification(loaderId, locale); |
276 | |
|
277 | 1 | if (!exists(loaderId, locale)) |
278 | |
{ |
279 | 1 | var message:String = "There is no ILoader object stored with specified arguments:\n"; |
280 | 1 | message += "<loaderId>: <" + loaderId + ">\n"; |
281 | 1 | message += "<locale>: <" + locale + ">\n"; |
282 | 1 | message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n"; |
283 | |
|
284 | 1 | throw new LoaderNotFoundError(identification, message); |
285 | |
} |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | 1 | var loader:ILoader = globalQueueLoader.getChild(identification); |
291 | 1 | return loader.isLoading; |
292 | |
} |
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
public function isQueued(loaderId:String, locale:String = null): Boolean |
301 | |
{ |
302 | 1 | if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String."); |
303 | |
|
304 | 1 | if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; |
305 | 1 | var identification:VostokIdentification = new VostokIdentification(loaderId, locale); |
306 | |
|
307 | 1 | if (!exists(loaderId, locale)) |
308 | |
{ |
309 | 1 | var message:String = "There is no ILoader object stored with specified arguments:\n"; |
310 | 1 | message += "<loaderId>: <" + loaderId + ">\n"; |
311 | 1 | message += "<locale>: <" + locale + ">\n"; |
312 | 1 | message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n"; |
313 | |
|
314 | 1 | throw new LoaderNotFoundError(identification, message); |
315 | |
} |
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | 1 | var loader:ILoader = globalQueueLoader.getChild(identification); |
321 | 1 | return loader.isQueued; |
322 | |
} |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
public function isStopped(loaderId:String, locale:String = null): Boolean |
331 | |
{ |
332 | 1 | if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String."); |
333 | |
|
334 | 1 | if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; |
335 | 1 | var identification:VostokIdentification = new VostokIdentification(loaderId, locale); |
336 | |
|
337 | 1 | if (!exists(loaderId, locale)) |
338 | |
{ |
339 | 0 | var message:String = "There is no ILoader object stored with specified arguments:\n"; |
340 | 0 | message += "<loaderId>: <" + loaderId + ">\n"; |
341 | 0 | message += "<locale>: <" + locale + ">\n"; |
342 | 0 | message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n"; |
343 | |
|
344 | 0 | throw new LoaderNotFoundError(identification, message); |
345 | |
} |
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | 1 | var loader:ILoader = globalQueueLoader.getChild(identification); |
351 | 1 | return loader.isStopped; |
352 | |
} |
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
public function load(loaderId:String, assets:IList, priority:LoadPriority = null, concurrentConnections:int = 1): ILoadingMonitor |
364 | |
{ |
365 | 1 | if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String."); |
366 | 1 | if (!assets || assets.isEmpty()) throw new ArgumentError("Argument <assets> must not be null nor empty."); |
367 | 1 | if (concurrentConnections < 1) throw new ArgumentError("Argument <concurrentConnections> must be greater than zero. Received: <" + concurrentConnections + ">"); |
368 | |
|
369 | |
|
370 | |
|
371 | 1 | assets = new TypedList(assets, Asset); |
372 | |
|
373 | |
|
374 | |
|
375 | 1 | validateDuplicateAsset(assets); |
376 | |
|
377 | 1 | if (!priority) priority = LoadPriority.MEDIUM; |
378 | |
|
379 | 1 | var identification:VostokIdentification = new VostokIdentification(loaderId, VostokFramework.CROSS_LOCALE_ID); |
380 | |
var errorMessage:String; |
381 | |
|
382 | 1 | if (globalQueueLoader.containsChild(identification)) |
383 | |
{ |
384 | 1 | errorMessage = "There is already a ILoader object stored with specified arguments:\n"; |
385 | 1 | errorMessage += "<loaderId>: <" + loaderId + ">\n"; |
386 | 1 | errorMessage += "utilized locale: <" + VostokFramework.CROSS_LOCALE_ID + ">\n"; |
387 | 1 | errorMessage += "Use method <LoadingService().exists()> to check if a ILoader object already exists.\n"; |
388 | 1 | errorMessage += "For further information please read the documentation section about ILoader object."; |
389 | |
|
390 | 1 | throw new DuplicateLoaderError(identification, errorMessage); |
391 | |
} |
392 | |
|
393 | 1 | if (globalMonitor.containsChild(identification)) |
394 | |
{ |
395 | 0 | errorMessage = "There is already a ILoadingMonitor object stored for a ILoader with identification:\n"; |
396 | 0 | errorMessage += "<identification>: <" + identification + ">\n"; |
397 | 0 | errorMessage += "Use method <LoadingService().exists()> to check if a ILoadingMonitor object already exists for the specified ILoader.\n"; |
398 | 0 | errorMessage += "For further information please read the documentation section about ILoadingMonitor object."; |
399 | |
|
400 | 0 | throw new DuplicateLoadingMonitorError(errorMessage); |
401 | |
} |
402 | |
|
403 | 1 | var globalLoadingSettings:GlobalLoadingSettings = LoadingContext.getInstance().globalLoadingSettings; |
404 | 1 | var queueLoader:ILoader = loaderFactory.createComposite(identification, loaderRepository, globalLoadingSettings, priority, concurrentConnections); |
405 | |
|
406 | |
|
407 | |
|
408 | 1 | checkIfSomeAssetIsAlreadyLoadedAndCached(assets); |
409 | |
|
410 | |
|
411 | |
|
412 | 1 | var loaders:IList = createLeafLoaders(assets); |
413 | |
|
414 | 1 | queueLoader.addChildren(loaders); |
415 | |
|
416 | 1 | var assetsAndLoadersMap:IListMap = createAssetsAndLoadersMap(assets, loaders); |
417 | 1 | var assetLoadingMonitors:IList = createAssetLoadingMonitors(assetsAndLoadersMap); |
418 | |
|
419 | 1 | var loadingMonitorDispatcher:LoadingMonitorDispatcher = new QueueLoadingMonitorDispatcher(identification.id, identification.locale); |
420 | 1 | var monitor:ILoadingMonitor = new CompositeLoadingMonitor(queueLoader, loadingMonitorDispatcher); |
421 | 1 | monitor.addChildren(assetLoadingMonitors); |
422 | |
|
423 | 1 | globalMonitor.addChild(monitor); |
424 | |
|
425 | 1 | globalQueueLoader.addChild(queueLoader); |
426 | 1 | globalQueueLoader.load(); |
427 | |
|
428 | 1 | return monitor; |
429 | |
} |
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
public function loadSingle(loaderId:String, asset:Asset, priority:LoadPriority = null, concurrentConnections:int = 1): ILoadingMonitor |
441 | |
{ |
442 | 1 | return load(loaderId, new ArrayList([asset]), priority, concurrentConnections); |
443 | |
} |
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
public function mergeAssets(loaderId:String, assets:IList, locale:String = null): ILoadingMonitor |
453 | |
{ |
454 | 1 | if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String."); |
455 | 1 | if (!assets || assets.isEmpty()) throw new ArgumentError("Argument <assets> must not be null nor empty."); |
456 | |
|
457 | 1 | if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; |
458 | 1 | var identification:VostokIdentification = new VostokIdentification(loaderId, locale); |
459 | |
|
460 | 1 | if (!exists(loaderId, locale)) |
461 | |
{ |
462 | 1 | var message:String = "There is no ILoader object stored with specified arguments:\n"; |
463 | 1 | message += "<loaderId>: <" + loaderId + ">\n"; |
464 | 1 | message += "<locale>: <" + locale + ">\n"; |
465 | 1 | message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n"; |
466 | |
|
467 | 1 | throw new LoaderNotFoundError(identification, message); |
468 | |
} |
469 | |
|
470 | |
|
471 | |
|
472 | 1 | assets = new TypedList(assets, Asset); |
473 | |
|
474 | |
|
475 | |
|
476 | 1 | validateDuplicateAsset(assets); |
477 | |
|
478 | |
|
479 | |
|
480 | 1 | checkIfSomeAssetIsAlreadyLoadedAndCached(assets); |
481 | |
|
482 | |
|
483 | |
|
484 | 1 | var loaders:IList = createLeafLoaders(assets); |
485 | |
|
486 | 1 | var assetsAndLoadersMap:IListMap = createAssetsAndLoadersMap(assets, loaders); |
487 | 1 | var assetLoadingMonitors:IList = createAssetLoadingMonitors(assetsAndLoadersMap); |
488 | |
|
489 | 1 | var queueLoader:ILoader = globalQueueLoader.getChild(identification); |
490 | 1 | queueLoader.addChildren(loaders); |
491 | |
|
492 | 1 | var monitor:ILoadingMonitor = globalMonitor.getChild(identification); |
493 | 1 | monitor.addChildren(assetLoadingMonitors); |
494 | |
|
495 | 1 | return monitor; |
496 | |
} |
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
public function removeAssetData(assetId:String, locale:String = null): void |
506 | |
{ |
507 | 1 | if (StringUtil.isBlank(assetId)) throw new ArgumentError("Argument <assetId> must not be null nor an empty String."); |
508 | 1 | if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; |
509 | |
|
510 | 1 | var identification:VostokIdentification = new VostokIdentification(assetId, locale); |
511 | |
|
512 | 1 | if (!containsAssetData(assetId, locale)) |
513 | |
{ |
514 | 1 | var message:String = "There is no data cached for an Asset object with identification:\n"; |
515 | 1 | message += "<" + identification + ">\n"; |
516 | 1 | message += "Use method <LoadingService().containsAssetData()> to check if an Asset object was loaded and cached.\n"; |
517 | |
|
518 | 1 | throw new CachedAssetDataNotFoundError(identification, message); |
519 | |
} |
520 | |
|
521 | 0 | cachedAssetDataRepository.remove(identification); |
522 | 0 | } |
523 | |
|
524 | |
|
525 | |
|
526 | |
|
527 | |
|
528 | |
|
529 | |
|
530 | |
public function resume(loaderId:String, locale:String = null): void |
531 | |
{ |
532 | 1 | if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String."); |
533 | |
|
534 | 1 | if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; |
535 | 1 | var identification:VostokIdentification = new VostokIdentification(loaderId, locale); |
536 | |
|
537 | 1 | if (!exists(loaderId, locale)) |
538 | |
{ |
539 | 1 | var message:String = "There is no ILoader object stored with specified arguments:\n"; |
540 | 1 | message += "<loaderId>: <" + loaderId + ">\n"; |
541 | 1 | message += "<locale>: <" + locale + ">\n"; |
542 | 1 | message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n"; |
543 | |
|
544 | 1 | throw new LoaderNotFoundError(identification, message); |
545 | |
} |
546 | |
|
547 | 1 | globalQueueLoader.resumeChild(identification); |
548 | 1 | } |
549 | |
|
550 | |
|
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
public function stop(loaderId:String, locale:String = null): void |
557 | |
{ |
558 | 1 | if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String."); |
559 | |
|
560 | 1 | if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; |
561 | 1 | var identification:VostokIdentification = new VostokIdentification(loaderId, locale); |
562 | |
|
563 | 1 | if (!exists(loaderId, locale)) |
564 | |
{ |
565 | 1 | var message:String = "There is no ILoader object stored with specified arguments:\n"; |
566 | 1 | message += "<loaderId>: <" + loaderId + ">\n"; |
567 | 1 | message += "<locale>: <" + locale + ">\n"; |
568 | 1 | message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n"; |
569 | |
|
570 | 1 | throw new LoaderNotFoundError(identification, message); |
571 | |
} |
572 | |
|
573 | 1 | globalQueueLoader.stopChild(identification); |
574 | 1 | } |
575 | |
|
576 | |
private function checkIfSomeAssetIsAlreadyLoadedAndCached(assets:IList):void |
577 | |
{ |
578 | |
var asset:Asset; |
579 | 1 | var it:IIterator = assets.iterator(); |
580 | |
|
581 | 1 | while (it.hasNext()) |
582 | |
{ |
583 | 1 | asset = it.next(); |
584 | |
|
585 | 1 | if (cachedAssetDataRepository.exists(asset.identification)) |
586 | |
{ |
587 | 1 | var cachedAssetData:CachedAssetData = cachedAssetDataRepository.find(asset.identification); |
588 | |
|
589 | 1 | var errorMessage:String = "The Asset object with identification:\n"; |
590 | 1 | errorMessage += "<" + asset.identification + ">\n"; |
591 | 1 | errorMessage += "Is already loaded and cached internally.\n"; |
592 | 1 | errorMessage += "It belonged to a queue loader object with identification:\n"; |
593 | 1 | errorMessage += "<" + cachedAssetData.queueIdentification + ">\n"; |
594 | 1 | errorMessage += "Use the method <LoadingService().containsAssetData()> to find it out.\n"; |
595 | 1 | errorMessage += "Also, cached asset data can be retrieved using <LoadingService().getAssetData()>."; |
596 | |
|
597 | 1 | throw new AssetDataAlreadyCachedError(asset.identification, errorMessage); |
598 | |
} |
599 | |
} |
600 | 1 | } |
601 | |
|
602 | |
private function createAssetsAndLoadersMap(assets:IList, loaders:IList):IListMap |
603 | |
{ |
604 | 1 | if (!assets || assets.isEmpty()) throw new ArgumentError("Argument <assets> must not be null nor empty."); |
605 | 1 | if (!loaders || loaders.isEmpty()) throw new ArgumentError("Argument <loaders> must not be null nor empty."); |
606 | 1 | if (assets.size() != loaders.size()) |
607 | |
{ |
608 | 0 | var errorMessage:String = "Both arguments must have same size.\n"; |
609 | 0 | errorMessage += "<assets.size()>: " + assets.size(); |
610 | 0 | errorMessage += "<loaders.size()>: " + loaders.size(); |
611 | |
|
612 | 0 | throw new ArgumentError(errorMessage); |
613 | |
} |
614 | |
|
615 | 1 | var map:IListMap = new ArrayListMap(); |
616 | 1 | var itAssets:IIterator = assets.iterator(); |
617 | 1 | var itLoaders:IIterator = loaders.iterator(); |
618 | |
|
619 | 1 | while (itAssets.hasNext()) |
620 | |
{ |
621 | 1 | map.put(itAssets.next(), itLoaders.next()); |
622 | |
} |
623 | |
|
624 | 1 | return map; |
625 | |
} |
626 | |
|
627 | |
private function createAssetLoadingMonitors(assetsAndLoaders:IListMap):IList |
628 | |
{ |
629 | |
var asset:Asset; |
630 | |
var loader:ILoader; |
631 | |
var assetLoadingMonitor:ILoadingMonitor; |
632 | 1 | var assetLoadingMonitors:IList = new ArrayList(); |
633 | |
var loadingMonitorDispatcher:LoadingMonitorDispatcher; |
634 | 1 | var it:IIterator = assetsAndLoaders.iterator(); |
635 | |
|
636 | 1 | while (it.hasNext()) |
637 | |
{ |
638 | 1 | loader = it.next(); |
639 | 1 | asset = it.pointer(); |
640 | |
|
641 | 1 | loadingMonitorDispatcher = new AssetLoadingMonitorDispatcher(asset.identification.id, asset.identification.locale, asset.type); |
642 | 1 | assetLoadingMonitor = new LoadingMonitor(loader, loadingMonitorDispatcher); |
643 | 1 | assetLoadingMonitors.add(assetLoadingMonitor); |
644 | |
} |
645 | |
|
646 | 1 | return assetLoadingMonitors; |
647 | |
} |
648 | |
|
649 | |
private function createLeafLoaders(assets:IList):IList |
650 | |
{ |
651 | |
var asset:Asset; |
652 | |
var loadingSettings:LoadingSettings; |
653 | |
var loader:ILoader; |
654 | 1 | var loaders:IList = new ArrayList(); |
655 | 1 | var it:IIterator = assets.iterator(); |
656 | |
|
657 | 1 | while (it.hasNext()) |
658 | |
{ |
659 | 1 | asset = it.next(); |
660 | 1 | loadingSettings = _context.loadingSettingsRepository.find(asset); |
661 | 1 | loader = loaderFactory.createLeaf(asset.identification, asset.src, asset.type, loadingSettings); |
662 | |
|
663 | 1 | if (globalQueueLoader.containsChild(loader.identification)) |
664 | |
{ |
665 | |
|
666 | 1 | var parent:ILoader = globalQueueLoader.getParent(loader.identification); |
667 | |
|
668 | 1 | var errorMessage:String = "There is already a ILoader object stored with identification:\n"; |
669 | 1 | errorMessage += "<" + loader.identification + ">\n"; |
670 | |
|
671 | 1 | errorMessage += "And it belongs to a parent ILoader object with identification: <" + parent.identification + ">\n"; |
672 | 1 | errorMessage += "Use method <LoadingService().exists()> to check if a ILoader object already exists for some Asset.\n"; |
673 | 1 | errorMessage += "For further information please read the documentation section about the ILoader object."; |
674 | |
|
675 | 1 | throw new DuplicateLoaderError(loader.identification, errorMessage); |
676 | |
} |
677 | |
|
678 | 1 | loaders.add(loader); |
679 | |
} |
680 | |
|
681 | 1 | return loaders; |
682 | |
} |
683 | |
|
684 | |
private function validateDuplicateAsset(assets:IList):void |
685 | |
{ |
686 | 1 | var duplicate:ICollection = CollectionUtil.getDuplicate(assets); |
687 | |
|
688 | 1 | if (!duplicate.isEmpty()) |
689 | |
{ |
690 | 1 | var errorMessage:String = "Argument <assets> must not have duplicate elements.\n"; |
691 | 1 | errorMessage += "The following duplicates were found:\n"; |
692 | 1 | errorMessage += duplicate; |
693 | |
|
694 | 1 | throw new ArgumentError(errorMessage); |
695 | |
} |
696 | 1 | } |
697 | |
|
698 | |
} |
699 | |
|
700 | |
} |