Class FileRepo

java.lang.Object
aQute.lib.deployer.FileRepo
All Implemented Interfaces:
Actionable, Plugin, Refreshable, RegistryPlugin, RepositoryPlugin, Closeable, AutoCloseable
Direct Known Subclasses:
InfoFileRepo, Workspace.CachedFileRepo

public class FileRepo extends Object implements Plugin, RepositoryPlugin, Refreshable, RegistryPlugin, Actionable, Closeable
A FileRepo is the primary and example implementation of a repository based on a file system. It maintains its files in a bsn/bsn-version.jar style from a given location. It implements all the functions of the RepositoryPlugin, Refreshable, Actionable, and Closeable. The FileRepo can be extended or used as is. When used as is, it is possible to add shell commands to the life cycle of the FileRepo. This life cycle is as follows: Additionally, it is possible to set the CMD_SHELL and the CMD_PATH. Notice that you can use the ${global} macro to read global (that is, machine local) settings from the ~/.bnd/settings.json file (can be managed with bnd).
  • Field Details

    • logger

      private static final org.slf4j.Logger logger
    • TRACE

      public static final String TRACE
      If set, will trace to stdout. Works only if no reporter is set.
      See Also:
    • LOCATION

      public static final String LOCATION
      Property name for the location of the repo, must be a valid path name using forward slashes (see IO.getFile(String).
      See Also:
    • READONLY

      public static final String READONLY
      Property name for the readonly state of the repository. If no, will read/write, otherwise it must be a boolean value read by Boolean.parseBoolean(String). Read only repositories will not accept writes. Defaults to false.
      See Also:
    • LATEST_OPTION

      public static final String LATEST_OPTION
      Property name for the latest option of the repository. If true, will copy the put jar to a 'latest' file (option must be a boolean value read by Boolean.parseBoolean(String)). Defaults to true.
      See Also:
    • NAME

      public static final String NAME
      Set the name of this repository (optional)
      See Also:
    • INDEX

      public static final String INDEX
      Should this file repo have an index? Either true or false (absent)
      See Also:
    • CMD_PATH

      public static final String CMD_PATH
      Path property for commands. A comma separated path for directories to be searched for command. May contain $ @} which will be replaced by the system path. If this property is not set, the system path is assumed.
      See Also:
    • CMD_SHELL

      public static final String CMD_SHELL
      The name ( and path) of the shell to execute the commands. By default this is sh and searched in the path.
      See Also:
    • CMD_INIT

      public static final String CMD_INIT
      Property for commands. The command only runs when the location does not exist.

      See Also:
    • CMD_OPEN

      public static final String CMD_OPEN
      Property for commands. Command is run before the repo is first used.

      See Also:
    • CMD_AFTER_PUT

      public static final String CMD_AFTER_PUT
      Property for commands. The command runs after a put operation.

      See Also:
    • CMD_REFRESH

      public static final String CMD_REFRESH
      Property for commands. The command runs when the repository is refreshed.

      See Also:
    • CMD_BEFORE_PUT

      public static final String CMD_BEFORE_PUT
      Property for commands. The command runs after the file is put.

      See Also:
    • CMD_ABORT_PUT

      public static final String CMD_ABORT_PUT
      Property for commands. The command runs when a put is aborted after file changes were made.

      See Also:
    • CMD_CLOSE

      public static final String CMD_CLOSE
      Property for commands. The command runs after the file is put.

      See Also:
    • CMD_AFTER_ACTION

      public static final String CMD_AFTER_ACTION
      Property for commands. Will be run after an action has been executed.

      See Also:
    • CMD_BEFORE_GET

      public static final String CMD_BEFORE_GET
      Called before a before get.
      See Also:
    • DEFAULTOPTIONS

      static final RepositoryPlugin.PutOptions DEFAULTOPTIONS
      Options used when the options are null
    • MAX_MAJOR

      public static final int MAX_MAJOR
      See Also:
    • LATEST_POSTFIX

      private static final String LATEST_POSTFIX
      See Also:
    • LATEST_VERSION

      public static final Version LATEST_VERSION
    • LATEST_SET

      private static final SortedSet<Version> LATEST_SET
    • codec

      static final JSONCodec codec
    • shell

      String shell
    • path

      String path
    • init

      String init
    • open

      String open
    • refresh

      String refresh
    • beforePut

      String beforePut
    • afterPut

      String afterPut
    • abortPut

      String abortPut
    • beforeGet

      String beforeGet
    • close

      String close
    • action

      String action
    • EMPTY_FILES

      File[] EMPTY_FILES
    • root

      protected File root
    • registry

      Registry registry
    • createLatest

      boolean createLatest
    • canWrite

      boolean canWrite
    • REPO_FILE

      private static final Pattern REPO_FILE
    • reporter

      Reporter reporter
    • dirty

      boolean dirty
    • name

      String name
    • inited

      boolean inited
    • trace

      boolean trace
    • index

    • hasIndex

      private boolean hasIndex
    • names

      private static String[] names
  • Constructor Details

    • FileRepo

      public FileRepo()
    • FileRepo

      public FileRepo(String name, File location, boolean canWrite)
  • Method Details

    • init

      protected boolean init() throws Exception
      Initialize the repository Subclasses should first call this method and then if it returns true, do their own initialization
      Returns:
      true if initialized, false if already had been initialized.
      Throws:
      Exception
    • setProperties

      public void setProperties(Map<String,String> map)
      Description copied from interface: Plugin
      Give the plugin the remaining properties. When a plugin is declared, the clause can contain extra properties. All the properties and directives are given to the plugin to use.
      Specified by:
      setProperties in interface Plugin
      Parameters:
      map - attributes and directives for this plugin's clause
      See Also:
    • canWrite

      public boolean canWrite()
      Answer if this repository can write.
      Specified by:
      canWrite in interface RepositoryPlugin
      Returns:
      true if writable
    • putArtifact

      protected File putArtifact(File tmpFile, byte[] digest) throws Exception
      Local helper method that tries to insert a file in the repository. This method can be overridden but MUST not change the content of the tmpFile. This method should also create a latest version of the artifact for reference by tools like ant etc.

      It is allowed to rename the file, the tmp file must be beneath the root directory to prevent rename problems.
      Parameters:
      tmpFile - source file
      digest -
      Returns:
      a File that contains the content of the tmpFile
      Throws:
      Exception
    • putArtifact

      protected File putArtifact(File tmpFile, RepositoryPlugin.PutOptions options, byte[] digest) throws Exception
      Throws:
      Exception
    • put

      Description copied from interface: RepositoryPlugin
      Put an artifact (from the InputStream) into the repository.

      There is no guarantee that the artifact on the input stream has not been modified after it's been put in the repository since that is dependent on the implementation of the repository.
      Specified by:
      put in interface RepositoryPlugin
      Parameters:
      stream - The input stream with the artifact
      options - The put options. See RepositoryPlugin.PutOptions, can be null, which will then take the default options like new PutOptions().
      Returns:
      The result of the put, never null. See RepositoryPlugin.PutResult
      Throws:
      Exception - When the repository root directory doesn't exist, when the repository is read-only, when the specified checksum doesn't match the checksum of the fetched artifact (see RepositoryPlugin.PutOptions.digest), when the implementation wants to modify the artifact but isn't allowed, or when another error has occurred.
    • setLocation

      public void setLocation(String string)
    • setReporter

      public void setReporter(Reporter reporter)
      Description copied from interface: Plugin
      Set the current reporter. This is called at init time. This plugin should report all errors and warnings to this reporter.
      Specified by:
      setReporter in interface Plugin
    • list

      public List<String> list(String regex) throws Exception
      Description copied from interface: RepositoryPlugin
      Return a list of bsns that are present in the repository.
      Specified by:
      list in interface RepositoryPlugin
      Parameters:
      regex - A <ahref="https://en.wikipedia.org/wiki/Glob_%28programming%29"> glob pattern to be matched against bsns present in the repository, or null.
      Returns:
      A list of bsns that match the pattern parameter or all if pattern is null; repositories that do not support browsing or querying should return an empty list.
      Throws:
      Exception
    • versions

      public SortedSet<Version> versions(String bsn) throws Exception
      Description copied from interface: RepositoryPlugin
      Return a list of versions.
      Specified by:
      versions in interface RepositoryPlugin
      Throws:
      Exception
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getRoot

      public File getRoot()
      Specified by:
      getRoot in interface Refreshable
    • refresh

      public boolean refresh() throws Exception
      Description copied from interface: Refreshable
      Instructs a Refreshable to refresh itself
      Specified by:
      refresh in interface Refreshable
      Returns:
      true if refreshed, false if not refreshed possibly due to error.
      Throws:
      Exception
    • getName

      public String getName()
      Specified by:
      getName in interface RepositoryPlugin
      Returns:
      The name of the repository
    • get

      public File get(String bsn, Version version, Map<String,String> properties, RepositoryPlugin.DownloadListener... listeners) throws Exception
      Description copied from interface: RepositoryPlugin
      Return a URL to a matching version of the given bundle.

      If download listeners are specified then the returned file is not guaranteed to exist before a download listener is notified of success or failure. The callback can happen before the method has returned. If the returned file is null then download listeners are not called back.

      The intention of the Download Listeners is to allow a caller to obtain references to files that do not yet exist but are to be downloaded. If the downloads were done synchronously in the call, then no overlap of downloads could take place.

      Specified by:
      get in interface RepositoryPlugin
      Parameters:
      bsn - Bundle-SymbolicName of the searched bundle
      version - Version requested
      listeners - Zero or more download listener that will be notified of the outcome.
      Returns:
      A file to the revision or null if not found
      Throws:
      Exception - when anything goes wrong, in this case no listeners will be called back.
    • setRegistry

      public void setRegistry(Registry registry)
      Specified by:
      setRegistry in interface RegistryPlugin
    • getLocation

      public String getLocation()
      Description copied from interface: RepositoryPlugin
      Return a location identifier of this repository
      Specified by:
      getLocation in interface RepositoryPlugin
    • actions

      public Map<String,Runnable> actions(Object... target) throws Exception
      Description copied from interface: Actionable
      Return a map with command names (potentially localized) and a Runnable. The caller can execute the caller at will.
      Specified by:
      actions in interface Actionable
      Parameters:
      target - the target object, null if commands for the encompassing entity is sought (e.g. the repo itself).
      Returns:
      A Map with the actions or null if no actions are available.
      Throws:
      Exception
    • afterAction

      protected void afterAction(File f, String key)
    • tooltip

      public String tooltip(Object... target) throws Exception
      Description copied from interface: Actionable
      Return a tooltip for the given target or the encompassing entity if null is passed.
      Specified by:
      tooltip in interface Actionable
      Parameters:
      target - the target, any number of parameters to identify
      Returns:
      the tooltip or null
      Throws:
      Exception
    • title

      public String title(Object... target) throws Exception
      Description copied from interface: Actionable
      Provide a title for an element.
      Specified by:
      title in interface Actionable
      Parameters:
      target - the target, any number of parameters to identify
      Returns:
      the text for this element
      Throws:
      Exception
    • getLocal

      protected File getLocal(String bsn, Version version, Map<String,String> properties)
    • status

      protected String status(String bsn, Version version)
    • readable

      private Object readable(long length, int n)
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • open

      protected void open()
    • beforePut

      protected void beforePut(File tmp)
    • afterPut

      protected void afterPut(File file, String bsn, Version version, String sha)
    • abortPut

      protected void abortPut(File tmpFile)
    • beforeGet

      protected void beforeGet(String bsn, Version version)
    • fireBundleAdded

      protected void fireBundleAdded(File file) throws Exception
      Throws:
      Exception
    • exec

      void exec(String line, Object... args)
      Execute a command. Used in different stages so that the repository can be synced with external tools.
    • setDir

      public void setDir(File repoDir)
    • delete

      public void delete(String bsn, Version version) throws Exception
      Delete an entry from the repository and cleanup the directory
      Parameters:
      bsn -
      version -
      Throws:
      Exception
    • getDescriptor

      public SearchableRepository.ResourceDescriptor getDescriptor(String bsn, Version version) throws Exception
      Throws:
      Exception
    • getResources

      Throws:
      Exception
    • getResource

      public SearchableRepository.ResourceDescriptor getResource(byte[] sha) throws Exception
      Throws:
      Exception
    • rebuildIndex

      void rebuildIndex() throws Exception
      Throws:
      Exception
    • buildDescriptor

      private SearchableRepository.ResourceDescriptor buildDescriptor(File f, Jar jar, byte[] digest, String bsn, Version version) throws NoSuchAlgorithmException, Exception
      Throws:
      NoSuchAlgorithmException
      Exception
    • setIndex

      public void setIndex(boolean b)