Class WorkspaceImpl

java.lang.Object
org.opencastproject.workspace.impl.WorkspaceImpl
All Implemented Interfaces:
StorageUsage, Workspace

public final class WorkspaceImpl extends Object implements Workspace
Implements a simple cache for remote URIs. Delegates methods to WorkingFileRepository wherever possible.

Note that if you are running the workspace on the same machine as the singleton working file repository, you can save a lot of space if you configure both root directories onto the same volume (that is, if your file system supports hard links). TODO Implement cache invalidation using the caching headers, if provided, from the remote server.

  • Field Details

    • WORKSPACE_DIR_KEY

      public static final String WORKSPACE_DIR_KEY
      Configuration key for the workspace root directory
      See Also:
    • STORAGE_DIR_KEY

      public static final String STORAGE_DIR_KEY
      Configuration key for the storage directory
      See Also:
    • WORKSPACE_CLEANUP_PERIOD_KEY

      public static final String WORKSPACE_CLEANUP_PERIOD_KEY
      Configuration key for garbage collection period.
      See Also:
    • WORKSPACE_CLEANUP_MAX_AGE_KEY

      public static final String WORKSPACE_CLEANUP_MAX_AGE_KEY
      Configuration key for garbage collection max age.
      See Also:
  • Constructor Details

    • WorkspaceImpl

      public WorkspaceImpl()
    • WorkspaceImpl

      public WorkspaceImpl(String rootDirectory, boolean waitForResource)
      Creates a workspace implementation which is located at the given root directory.

      Note that if you are running the workspace on the same machine as the singleton working file repository, you can save a lot of space if you configure both root directories onto the same volume (that is, if your file system supports hard links).

      Parameters:
      rootDirectory - the repository root directory
  • Method Details

    • activate

      public void activate(org.osgi.service.component.ComponentContext cc)
      OSGi service activation callback.
      Parameters:
      cc - the OSGi component context
    • deactivate

      public void deactivate()
      Callback from OSGi on service deactivation.
    • toSafeName

      public String toSafeName(String fileName)
      Returns the filename translated into a version that can safely be used as part of a file system path. The method shortens both the base file name and the extension to a maximum of 255 characters each, and replaces unsafe characters with <doce>_</doce>.
      Specified by:
      toSafeName in interface Workspace
      Parameters:
      fileName - The file name
      Returns:
      the safe version
    • get

      public File get(URI uri) throws NotFoundException, IOException
      Description copied from interface: Workspace
      Gets a locally cached File for the given URI.
      Specified by:
      get in interface Workspace
      Parameters:
      uri -
      Returns:
      The locally cached file
      Throws:
      NotFoundException - if the file does not exist
      IOException - if reading the file from the workspace fails
    • get

      public File get(URI uri, boolean uniqueFilename) throws NotFoundException, IOException
      Description copied from interface: Workspace
      Get a locally cached File for a given URI, optionally ensuring that the file is cached in a unique path so that it can safely be removed afterwards.
      Specified by:
      get in interface Workspace
      Parameters:
      uri - URI to the resource to get
      uniqueFilename - If a unique path should be used
      Returns:
      The locally cached file
      Throws:
      NotFoundException - if the file does not exist
      IOException - if reading the file from the workspace fails
    • read

      public InputStream read(URI uri) throws NotFoundException, IOException
      Description copied from interface: Workspace
      Get the File for the given URI directly from the working file repository. If shared storage is not available, then fall back to get(uri).
      Specified by:
      read in interface Workspace
      Parameters:
      uri - URI identifying the resource to load
      Returns:
      The file
      Throws:
      NotFoundException - if the file does not exist
      IOException - if reading the file from the working file repository fails
    • md5

      Returns the md5 of a file
      Parameters:
      file - the source file
      Returns:
      the md5 hash
      Throws:
      IOException - if the file cannot be accessed
      IllegalArgumentException - if file is null
      IllegalStateException - if file does not exist or is not a regular file
    • delete

      public void delete(URI uri) throws NotFoundException, IOException
      Description copied from interface: Workspace
      Delete the file stored at the given uri.
      Specified by:
      delete in interface Workspace
      Parameters:
      uri - the uri
      Throws:
      NotFoundException - if there was not file stored under this combination of mediapackage and element IDs.
      IOException - if deleting the data from the workspace fails
    • delete

      public void delete(String mediaPackageID, String mediaPackageElementID) throws NotFoundException, IOException
      Description copied from interface: Workspace
      Delete the file stored at the given media package and element IDs.
      Specified by:
      delete in interface Workspace
      Parameters:
      mediaPackageID -
      mediaPackageElementID -
      Throws:
      NotFoundException - if there was not file stored under this combination of mediapackage and element IDs.
      IOException - if deleting the data from the workspace fails
    • put

      public URI put(String mediaPackageID, String mediaPackageElementID, String fileName, InputStream in) throws IOException
      Description copied from interface: Workspace
      Store the data stream under the given media package and element IDs, specifying a filename.
      Specified by:
      put in interface Workspace
      Parameters:
      mediaPackageID -
      mediaPackageElementID -
      fileName -
      in -
      Throws:
      IOException - if writing the data to the workspace fails
    • putInCollection

      public URI putInCollection(String collectionId, String fileName, InputStream in) throws IOException
      Description copied from interface: Workspace
      Stores the data stream in the given collection, overwriting any data with the same collection id and file name.
      Specified by:
      putInCollection in interface Workspace
      Parameters:
      collectionId - The collection to use for storing this data
      fileName - the filename to use in the collection.
      in - the inputstream
      Returns:
      the URI of the stored data
      Throws:
      IOException - if writing the data to the workspace fails
    • getURI

      public URI getURI(String mediaPackageID, String mediaPackageElementID)
      Description copied from interface: Workspace
      Get the URL for a file stored under the given media package and element IDs. MediaPackages may reference elements that are not yet stored in the working file repository, so this method will return a URI even if the file is not yet stored.
      Specified by:
      getURI in interface Workspace
      Parameters:
      mediaPackageID - the mediapackage identifier
      mediaPackageElementID - the element identifier
      Returns:
      the URI to the file
    • getCollectionURI

      public URI getCollectionURI(String collectionID, String fileName)
      Description copied from interface: Workspace
      Get the URL for a file stored under the given collection.
      Specified by:
      getCollectionURI in interface Workspace
      Parameters:
      collectionID - the collection id
      fileName - the file name
      Returns:
      the file's uri
    • moveTo

      public URI moveTo(URI collectionURI, String toMediaPackage, String toMediaPackageElement, String toFileName) throws NotFoundException, IOException
      Description copied from interface: Workspace
      Moves a file from a collection into a mediapackage
      Specified by:
      moveTo in interface Workspace
      Parameters:
      collectionURI - the uri pointing to a workspace collection
      toMediaPackage - The media package ID to move the file into
      toMediaPackageElement - the media package element ID of the file
      toFileName - the name of the resulting file
      Returns:
      the URI pointing to the file's new location
      Throws:
      NotFoundException - if the element identified by collectionURI cannot be found
      IOException - if either the original element cannot be read or it cannot be moved to the new location
    • getCollectionContents

      public URI[] getCollectionContents(String collectionId) throws NotFoundException
      Description copied from interface: Workspace
      Gets the URIs of the members of this collection
      Specified by:
      getCollectionContents in interface Workspace
      Parameters:
      collectionId - the collection identifier
      Returns:
      the URIs for each member of the collection
      Throws:
      NotFoundException - if the collection cannot be found
    • deleteFromCollection

      public void deleteFromCollection(String collectionId, String fileName) throws NotFoundException, IOException
      Description copied from interface: Workspace
      Removes a file from a collection
      Specified by:
      deleteFromCollection in interface Workspace
      Parameters:
      collectionId - the collection identifier
      fileName - the filename to remove
      Throws:
      NotFoundException - if there was no file with the provided name stored under this collection.
      IOException - if deleting the data from the workspace fails
    • getTotalSpace

      public Optional<Long> getTotalSpace()
      Description copied from interface: StorageUsage
      Gets the total space of storage in Bytes
      Specified by:
      getTotalSpace in interface StorageUsage
      Returns:
      Number of all bytes in storage
    • getUsableSpace

      public Optional<Long> getUsableSpace()
      Description copied from interface: StorageUsage
      Gets the available space of storage in Bytes This is free storage that is not reserved
      Specified by:
      getUsableSpace in interface StorageUsage
      Returns:
      Number of available bytes in storage
    • getUsedSpace

      public Optional<Long> getUsedSpace()
      Description copied from interface: StorageUsage
      Gets the used space of storage in Bytes
      Specified by:
      getUsedSpace in interface StorageUsage
      Returns:
      Number of used bytes in storage
    • getBaseUri

      public URI getBaseUri()
      Description copied from interface: Workspace
      Gets the base URI for files stored using this service.
      Specified by:
      getBaseUri in interface Workspace
      Returns:
      The base URI
    • setRepository

      public void setRepository(WorkingFileRepository repo)
    • setTrustedHttpClient

      public void setTrustedHttpClient(TrustedHttpClient trustedHttpClient)
    • setSecurityService

      public void setSecurityService(SecurityService securityService)
    • cleanup

      public void cleanup(int maxAgeInSeconds)
      Description copied from interface: Workspace
      Cleans up files not belonging to a mediapackage or a collection. If the optional maxAge parameter is set, only files older than the maxAge are deleted.
      Specified by:
      cleanup in interface Workspace
      Parameters:
      maxAgeInSeconds - the maximal age in seconds of a file before deletion is performed
    • cleanup

      public void cleanup(Id mediaPackageId) throws IOException
      Description copied from interface: Workspace
      Clean up all elements of one media package from the local workspace, not touching the working file repository.
      Specified by:
      cleanup in interface Workspace
      Parameters:
      mediaPackageId - Id specifying the media package to remove files for.
      Throws:
      IOException
    • cleanup

      public void cleanup(Id mediaPackageId, boolean filesOnly) throws IOException
      Description copied from interface: Workspace
      Clean up elements of one media package from the local workspace, not touching the working file repository.
      Specified by:
      cleanup in interface Workspace
      Parameters:
      mediaPackageId - Id specifying the media package to remove files for.
      filesOnly - Boolean specifying whether only files or also directories (including the root directory) are deleted.
      Throws:
      IOException
    • rootDirectory

      public String rootDirectory()
      Description copied from interface: Workspace
      Returns the workspace's root directory
      Specified by:
      rootDirectory in interface Workspace
      Returns:
      Path to the workspace root directory