for (AssetEventListener listener : eventListeners){
listener.assetRequested(key);
}
AssetCache cache = handler.getCache(key.getCacheType());
AssetProcessor proc = handler.getProcessor(key.getProcessorType());
Object obj = cache != null ? cache.getFromCache(key) : null;
if (obj == null){
// Asset not in cache, load it from file system.
AssetLoader loader = handler.aquireLoader(key);
AssetInfo info = handler.tryLocate(key);
if (info == null){
if (handler.getParentKey() != null){
// Inform event listener that an asset has failed to load.
// If the parent AssetLoader chooses not to propagate
// the exception, this is the only means of finding
// that something went wrong.
for (AssetEventListener listener : eventListeners){
listener.assetDependencyNotFound(handler.getParentKey(), key);
}
}
throw new AssetNotFoundException(key.toString());
}
try {
handler.establishParentKey(key);
obj = loader.load(info);
} catch (IOException ex) {
throw new AssetLoadException("An exception has occured while loading asset: " + key, ex);
} finally {
handler.releaseParentKey(key);
}
if (obj == null){
throw new AssetLoadException("Error occured while loading asset \"" + key + "\" using " + loader.getClass().getSimpleName());
}else{
if (logger.isLoggable(Level.FINER)){
logger.log(Level.FINER, "Loaded {0} with {1}",
new Object[]{key, loader.getClass().getSimpleName()});
}
if (proc != null){
// do processing on asset before caching
obj = proc.postProcess(key, obj);
}
if (cache != null){
// At this point, obj should be of type T
cache.addToCache(key, (T) obj);
}
for (AssetEventListener listener : eventListeners){
listener.assetLoaded(key);
}
}
}
// object obj is the original asset
// create an instance for user
T clone = (T) obj;
if (clone instanceof CloneableSmartAsset){
if (proc == null){
throw new IllegalStateException("Asset implements "
+ "CloneableSmartAsset but doesn't "
+ "have processor to handle cloning");
}else{
clone = (T) proc.createClone(obj);
if (cache != null && clone != obj){
cache.registerAssetClone(key, clone);
} else{
throw new IllegalStateException("Asset implements "
+ "CloneableSmartAsset but doesn't have cache or "
+ "was not cloned");
}