3788N/A/*
3788N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3788N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3788N/A *
3788N/A * This code is free software; you can redistribute it and/or modify it
3788N/A * under the terms of the GNU General Public License version 2 only, as
3788N/A * published by the Free Software Foundation. Oracle designates this
3788N/A * particular file as subject to the "Classpath" exception as provided
3788N/A * by Oracle in the LICENSE file that accompanied this code.
3788N/A *
3788N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3788N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3788N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3788N/A * version 2 for more details (a copy is included in the LICENSE file that
3788N/A * accompanied this code).
3788N/A *
3788N/A * You should have received a copy of the GNU General Public License version
3788N/A * 2 along with this work; if not, write to the Free Software Foundation,
3788N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3788N/A *
3788N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3788N/A * or visit www.oracle.com if you need additional information or have any
3788N/A * questions.
3788N/A */
3788N/A
3788N/Apackage sun.net;
3788N/A
3788N/Aimport java.net.SocketException;
3788N/Aimport java.util.concurrent.atomic.AtomicInteger;
3788N/Aimport sun.security.action.GetPropertyAction;
3788N/A
3788N/A/**
3788N/A * Manages count of total number of UDP sockets and ensures
3788N/A * that exception is thrown if we try to create more than the
3788N/A * configured limit.
3788N/A *
3788N/A * This functionality could be put in NetHooks some time in future.
3788N/A */
3788N/A
3788N/Apublic class ResourceManager {
3788N/A
3788N/A /* default maximum number of udp sockets per VM
3788N/A * when a security manager is enabled.
4435N/A * The default is 25 which is high enough to be useful
3788N/A * but low enough to be well below the maximum number
4435N/A * of port numbers actually available on all OSes
4435N/A * when multiplied by the maximum feasible number of VM processes
4435N/A * that could practically be spawned.
3788N/A */
3788N/A
4435N/A private static final int DEFAULT_MAX_SOCKETS = 25;
3788N/A private static final int maxSockets;
3788N/A private static final AtomicInteger numSockets;
3788N/A
3788N/A static {
3788N/A String prop = java.security.AccessController.doPrivileged(
3788N/A new GetPropertyAction("sun.net.maxDatagramSockets")
3788N/A );
3788N/A int defmax = DEFAULT_MAX_SOCKETS;
3788N/A try {
3788N/A if (prop != null) {
3788N/A defmax = Integer.parseInt(prop);
3788N/A }
3788N/A } catch (NumberFormatException e) {}
3788N/A maxSockets = defmax;
3788N/A numSockets = new AtomicInteger(0);
3788N/A }
3788N/A
3788N/A public static void beforeUdpCreate() throws SocketException {
3788N/A if (System.getSecurityManager() != null) {
3788N/A if (numSockets.incrementAndGet() > maxSockets) {
3788N/A numSockets.decrementAndGet();
3788N/A throw new SocketException("maximum number of DatagramSockets reached");
3788N/A }
3788N/A }
3788N/A }
3788N/A
3788N/A public static void afterUdpClose() {
3788N/A if (System.getSecurityManager() != null) {
3788N/A numSockets.decrementAndGet();
3788N/A }
3788N/A }
3788N/A}