Class AbstractAssetManager<K,V>
- java.lang.Object
-
- com.inductiveautomation.ignition.common.lifecycle.AbstractLifecycle
-
- com.inductiveautomation.perspective.gateway.assets.AbstractAssetManager<K,V>
-
- Type Parameters:
K- The type of key for the asset cacheV- The type of value for the asset cache
- Direct Known Subclasses:
FontManagerImpl,IconManagerImpl,ThemeManagerImpl
public abstract class AbstractAssetManager<K,V> extends AbstractLifecycle
Base class for managing static assets. Duringsetup()files from the injected asset's defaults will be moved to the respective assets directory in thesystem home directoryeven if they do exist. Uses aFileWatcherto react to changes to the injected asset directory under the perspective modules directory of the system home directory. Changes in the watched asset directory will cause the in memory cache to be updated. Extensions of this class must overridegenerateKey(Path)andparseValue(Path)so that this class may update its cache with the appropriate entry.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.nio.file.PathassetPathprotected java.lang.ObjectcacheLockprotected PerspectiveContextperspectiveContext
-
Constructor Summary
Constructors Constructor Description AbstractAssetManager(PerspectiveContext perspectiveContext, java.lang.String asset)AbstractAssetManager(PerspectiveContext perspectiveContext, java.lang.String asset, java.io.FileFilter fileFilter)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidcopyInputStreamToPath(java.io.InputStream is, java.nio.file.Path pathTo)protected voidcopyInputStreamToPath(java.lang.String name, byte[] defaultAssetData, byte[] defaultAssetDigest, byte[] existingAssetDigest)protected abstract java.util.Optional<K>generateKey(java.nio.file.Path path)Generate a cache key for the givenPathprotected java.util.Map<K,V>getCache()Fetch a copy of the cacheprotected java.util.Optional<V>getCachedValue(K key)Fetch the cached value associated with a keyjava.util.Optional<com.inductiveautomation.ignition.common.gson.JsonObject>getDigestCopy()protected java.util.Optional<java.lang.String>getDigestFor(K assetKey)If the assetKey exists in the cache and it's been hashed, the digest may be retrieved for the asset nameprotected voidonCacheChanged()Invoked within a synchronized block, that holds onto the cache lock, when there is a create, modify, or delete watch event.protected voidonShutdown()protected voidonStartup()protected abstract java.util.Optional<V>parseValue(java.nio.file.Path path)Parse the cache value for the givenPathvoidsetup()Setup the Asset Manager.-
Methods inherited from class com.inductiveautomation.ignition.common.lifecycle.AbstractLifecycle
isRunning, shutdown, startup
-
-
-
-
Field Detail
-
perspectiveContext
protected final PerspectiveContext perspectiveContext
-
assetPath
protected final java.nio.file.Path assetPath
-
cacheLock
protected final java.lang.Object cacheLock
-
-
Constructor Detail
-
AbstractAssetManager
public AbstractAssetManager(@Nonnull PerspectiveContext perspectiveContext, @Nonnull java.lang.String asset)
-
AbstractAssetManager
public AbstractAssetManager(@Nonnull PerspectiveContext perspectiveContext, @Nonnull java.lang.String asset, @Nonnull java.io.FileFilter fileFilter)
-
-
Method Detail
-
getCache
protected java.util.Map<K,V> getCache()
Fetch a copy of the cache- Returns:
- A copy of the cache
-
getCachedValue
protected java.util.Optional<V> getCachedValue(K key)
Fetch the cached value associated with a key- Parameters:
key- The cache key- Returns:
- An
Optionalcontaining the cached value associated with the given key, or an empty Optional of there is no such cached value associated with the given key
-
copyInputStreamToPath
protected void copyInputStreamToPath(@Nonnull java.io.InputStream is, @Nonnull java.nio.file.Path pathTo) throws java.io.IOException- Throws:
java.io.IOException
-
copyInputStreamToPath
protected void copyInputStreamToPath(@Nonnull java.lang.String name, @Nonnull byte[] defaultAssetData, @Nonnull byte[] defaultAssetDigest, @Nullable byte[] existingAssetDigest) throws java.io.IOException- Throws:
java.io.IOException
-
getDigestCopy
public java.util.Optional<com.inductiveautomation.ignition.common.gson.JsonObject> getDigestCopy()
- Returns:
- a copy of the digests object, if present, otherwise empty optional
-
getDigestFor
protected java.util.Optional<java.lang.String> getDigestFor(@Nullable K assetKey)If the assetKey exists in the cache and it's been hashed, the digest may be retrieved for the asset name- Parameters:
assetKey- the name of the asset being cached, generally the filename of the asset- Returns:
- optional containing the digest for the named asset
-
setup
public void setup()
Setup the Asset Manager. Copies any defaults into the asset home directory. Should be run beforeAbstractLifecycle.startup()
-
onStartup
protected void onStartup()
- Specified by:
onStartupin classAbstractLifecycle
-
onShutdown
protected void onShutdown()
- Specified by:
onShutdownin classAbstractLifecycle
-
generateKey
@Nonnull protected abstract java.util.Optional<K> generateKey(@Nonnull java.nio.file.Path path)
Generate a cache key for the givenPath- Parameters:
path- ThePathfor which we want to generate a cache key- Returns:
- An
Optionalcontaining the cache key or an empty Optional if the cache key could not be generated
-
parseValue
@Nonnull protected abstract java.util.Optional<V> parseValue(@Nonnull java.nio.file.Path path)
Parse the cache value for the givenPath- Parameters:
path- ThePathfor which we want to generate a cache value- Returns:
- An
Optionalcontaining the cache value or an empty Optional if the cache value could not be generated
-
onCacheChanged
protected void onCacheChanged()
Invoked within a synchronized block, that holds onto the cache lock, when there is a create, modify, or delete watch event. Invoked immediately following modification of the cache, the cache values and digest will be updated prior to being called. Override this method to update any cache local to the subclass.
-
-