Class FileRepo

  • All Implemented Interfaces:
    Actionable, Plugin, Refreshable, RegistryPlugin, RepositoryPlugin, java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    InfoFileRepo

    public class FileRepo
    extends java.lang.Object
    implements Plugin, RepositoryPlugin, Refreshable, RegistryPlugin, Actionable, java.io.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 Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String CMD_ABORT_PUT
      Property for commands.
      static java.lang.String CMD_AFTER_ACTION
      Property for commands.
      static java.lang.String CMD_AFTER_PUT
      Property for commands.
      static java.lang.String CMD_BEFORE_GET
      Called before a before get.
      static java.lang.String CMD_BEFORE_PUT
      Property for commands.
      static java.lang.String CMD_CLOSE
      Property for commands.
      static java.lang.String CMD_INIT
      Property for commands.
      static java.lang.String CMD_OPEN
      Property for commands.
      static java.lang.String CMD_PATH
      Path property for commands.
      static java.lang.String CMD_REFRESH
      Property for commands.
      static java.lang.String CMD_SHELL
      The name ( and path) of the shell to execute the commands.
      static java.lang.String INDEX
      Should this file repo have an index? Either true or false (absent)
      static java.lang.String LATEST_OPTION
      Property name for the latest option of the repository.
      static Version LATEST_VERSION  
      static java.lang.String LOCATION
      Property name for the location of the repo, must be a valid path name using forward slashes (see IO.getFile(String).
      static int MAX_MAJOR  
      static java.lang.String NAME
      Set the name of this repository (optional)
      static java.lang.String READONLY
      Property name for the readonly state of the repository.
      protected java.io.File root  
      static java.lang.String TRACE
      If set, will trace to stdout.
    • Constructor Summary

      Constructors 
      Constructor Description
      FileRepo()  
      FileRepo​(java.lang.String name, java.io.File location, boolean canWrite)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void abortPut​(java.io.File tmpFile)  
      java.util.Map<java.lang.String,​java.lang.Runnable> actions​(java.lang.Object... target)
      Return a map with command names (potentially localized) and a Runnable.
      protected void afterAction​(java.io.File f, java.lang.String key)  
      protected void afterPut​(java.io.File file, java.lang.String bsn, Version version, java.lang.String sha)  
      protected void beforeGet​(java.lang.String bsn, Version version)  
      protected void beforePut​(java.io.File tmp)  
      boolean canWrite()
      Answer if this repository can write.
      void close()  
      void delete​(java.lang.String bsn, Version version)
      Delete an entry from the repository and cleanup the directory
      protected void fireBundleAdded​(java.io.File file)  
      java.io.File get​(java.lang.String bsn, Version version, java.util.Map<java.lang.String,​java.lang.String> properties, RepositoryPlugin.DownloadListener... listeners)
      Return a URL to a matching version of the given bundle.
      SearchableRepository.ResourceDescriptor getDescriptor​(java.lang.String bsn, Version version)  
      protected java.io.File getLocal​(java.lang.String bsn, Version version, java.util.Map<java.lang.String,​java.lang.String> properties)  
      java.lang.String getLocation()
      Return a location identifier of this repository
      java.lang.String getName()  
      SearchableRepository.ResourceDescriptor getResource​(byte[] sha)  
      java.util.SortedSet<SearchableRepository.ResourceDescriptor> getResources()  
      java.io.File getRoot()  
      protected boolean init()
      Initialize the repository Subclasses should first call this method and then if it returns true, do their own initialization
      java.util.List<java.lang.String> list​(java.lang.String regex)
      Return a list of bsns that are present in the repository.
      protected void open()  
      RepositoryPlugin.PutResult put​(java.io.InputStream stream, RepositoryPlugin.PutOptions options)
      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.
      protected java.io.File putArtifact​(java.io.File tmpFile, byte[] digest)
      Local helper method that tries to insert a file in the repository.
      protected java.io.File putArtifact​(java.io.File tmpFile, RepositoryPlugin.PutOptions options, byte[] digest)  
      boolean refresh()
      Instructs a Refreshable to refresh itself
      void setDir​(java.io.File repoDir)  
      void setIndex​(boolean b)  
      void setLocation​(java.lang.String string)  
      void setProperties​(java.util.Map<java.lang.String,​java.lang.String> map)
      Give the plugin the remaining properties.
      void setRegistry​(Registry registry)  
      void setReporter​(aQute.service.reporter.Reporter reporter)
      Set the current reporter.
      protected java.lang.String status​(java.lang.String bsn, Version version)  
      java.lang.String title​(java.lang.Object... target)
      Provide a title for an element.
      java.lang.String tooltip​(java.lang.Object... target)
      Return a tooltip for the given target or the encompassing entity if null is passed.
      java.lang.String toString()  
      java.util.SortedSet<Version> versions​(java.lang.String bsn)
      Return a list of versions.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • TRACE

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

        public static final java.lang.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:
        Constant Field Values
      • READONLY

        public static final java.lang.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:
        Constant Field Values
      • LATEST_OPTION

        public static final java.lang.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:
        Constant Field Values
      • NAME

        public static final java.lang.String NAME
        Set the name of this repository (optional)
        See Also:
        Constant Field Values
      • INDEX

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

        public static final java.lang.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:
        Constant Field Values
      • CMD_SHELL

        public static final java.lang.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:
        Constant Field Values
      • CMD_INIT

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

        See Also:
        Constant Field Values
      • CMD_OPEN

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

        See Also:
        Constant Field Values
      • CMD_AFTER_PUT

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

        See Also:
        Constant Field Values
      • CMD_REFRESH

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

        See Also:
        Constant Field Values
      • CMD_BEFORE_PUT

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

        See Also:
        Constant Field Values
      • CMD_ABORT_PUT

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

        See Also:
        Constant Field Values
      • CMD_CLOSE

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

        See Also:
        Constant Field Values
      • CMD_AFTER_ACTION

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

        See Also:
        Constant Field Values
      • CMD_BEFORE_GET

        public static final java.lang.String CMD_BEFORE_GET
        Called before a before get.
        See Also:
        Constant Field Values
      • LATEST_VERSION

        public static final Version LATEST_VERSION
      • root

        protected java.io.File root
    • Constructor Detail

      • FileRepo

        public FileRepo()
      • FileRepo

        public FileRepo​(java.lang.String name,
                        java.io.File location,
                        boolean canWrite)
    • Method Detail

      • init

        protected boolean init()
                        throws java.lang.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:
        java.lang.Exception
      • setProperties

        public void setProperties​(java.util.Map<java.lang.String,​java.lang.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:
        Plugin.setProperties(java.util.Map)
      • canWrite

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

        protected java.io.File putArtifact​(java.io.File tmpFile,
                                           byte[] digest)
                                    throws java.lang.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:
        java.lang.Exception
      • putArtifact

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

        public RepositoryPlugin.PutResult put​(java.io.InputStream stream,
                                              RepositoryPlugin.PutOptions options)
                                       throws java.lang.Exception
        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:
        java.lang.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​(java.lang.String string)
      • setReporter

        public void setReporter​(aQute.service.reporter.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 java.util.List<java.lang.String> list​(java.lang.String regex)
                                              throws java.lang.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 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:
        java.lang.Exception
      • versions

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

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

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

        public boolean refresh()
                        throws java.lang.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:
        java.lang.Exception
      • getName

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

        public java.io.File get​(java.lang.String bsn,
                                Version version,
                                java.util.Map<java.lang.String,​java.lang.String> properties,
                                RepositoryPlugin.DownloadListener... listeners)
                         throws java.lang.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:
        java.lang.Exception - when anything goes wrong, in this case no listeners will be called back.
      • actions

        public java.util.Map<java.lang.String,​java.lang.Runnable> actions​(java.lang.Object... target)
                                                                         throws java.lang.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:
        java.lang.Exception
      • afterAction

        protected void afterAction​(java.io.File f,
                                   java.lang.String key)
      • tooltip

        public java.lang.String tooltip​(java.lang.Object... target)
                                 throws java.lang.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:
        java.lang.Exception
      • title

        public java.lang.String title​(java.lang.Object... target)
                               throws java.lang.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:
        java.lang.Exception
      • getLocal

        protected java.io.File getLocal​(java.lang.String bsn,
                                        Version version,
                                        java.util.Map<java.lang.String,​java.lang.String> properties)
      • status

        protected java.lang.String status​(java.lang.String bsn,
                                          Version version)
      • close

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

        protected void open()
      • beforePut

        protected void beforePut​(java.io.File tmp)
      • afterPut

        protected void afterPut​(java.io.File file,
                                java.lang.String bsn,
                                Version version,
                                java.lang.String sha)
      • abortPut

        protected void abortPut​(java.io.File tmpFile)
      • beforeGet

        protected void beforeGet​(java.lang.String bsn,
                                 Version version)
      • fireBundleAdded

        protected void fireBundleAdded​(java.io.File file)
                                throws java.lang.Exception
        Throws:
        java.lang.Exception
      • setDir

        public void setDir​(java.io.File repoDir)
      • delete

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

        public void setIndex​(boolean b)