Example usage for java.security PrivilegedAction run

List of usage examples for java.security PrivilegedAction run

Introduction

In this page you can find the example usage for java.security PrivilegedAction run.

Prototype

T run();

Source Link

Document

Performs the computation.

Usage

From source file:com.buaa.cfs.utils.SecurityUtil.java

/**
 * Perform the given action as the daemon's login user. If the login user cannot be determined, this will log a
 * FATAL error and exit the whole JVM.// w w  w .  j  ava2  s. co m
 */
public static <T> T doAsLoginUserOrFatal(PrivilegedAction<T> action) {
    if (UserGroupInformation.isSecurityEnabled()) {
        UserGroupInformation ugi = null;
        try {
            ugi = UserGroupInformation.getLoginUser();
        } catch (IOException e) {
            LOG.fatal("Exception while getting login user", e);
            e.printStackTrace();
            Runtime.getRuntime().exit(-1);
        }
        return ugi.doAs(action);
    } else {
        return action.run();
    }
}

From source file:com.adito.activedirectory.ActiveDirectoryUserDatabaseConfiguration.java

public Object doAs(PrivilegedAction<?> action) throws UserDatabaseException {
    Object result = null;//www  . ja v  a 2 s.  c om
    if (isServiceAuthenticationGssApi()) {
        try {
            LoginContext context = getServiceAccountLoginContext();
            result = Subject.doAs(context.getSubject(), action);
            logoutContext(context);
        } catch (Exception e) {
            logger.error("Failure to create Login Context", e);
            throw new UserDatabaseException("", e);
        }
    } else {
        result = action.run();
    }

    if (result instanceof Throwable) {
        Throwable e = (Throwable) result;
        logger.error("Failure to doAs", e);
        throw new UserDatabaseException("", e);
    }
    return result;
}

From source file:org.apache.ranger.audit.provider.MiscUtil.java

/**
 * Execute the {@link PrivilegedAction} on the {@link UserGroupInformation} if it's set, otherwise call it directly.
 *///from  ww  w .j a  va2 s  .  co m
public static <X> X executePrivilegedAction(final PrivilegedAction<X> action) {
    final UserGroupInformation ugi = getUGILoginUser();
    if (ugi != null) {
        return ugi.doAs(action);
    } else {
        return action.run();
    }
}

From source file:org.apache.tez.dag.history.logging.proto.DagManifesFileScanner.java

private ProtoMessageReader<ManifestEntryProto> getNextReader() throws IOException {
    FileStatus status = newFiles.remove(0);
    PrivilegedAction<ProtoMessageReader<ManifestEntryProto>> action = () -> {
        try {//from   w  w w .  ja v  a 2s.  com
            return manifestLogger.getReader(status.getPath());
        } catch (IOException e) {
            String path = status.getPath().toString();
            LOG.error("Error trying to open file: {}", path, e);
            incrementError(path);
            return null;
        }
    };
    if (withDoas) {
        UserGroupInformation proxyUser = UserGroupInformation.createProxyUser(status.getOwner(),
                UserGroupInformation.getCurrentUser());
        return proxyUser.doAs(action);
    } else {
        return action.run();
    }
}

From source file:org.codehaus.groovy.grails.web.pages.discovery.CachingGrailsConventionGroovyPageLocator.java

protected GroovyPageScriptSource lookupCache(final GroovyPageLocatorCacheKey cacheKey,
        PrivilegedAction<GroovyPageScriptSource> updater) {
    GroovyPageScriptSource scriptSource = null;
    if (cacheTimeout == 0) {
        scriptSource = updater.run();
    } else {/*w w w . j  av a  2 s .com*/
        CacheEntry<GroovyPageScriptSource> entry = uriResolveCache.get(cacheKey);
        if (entry == null) {
            scriptSource = updater.run();
            uriResolveCache.put(cacheKey, new CustomCacheEntry<GroovyPageScriptSource>(scriptSource));
        } else {
            scriptSource = entry.getValue(cacheTimeout, updater);
        }
    }
    return scriptSource == NULL_SCRIPT ? null : scriptSource;
}

From source file:org.codehaus.groovy.grails.web.pages.GroovyPageMetaInfo.java

public Resource checkIfReloadableResourceHasChanged(final PrivilegedAction<Resource> resourceCallable) {
    PrivilegedAction<Resource> checkerCallable = new PrivilegedAction<Resource>() {
        public Resource run() {
            Resource resource = resourceCallable.run();
            if (resource != null && resource.exists()) {
                long currentLastmodified = establishLastModified(resource);
                // granularity is required since lastmodified information is rounded some where in copying & war (zip) file information
                // usually the lastmodified time is 1000L apart in files and in files extracted from the zip (war) file
                if (currentLastmodified > 0
                        && Math.abs(currentLastmodified - lastModified) > LASTMODIFIED_CHECK_GRANULARITY) {
                    return resource;
                }/* w ww. j  a  v a 2s .c  om*/
            }
            return null;
        }
    };
    return shouldReloadCacheEntry.getValue(LASTMODIFIED_CHECK_INTERVAL, checkerCallable);
}

From source file:org.codehaus.groovy.grails.web.servlet.view.GrailsViewResolver.java

@Override
protected View loadView(String viewName, Locale locale) throws Exception {
    Assert.notNull(templateEngine, "Property [templateEngine] cannot be null");
    if (viewName.endsWith(GSP_SUFFIX)) {
        viewName = viewName.substring(0, viewName.length() - GSP_SUFFIX.length());
    }// w  w  w  .j  av a  2  s .c  om

    if (developmentMode) {
        return createGrailsView(viewName);
    }

    String viewCacheKey = groovyPageLocator.resolveViewFormat(viewName);

    CacheEntry<View> entry = VIEW_CACHE.get(viewCacheKey);

    final String lookupViewName = viewName;
    PrivilegedAction<View> updater = new PrivilegedAction<View>() {
        public View run() {
            try {
                return createGrailsView(lookupViewName);
            } catch (Exception e) {
                throw new WrappedInitializationException(e);
            }
        }
    };

    View view = null;
    if (entry == null) {
        try {
            view = updater.run();
        } catch (WrappedInitializationException e) {
            e.rethrow();
        }
        entry = new CacheEntry<View>(view);
        VIEW_CACHE.put(viewCacheKey, entry);
        return view;
    }

    try {
        view = entry.getValue(cacheTimeout, updater);
    } catch (WrappedInitializationException e) {
        e.rethrow();
    }

    return view;
}

From source file:org.grails.gsp.GroovyPageMetaInfo.java

public Resource checkIfReloadableResourceHasChanged(final PrivilegedAction<Resource> resourceCallable) {
    Callable<Resource> checkerCallable = new Callable<Resource>() {
        public Resource call() {
            Resource resource = resourceCallable.run();
            if (resource != null && resource.exists()) {
                long currentLastmodified = establishLastModified(resource);
                // granularity is required since lastmodified information is rounded some where in copying & war (zip) file information
                // usually the lastmodified time is 1000L apart in files and in files extracted from the zip (war) file
                if (currentLastmodified > 0
                        && Math.abs(currentLastmodified - lastModified) > LASTMODIFIED_CHECK_GRANULARITY) {
                    return resource;
                }/*from  ww w  .ja  va 2  s .  co m*/
            }
            return null;
        }
    };
    return shouldReloadCacheEntry.getValue(LASTMODIFIED_CHECK_INTERVAL, checkerCallable, true, null);
}