Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/ResourceManager.java
38829 views
/*1* Copyright (c) 2011, 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 = java.security.AccessController.doPrivileged(56new GetPropertyAction("sun.net.maxDatagramSockets")57);58int defmax = DEFAULT_MAX_SOCKETS;59try {60if (prop != null) {61defmax = Integer.parseInt(prop);62}63} catch (NumberFormatException e) {}64maxSockets = defmax;65numSockets = new AtomicInteger(0);66}6768public 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}7677public static void afterUdpClose() {78if (System.getSecurityManager() != null) {79numSockets.decrementAndGet();80}81}82}838485