View Javadoc
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   * @since 1.2
27   */
28  public class NoExitSecurityManager extends SecurityManager {
29  
30      /**
31       * The parent SecurityManager.
32       */
33      private final SecurityManager parent;
34  
35      /**
36       * Construct a new NoExitSecurityManager from the parent.
37       *
38       * @param newParent the parent to set
39       */
40      public NoExitSecurityManager(final SecurityManager newParent) {
41          parent = newParent;
42      }
43  
44      /**
45       * Construct a new NoExitSecurityManager, using the System SecurityManager as the parent.
46       */
47      public NoExitSecurityManager() {
48          this(System.getSecurityManager());
49      }
50  
51      /**
52       * Check the given Permission.
53       *
54       * @param permission the Permission to check
55       */
56      public void checkPermission(final Permission permission) {
57          if (parent != null) {
58              parent.checkPermission(permission);
59          }
60      }
61  
62      /**
63       * Always throws {@link SecurityException}.
64       *
65       * @param code the exit code that is completely ignored
66       */
67      public void checkExit(final int code) {
68          throw new SecurityException("Use of System.exit() is forbidden!");
69      }
70  
71  }