Skip to content

Commit

Permalink
HSEARCH-5131 Remove usage and mentions of a security manager
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Aug 26, 2024
1 parent b422fef commit beca725
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private static <T> Constructor<T> getConstructor(TypeToken<T> type) {
try {
return (Constructor<T>) type.getRawType().getConstructor();
}
catch (NoSuchMethodException | SecurityException e) {
catch (NoSuchMethodException e) {
throw new AssertionFailure( "Missing or inaccessible no-arg constructor on type " + type );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static <T> T instanceFromName(Class<T> targetSuperType,
* @return a new instance of classToLoad
*
* @throws SearchException wrapping other error types with a proper error message for all kind of problems, like
* missing proper constructor, securitymanager errors.
* missing proper constructor errors.
*/
public static <T> T untypedInstanceFromClass(final Class<T> classToLoad) {
try {
Expand Down Expand Up @@ -140,9 +140,6 @@ private static <T> T callNoArgConstructor(Class<T> classToLoad)
Constructor<T> constructor = classToLoad.getConstructor();
return constructor.newInstance();
}
catch (SecurityException e) {
throw log.securityManagerLoadingError( classToLoad, e.getMessage(), e );
}
catch (NoSuchMethodException e) {
throw log.noPublicNoArgConstructor( classToLoad );
}
Expand All @@ -158,9 +155,6 @@ private static <T> T callMapArgConstructor(Class<T> classToLoad, Map<String, Str
Constructor<T> singleMapConstructor = classToLoad.getConstructor( Map.class );
return singleMapConstructor.newInstance( constructorParameter );
}
catch (SecurityException e) {
throw log.securityManagerLoadingError( classToLoad, e.getMessage(), e );
}
catch (NoSuchMethodException e) {
throw log.noPublicMapArgConstructor( classToLoad );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,13 @@
*/
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 @@ -68,10 +28,8 @@ public String createThreadName(String prefix, int threadNumber) {
}

@Override
@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) {
ThreadGroup group = THREAD_GROUP_PROVIDER.get();
ThreadGroup group = Thread.currentThread().getThreadGroup();
String namePrefix = createFullThreadNamePrefix( prefix );
return new SimpleThreadFactory( group, namePrefix );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,6 @@ void newCollectedFailure(String process,
)
SearchException dslExtensionNoMatch(List<?> attemptedExtensions);

@Message(id = ID_OFFSET + 28, value = "Security manager does not allow access to the constructor of type '%1$s': %2$s")
SearchException securityManagerLoadingError(@FormatWith(ClassFormatter.class) Class<?> classToLoad,
String causeMessage, @Cause Exception cause);

@Message(id = ID_OFFSET + 30, value = "Unable to load class '%1$s': %2$s")
ClassLoadingException unableToLoadTheClass(String className, String causeMessage, @Cause Throwable cause);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
Expand Down Expand Up @@ -152,17 +151,10 @@ private <T> HibernateOrmClassRawTypeModel<T> createClassTypeModel(Class<T> type)
}

private static void setAccessible(Member member) {
try {
// always try to set accessible to true regardless of visibility
// as it's faster even for public fields:
// it bypasses the security model checks at execution time.
( (AccessibleObject) member ).setAccessible( true );
}
catch (SecurityException se) {
if ( !Modifier.isPublic( member.getModifiers() ) ) {
throw se;
}
}
// always try to set accessible to true regardless of visibility
// as it's faster even for public fields:
// it bypasses the security model checks at execution time.
( (AccessibleObject) member ).setAccessible( true );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -92,16 +91,9 @@ private <T> PojoRawTypeModel<T> createTypeModel(Class<T> clazz) {
}

private static void setAccessible(Member member) {
try {
// always try to set accessible to true regardless of visibility
// as it's faster even for public fields:
// it bypasses the security model checks at execution time.
( (AccessibleObject) member ).setAccessible( true );
}
catch (SecurityException se) {
if ( !Modifier.isPublic( member.getModifiers() ) ) {
throw se;
}
}
// always try to set accessible to true regardless of visibility
// as it's faster even for public fields:
// it bypasses the security model checks at execution time.
( (AccessibleObject) member ).setAccessible( true );
}
}

0 comments on commit beca725

Please sign in to comment.