Search in sources :

Example 1 with AuthorizationContext

use of co.cask.cdap.security.spi.authorization.AuthorizationContext in project cdap by caskdata.

the class AuthorizerInstantiator method createAndInitializeAuthorizerInstance.

/**
   * Creates a new instance of the configured {@link Authorizer} extension, based on the provided extension jar
   * file.
   *
   * @return a new instance of the configured {@link Authorizer} extension
   */
private Authorizer createAndInitializeAuthorizerInstance(File authorizerExtensionJar) throws IOException, InvalidAuthorizerException {
    Class<? extends Authorizer> authorizerClass = loadAuthorizerClass(authorizerExtensionJar);
    // Set the context class loader to the AuthorizerClassLoader before creating a new instance of the extension,
    // so all classes required in this process are created from the AuthorizerClassLoader.
    ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(authorizerClassLoader);
    LOG.debug("Setting context classloader to {}. Old classloader was {}.", authorizerClassLoader, oldClassLoader);
    try {
        Authorizer authorizer;
        try {
            authorizer = instantiatorFactory.get(TypeToken.of(authorizerClass)).create();
        } catch (Exception e) {
            throw new InvalidAuthorizerException(String.format("Error while instantiating for authorizer extension %s. Please make sure that the extension " + "is a public class with a default constructor.", authorizerClass.getName()), e);
        }
        AuthorizationContext context = authorizationContextFactory.create(createExtensionProperties());
        try {
            authorizer.initialize(context);
        } catch (Exception e) {
            throw new InvalidAuthorizerException(String.format("Error while initializing authorizer extension %s.", authorizerClass.getName()), e);
        }
        return authorizer;
    } finally {
        // After the process of creation of a new instance has completed (success or failure), reset the context
        // classloader back to the original class loader.
        ClassLoaders.setContextClassLoader(oldClassLoader);
        LOG.debug("Resetting context classloader to {} from {}.", oldClassLoader, authorizerClassLoader);
    }
}
Also used : NoOpAuthorizer(co.cask.cdap.security.spi.authorization.NoOpAuthorizer) Authorizer(co.cask.cdap.security.spi.authorization.Authorizer) AuthorizationContext(co.cask.cdap.security.spi.authorization.AuthorizationContext) ZipException(java.util.zip.ZipException) IOException(java.io.IOException)

Aggregations

AuthorizationContext (co.cask.cdap.security.spi.authorization.AuthorizationContext)1 Authorizer (co.cask.cdap.security.spi.authorization.Authorizer)1 NoOpAuthorizer (co.cask.cdap.security.spi.authorization.NoOpAuthorizer)1 IOException (java.io.IOException)1 ZipException (java.util.zip.ZipException)1