36 *
37 * This functionality could be put in NetHooks some time in future.
38 */
39
40 public class ResourceManager {
41
42 /* default maximum number of udp sockets per VM
43 * when a security manager is enabled.
44 * The default is 25 which is high enough to be useful
45 * but low enough to be well below the maximum number
46 * of port numbers actually available on all OSes
47 * when multiplied by the maximum feasible number of VM processes
48 * that could practically be spawned.
49 */
50
51 private static final int DEFAULT_MAX_SOCKETS = 25;
52 private static final int maxSockets;
53 private static final AtomicInteger numSockets;
54
55 static {
56 String prop = java.security.AccessController.doPrivileged(
57 new GetPropertyAction("sun.net.maxDatagramSockets")
58 );
59 int defmax = DEFAULT_MAX_SOCKETS;
60 try {
61 if (prop != null) {
62 defmax = Integer.parseInt(prop);
63 }
64 } catch (NumberFormatException e) {}
65 maxSockets = defmax;
66 numSockets = new AtomicInteger(0);
67 }
68
69 public static void beforeUdpCreate() throws SocketException {
70 if (System.getSecurityManager() != null) {
71 if (numSockets.incrementAndGet() > maxSockets) {
72 numSockets.decrementAndGet();
73 throw new SocketException("maximum number of DatagramSockets reached");
74 }
75 }
76 }
77
78 public static void afterUdpClose() {
|
36 *
37 * This functionality could be put in NetHooks some time in future.
38 */
39
40 public class ResourceManager {
41
42 /* default maximum number of udp sockets per VM
43 * when a security manager is enabled.
44 * The default is 25 which is high enough to be useful
45 * but low enough to be well below the maximum number
46 * of port numbers actually available on all OSes
47 * when multiplied by the maximum feasible number of VM processes
48 * that could practically be spawned.
49 */
50
51 private static final int DEFAULT_MAX_SOCKETS = 25;
52 private static final int maxSockets;
53 private static final AtomicInteger numSockets;
54
55 static {
56 String prop =
57 GetPropertyAction.getProperty("sun.net.maxDatagramSockets");
58 int defmax = DEFAULT_MAX_SOCKETS;
59 try {
60 if (prop != null) {
61 defmax = Integer.parseInt(prop);
62 }
63 } catch (NumberFormatException e) {}
64 maxSockets = defmax;
65 numSockets = new AtomicInteger(0);
66 }
67
68 public static void beforeUdpCreate() throws SocketException {
69 if (System.getSecurityManager() != null) {
70 if (numSockets.incrementAndGet() > maxSockets) {
71 numSockets.decrementAndGet();
72 throw new SocketException("maximum number of DatagramSockets reached");
73 }
74 }
75 }
76
77 public static void afterUdpClose() {
|