Package org.pkl.core

Class EvaluatorBuilder


  • public final class EvaluatorBuilder
    extends java.lang.Object
    A builder for an Evaluator. Can be reused to build multiple evaluators.
    • Method Detail

      • unconfigured

        public static EvaluatorBuilder unconfigured()
        Creates a builder that is unconfigured. At a minimum, a security manager will need to be set before building an instance.
      • setStackFrameTransformer

        public EvaluatorBuilder setStackFrameTransformer​(StackFrameTransformer stackFrameTransformer)
        Sets the given stack frame transformer, replacing any previously set transformer.
      • getSecurityManager

        public @Nullable SecurityManager getSecurityManager()
        Returns the currently set security manager.
      • setAllowedModules

        public EvaluatorBuilder setAllowedModules​(java.util.Collection<java.util.regex.Pattern> patterns)
        Sets the set of URI patterns to be allowed when importing modules.
        Throws:
        java.lang.IllegalStateException - if setSecurityManager(SecurityManager) was also called.
      • getAllowedModules

        public java.util.List<java.util.regex.Pattern> getAllowedModules()
        Returns the set of patterns to be allowed when importing modules.
      • setAllowedResources

        public EvaluatorBuilder setAllowedResources​(java.util.Collection<java.util.regex.Pattern> patterns)
        Sets the set of URI patterns to be allowed when reading resources.
        Throws:
        java.lang.IllegalStateException - if setSecurityManager(SecurityManager) was also called.
      • getAllowedResources

        public java.util.List<java.util.regex.Pattern> getAllowedResources()
        Returns the set of patterns to be allowed when reading resources.
      • setRootDir

        public EvaluatorBuilder setRootDir​(@Nullable java.nio.file.Path rootDir)
        Sets the root directory, which restricts access to file-based modules and resources located under this directory.
      • getRootDir

        public @Nullable java.nio.file.Path getRootDir()
        Returns the currently set root directory, if set.
      • setLogger

        public EvaluatorBuilder setLogger​(Logger logger)
        Sets the given logger, replacing any previously set logger.
      • getLogger

        public Logger getLogger()
        Returns the currently set logger.
      • addModuleKeyFactory

        public EvaluatorBuilder addModuleKeyFactory​(ModuleKeyFactory factory)
        Adds the given module key factory. Factories will be asked to resolve module keys in the order they have been added to this builder.
      • addModuleKeyFactories

        public EvaluatorBuilder addModuleKeyFactories​(java.util.Collection<ModuleKeyFactory> factories)
        Adds the given module key factories. Factories will be asked to resolve module keys in the order they have been added to this builder.
      • setModuleKeyFactories

        public EvaluatorBuilder setModuleKeyFactories​(java.util.Collection<ModuleKeyFactory> factories)
        Removes any existing module key factories, then adds the given factories.
      • getModuleKeyFactories

        public java.util.List<ModuleKeyFactory> getModuleKeyFactories()
        Returns the currently set module key factories.
      • getResourceReaders

        public java.util.List<ResourceReader> getResourceReaders()
        Returns the currently set resource readers.
      • addEnvironmentVariable

        public EvaluatorBuilder addEnvironmentVariable​(java.lang.String name,
                                                       java.lang.String value)
        Adds the given environment variable, overriding any environment variable previously added under the same name.

        Pkl code can read environment variables with read("env:<NAME>").

      • addEnvironmentVariables

        public EvaluatorBuilder addEnvironmentVariables​(java.util.Map<java.lang.String,​java.lang.String> envVars)
        Adds the given environment variables, overriding any environment variables previously added under the same name.

        Pkl code can read environment variables with read("env:<NAME>").

      • setEnvironmentVariables

        public EvaluatorBuilder setEnvironmentVariables​(java.util.Map<java.lang.String,​java.lang.String> envVars)
        Removes any existing environment variables, then adds the given environment variables.
      • getEnvironmentVariables

        public java.util.Map<java.lang.String,​java.lang.String> getEnvironmentVariables()
        Returns the currently set environment variables.
      • addExternalProperty

        public EvaluatorBuilder addExternalProperty​(java.lang.String name,
                                                    java.lang.String value)
        Adds the given external property, overriding any property previously set under the same name.

        Pkl code can read external properties with read("prop:<name>").

      • addExternalProperties

        public EvaluatorBuilder addExternalProperties​(java.util.Map<java.lang.String,​java.lang.String> properties)
        Adds the given external properties, overriding any properties previously set under the same name.

        Pkl code can read external properties with read("prop:<name>").

      • setExternalProperties

        public EvaluatorBuilder setExternalProperties​(java.util.Map<java.lang.String,​java.lang.String> properties)
        Removes any existing external properties, then adds the given properties.
      • getExternalProperties

        public java.util.Map<java.lang.String,​java.lang.String> getExternalProperties()
        Returns the currently set external properties.
      • setTimeout

        public EvaluatorBuilder setTimeout​(@Nullable java.time.Duration timeout)
        Sets an evaluation timeout to be enforced by the Evaluator's evaluate methods.
      • getTimeout

        public @Nullable java.time.Duration getTimeout()
        Returns the currently set evaluation timeout.
      • setModuleCacheDir

        public EvaluatorBuilder setModuleCacheDir​(@Nullable java.nio.file.Path moduleCacheDir)
        Sets the directory where `package:` modules are cached.

        If null, the module cache is disabled.

      • getModuleCacheDir

        public @Nullable java.nio.file.Path getModuleCacheDir()
        Returns the directory where `package:` modules are cached. If null, the module cache is disabled.
      • setOutputFormat

        public EvaluatorBuilder setOutputFormat​(@Nullable java.lang.String outputFormat)
        Sets the desired output format, if any.

        By default, modules support the formats described by OutputFormat. and fall back to OutputFormat.PCF if no format is specified.

        Modules that override output.renderer in their source code may ignore this option or may support formats other than those described by OutputFormat. In particular, most templates ignore this option and always render the same format.

      • setOutputFormat

        public EvaluatorBuilder setOutputFormat​(@Nullable OutputFormat outputFormat)
        Sets the desired output format, if any.

        By default, modules support the formats described by OutputFormat. and fall back to OutputFormat.PCF if no format is specified.

        Modules that override output.renderer in their source code may ignore this option or may support formats other than those described by OutputFormat. In particular, most templates ignore this option and always render the same format.

      • getOutputFormat

        public @Nullable java.lang.String getOutputFormat()
        Returns the currently set output format, if any.