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 Summary
Fields Modifier and Type Field Description protected static Boolean
isOSGiServiceSupported
Whether or not services should be exported as OSGI services.static String
NUXEO_DEV_SYSTEM_PROP
Global dev propertystatic String
NUXEO_STRICT_RUNTIME_SYSTEM_PROP
Deprecated.since 9.1 This property is not documented and doesn't work.static String
NUXEO_TESTING_SYSTEM_PROP
Global testing propertyprotected static SharedResourceLoader
resourceLoader
A class loader used to share resources between all bundles.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
addListener(RuntimeServiceListener listener)
Registers a listener to be notified about runtime events.protected static void
checkRuntimeInitialized()
static Path
createTempDirectory(String prefix, FileAttribute<?>... attrs)
Creates a new directory in the framework temporary-file directory (nuxeo.tmp.dir
vsjava.io.tmpdir
), using the given prefix to generate its name.static File
createTempFile(String prefix, String suffix)
Creates an empty file in the framework temporary-file directory (nuxeo.tmp.dir
vsjava.io.tmpdir
), using the given prefix and suffix to generate its name.static Path
createTempFilePath(String prefix, String suffix, FileAttribute<?>... attrs)
Creates an empty file in the framework temporary-file directory (nuxeo.tmp.dir
vsjava.io.tmpdir
), using the given prefix and suffix to generate its name.static void
doPrivileged(Runnable runnable)
Runs the givenRunnable
while logged in as a system user.static <T> T
doPrivileged(Supplier<T> supplier)
Calls the givenSupplier
while logged in as a system user and returns its result.static <E extends Throwable>
voiddoPrivilegedThrowing(ThrowableRunnable<E> runnable)
Runs the givenThrowableRunnable
while logged in as a system user.static <T,E extends Throwable>
TdoPrivilegedThrowing(ThrowableSupplier<T,E> supplier)
Calls the givenThrowableSupplier
while logged in as a system user and returns its result.static String
expandVars(String expression)
Expands any variable found in the given expression with the value of the corresponding framework property.static <T> T
getLocalService(Class<T> serviceClass)
Deprecated.since 9.10, usegetService(java.lang.Class<T>)
insteadstatic Properties
getProperties()
Gets all the framework properties.static String
getProperty(String key)
Gets the given property value if any, otherwise null.static String
getProperty(String key, String defValue)
Gets the given property value if any, otherwise returns the given default value.static SharedResourceLoader
getResourceLoader()
static RuntimeService
getRuntime()
Gets the runtime service instance.static <T> T
getService(Class<T> serviceClass)
Gets a service given its class.static void
initialize(RuntimeService runtimeService)
static boolean
isBooleanPropertyFalse(String propName)
Returns true if given property is false when compared to a boolean value.static boolean
isBooleanPropertyTrue(String propName)
Returns true if given property is true when compared to a boolean value.static boolean
isDevModeSet()
Returns true if dev mode is set.static boolean
isInitialized()
Tests whether or not the runtime was initialized.static boolean
isOSGiServiceSupported()
static boolean
isTestModeSet()
Returns true if test mode is set.static LoginContext
login()
Deprecated.since 11.1, useloginSystem()
insteadstatic LoginContext
login(String username, Object password)
Deprecated.since 11.1, useloginUser(java.lang.String)
insteadprotected static <T,E extends Throwable>
TloginAndDo(ThrowableSupplier<NuxeoLoginContext,LoginException> authSupplier, ThrowableSupplier<T,E> supplier)
Logs in the Nuxeo platform using the givenauthSupplier
, then gets the givensupplier
before logging out.static LoginContext
loginAs(String originatingUser)
Deprecated.since 11.1, useloginSystem(String)
insteadstatic LoginContext
loginAsUser(String username)
Deprecated.since 11.1, useloginUser(java.lang.String)
insteadstatic NuxeoLoginContext
loginSystem()
Login as the system user (with no originating user).static NuxeoLoginContext
loginSystem(String originatingUser)
Login as the system user, remembering the originating user.static NuxeoLoginContext
loginUser(String username)
Login as the given user.static void
reloadResourceLoader()
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.static void
removeListener(RuntimeServiceListener listener)
Removes the given listener.static void
sendEvent(RuntimeServiceEvent event)
static void
shutdown()
static void
trackDirectory(File file, Object marker)
Tracks the specifiedfile
, using the providedmarker
, deleting the file when the marker instance is garbage collected.static void
trackFile(File file, Object marker)
Tracks the specifiedfile
, using the providedmarker
, deleting the file when the marker instance is garbage collected.
-
-
-
Field Detail
-
NUXEO_DEV_SYSTEM_PROP
public static final String NUXEO_DEV_SYSTEM_PROP
Global dev property- Since:
- 5.6
- See Also:
isDevModeSet()
, Constant Field Values
-
NUXEO_TESTING_SYSTEM_PROP
public static final String NUXEO_TESTING_SYSTEM_PROP
Global testing property- Since:
- 5.6
- See Also:
isTestModeSet()
, Constant Field Values
-
NUXEO_STRICT_RUNTIME_SYSTEM_PROP
@Deprecated(since="9.1") public static final String NUXEO_STRICT_RUNTIME_SYSTEM_PROP
Deprecated.since 9.1 This property is not documented and doesn't work.Property to control strict runtime mode- Since:
- 5.6
- See Also:
- Constant Field Values
-
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 Detail
-
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.
-
getLocalService
@Deprecated(since="9.10") public static <T> T getLocalService(Class<T> serviceClass)
Deprecated.since 9.10, usegetService(java.lang.Class<T>)
insteadGets a service given its class.
-
doPrivileged
public static void doPrivileged(Runnable runnable)
Runs the givenRunnable
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 givenSupplier
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 extends Throwable
Runs the givenThrowableRunnable
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 extends Throwable
Calls the givenThrowableSupplier
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 extends Throwable
Logs in the Nuxeo platform using the givenauthSupplier
, then gets the givensupplier
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(String)
-
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 tologinSystem()
.- Parameters:
originatingUser
- the originating user- Returns:
- the login context, to be closed when done
- Since:
- 11.1
- See Also:
loginSystem()
-
loginUser
public static NuxeoLoginContext loginUser(String username) throws LoginException
Login as the given user. The returnedNuxeoLoginContext
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, useloginSystem()
insteadLogin 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, useloginSystem(String)
insteadLogin 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, useloginUser(java.lang.String)
insteadLogin 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, useloginUser(java.lang.String)
insteadLogin in the system as the given user using the given password.- Parameters:
username
- the username to loginpassword
- 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 keydefValue
- 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 specifiedfile
, using the providedmarker
, 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, usetrackDirectory(File, Object)
.- Parameters:
file
- the file to deletemarker
- the marker Object- See Also:
FileEventTracker
-
trackDirectory
public static void trackDirectory(File file, Object marker)
Tracks the specifiedfile
, using the providedmarker
, deleting the file when the marker instance is garbage collected.Unlike
trackFile(File, Object)
, thefile
is deleted even if it is a non empty directory.- Parameters:
file
- the file to deletemarker
- the marker Object- Since:
- 2023.5
- See Also:
trackFile(File, Object)
-
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
vsjava.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 longsuffix
- The suffix string to be used in generating the file's name; may benull
, in which case the suffix".tmp"
will be used- Returns:
- An abstract pathname denoting a newly-created empty file
- Throws:
IllegalArgumentException
- If theprefix
argument contains fewer than three charactersIOException
- If a file could not be createdSecurityException
- If a security manager exists and itsSecurityManager.checkWrite(java.lang.String)
- Since:
- 8.1
- See Also:
File.createTempFile(String, String, File)
,Environment.getTemp()
,createTempFilePath(String, String, FileAttribute...)
,createTempDirectory(String, FileAttribute...)
-
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
vsjava.io.tmpdir
), using the given prefix and suffix to generate its name. The resultingPath
is associated with the defaultFileSystem
.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 benull
suffix
- the suffix string to be used in generating the file's name; may benull
, in which case ".tmp
" is usedattrs
- 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 nameUnsupportedOperationException
- if the array contains an attribute that cannot be set atomically when creating the directoryIOException
- if an I/O error occurs or the temporary-file directory does not existSecurityException
- In the case of the default provider, and a security manager is installed, thecheckWrite
method is invoked to check write access to the file.- Since:
- 8.1
- See Also:
Files.createTempFile(Path, String, String, FileAttribute...)
,Environment.getTemp()
,createTempFile(String, String)
-
createTempDirectory
public static Path createTempDirectory(String prefix, FileAttribute<?>... attrs) throws IOException
Creates a new directory in the framework temporary-file directory (nuxeo.tmp.dir
vsjava.io.tmpdir
), using the given prefix to generate its name. The resultingPath
is associated with the defaultFileSystem
.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 benull
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 nameUnsupportedOperationException
- if the array contains an attribute that cannot be set atomically when creating the directoryIOException
- if an I/O error occurs or the temporary-file directory does not existSecurityException
- In the case of the default provider, and a security manager is installed, thecheckWrite
method is invoked to check write access when creating the directory.- Since:
- 8.1
- See Also:
Files.createTempDirectory(Path, String, FileAttribute...)
,Environment.getTemp()
,createTempFile(String, String)
-
-