diff --git a/classloader/src/main/java/io/smallrye/common/classloader/ClassDefiner.java b/classloader/src/main/java/io/smallrye/common/classloader/ClassDefiner.java index f478d9c1..fcb070dc 100644 --- a/classloader/src/main/java/io/smallrye/common/classloader/ClassDefiner.java +++ b/classloader/src/main/java/io/smallrye/common/classloader/ClassDefiner.java @@ -1,8 +1,6 @@ package io.smallrye.common.classloader; import java.lang.invoke.MethodHandles; -import java.security.AccessController; -import java.security.PrivilegedAction; /** * A utility to define classes within a target lookup. @@ -22,21 +20,11 @@ private ClassDefiner() { * @return the defined class (not {@code null}) */ public static Class defineClass(MethodHandles.Lookup lookup, Class parent, String className, byte[] classBytes) { - SecurityManager sm = System.getSecurityManager(); - if (sm != null) { - sm.checkPermission(DefineClassPermission.getInstance()); + try { + MethodHandles.Lookup privateLookupIn = MethodHandles.privateLookupIn(parent, lookup); + return privateLookupIn.defineClass(classBytes); + } catch (IllegalAccessException e) { + throw new IllegalAccessError(e.getMessage()); } - - return AccessController.doPrivileged(new PrivilegedAction>() { - @Override - public Class run() { - try { - MethodHandles.Lookup privateLookupIn = MethodHandles.privateLookupIn(parent, lookup); - return privateLookupIn.defineClass(classBytes); - } catch (IllegalAccessException e) { - throw new IllegalAccessError(e.getMessage()); - } - } - }); } } diff --git a/classloader/src/main/java/io/smallrye/common/classloader/DefineClassPermission.java b/classloader/src/main/java/io/smallrye/common/classloader/DefineClassPermission.java deleted file mode 100644 index 4d165465..00000000 --- a/classloader/src/main/java/io/smallrye/common/classloader/DefineClassPermission.java +++ /dev/null @@ -1,58 +0,0 @@ -package io.smallrye.common.classloader; - -import java.security.Permission; - -/** - * A security manager permission which indicates the ability to define a class. - */ -public class DefineClassPermission extends Permission { - private static final long serialVersionUID = 142067672163413424L; - private static final DefineClassPermission INSTANCE = new DefineClassPermission(); - - /** - * Construct a new instance. - */ - public DefineClassPermission() { - super(""); - } - - /** - * Construct a new instance. - * - * @param name ignored - * @param actions ignored - */ - public DefineClassPermission(final String name, final String actions) { - this(); - } - - /** - * {@return the singular instance} - */ - public static DefineClassPermission getInstance() { - return INSTANCE; - } - - /** - * {@return {@code true} if this permission implies the given permission} - */ - @Override - public boolean implies(final Permission permission) { - return permission != null && permission.getClass() == this.getClass(); - } - - @Override - public boolean equals(final Object obj) { - return obj instanceof DefineClassPermission; - } - - @Override - public int hashCode() { - return getClass().hashCode(); - } - - @Override - public String getActions() { - return ""; - } -} diff --git a/cpu/src/main/java/io/smallrye/common/cpu/CacheInfo.java b/cpu/src/main/java/io/smallrye/common/cpu/CacheInfo.java index bec0ecfb..541b396b 100644 --- a/cpu/src/main/java/io/smallrye/common/cpu/CacheInfo.java +++ b/cpu/src/main/java/io/smallrye/common/cpu/CacheInfo.java @@ -7,8 +7,6 @@ import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Locale; @@ -81,93 +79,77 @@ public static int getSmallestInstructionCacheLineSize() { } static { - cacheLevels = AccessController.doPrivileged(new PrivilegedAction<>() { - @Override - public CacheLevelInfo[] run() { - if (determineCacheLevel()) { - try { - String osArch = System.getProperty("os.name", "unknown").toLowerCase(Locale.US); - if (osArch.contains("linux")) { - // try to read /sys fs - final File cpu0 = new File("/sys/devices/system/cpu/cpu0/cache"); - if (cpu0.exists()) { - // great! - final File[] files = cpu0.listFiles(); - if (files != null) { - ArrayList indexes = new ArrayList(); - for (File file : files) { - if (file.getName().startsWith("index")) { - indexes.add(file); - } - } - final CacheLevelInfo[] levelInfoArray = new CacheLevelInfo[indexes.size()]; - for (int i = 0; i < indexes.size(); i++) { - File file = indexes.get(i); - int index = parseIntFile(new File(file, "level")); - final CacheType type; - switch (parseStringFile(new File(file, "type"))) { - case "Data": - type = CacheType.DATA; - break; - case "Instruction": - type = CacheType.INSTRUCTION; - break; - case "Unified": - type = CacheType.UNIFIED; - break; - default: - type = CacheType.UNKNOWN; - break; - } - int size = parseIntKBFile(new File(file, "size")); - int lineSize = parseIntFile(new File(file, "coherency_line_size")); - levelInfoArray[i] = new CacheLevelInfo(index, type, size, lineSize); - } - return levelInfoArray; + CacheLevelInfo[] cacheLevelInfos; + if (Boolean.getBoolean("smallrye.cpu.determine-cache-level")) { + try { + String osArch = System.getProperty("os.name", "unknown").toLowerCase(Locale.US); + if (osArch.contains("linux")) { + // try to read /sys fs + final File cpu0 = new File("/sys/devices/system/cpu/cpu0/cache"); + if (cpu0.exists()) { + // great! + final File[] files = cpu0.listFiles(); + if (files != null) { + ArrayList indexes = new ArrayList(); + for (File file : files) { + if (file.getName().startsWith("index")) { + indexes.add(file); } } - } else if (osArch.contains("mac os x")) { - // cache line size - final int lineSize = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.cachelinesize")); - if (lineSize != 0) { - // cache sizes - final int l1d = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l1dcachesize")); - final int l1i = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l1icachesize")); - final int l2 = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l2cachesize")); - final int l3 = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l3cachesize")); - ArrayList list = new ArrayList(); - if (l1d != 0) { - list.add(new CacheLevelInfo(1, CacheType.DATA, l1d / 1024, lineSize)); - } - if (l1i != 0) { - list.add(new CacheLevelInfo(1, CacheType.INSTRUCTION, l1i / 1024, lineSize)); - } - if (l2 != 0) { - list.add(new CacheLevelInfo(2, CacheType.UNIFIED, l2 / 1024, lineSize)); - } - if (l3 != 0) { - list.add(new CacheLevelInfo(3, CacheType.UNIFIED, l3 / 1024, lineSize)); - } - if (list.size() > 0) { - return list.toArray(new CacheLevelInfo[list.size()]); - } + final CacheLevelInfo[] levelInfoArray = new CacheLevelInfo[indexes.size()]; + for (int i = 0; i < indexes.size(); i++) { + File file = indexes.get(i); + int index = parseIntFile(new File(file, "level")); + final CacheType type = switch (parseStringFile(new File(file, "type"))) { + case "Data" -> CacheType.DATA; + case "Instruction" -> CacheType.INSTRUCTION; + case "Unified" -> CacheType.UNIFIED; + default -> CacheType.UNKNOWN; + }; + int size = parseIntKBFile(new File(file, "size")); + int lineSize = parseIntFile(new File(file, "coherency_line_size")); + levelInfoArray[i] = new CacheLevelInfo(index, type, size, lineSize); } - } else if (osArch.contains("windows")) { - // TODO: use the wmic utility to get cache line info + cacheLevelInfos = levelInfoArray; } - } catch (Throwable ignored) { } - // all has failed - return new CacheLevelInfo[0]; - } else { - return new CacheLevelInfo[] { new CacheLevelInfo(0, CacheType.UNKNOWN, 0, 64) }; + } else if (osArch.contains("mac os x")) { + // cache line size + final int lineSize = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.cachelinesize")); + if (lineSize != 0) { + // cache sizes + final int l1d = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l1dcachesize")); + final int l1i = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l1icachesize")); + final int l2 = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l2cachesize")); + final int l3 = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l3cachesize")); + ArrayList list = new ArrayList(); + if (l1d != 0) { + list.add(new CacheLevelInfo(1, CacheType.DATA, l1d / 1024, lineSize)); + } + if (l1i != 0) { + list.add(new CacheLevelInfo(1, CacheType.INSTRUCTION, l1i / 1024, lineSize)); + } + if (l2 != 0) { + list.add(new CacheLevelInfo(2, CacheType.UNIFIED, l2 / 1024, lineSize)); + } + if (l3 != 0) { + list.add(new CacheLevelInfo(3, CacheType.UNIFIED, l3 / 1024, lineSize)); + } + if (!list.isEmpty()) { + cacheLevelInfos = list.toArray(new CacheLevelInfo[0]); + } + } + } else if (osArch.contains("windows")) { + // TODO: use the wmic utility to get cache line info } + } catch (Throwable ignored) { } - }); - } - - private static boolean determineCacheLevel() { - return Boolean.parseBoolean(System.getProperty("smallrye.cpu.determine-cache-level", "false")); + // all has failed + cacheLevelInfos = new CacheLevelInfo[0]; + } else { + cacheLevelInfos = new CacheLevelInfo[] { new CacheLevelInfo(0, CacheType.UNKNOWN, 0, 64) }; + } + cacheLevels = cacheLevelInfos; } static int parseIntFile(final File file) { diff --git a/net/src/main/java/io/smallrye/common/net/GetHostInfoAction.java b/net/src/main/java/io/smallrye/common/net/GetHostInfoAction.java deleted file mode 100644 index fc773148..00000000 --- a/net/src/main/java/io/smallrye/common/net/GetHostInfoAction.java +++ /dev/null @@ -1,60 +0,0 @@ -package io.smallrye.common.net; - -import java.net.UnknownHostException; -import java.security.PrivilegedAction; -import java.util.regex.Pattern; - -final class GetHostInfoAction implements PrivilegedAction { - GetHostInfoAction() { - } - - public String[] run() { - // allow host name to be overridden - String qualifiedHostName = System.getProperty("jboss.qualified.host.name"); - String providedHostName = System.getProperty("jboss.host.name"); - String providedNodeName = System.getProperty("jboss.node.name"); - if (qualifiedHostName == null) { - // if host name is specified, don't pick a qualified host name that isn't related to it - qualifiedHostName = providedHostName; - if (qualifiedHostName == null) { - // POSIX-like OSes including Mac should have this set - qualifiedHostName = System.getenv("HOSTNAME"); - } - if (qualifiedHostName == null) { - // Certain versions of Windows - qualifiedHostName = System.getenv("COMPUTERNAME"); - } - if (qualifiedHostName == null) { - try { - qualifiedHostName = HostName.getLocalHost().getHostName(); - } catch (UnknownHostException e) { - qualifiedHostName = null; - } - } - if (qualifiedHostName != null - && Pattern.compile("^\\d+\\.\\d+\\.\\d+\\.\\d+$|:").matcher(qualifiedHostName).find()) { - // IP address is not acceptable - qualifiedHostName = null; - } - if (qualifiedHostName == null) { - // Give up - qualifiedHostName = "unknown-host.unknown-domain"; - } else { - qualifiedHostName = qualifiedHostName.trim().toLowerCase(); - } - } - if (providedHostName == null) { - // Use the host part of the qualified host name - final int idx = qualifiedHostName.indexOf('.'); - providedHostName = idx == -1 ? qualifiedHostName : qualifiedHostName.substring(0, idx); - } - if (providedNodeName == null) { - providedNodeName = providedHostName; - } - return new String[] { - providedHostName, - qualifiedHostName, - providedNodeName - }; - } -} diff --git a/net/src/main/java/io/smallrye/common/net/HostName.java b/net/src/main/java/io/smallrye/common/net/HostName.java index a5d4e508..51cc0a77 100644 --- a/net/src/main/java/io/smallrye/common/net/HostName.java +++ b/net/src/main/java/io/smallrye/common/net/HostName.java @@ -2,7 +2,7 @@ import java.net.InetAddress; import java.net.UnknownHostException; -import java.security.AccessController; +import java.util.regex.Pattern; import io.smallrye.common.constraint.Assert; @@ -19,7 +19,7 @@ public final class HostName { private static volatile String nodeName; static { - String[] names = AccessController.doPrivileged(new GetHostInfoAction()); + String[] names = resolveHosts(); hostName = names[0]; qualifiedHostName = names[1]; nodeName = names[2]; @@ -90,4 +90,55 @@ public static void setNodeName(final String nodeName) { Assert.checkNotNullParam("nodeName", nodeName); HostName.nodeName = nodeName; } + + private static String[] resolveHosts() { + // allow host name to be overridden + String qualifiedHostName = System.getProperty("jboss.qualified.host.name"); + String providedHostName = System.getProperty("jboss.host.name"); + String providedNodeName = System.getProperty("jboss.node.name"); + if (qualifiedHostName == null) { + // if host name is specified, don't pick a qualified host name that isn't related to it + qualifiedHostName = providedHostName; + if (qualifiedHostName == null) { + // POSIX-like OSes including Mac should have this set + qualifiedHostName = System.getenv("HOSTNAME"); + } + if (qualifiedHostName == null) { + // Certain versions of Windows + qualifiedHostName = System.getenv("COMPUTERNAME"); + } + if (qualifiedHostName == null) { + try { + qualifiedHostName = HostName.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + qualifiedHostName = null; + } + } + if (qualifiedHostName != null + && Pattern.compile("^\\d+\\.\\d+\\.\\d+\\.\\d+$|:").matcher(qualifiedHostName).find()) { + // IP address is not acceptable + qualifiedHostName = null; + } + if (qualifiedHostName == null) { + // Give up + qualifiedHostName = "unknown-host.unknown-domain"; + } else { + qualifiedHostName = qualifiedHostName.trim().toLowerCase(); + } + } + if (providedHostName == null) { + // Use the host part of the qualified host name + final int idx = qualifiedHostName.indexOf('.'); + providedHostName = idx == -1 ? qualifiedHostName : qualifiedHostName.substring(0, idx); + } + if (providedNodeName == null) { + providedNodeName = providedHostName; + } + return new String[] { + providedHostName, + qualifiedHostName, + providedNodeName + }; + } + } diff --git a/net/src/main/java/io/smallrye/common/net/Inet.java b/net/src/main/java/io/smallrye/common/net/Inet.java index beb9c9f5..79df3166 100644 --- a/net/src/main/java/io/smallrye/common/net/Inet.java +++ b/net/src/main/java/io/smallrye/common/net/Inet.java @@ -13,8 +13,6 @@ import java.net.URISyntaxException; import java.net.UnknownHostException; import java.nio.ByteBuffer; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.util.Enumeration; import java.util.regex.Pattern; @@ -1034,21 +1032,20 @@ public static int getScopeId(NetworkInterface networkInterface) { public static int getScopeId(NetworkInterface networkInterface, InetAddress compareWith) { Assert.checkNotNullParam("networkInterface", networkInterface); Inet6Address cw6 = compareWith instanceof Inet6Address ? (Inet6Address) compareWith : null; - Inet6Address address = AccessController.doPrivileged((PrivilegedAction) () -> { - final Enumeration addresses = networkInterface.getInetAddresses(); - while (addresses.hasMoreElements()) { - final InetAddress a = addresses.nextElement(); - if (a instanceof Inet6Address) { - final Inet6Address a6 = (Inet6Address) a; - if (cw6 == null || - a6.isLinkLocalAddress() == cw6.isLinkLocalAddress() && - a6.isSiteLocalAddress() == cw6.isSiteLocalAddress()) { - return a6; - } + Inet6Address address = null; + final Enumeration addresses = networkInterface.getInetAddresses(); + while (addresses.hasMoreElements()) { + final InetAddress a = addresses.nextElement(); + if (a instanceof Inet6Address) { + final Inet6Address a6 = (Inet6Address) a; + if (cw6 == null || + a6.isLinkLocalAddress() == cw6.isLinkLocalAddress() && + a6.isSiteLocalAddress() == cw6.isSiteLocalAddress()) { + address = a6; + break; } } - return null; - }); + } return address == null ? 0 : address.getScopeId(); } diff --git a/os/src/main/java/io/smallrye/common/os/GetAllProcessesInfoAction.java b/os/src/main/java/io/smallrye/common/os/GetAllProcessesInfoAction.java deleted file mode 100644 index d16de967..00000000 --- a/os/src/main/java/io/smallrye/common/os/GetAllProcessesInfoAction.java +++ /dev/null @@ -1,15 +0,0 @@ -package io.smallrye.common.os; - -import java.security.PrivilegedAction; -import java.util.List; -import java.util.stream.Collectors; - -final class GetAllProcessesInfoAction implements PrivilegedAction> { - - @Override - public List run() { - return ProcessHandle.allProcesses() - .map(processHandle -> new ProcessInfo(processHandle.pid(), processHandle.info().command().orElse(null))) - .collect(Collectors.toList()); - } -} diff --git a/os/src/main/java/io/smallrye/common/os/Process.java b/os/src/main/java/io/smallrye/common/os/Process.java index 156576b4..6660e961 100644 --- a/os/src/main/java/io/smallrye/common/os/Process.java +++ b/os/src/main/java/io/smallrye/common/os/Process.java @@ -18,9 +18,8 @@ package io.smallrye.common.os; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.util.List; +import java.util.stream.Collectors; /** * Utilities for getting information about the current process. @@ -29,10 +28,9 @@ */ @SuppressWarnings("removal") public final class Process { - private static final ProcessHandle current = AccessController - .doPrivileged((PrivilegedAction) ProcessHandle::current); + private static final ProcessHandle current = ProcessHandle.current(); private static final ProcessHandle.Info currentInfo = current.info(); - private static final String name = AccessController.doPrivileged((PrivilegedAction) Process::computeProcessName); + private static final String name = Process.computeProcessName(); private Process() { } @@ -89,6 +87,8 @@ public static ProcessInfo getCurrentProcess() { */ @Deprecated(since = "2.4", forRemoval = true) public static List getAllProcesses() { - return AccessController.doPrivileged(new GetAllProcessesInfoAction()); + return ProcessHandle.allProcesses() + .map(processHandle -> new ProcessInfo(processHandle.pid(), processHandle.info().command().orElse(null))) + .collect(Collectors.toList()); } } diff --git a/ref/src/main/java/io/smallrye/common/ref/References.java b/ref/src/main/java/io/smallrye/common/ref/References.java index fd8a1491..beb02e98 100644 --- a/ref/src/main/java/io/smallrye/common/ref/References.java +++ b/ref/src/main/java/io/smallrye/common/ref/References.java @@ -1,8 +1,6 @@ package io.smallrye.common.ref; import java.lang.ref.ReferenceQueue; -import java.security.AccessController; -import java.security.PrivilegedAction; import org.graalvm.nativeimage.ImageInfo; @@ -31,12 +29,7 @@ static ReferenceQueue getReaperQueue() { if (isBuildTime()) { // do nothing (class should be reinitialized) } else { - AccessController.doPrivileged(new PrivilegedAction() { - @Override - public Void run() { - return startThreadAction(1); - } - }); + startThreadAction(1); } } @@ -48,13 +41,12 @@ private static boolean isBuildTime() { } } - private static Void startThreadAction(int id) { + private static void startThreadAction(int id) { final ReaperThread thr = new ReaperThread(); // avoid + because of startup cost of StringConcatFactory thr.setName("Reference Reaper #".concat(String.valueOf(id))); thr.setDaemon(true); thr.start(); - return null; } public void run() {