1 package org.codehaus.gmavenplus.util;
2
3 import java.security.Permission;
4
5
6 /**
7 * Custom security manager to {@link System#exit} (and related) from being used.
8 *
9 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
10 */
11 public class NoExitSecurityManager extends SecurityManager {
12
13 /**
14 * The parent SecurityManager.
15 */
16 private final SecurityManager parent;
17
18 /**
19 * Construct a new NoExitSecurityManager from the parent.
20 *
21 * @param newParent the parent to set
22 */
23 public NoExitSecurityManager(final SecurityManager newParent) {
24 parent = newParent;
25 }
26
27 /**
28 * Construct a new NoExitSecurityManager, using the System SecurityManager as the parent.
29 */
30 public NoExitSecurityManager() {
31 this(System.getSecurityManager());
32 }
33
34 /**
35 * Check the given Permission.
36 *
37 * @param permission the Permission to check
38 */
39 public void checkPermission(final Permission permission) {
40 if (parent != null) {
41 parent.checkPermission(permission);
42 }
43 }
44
45 /**
46 * Always throws {@link SecurityException}.
47 *
48 * @param code the exit code that is completely ignored
49 */
50 public void checkExit(final int code) {
51 throw new SecurityException("Use of System.exit() is forbidden!");
52 }
53
54 }