Class ArtifactFactory


  • public class ArtifactFactory
    extends java.lang.Object
    A factory to create individual artifacts. Please note that this factory does not store an entire artifact model rather than it creates individual artifacts. See createArtifactModel(File) for creating an artifact model instance. Call release(ArtifactModel) if an artifact model instance is not used anymore.
    Author:
    Holger Eichelberger
    • Field Detail

      • DEFAULT_MODEL_BASE

        private static final java.lang.String DEFAULT_MODEL_BASE
        See Also:
        Constant Field Values
      • ARTIFACT_CREATORS

        private static final ArtifactCreatorNode ARTIFACT_CREATORS
        Stores a tree of artifact creator nodes. The top level tree node is the most generic (no creator) in order to simplify the algorithms.
      • MODELS

        private static final java.util.List<ArtifactModel> MODELS
    • Constructor Detail

      • ArtifactFactory

        private ArtifactFactory()
        Prevents external creation.
    • Method Detail

      • initialize

        private static void initialize()
        Initializes the artifact factory models with one default model storing unmatching artifacts.
      • clear

        public static void clear()
        Clears the whole artifact model (for testing, failure case).
      • createArtifactModel

        public static ArtifactModel createArtifactModel​(java.io.File base)
        Creates an artifact model which is linked to its artifact so that it is notified about relevant changes. Each obtained artifact model must be released by calling release(ArtifactModel).
        Parameters:
        base - the base directory for file relocation, shall be the related project root
        Returns:
        the instantiation environment
      • release

        public static void release​(ArtifactModel model)
        Releases and unregisters the given artifact model.
        Parameters:
        model - the model to be released
      • clearDefaultModel

        public static void clearDefaultModel()
        Clears the default model containing unmatched artifacts.
      • createArtifact

        public static <T extends IArtifact> T createArtifact​(java.lang.Class<T> kind,
                                                             java.lang.Object real,
                                                             ArtifactModel model)
                                                      throws VilException
        Creates an artifact instance.
        Type Parameters:
        T - the type of the artifact to be returned
        Parameters:
        kind - the kind of artifact in terms of a class (typically the top-level interfaces)
        real - the real (underlying) artifact instance (must be instance of Comparable)
        model - the model to add the artifact to, null if the factory shall determine the artifact model
        Returns:
        the resulting handling artifact
        Throws:
        VilException - in case that the creation fails, e.g., real is null
      • createFileSystemArtifact

        static IFileSystemArtifact createFileSystemArtifact​(java.io.File real,
                                                            ArtifactModel model)
                                                     throws VilException
        Creates an artifact instance.
        Parameters:
        real - the real (underlying) artifact instance
        model - the model to add the artifact to, null if the factory shall determine the artifact model
        Returns:
        the resulting handling artifact
        Throws:
        VilException - in case that the creation fails, e.g., real is null
        See Also:
        createArtifact(Class, Object, ArtifactModel)
      • getCreator

        static ArtifactCreator getCreator​(java.lang.Class<?> cls)
        Returns the ArtifactCreator annotation for cls.
        Parameters:
        cls - the class to be examined
        Returns:
        the annotation instance of null
      • checkReplacement

        public static java.lang.String checkReplacement​(java.lang.Class<?> registered,
                                                        java.lang.Class<?> replacing)
        Checks the replacement of registered by replacing from the perspective of the ArtifactFactory.
        Parameters:
        registered - the already registered class
        replacing - the replacing class
        Returns:
        an error message or null if replacement is ok
      • registered

        public static void registered​(java.lang.Class<?> cls)
        Is called to notify that the given class cls was registered in TypeRegistry. A more loose coupling would be preferrable but this would require more explicit registration...
        Parameters:
        cls - the class which that registered
      • addArtifactCreator

        public static void addArtifactCreator​(IArtifactCreator creator)
        Add artifact creator to the set of artifact creators for automatic (default) creation of artifacts.
        Parameters:
        creator - the creator to be added
      • findCreator

        private static IArtifactCreator findCreator​(java.lang.Class<? extends IArtifact> kind,
                                                    java.lang.Object real)
        Returns a creator for real.
        Parameters:
        kind - the kind of artifact in terms of a class (typically the top-level interfaces)
        real - the real artifact instance
        Returns:
        the creator, may be null if no artifact creator is able to handle real
      • findCreator

        private static ArtifactCreatorNode findCreator​(java.lang.Class<? extends IArtifact> kind,
                                                       java.lang.Object real,
                                                       ArtifactCreatorNode node,
                                                       ArtifactCreatorNode actualResult)
        Recursive search for a creator.
        Parameters:
        kind - the kind of artifact in terms of a class (typically the top-level interfaces)
        real - the real artifact instance
        node - the node to analyze (including children)
        actualResult - the actual result (in order to issue warnings in case of multiple results)
        Returns:
        the actual (refined) result
      • configureArtifactCreators

        private static void configureArtifactCreators​(ArtifactCreatorNode node,
                                                      java.util.Properties properties,
                                                      java.util.List<Message> messages)
        Configures the given node and its children.
        Parameters:
        node - the node to be configured
        properties - the properties containing the configuration
        messages - configuration messages (to be modified as a side effect)
      • configureArtifactCreators

        public static java.util.List<Message> configureArtifactCreators​(java.util.Properties properties)
        Configures the artifact creators. Each artifact creator may look for a property with its own single name and read a comma separated list of regular file path patterns for its configuration. If empty, the artifact creator shall be disabled. If If IArtifactCreator.DEFAULT_PATTERN is contained, then the default implementation shall be called, otherwise only the configured entries shall be considered.
        Please call this method when all types are registered so that all artifact creators are known.
        Parameters:
        properties - the properties for configuring the artifact creators
        Returns:
        configuration messages, i.e., warnings and errors which occurred while configuring the artifact creators
      • selectByType

        public static Set<FileArtifact> selectByType​(Path path,
                                                     java.lang.Class<?> type,
                                                     boolean byKind,
                                                     boolean negate)
        Searches for paths in the artifact model by type.
        Parameters:
        path - the path denoting the artifacts to be selected
        type - the type of the artifacts to be returned (may be null if type selection does not apply)
        byKind - by subtyping (true) or by equality (false)
        negate - negate the selection
        Returns:
        the found artifacts
      • findModel

        public static ArtifactModel findModel​(java.lang.Object real)
        Returns the artifact model which is most specific for real.
        Parameters:
        real - the real artifact (or its string path)
        Returns:
        the matching artifact model
      • getDefaultArtifactModel

        static ArtifactModel getDefaultArtifactModel()
        Returns the default artifact model.
        Returns:
        the default artifact model