Class OptionalDependency

  • All Implemented Interfaces:
    Dependency, Dependent, Value<java.lang.Object>

    class OptionalDependency
    extends java.lang.Object
    implements Dependency, Dependent
    An OptionalDependency.
    This class establishes a transitive dependency relationship between the dependent and the real dependency. The intermediation performed by this class adds the required optional behavior to the dependency relation, by:
    • notifies the dependent that it is in the UP state when the real dependency is unresolved or unavailable
    • once the real dependency is available, if there is a demand previously added by the dependent, this dependency does not start forwarding the notifications to the dependent, meaning that the dependent won't even be aware that the dependency is down
    • waits for the dependency to be available and the dependent to be inactive, so it can finally start forwarding notifications in both directions (from dependency to dependent and vice-versa)
    • Field Detail

      • optionalDependency

        private final Dependency optionalDependency
        The actual dependency.
      • transitiveDependencyUnavailable

        private boolean transitiveDependencyUnavailable
        Indicates if optional dependency has a transitive dependency unavailable.
      • dependencyFailed

        private boolean dependencyFailed
        Indicates if optionalDependency notified a dependency failure.
      • dependent

        private Dependent dependent
        The dependent on this optional dependency
      • demandedByDependent

        private boolean demandedByDependent
        Indicates if this dependency has been demanded by the dependent
      • forwardNotifications

        boolean forwardNotifications
        Indicates if notification should take place
      • dependentStartedNotified

        private boolean dependentStartedNotified
        Keeps track of whether optionalDependency has been notified of a dependent started. This field is useful for avoiding dependentStopped notifications that don't have a corresponding previous dependentStarted notification.
    • Constructor Detail

      • OptionalDependency

        OptionalDependency​(Dependency optionalDependency)
    • Method Detail

      • addDependent

        public void addDependent​(Dependent dependent)
        Description copied from interface: Dependency
        Add a dependent to this dependency, establishing the dependency relation between this dependency and its dependent. This method must not be called under a lock.
        Specified by:
        addDependent in interface Dependency
        Parameters:
        dependent - the dependent to add
      • removeDependent

        public void removeDependent​(Dependent dependent)
        Description copied from interface: Dependency
        Remove a dependent from this dependency, breaking the dependency relation between this dependency and its dependent. This method must not be called under a lock.
        Specified by:
        removeDependent in interface Dependency
        Parameters:
        dependent - the dependent to remove
      • dependentStarted

        public void dependentStarted()
        Description copied from interface: Dependency
        Notify that a dependent is starting. This method must not be called under a lock.
        Specified by:
        dependentStarted in interface Dependency
      • dependentStopped

        public void dependentStopped()
        Description copied from interface: Dependency
        Notify that a dependent is stopping. This method must not be called under a lock.
        Specified by:
        dependentStopped in interface Dependency
      • getValue

        public java.lang.Object getValue()
                                  throws java.lang.IllegalStateException
        Description copied from interface: Dependency
        Get the installed instance value, if any exists.
        Specified by:
        getValue in interface Dependency
        Specified by:
        getValue in interface Value<java.lang.Object>
        Returns:
        the installed service value
        Throws:
        java.lang.IllegalStateException - if an error occurs
      • immediateDependencyAvailable

        public void immediateDependencyAvailable​(ServiceName dependencyName)
        Description copied from interface: Dependent
        Notify this dependent that one of its immediate dependencies is available, i.e., it is installed and, if not started, should start shortly.

        This method must not be called under a lock.

        Specified by:
        immediateDependencyAvailable in interface Dependent
        Parameters:
        dependencyName - the name of the immediate dependency that is now available
      • immediateDependencyUnavailable

        public void immediateDependencyUnavailable​(ServiceName dependencyName)
        Description copied from interface: Dependent
        Notify this dependent that one of its immediate dependencies is unavailable.
        A dependency is unavailable when it is not installed or when it is in NEVER mode.

        This method must not be called under a lock.

        Specified by:
        immediateDependencyUnavailable in interface Dependent
        Parameters:
        dependencyName - the name of the immediate dependency that is now unavailable
      • immediateDependencyUp

        public void immediateDependencyUp()
        Description copied from interface: Dependent
        Notify this dependent that one of its immediate dependencies entered UP state.

        This method must not be called under a lock.

        Specified by:
        immediateDependencyUp in interface Dependent
      • immediateDependencyDown

        public void immediateDependencyDown()
        Description copied from interface: Dependent
        Notify this dependent that one of its immediate dependencies is leaving the UP state.

        This method must not be called under a lock.

        Specified by:
        immediateDependencyDown in interface Dependent
      • dependencyFailed

        public void dependencyFailed()
        Description copied from interface: Dependent
        Notify this dependent that one of its dependencies (immediate or transitive) failed to start. This method is called after the dependency state transitions from STARTING to START_FAILED.

        Dependency failures that occur after the notified failure do not result in new dependencyFailed notifications, as the dependent will never receive two or more dependencyFailed calls in a row. A dependencyFailed notification is only invoked again to notify of new failures if the previous failures have been cleared.

        This method must not be called under a lock.

        Specified by:
        dependencyFailed in interface Dependent
      • dependencyFailureCleared

        public void dependencyFailureCleared()
        Description copied from interface: Dependent
        Notify this dependent that all dependency failures previously notified are now cleared. This method is called only after all affected dependencies left START_FAILED state.

        This method must not be called under a lock.

        Specified by:
        dependencyFailureCleared in interface Dependent
      • transitiveDependencyAvailable

        public void transitiveDependencyAvailable()
        Description copied from interface: Dependent
        Notify this dependent that all unavailable transitive dependencies are now available (i.e., they are installed and will perform an attempt to start shortly).

        This method must not be called under a lock.

        Specified by:
        transitiveDependencyAvailable in interface Dependent
      • transitiveDependencyUnavailable

        public void transitiveDependencyUnavailable()
        Description copied from interface: Dependent
        Notify this dependent that one of its transitive dependencies is unavailable (either uninstalled, or in NEVER mode).

        New transitive dependencies that become unavailable after the notified one do not result in new dependencyUnavailable notifications, as the dependent will never receive two or more dependencyUnavailable calls in a row. A dependencyUnavailable notification is only invoked again to notify of newly found unavailable dependencies if all the previously unavailable dependencies have become available.

        This method must not be called under a lock.

        Specified by:
        transitiveDependencyUnavailable in interface Dependent