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