Class Framework

java.lang.Object
org.nuxeo.runtime.api.Framework

public final class Framework extends Object
This class is the main entry point to a Nuxeo runtime application.

It offers an easy way to create new sessions, to access system services and other resources.

There are two type of services:

  • Global Services - these services are uniquely defined by a service class, and there is an unique instance of the service in the system per class.
  • Local Services - these services are defined by a class and an URI. This type of service allows multiple service instances for the same class of services. Each instance is uniquely defined in the system by an URI.
Author:
Bogdan Stefanescu
  • Field Details

    • NUXEO_DEV_SYSTEM_PROP

      public static final String NUXEO_DEV_SYSTEM_PROP
      Global dev property
      Since:
      5.6
      See Also:
    • NUXEO_TESTING_SYSTEM_PROP

      public static final String NUXEO_TESTING_SYSTEM_PROP
      Global testing property
      Since:
      5.6
      See Also:
    • resourceLoader

      protected static SharedResourceLoader resourceLoader
      A class loader used to share resources between all bundles.

      This is useful to put resources outside any bundle (in a directory on the file system) and then refer them from XML contributions.

      The resource directory used by this loader is ${nuxeo_data_dir}/resources whee ${nuxeo_data_dir} is usually ${nuxeo_home}/data

    • isOSGiServiceSupported

      protected static Boolean isOSGiServiceSupported
      Whether or not services should be exported as OSGI services. This is controlled by the ${ecr.osgi.services} property. The default is false.
  • Method Details

    • initialize

      public static void initialize(RuntimeService runtimeService)
    • reloadResourceLoader

      public static void reloadResourceLoader()
    • reloadResourceLoader

      public static void reloadResourceLoader(List<URL> urlsToAdd, List<URL> urlsToRemove)
      Reload the resources loader, keeping URLs already tracked, and adding possibility to add or remove some URLs.

      Useful for hot reload of jars.

      Since:
      5.6
    • shutdown

      public static void shutdown() throws InterruptedException
      Throws:
      InterruptedException
    • isInitialized

      public static boolean isInitialized()
      Tests whether or not the runtime was initialized.
      Returns:
      true if the runtime was initialized, false otherwise
    • getResourceLoader

      public static SharedResourceLoader getResourceLoader()
    • getRuntime

      public static RuntimeService getRuntime()
      Gets the runtime service instance.
      Returns:
      the runtime service instance
    • getService

      public static <T> T getService(Class<T> serviceClass)
      Gets a service given its class.
    • doPrivileged

      public static void doPrivileged(Runnable runnable)
      Runs the given Runnable while logged in as a system user.
      Parameters:
      runnable - what to run
      Since:
      8.4
    • doPrivileged

      public static <T> T doPrivileged(Supplier<T> supplier)
      Calls the given Supplier while logged in as a system user and returns its result.
      Parameters:
      supplier - what to call
      Returns:
      the supplier's result
      Since:
      8.4
    • doPrivilegedThrowing

      public static <E extends Throwable> void doPrivilegedThrowing(ThrowableRunnable<E> runnable) throws E
      Runs the given ThrowableRunnable while logged in as a system user.
      Parameters:
      runnable - what to run
      Throws:
      E extends Throwable
      Since:
      11.3
    • doPrivilegedThrowing

      public static <T, E extends Throwable> T doPrivilegedThrowing(ThrowableSupplier<T,E> supplier) throws E
      Calls the given ThrowableSupplier while logged in as a system user and returns its result.
      Parameters:
      supplier - what to call
      Returns:
      the supplier's result
      Throws:
      E extends Throwable
      Since:
      11.3
    • loginAndDo

      protected static <T, E extends Throwable> T loginAndDo(ThrowableSupplier<NuxeoLoginContext,LoginException> authSupplier, ThrowableSupplier<T,E> supplier) throws E
      Logs in the Nuxeo platform using the given authSupplier, then gets the given supplier before logging out.
      Throws:
      E extends Throwable
      Since:
      11.1
    • loginSystem

      public static NuxeoLoginContext loginSystem()
      Login as the system user (with no originating user).

      The returned NuxeoLoginContext MUST be closed when done.

      Returns:
      the login context, to be closed when done
      Since:
      11.1
      See Also:
    • loginSystem

      public static NuxeoLoginContext loginSystem(String originatingUser)
      Login as the system user, remembering the originating user.

      The returned NuxeoLoginContext MUST be closed when done.

      If the originating user is null, then this is equivalent to loginSystem().

      Parameters:
      originatingUser - the originating user
      Returns:
      the login context, to be closed when done
      Since:
      11.1
      See Also:
    • loginUser

      public static NuxeoLoginContext loginUser(String username) throws LoginException
      Login as the given user. The returned NuxeoLoginContext MUST be closed when done.
      Parameters:
      username - the username
      Returns:
      the login context, to be closed when done
      Throws:
      LoginException - on login failure
      Since:
      11.1
    • login

      @Deprecated public static LoginContext login() throws LoginException
      Deprecated.
      since 11.1, use loginSystem() instead
      Login in the system as the system user (a pseudo-user having all privileges).
      Returns:
      the login session if successful. Never returns null.
      Throws:
      LoginException - on login failure
    • loginAs

      @Deprecated public static LoginContext loginAs(String originatingUser) throws LoginException
      Deprecated.
      since 11.1, use loginSystem(String) instead
      Login in the system as the system user (a pseudo-user having all privileges). The given username will be used to identify the user id that called this method.
      Parameters:
      originatingUser - the originating user id
      Returns:
      the login session if successful. Never returns null.
      Throws:
      LoginException - on login failure
    • loginAsUser

      @Deprecated public static LoginContext loginAsUser(String username) throws LoginException
      Deprecated.
      since 11.1, use loginUser(java.lang.String) instead
      Login in the system as the given user without checking the password.
      Parameters:
      username - the user name to login as.
      Returns:
      the login context
      Throws:
      LoginException - if any error occurs
      Since:
      5.4.2
    • login

      @Deprecated public static LoginContext login(String username, Object password) throws LoginException
      Deprecated.
      since 11.1, use loginUser(java.lang.String) instead
      Login in the system as the given user using the given password.
      Parameters:
      username - the username to login
      password - the password
      Returns:
      a login session if login was successful. Never returns null.
      Throws:
      LoginException - if login failed
    • sendEvent

      public static void sendEvent(RuntimeServiceEvent event)
    • addListener

      public static void addListener(RuntimeServiceListener listener)
      Registers a listener to be notified about runtime events.

      If the listener is already registered, do nothing.

      Parameters:
      listener - the listener to register
    • removeListener

      public static void removeListener(RuntimeServiceListener listener)
      Removes the given listener.

      If the listener is not registered, do nothing.

      Parameters:
      listener - the listener to remove
    • getProperty

      public static String getProperty(String key)
      Gets the given property value if any, otherwise null.

      The framework properties will be searched first then if any matching property is found the system properties are searched too.

      Parameters:
      key - the property key
      Returns:
      the property value if any or null otherwise
    • getProperty

      public static String getProperty(String key, String defValue)
      Gets the given property value if any, otherwise returns the given default value.

      The framework properties will be searched first then if any matching property is found the system properties are searched too.

      Parameters:
      key - the property key
      defValue - the default value to use
      Returns:
      the property value if any otherwise the default value
    • getProperties

      public static Properties getProperties()
      Gets all the framework properties. The system properties are not included in the returned map.
      Returns:
      the framework properties map. Never returns null.
    • expandVars

      public static String expandVars(String expression)
      Expands any variable found in the given expression with the value of the corresponding framework property.

      The variable format is ${property_key}.

      System properties are also expanded.

    • isOSGiServiceSupported

      public static boolean isOSGiServiceSupported()
    • isDevModeSet

      public static boolean isDevModeSet()
      Returns true if dev mode is set.

      Activating this mode, some of the code may not behave as it would in production, to ease up debugging and working on developing the application.

      For instance, it'll enable hot-reload if some packages are installed while the framework is running. It will also reset some caches when that happens.

    • isTestModeSet

      public static boolean isTestModeSet()
      Returns true if test mode is set.

      Activating this mode, some of the code may not behave as it would in production, to ease up testing.

    • isBooleanPropertyFalse

      public static boolean isBooleanPropertyFalse(String propName)
      Returns true if given property is false when compared to a boolean value. Returns false if given property in unset.

      Checks for the system properties if property is not found in the runtime properties.

      Since:
      5.8
    • isBooleanPropertyTrue

      public static boolean isBooleanPropertyTrue(String propName)
      Returns true if given property is true when compared to a boolean value.

      Checks for the system properties if property is not found in the runtime properties.

      Since:
      5.6
    • trackFile

      public static void trackFile(File file, Object marker)
      Tracks the specified file, using the provided marker, deleting the file when the marker instance is garbage collected.

      If the file is a directory, it is not deleted if not empty. To make sure a directory is deleted if not empty, use trackDirectory(File, Object).

      Parameters:
      file - the file to delete
      marker - the marker Object
      See Also:
    • trackDirectory

      public static void trackDirectory(File file, Object marker)
      Tracks the specified file, using the provided marker, deleting the file when the marker instance is garbage collected.

      Unlike trackFile(File, Object), the file is deleted even if it is a non empty directory.

      Parameters:
      file - the file to delete
      marker - the marker Object
      Since:
      2023.5
      See Also:
    • checkRuntimeInitialized

      protected static void checkRuntimeInitialized()
      Since:
      6.0
    • createTempFile

      public static File createTempFile(String prefix, String suffix) throws IOException
      Creates an empty file in the framework temporary-file directory (nuxeo.tmp.dir vs java.io.tmpdir ), using the given prefix and suffix to generate its name.

      Invoking this method is equivalent to invoking File.createTempFile(prefix, suffix, Environment.getDefault().getTemp()).

      The createTempFilePath(String, String, FileAttribute...) method provides an alternative method to create an empty file in the framework temporary-file directory. Files created by that method may have more restrictive access permissions to files created by this method and so may be more suited to security-sensitive applications.

      Parameters:
      prefix - The prefix string to be used in generating the file's name; must be at least three characters long
      suffix - The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
      Returns:
      An abstract pathname denoting a newly-created empty file
      Throws:
      IllegalArgumentException - If the prefix argument contains fewer than three characters
      IOException - If a file could not be created
      SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method does not allow a file to be created
      Since:
      8.1
      See Also:
    • createTempFilePath

      public static Path createTempFilePath(String prefix, String suffix, FileAttribute<?>... attrs) throws IOException
      Creates an empty file in the framework temporary-file directory (nuxeo.tmp.dir vs java.io.tmpdir ), using the given prefix and suffix to generate its name. The resulting Path is associated with the default FileSystem.

      Invoking this method is equivalent to invoking Files.createTempFile(Environment.getDefault().getTemp().toPath(), prefix, suffix, attrs).

      Parameters:
      prefix - the prefix string to be used in generating the file's name; may be null
      suffix - the suffix string to be used in generating the file's name; may be null, in which case " .tmp" is used
      attrs - an optional list of file attributes to set atomically when creating the file
      Returns:
      the path to the newly created file that did not exist before this method was invoked
      Throws:
      IllegalArgumentException - if the prefix or suffix parameters cannot be used to generate a candidate file name
      UnsupportedOperationException - if the array contains an attribute that cannot be set atomically when creating the directory
      IOException - if an I/O error occurs or the temporary-file directory does not exist
      SecurityException - In the case of the default provider, and a security manager is installed, the checkWrite method is invoked to check write access to the file.
      Since:
      8.1
      See Also:
    • createTempDirectory

      public static Path createTempDirectory(String prefix, FileAttribute<?>... attrs) throws IOException
      Creates a new directory in the framework temporary-file directory (nuxeo.tmp.dir vs java.io.tmpdir), using the given prefix to generate its name. The resulting Path is associated with the default FileSystem.

      Invoking this method is equivalent to invoking Files.createTempDirectory(Environment.getDefault().getTemp().toPath(), prefix, suffix, attrs).

      Parameters:
      prefix - the prefix string to be used in generating the directory's name; may be null
      attrs - an optional list of file attributes to set atomically when creating the directory
      Returns:
      the path to the newly created directory that did not exist before this method was invoked
      Throws:
      IllegalArgumentException - if the prefix cannot be used to generate a candidate directory name
      UnsupportedOperationException - if the array contains an attribute that cannot be set atomically when creating the directory
      IOException - if an I/O error occurs or the temporary-file directory does not exist
      SecurityException - In the case of the default provider, and a security manager is installed, the checkWrite method is invoked to check write access when creating the directory.
      Since:
      8.1
      See Also: