1 /*
2 * Copyright 2014 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * You may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.codehaus.gmavenplus.util;
18
19 import java.security.Permission;
20
21
22 /**
23 * Custom security manager to {@link System#exit} (and related) from being used.
24 *
25 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
26 */
27 public class NoExitSecurityManager extends SecurityManager {
28
29 /**
30 * The parent SecurityManager.
31 */
32 private final SecurityManager parent;
33
34 /**
35 * Construct a new NoExitSecurityManager from the parent.
36 *
37 * @param newParent the parent to set
38 */
39 public NoExitSecurityManager(final SecurityManager newParent) {
40 parent = newParent;
41 }
42
43 /**
44 * Construct a new NoExitSecurityManager, using the System SecurityManager as the parent.
45 */
46 public NoExitSecurityManager() {
47 this(System.getSecurityManager());
48 }
49
50 /**
51 * Check the given Permission.
52 *
53 * @param permission the Permission to check
54 */
55 public void checkPermission(final Permission permission) {
56 if (parent != null) {
57 parent.checkPermission(permission);
58 }
59 }
60
61 /**
62 * Always throws {@link SecurityException}.
63 *
64 * @param code the exit code that is completely ignored
65 */
66 public void checkExit(final int code) {
67 throw new SecurityException("Use of System.exit() is forbidden!");
68 }
69
70 }