Package org.apache.tomcat.util.compat
Class JreCompat
- java.lang.Object
-
- org.apache.tomcat.util.compat.JreCompat
-
- Direct Known Subclasses:
Jre12Compat
public class JreCompat extends java.lang.Object
This is the base implementation class for JRE compatibility and provides an implementation based on Java 8. Subclasses may extend this class and provide alternative implementations for later JRE versions
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.Boolean
canonCachesDisabled
protected static java.lang.Object
canonCachesDisabledLock
protected static java.lang.reflect.Method
getApplicationProtocolMethod
protected static java.lang.reflect.Method
setApplicationProtocolsMethod
protected static java.lang.String
USE_CANON_CACHES_CMD_ARG
protected static java.util.Optional<java.lang.reflect.Field>
useCanonCachesField
protected static java.lang.Object
useCanonCachesFieldLock
-
Constructor Summary
Constructors Constructor Description JreCompat()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addBootModulePath(java.util.Deque<java.net.URL> classPathUrlsToProcess)
Obtains the URLs for all the JARs on the module path when the JVM starts and adds them to the provided Deque.<T> T
callAs(javax.security.auth.Subject subject, java.util.concurrent.Callable<T> action)
boolean
canAccess(java.lang.Object base, java.lang.reflect.AccessibleObject accessibleObject)
Is the accessibleObject accessible (as a result of appropriate module exports) on the provided instance?java.lang.Object
createVirtualThreadBuilder(java.lang.String name)
Create a thread builder for virtual threads using the given name to name the threads.void
disableCachingForJarUrlConnections()
Disables caching for JAR URL connections.boolean
disableCanonCaches()
Disable the global canonical file cache.protected void
ensureUseCanonCachesFieldIsPopulated()
java.lang.String
getApplicationProtocol(javax.net.ssl.SSLEngine sslEngine)
Get the application protocol that has been negotiated for connection associated with the given SSLEngine.java.lang.Object
getExecutor(java.lang.Thread thread)
Obtains the executor, if any, used to create the provided thread.static JreCompat
getInstance()
java.lang.String
getModuleName(java.lang.Class<?> type)
What is the module of the given class?java.net.SocketAddress
getUnixDomainSocketAddress(java.lang.String path)
Return Unix domain socket address for given path.static boolean
isAlpnSupported()
boolean
isCanonCachesDisabled()
boolean
isExported(java.lang.Class<?> type)
Is the given class in an exported package?static boolean
isGraalAvailable()
boolean
isInstanceOfInaccessibleObjectException(java.lang.Throwable t)
Test if the provided exception is an instance of java.lang.reflect.InaccessibleObjectException.static boolean
isJre11Available()
static boolean
isJre12Available()
static boolean
isJre16Available()
static boolean
isJre19Available()
static boolean
isJre21Available()
static boolean
isJre22Available()
static boolean
isJre9Available()
boolean
jarFileIsMultiRelease(java.util.jar.JarFile jarFile)
Is this JarFile a multi-release JAR file.java.util.jar.JarFile
jarFileNewInstance(java.io.File f)
Creates a new JarFile instance.java.util.jar.JarFile
jarFileNewInstance(java.lang.String s)
Creates a new JarFile instance.int
jarFileRuntimeMajorVersion()
java.nio.channels.ServerSocketChannel
openUnixDomainServerSocketChannel()
Create server socket channel using the Unix domain socket ProtocolFamily.java.nio.channels.SocketChannel
openUnixDomainSocketChannel()
Create socket channel using the Unix domain socket ProtocolFamily.void
setApplicationProtocols(javax.net.ssl.SSLParameters sslParameters, java.lang.String[] protocols)
Set the application protocols the server will accept for ALPNvoid
threadBuilderStart(java.lang.Object threadBuilder, java.lang.Runnable command)
Create a thread with the given thread builder and use it to execute the given runnable.
-
-
-
Field Detail
-
USE_CANON_CACHES_CMD_ARG
protected static final java.lang.String USE_CANON_CACHES_CMD_ARG
- See Also:
- Constant Field Values
-
canonCachesDisabled
protected static volatile java.lang.Boolean canonCachesDisabled
-
canonCachesDisabledLock
protected static final java.lang.Object canonCachesDisabledLock
-
useCanonCachesField
protected static volatile java.util.Optional<java.lang.reflect.Field> useCanonCachesField
-
useCanonCachesFieldLock
protected static final java.lang.Object useCanonCachesFieldLock
-
setApplicationProtocolsMethod
protected static final java.lang.reflect.Method setApplicationProtocolsMethod
-
getApplicationProtocolMethod
protected static final java.lang.reflect.Method getApplicationProtocolMethod
-
-
Method Detail
-
getInstance
public static JreCompat getInstance()
-
isGraalAvailable
public static boolean isGraalAvailable()
-
isAlpnSupported
public static boolean isAlpnSupported()
-
isJre9Available
public static boolean isJre9Available()
-
isJre11Available
public static boolean isJre11Available()
-
isJre12Available
public static boolean isJre12Available()
-
isJre16Available
public static boolean isJre16Available()
-
isJre19Available
public static boolean isJre19Available()
-
isJre21Available
public static boolean isJre21Available()
-
isJre22Available
public static boolean isJre22Available()
-
isInstanceOfInaccessibleObjectException
public boolean isInstanceOfInaccessibleObjectException(java.lang.Throwable t)
Test if the provided exception is an instance of java.lang.reflect.InaccessibleObjectException.- Parameters:
t
- The exception to test- Returns:
true
if the exception is an instance of InaccessibleObjectException, otherwisefalse
-
setApplicationProtocols
public void setApplicationProtocols(javax.net.ssl.SSLParameters sslParameters, java.lang.String[] protocols)
Set the application protocols the server will accept for ALPN- Parameters:
sslParameters
- The SSL parameters for a connectionprotocols
- The application protocols to be allowed for that connection
-
getApplicationProtocol
public java.lang.String getApplicationProtocol(javax.net.ssl.SSLEngine sslEngine)
Get the application protocol that has been negotiated for connection associated with the given SSLEngine.- Parameters:
sslEngine
- The SSLEngine for which to obtain the negotiated protocol- Returns:
- The name of the negotiated protocol
-
disableCachingForJarUrlConnections
public void disableCachingForJarUrlConnections() throws java.io.IOException
Disables caching for JAR URL connections. For Java 8 and earlier, this also disables caching for ALL URL connections.- Throws:
java.io.IOException
- If a dummy JAR URLConnection can not be created
-
addBootModulePath
public void addBootModulePath(java.util.Deque<java.net.URL> classPathUrlsToProcess)
Obtains the URLs for all the JARs on the module path when the JVM starts and adds them to the provided Deque.- Parameters:
classPathUrlsToProcess
- The Deque to which the modules should be added
-
jarFileNewInstance
public final java.util.jar.JarFile jarFileNewInstance(java.lang.String s) throws java.io.IOException
Creates a new JarFile instance. When running on Java 9 and later, the JarFile will be multi-release JAR aware. While this isn't strictly required to be in this package, it is provided as a convenience method.- Parameters:
s
- The JAR file to open- Returns:
- A JarFile instance based on the provided path
- Throws:
java.io.IOException
- If an I/O error occurs creating the JarFile instance
-
jarFileNewInstance
public java.util.jar.JarFile jarFileNewInstance(java.io.File f) throws java.io.IOException
Creates a new JarFile instance. When running on Java 9 and later, the JarFile will be multi-release JAR aware.- Parameters:
f
- The JAR file to open- Returns:
- A JarFile instance based on the provided file
- Throws:
java.io.IOException
- If an I/O error occurs creating the JarFile instance
-
jarFileIsMultiRelease
public boolean jarFileIsMultiRelease(java.util.jar.JarFile jarFile)
Is this JarFile a multi-release JAR file.- Parameters:
jarFile
- The JarFile to test- Returns:
true
If it is a multi-release JAR file and is configured to behave as such.
-
jarFileRuntimeMajorVersion
public int jarFileRuntimeMajorVersion()
-
canAccess
public boolean canAccess(java.lang.Object base, java.lang.reflect.AccessibleObject accessibleObject)
Is the accessibleObject accessible (as a result of appropriate module exports) on the provided instance?- Parameters:
base
- The specific instance to be tested.accessibleObject
- The method/field/constructor to be tested.- Returns:
- {code true} if the AccessibleObject can be accessed otherwise {code false}
-
isExported
public boolean isExported(java.lang.Class<?> type)
Is the given class in an exported package?- Parameters:
type
- The class to test- Returns:
- Always
true
for Java 8.true
if the enclosing package is exported for Java 9+
-
getModuleName
public java.lang.String getModuleName(java.lang.Class<?> type)
What is the module of the given class?- Parameters:
type
- The class to test- Returns:
- Always
true
for Java 8.true
if the enclosing package is exported for Java 9+
-
getUnixDomainSocketAddress
public java.net.SocketAddress getUnixDomainSocketAddress(java.lang.String path)
Return Unix domain socket address for given path.- Parameters:
path
- The path- Returns:
- the socket address
-
openUnixDomainServerSocketChannel
public java.nio.channels.ServerSocketChannel openUnixDomainServerSocketChannel()
Create server socket channel using the Unix domain socket ProtocolFamily.- Returns:
- the server socket channel
-
openUnixDomainSocketChannel
public java.nio.channels.SocketChannel openUnixDomainSocketChannel()
Create socket channel using the Unix domain socket ProtocolFamily.- Returns:
- the socket channel
-
getExecutor
public java.lang.Object getExecutor(java.lang.Thread thread) throws java.lang.NoSuchFieldException, java.lang.SecurityException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException
Obtains the executor, if any, used to create the provided thread.- Parameters:
thread
- The thread to examine- Returns:
- The executor, if any, that created the provided thread
- Throws:
java.lang.NoSuchFieldException
- If a field used via reflection to obtain the executor cannot be foundjava.lang.SecurityException
- If a security exception occurs while trying to identify the executorjava.lang.IllegalArgumentException
- If the instance object does not match the class of the field when obtaining a field value via reflectionjava.lang.IllegalAccessException
- If a field is not accessible due to access restrictions
-
createVirtualThreadBuilder
public java.lang.Object createVirtualThreadBuilder(java.lang.String name)
Create a thread builder for virtual threads using the given name to name the threads.- Parameters:
name
- The base name for the threads- Returns:
- The thread buidler for virtual threads
-
threadBuilderStart
public void threadBuilderStart(java.lang.Object threadBuilder, java.lang.Runnable command)
Create a thread with the given thread builder and use it to execute the given runnable.- Parameters:
threadBuilder
- The thread builder to use to create a threadcommand
- The command to run
-
callAs
public <T> T callAs(javax.security.auth.Subject subject, java.util.concurrent.Callable<T> action) throws java.util.concurrent.CompletionException
- Throws:
java.util.concurrent.CompletionException
-
isCanonCachesDisabled
public boolean isCanonCachesDisabled()
-
disableCanonCaches
public boolean disableCanonCaches()
Disable the global canonical file cache.- Returns:
true
if the global canonical file cache was already disabled prior to this call or was disabled as a result of this call, otherwisefalse
-
ensureUseCanonCachesFieldIsPopulated
protected void ensureUseCanonCachesFieldIsPopulated()
-
-