Class Microservice.Builder

java.lang.Object
org.apache.juneau.microservice.Microservice.Builder
All Implemented Interfaces:
BeanStoreOverridable<Microservice.Builder>
Enclosing class:
Microservice

Builder class.
  • Constructor Details

  • Method Details

    • initWorkingDirFromEnv

      @Inject public void initWorkingDirFromEnv(@Value("${juneau.workingDir}") String workingDirEnv)
      @Inject-annotated initializer that populates workingDir from the juneau.workingDir property (system property → environment variable → registered property sources) when the property is set. Has no effect if workingDir was already set programmatically.
      Parameters:
      workingDirEnv - The resolved juneau.workingDir value, or null/empty when not configured.
    • args

      Specifies the command-line arguments passed into the Java command.

      This is required if you use Microservice.getArgs() or $A string variables.

      Parameters:
      args - The command-line arguments passed into the Java command as a pre-parsed Args object.
      Can be null (an empty Args is used).
      Returns:
      This object.
    • args

      public Microservice.Builder args(String... args)
      Specifies the command-line arguments passed into the Java command.

      This is required if you use Microservice.getArgs() or $A string variables.

      Parameters:
      args - The command-line arguments passed into the Java command as the raw command-line arguments.
      Returns:
      This object.
    • build

      public Microservice build() throws Exception
      Instantiate a new microservice using the settings defined on this builder.
      Returns:
      A new microservice.
      Throws:
      Exception - Error occurred.
    • config

      Specifies the config for initializing this microservice.

      Calling this method overrides the default configuration controlled by the configName(String) and configStore(ConfigStore) methods.

      Parameters:
      config - The configuration. Can be null (the configuration is auto-resolved).
      Returns:
      This object.
    • configName

      public Microservice.Builder configName(String configName)
      Specifies the config name for initializing this microservice.

      If you do not specify the config file location, we attempt to resolve it through the following methods:

      1. Resolve file first in working directory, then in classpath, using the following names:
        • The "configFile" argument in the command line arguments passed in through the constructor.
        • The value of the Main-Config entry in the manifest file.
        • A config file in the same location and with the same name as the executable jar file. (e.g. "java -jar myjar.jar" will look for "myjar.cfg").
      2. Resolve any "*.cfg" file that can be found in the working directory.
      3. Resolve any of the following files in the classpath: "juneau.cfg", "system.cfg"

      If no configuration file is found, and empty in-memory configuration is used.

      Parameters:
      configName - The configuration name. Can be null (the configuration name is auto-resolved).
      Returns:
      This object.
    • configStore

      Specifies the config store to use for storing and retrieving configurations.

      By default, we use a FileStore store for configuration files.

      Parameters:
      configStore - The configuration store. Can be null (a default FileStore is used).
      Returns:
      This object.
    • console

      public Microservice.Builder console(Scanner consoleReader, PrintWriter consoleWriter)
      Specifies the console input and output.

      If not specified, uses the console returned by System.console(). If that is not available, uses System.in and System.out.

      Note that these are ignored if the console is not enabled via consoleEnabled(boolean).

      Parameters:
      consoleReader - The console input. Can be null (the default console input is used).
      consoleWriter - The console output. Can be null (the default console output is used).
      Returns:
      This object.
    • consoleCommands

      public Microservice.Builder consoleCommands(Class<? extends ConsoleCommand>... consoleCommands) throws ExecutableException
      Specifies console commands to make available on the Java console.

      Note that these are ignored if the console is not enabled via consoleEnabled(boolean).

      This list augments the commands defined via the "Console/commands" configuration setting.

      This method can only be used on console commands with no-arg constructors.

      Parameters:
      consoleCommands - The list of console commands to append to the list of available commands.
      Returns:
      This object.
      Throws:
      ExecutableException - Exception occurred on invoked constructor/method/field.
    • consoleCommands

      Specifies console commands to make available on the Java console.

      Note that these are ignored if the console is not enabled via consoleEnabled(boolean).

      This list augments the commands defined via the "Console/commands" configuration setting.

      Parameters:
      consoleCommands - The list of console commands to append to the list of available commands.
      Returns:
      This object.
    • consoleEnabled

      public Microservice.Builder consoleEnabled(boolean consoleEnabled)
      Specifies that the Java console is enabled for this microservice.

      If not specified, this value is taken from the "Console/enabled" configuration setting. If not specified in the configuration, defaults to false.

      Parameters:
      consoleEnabled - true if the Java console is enabled for this microservice.
      Returns:
      This object.
    • copy

      Creates a copy of this builder.
      Returns:
      A new copy of this builder.
    • listener

      Registers an event listener for this microservice.
      Parameters:
      listener - An event listener for this microservice. Can be null (no listener is registered).
      Returns:
      This object.
    • logConfig

      Specifies logging instructions for the microservice.

      If not specified, the values are taken from the "Logging" section of the configuration.

      This method is ignored if logger(Logger) is used to set the microservice logger.

      Parameters:
      logConfig - The log configuration. Can be null (a default LogConfig is used).
      Returns:
      This object.
    • logger

      Specifies the logger used by the microservice and returned by the Microservice.getLogger() method.

      Calling this method overrides the default logging mechanism controlled by the logConfig(LogConfig) method.

      Parameters:
      logger - The logger to use for logging microservice messages. Can be null (a default logger is created).
      Returns:
      This object.
    • manifest

      Specifies the manifest file of the jar file this microservice is contained within.

      This is required if you use Microservice.getManifest(). It's also used to locate initialization values such as Main-Config.

      If you do not specify the manifest file, we attempt to resolve it through the following methods:

      1. Looking on the file system for a file at "META-INF/MANIFEST.MF". This is primarily to allow for running microservices from within eclipse workspaces where the manifest file is located in the project root.
      2. Using the class loader for this class to find the file at the URL "META-INF/MANIFEST.MF".
      Parameters:
      value - The manifest file of this microservice.
      Can be any of the following types:
      • ManifestFile
      • Manifest
      • Reader - Containing the raw contents of the manifest. Note that the input must end with a newline.
      • InputStream - Containing the raw contents of the manifest. Note that the input must end with a newline.
      • File - File containing the raw contents of the manifest.
      • String - Path to file containing the raw contents of the manifest.
      • Class - Finds and loads the manifest file of the jar file that the specified class is contained within.

      Can be null (the manifest is auto-resolved).
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • varBean

      public <T> Microservice.Builder varBean(Class<T> c, T value)
      Adds a bean for vars defined in the var resolver.

      This calls VarResolver.Builder#bean(Class,Object) on the var resolver used to construct the configuration object returned by Microservice.getConfig() and the var resolver returned by Microservice.getVarResolver().

      Type Parameters:
      T - The bean type.
      Parameters:
      c - The bean type.
      value - The bean.
      Returns:
      This object.
    • vars

      public Microservice.Builder vars(Class<? extends Var>... vars)
      Augments the set of variables defined in the configuration and var resolver.

      This calls VarResolver.Builder#vars(Class[]) on the var resolver used to construct the configuration object returned by Microservice.getConfig() and the var resolver returned by Microservice.getVarResolver().

      Parameters:
      vars - The set of variables to append to the var resolver builder.
      Returns:
      This object.
    • workingDir

      public Microservice.Builder workingDir(File workingDir)
      Specifies the directory to use to resolve the config file and other paths defined with the config file.
      Parameters:
      workingDir - The working directory, or null to use the underlying working directory.
      Returns:
      This object.
    • workingDir

      public Microservice.Builder workingDir(String workingDir)
      Specifies the directory to use to resolve the config file and other paths defined with the config file.
      Parameters:
      workingDir - The working directory. Must not be null.
      Returns:
      This object.
    • configurations

      public Microservice.Builder configurations(Class<?>... configurations)
      Registers one or more @Configuration-annotated classes whose @Bean methods/fields will be processed during microservice bootstrap.

      Beans contributed by @Configuration classes are stored in the microservice's internal WritableBeanStore (accessible via Microservice.getBeanStore()). They become available to other @Bean factory methods via constructor injection.

      Explicit builder calls (e.g. config(Config), args(Args)) take precedence over @Bean-supplied values — the resolved field is registered into the bean store after configurations are processed, overwriting any same-type @Bean contribution.

      Parameters:
      configurations - The configuration classes. Can be null or empty (no-op); null elements are ignored.
      Returns:
      This object.
      Since:
      10.0.0
    • configurations

      public Microservice.Builder configurations(List<Class<?>> configurations)
      Registers one or more @Configuration-annotated classes.
      Parameters:
      configurations - The configuration classes. Can be null or empty (no-op); null elements are ignored.
      Returns:
      This object.
      Since:
      10.0.0
      See Also:
    • beanStore

      Specifies an external WritableBeanStore to use instead of constructing a fresh one.

      Useful for testing and for composing a microservice into a larger application that already owns a bean store (e.g. a Spring-backed parent store). When set, configurations supplied via configurations(Class...) are registered into this store, and the microservice's resolved values (args, manifest, config, var resolver, listener, microservice itself) are also registered into it.

      Parameters:
      beanStore - The external bean store. Can be null to use a fresh internal store.
      Returns:
      This object.
      Since:
      10.0.0
    • overridingBeanStore

      Installs the supplied BeanStore as the overridingParent of this microservice's bean store.

      The overlay is consulted before any locally-registered beans during construction-time reflective injection (see BasicBeanStore(BeanStore, BeanStore)), so test-time overrides win over the microservice's own @Bean factory contributions. This is the Mode INJECT wiring pattern for Microservice-based tests — the overlay is installed before the microservice is constructed and lives for the microservice's full lifetime. For per-test push/pop overlays against an already-started microservice, see Microservice.getBeanStore() and the push/pop primitives on WritableBeanStore.

      Composes with beanStore(WritableBeanStore): when both are supplied, the supplied external store becomes the regular parent and the overlay becomes the overriding parent of a fresh wrapping BasicBeanStore.

      Specified by:
      overridingBeanStore in interface BeanStoreOverridable<Microservice.Builder>
      Parameters:
      store - The override layer. Can be null to clear a previously-set value.
      Returns:
      This object.
      Since:
      10.0.0
    • resolveFile

      protected File resolveFile(String path)
      Resolves the specified path.

      If the working directory has been explicitly specified, relative paths are resolved relative to that.

      Parameters:
      path - The path to resolve. Must not be null.
      Returns:
      The resolved file.