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, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.weaver.privilizer; 020 021import java.security.PrivilegedAction; 022import java.util.Locale; 023 024import org.apache.commons.lang3.StringUtils; 025 026/** 027 * Weaving policy: when to use {@link PrivilegedAction}s. 028 */ 029public enum Policy { 030 /** 031 * Disables weaving. 032 */ 033 NEVER, 034 035 /** 036 * Weaves such that the check for an active {@link SecurityManager} is done once only. 037 */ 038 ON_INIT, 039 040 /** 041 * Weaves such that the check for an active {@link SecurityManager} is done for each {@link Privileged} method 042 * execution. 043 */ 044 DYNAMIC, 045 046 /** 047 * Weaves such that {@link Privileged} methods are always executed as such. 048 */ 049 ALWAYS; 050 051 /** 052 * Get the {@link Policy} value that should be used as a default. 053 * @return {@link Policy#DYNAMIC} 054 */ 055 public static Policy defaultValue() { 056 return DYNAMIC; 057 } 058 059 /** 060 * Parse from a {@link String} returning {@link #defaultValue()} for blank/null input. 061 * @param str to parse 062 * @return {@link Policy} 063 */ 064 public static Policy parse(final String str) { 065 if (StringUtils.isBlank(str)) { 066 return defaultValue(); 067 } 068 return valueOf(str.trim().toUpperCase(Locale.US)); 069 } 070 071 /** 072 * Learn whether this is a conditional {@link Policy}. 073 * @return {@code this == ON_INIT || this == DYNAMIC} 074 */ 075 public boolean isConditional() { 076 return this == ON_INIT || this == DYNAMIC; 077 } 078 079}