All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.helger.commons.lang.priviledged.IPrivilegedAction Maven / Gradle / Ivy

There is a newer version: 9.5.5
Show newest version
/**
 * Copyright (C) 2014-2016 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.helger.commons.lang.priviledged;

import java.security.PrivilegedAction;
import java.util.Properties;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.helger.commons.ValueEnforcer;

/**
 * Extension to {@link PrivilegedAction} with an {@link #invokeSafe()} method
 * that invokes the {@link java.security.AccessController} only if a security
 * manager is present.
 *
 * @author Philip Helger
 * @param 
 *        The action return type.
 */
@FunctionalInterface
public interface IPrivilegedAction  extends PrivilegedAction 
{
  @Nullable
  default T invokeSafe ()
  {
    if (System.getSecurityManager () == null)
    {
      // No need to use AccessController
      return run ();
    }
    return AccessControllerHelper.call (this);
  }

  @Nonnull
  static IPrivilegedAction  classLoaderGetParent (@Nonnull final ClassLoader aBaseClassLoader)
  {
    return () -> aBaseClassLoader.getParent ();
  }

  @Nonnull
  static IPrivilegedAction  getClassLoader (@Nonnull final Class  aClass)
  {
    return () -> aClass.getClassLoader ();
  }

  @Nonnull
  static IPrivilegedAction  getContextClassLoader ()
  {
    return () -> Thread.currentThread ().getContextClassLoader ();
  }

  @Nonnull
  static IPrivilegedAction  setContextClassLoader (@Nonnull final ClassLoader aClassLoader)
  {
    ValueEnforcer.notNull (aClassLoader, "ClassLoader");
    return () -> {
      Thread.currentThread ().setContextClassLoader (aClassLoader);
      return null;
    };
  }

  @Nonnull
  static IPrivilegedAction  getSystemClassLoader ()
  {
    return () -> ClassLoader.getSystemClassLoader ();
  }

  @Nonnull
  static IPrivilegedAction  systemClearProperty (@Nonnull final String sKey)
  {
    ValueEnforcer.notNull (sKey, "Key");
    return () -> System.clearProperty (sKey);
  }

  @Nonnull
  static IPrivilegedAction  systemGetProperty (@Nonnull final String sKey)
  {
    ValueEnforcer.notNull (sKey, "Key");
    return () -> System.getProperty (sKey);
  }

  @Nonnull
  static IPrivilegedAction  systemGetProperties ()
  {
    return () -> System.getProperties ();
  }

  @Nonnull
  static IPrivilegedAction  systemSetProperty (@Nonnull final String sKey, @Nonnull final String sValue)
  {
    ValueEnforcer.notNull (sKey, "Key");
    ValueEnforcer.notNull (sValue, "Value");
    return () -> System.setProperty (sKey, sValue);
  }
}