Skip to content

Commit

Permalink
HSEARCH-5132 Do not directly rely on the Security Manager in Embedded…
Browse files Browse the repository at this point in the history
…ThreadProvider
  • Loading branch information
marko-bekhta authored and yrodiere committed May 15, 2024
1 parent 08be4e3 commit a08ec86
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,53 @@
*/
package org.hibernate.search.engine.environment.thread.impl;

import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.ThreadFactory;
import java.util.function.Supplier;

import org.hibernate.search.engine.environment.thread.spi.ThreadProvider;
import org.hibernate.search.engine.logging.impl.Log;
import org.hibernate.search.util.common.annotation.impl.SuppressForbiddenApis;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;

public final class EmbeddedThreadProvider implements ThreadProvider {

private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );

public static final String NAME = "embedded";
private static final Supplier<ThreadGroup> THREAD_GROUP_PROVIDER;

static {
Supplier<ThreadGroup> provider = null;
try {
// if the SM is loaded then it means it is still around and we'll try getting the thread group out of it if it is configured:
Class<?> smClass = Thread.currentThread().getContextClassLoader().loadClass( "java.lang.SecurityManager" );
Method getSecurityManager = System.class.getDeclaredMethod( "getSecurityManager" );
Method getThreadGroup = smClass.getDeclaredMethod( "getThreadGroup" );

provider = () -> {
Object sm;
try {
sm = getSecurityManager.invoke( null );
if ( sm != null ) {
return (ThreadGroup) getThreadGroup.invoke( sm );
}
}
catch (InvocationTargetException | IllegalAccessException e) {
// shouldn't really happen, but just in case:
throw log.securityManagerInvocationProblem( e.getMessage(), e );
}

return Thread.currentThread().getThreadGroup();
};
}
catch (ClassNotFoundException | NoSuchMethodException e) {
provider = () -> Thread.currentThread().getThreadGroup();
}
THREAD_GROUP_PROVIDER = provider;
}

private final String commonThreadNamePrefix;

Expand All @@ -34,16 +73,12 @@ public String createThreadName(String prefix, int threadNumber) {
@SuppressForbiddenApis(reason = "It's unclear how we will handle this without the security manager;"
+ " we'll see when the security manager actually gets removed from the JDK")
public ThreadFactory createThreadFactory(String prefix) {
@SuppressWarnings("removal")
SecurityManager s = System.getSecurityManager();
@SuppressWarnings({ "removal", "deprecation" })
ThreadGroup group = ( s != null ) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
ThreadGroup group = THREAD_GROUP_PROVIDER.get();
String namePrefix = createFullThreadNamePrefix( prefix );
return new SimpleThreadFactory( group, namePrefix );
}

private String createFullThreadNamePrefix(String prefix) {
return commonThreadNamePrefix + prefix + " - ";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -607,4 +607,7 @@ SearchException unexpectedQueryParameterType(String name, @FormatWith(ClassForma
+ " but instead got a value of type %3$s.")
SearchException namedValuesParameterIncorrectType(String name, @FormatWith(ClassFormatter.class) Class<?> expected,
@FormatWith(ClassFormatter.class) Class<?> actual);

@Message(id = ID_OFFSET + 128, value = "Unable to get a thread group from the SecurityManager: %1$s")
SearchException securityManagerInvocationProblem(String causeMessage, @Cause Exception cause);
}

0 comments on commit a08ec86

Please sign in to comment.