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