001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020package org.apache.hadoop.hbase.util; 021 022import java.security.Permission; 023 024/** 025 * class for masquerading System.exit(int). 026 * Use for test main method with System.exit(int ) 027 * usage: 028 * new LauncherSecurityManager(); 029 * try { 030 * CellCounter.main(args); 031 * fail("should be exception"); 032 * } catch (SecurityException e) { 033 * assert(.,e.getExitCode()); 034 * } 035 */ 036public class LauncherSecurityManager extends SecurityManager { 037 038 private int exitCode; 039 private SecurityManager securityManager; 040 041 public LauncherSecurityManager() { 042 reset(); 043 } 044 045 @Override 046 public void checkPermission(Permission perm, Object context) { 047 if (securityManager != null) { 048 // check everything with the original SecurityManager 049 securityManager.checkPermission(perm, context); 050 } 051 } 052 053 @Override 054 public void checkPermission(Permission perm) { 055 if (securityManager != null) { 056 // check everything with the original SecurityManager 057 securityManager.checkPermission(perm); 058 } 059 } 060 061 @Override 062 public void checkExit(int status) throws SecurityException { 063 exitCode = status; 064 throw new SecurityException("Intercepted System.exit(" + status + ")"); 065 } 066 067 public int getExitCode() { 068 return exitCode; 069 } 070 071 public void reset() { 072 exitCode = 0; 073 } 074 075}