Path: blob/master/src/java.base/share/classes/sun/net/ResourceManager.java
41152 views
/*1* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net;2627import java.net.SocketException;28import java.util.concurrent.atomic.AtomicInteger;29import sun.security.action.GetPropertyAction;3031/**32* Manages count of total number of UDP sockets and ensures33* that exception is thrown if we try to create more than the34* configured limit.35*36* This functionality could be put in NetHooks some time in future.37*/3839public class ResourceManager {4041/* default maximum number of udp sockets per VM42* when a security manager is enabled.43* The default is 25 which is high enough to be useful44* but low enough to be well below the maximum number45* of port numbers actually available on all OSes46* when multiplied by the maximum feasible number of VM processes47* that could practically be spawned.48*/4950private static final int DEFAULT_MAX_SOCKETS = 25;51private static final int maxSockets;52private static final AtomicInteger numSockets;5354static {55String prop = GetPropertyAction56.privilegedGetProperty("sun.net.maxDatagramSockets");57int defmax = DEFAULT_MAX_SOCKETS;58try {59if (prop != null) {60defmax = Integer.parseInt(prop);61}62} catch (NumberFormatException e) {}63maxSockets = defmax;64numSockets = new AtomicInteger();65}6667@SuppressWarnings("removal")68public static void beforeUdpCreate() throws SocketException {69if (System.getSecurityManager() != null) {70if (numSockets.incrementAndGet() > maxSockets) {71numSockets.decrementAndGet();72throw new SocketException("maximum number of DatagramSockets reached");73}74}75}7677@SuppressWarnings("removal")78public static void afterUdpClose() {79if (System.getSecurityManager() != null) {80numSockets.decrementAndGet();81}82}83}848586