Interface ServiceBinder<T>

Type Parameters:
T - the type of the service to bind
All Known Implementing Classes:
ServiceBinderImpl

public interface ServiceBinder<T>

Provides a binding of corresponding service on corresponding process. Binding a service to it's implementation must be performed via instance of this interface. No one instance of corresponding service could be performed until the binding for process binder for which this service binder is created is complete. Instance of corresponding service could not be created when the binding for this instance is not complete. If the binding for process binder for which this service binder is created is complete and the binding for this instance is complete, instance of corresponding service could be created within a corresponding separate process via IProcessManager.getInstance(String, Class). The binding could completed by calling bindTo(Class). An instance of this interface could be obtained by calling ProcessBinder.getServiceBinder(Class).

This interface is not designed for implementing. New features may be added to this interface in next versions, and therefore do not create any implementations of this interface. Instance of this interface must be obtained only by calling ProcessBinder.getServiceBinder(Class) method.

This interface is designed to be thread-safe. Any operations on this instance could be performed until the binding is complete or binding of process binder is complete.

See Also:
  • Method Details

    • getProcessBinder

      ProcessBinder getProcessBinder()

      Returns a process binder for which this service binder is created.

      Returns:
      a process binder for which this service binder is created
    • getService

      Class<T> getService()

      Returns a service corresponding to this service binder.

      Returns:
      a service corresponding to this service binder
    • addLibrariesProvider

      ServiceBinder<T> addLibrariesProvider(ILibrariesProvider librariesProvider)

      Adds all the libraries provided by given librariesProvider. This means that all the provided will be loaded before instantiating bound implementation within separate process. In other words, all this method does is calling addLibraries(librariesProvider.getLibraries())

      This method may be used any number of times within different threads as long as addLibraries(String...) until the binding is complete. Calling this method after the binding has been completed or the binding for process binder for that this ServiceBinder is created has been completed will cause an IllegalStateException. Binding can be completed by the bindTo(Class) method.

      Parameters:
      librariesProvider - given librariesProvider (not null)
      Returns:
      this instance
      Throws:
      IllegalStateException - if called after the binding has been completed
    • addLibraries

      ServiceBinder<T> addLibraries(String... libraries)

      Adds all the given libraries to this binder. All the given libraries will be loaded within a separate process before an instance of service will be created within a separate process.

      This method could be used any number of times within any threads as long as addLibrariesProvider(ILibrariesProvider) until the binding is complete. Calling this method after the binding has been completed or the binding for process binder for that this ServiceBinder is created has been completed will cause an IllegalStateException. Binding can be completed by the bindTo(Class) method.

      Parameters:
      libraries - given list of libraries (not null)
      Returns:
      this instance
      Throws:
      IllegalStateException - if called after the binding has been completed or the binding for process binder for that this ServiceBinder is created has been completed
    • addAdditionalClassesProvider

      ServiceBinder<T> addAdditionalClassesProvider(IAdditionalClassesProvider additionalClassesProvider)

      Adds all the additional classes provided by additionalClassesProvider to this binder. It means that locations of all the provided classes will be added to separate process's class path and all the provided classes will be added to the main process's context classloader before executing method of some created instance.

      *

      This method could be used any number of times within any threads as long as addAdditionalClasses(Class...) until the binding is complete. Calling this method after the binding has been completed or the binding for process binder for that this ServiceBinder is created has been completed will cause an IllegalStateException. Binding can be completed by the bindTo(Class) method.

      Note that using this method when the corresponding process is running, it may cause an unexpected effects. For example, all locations of provided classes will not be added to the separate process's classpath that will cause ClassNotFoundException at the time these classes will be used. You should not call this method when the corresponding process is running.

      Parameters:
      additionalClassesProvider - given additionalClassesProvider (not null)
      Returns:
      this instance
      Throws:
      IllegalStateException - if called after the binding has been completed or the binding for process binder for that this ServiceBinder is created has been completed
    • addAdditionalClasses

      ServiceBinder<T> addAdditionalClasses(Class<?>... additionalClasses)

      Adds all the given additional classes to this binder. It means that locations of all the provided classes will be added to separate process's class path and all the provided classes will be added to the main process's context classloader before executing method of some created instance.

      *

      This method could be used any number of times within any threads until the binding is complete. Calling this method after the binding has been completed or the binding for process binder for that this ServiceBinder is created has been completed will cause an IllegalStateException. Binding can be completed by the bindTo(Class) method.

      Note that using this method when the corresponding process is running, it may cause an unexpected effects. For example, all locations of provided classes will not be added to the separate process's classpath that will cause ClassNotFoundException at the time these classes will be used. You should not call this method when the corresponding process is running.

      Parameters:
      additionalClasses - given additional classes (not null)
      Returns:
      this instance
      Throws:
      IllegalStateException - if called after the binding has been completed or the binding for process binder for that this ServiceBinder is created has been completed
    • setSingleton

      ServiceBinder<T> setSingleton(boolean singleton)

      If value of the given parameter is true, only one implementation instance will be created within separate process and all the proxy instances of service (created by IProcessManager.getInstance(String, Class)) will delegate all the method calls to that instance. Otherwise, new instance of implementation will be created each time IProcessManager.getInstance(String, Class) is invoked.

      By default, it's set as false. There's no need to call setSingleton(false).

      This method could be used any number of times within any threads until the binding is complete. Calling this method after the binding has been completed or the binding for process binder for that this ServiceBinder is created has been completed will cause an IllegalStateException. Binding can be completed by the bindTo(Class) method.

      Parameters:
      singleton - given parameter
      Returns:
      this instance
      Throws:
      IllegalStateException - if called after the binding has been completed or the binding for process binder for that this ServiceBinder is created has been completed
    • bindTo

      ProcessBinder bindTo(Class<? extends T> implementation)

      Completes the binding of service and key (parameters for those this binder was created by getBinder(key, service)) with given implementation. Instance of implementation will be created in separate process to create an instance of service within a separate process with key key by IProcessManager.getInstance(String, Class) method. Instance of service within a separate process with key key could be created if and only if binding is complete, or, in other words, the binding is completed. Therefore, call this method to complete the binding.

      This method must be called on this instance only once to complete the binding. Any of the operations on this instance instead of isBound() are not allowed after the binding has been completed. IllegalStateException will be thrown if any operation instead of isBound() (include this method) is called when the binding is complete or the binding of process binder for that this ServiceBinder is created is complete.

      Throws IllegalArgumentException if implementation is not instantiable class or does not have public parameterless constructor.
      If IllegalArgumentException is thrown, binding is not complete and this method might be called for another time (with another implementation) to complete the binding and any of the operations on this instance still may be called.
      Binding is complete if this method is called and did not thrown any exceptions.

      Parameters:
      implementation - given implementation of service to bind to service for process with key key. Must be instantiable class. Not null.
      Returns:
      instance of ProcessBinder for that this service binder is created.
      Throws:
      IllegalStateException - if called when the binding is complete or the binding for process binder for that this ServiceBinder is created is complete
      IllegalArgumentException - if implementation is not instantiable class or does not have accessible (public) parameterless constructor
    • isBound

      boolean isBound()

      Returns true if this binding is complete, false otherwise.

      Returns:
      true if this binding is complete, false otherwise
    • getImplementation

      Class<? extends T> getImplementation()

      Returns a bound implementation. This method is available only after binding is complete (bindTo(Class) method is called. Before, it throws IllegalStateException.

      Returns:
      a bound implementation (not null)
      Throws:
      IllegalArgumentException - if binding is not completed
    • getLibraries

      String[] getLibraries()

      Returns a list of libraries to load. This method is available only after binding is complete (bindTo(Class) method is called. Before, it throws IllegalStateException.

      Returns:
      a list of libraries to load (not null)
      Throws:
      IllegalArgumentException - if binding is not completed
    • isSingleton

      boolean isSingleton()

      Returns whether the service is singleton. This method is available only after binding is complete (bindTo(Class) method is called. Before, it throws IllegalStateException.

      Returns:
      whether the service is singleton (true or false)
      Throws:
      IllegalArgumentException - if binding is not completed
    • getAdditionalClasses

      Collection<Class<?>> getAdditionalClasses()

      Returns a list of classes to load within separate process to use the implementation. This method is available only after binding is complete (bindTo(Class) method is called. Before, it throws IllegalStateException.

      Returns:
      a list of classes to load within separate process to use the implementation (not null)
      Throws:
      IllegalArgumentException - if binding is not completed