Example usage for java.security PrivilegedAction PrivilegedAction

List of usage examples for java.security PrivilegedAction PrivilegedAction

Introduction

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

Prototype

PrivilegedAction

Source Link

Usage

From source file:org.eclipse.gemini.blueprint.test.provisioning.internal.LocalFileSystemMavenRepository.java

/**
 * Initialization method It determines the repository path by checking the
 * existence of <code>localRepository</code> system property and falling
 * back to the <code>settings.xml</code> file and then the traditional
 * <code>user.home/.m2/repository</code>.
 * // www  .java 2  s.  c  o  m
 * <p/> This method is used to postpone initialization until an artifact is
 * actually located. As the test class is instantiated on each test run, the
 * init() method prevents repetitive, waste-less initialization.
 * 
 */
private void init() {
    // already discovered a repository home, bailing out
    if (repositoryHome != null)
        return;

    boolean trace = log.isDebugEnabled();

    final String[] sysProperties = new String[2];
    // check system property
    AccessController.doPrivileged(new PrivilegedAction() {

        public Object run() {
            sysProperties[0] = System.getProperty(SYS_PROPERTY);
            sysProperties[1] = System.getProperty(USER_HOME_PROPERTY);
            return null;
        }
    });
    String localRepository = sysProperties[0];
    String userHome = sysProperties[1];

    if (trace)
        log.trace("M2 system property [" + SYS_PROPERTY + "] has value=" + localRepository);

    if (localRepository == null) {
        // if it's not present then check settings.xml local repository property
        Resource settingsFile = new FileSystemResource(new File(userHome, M2_SETTINGS));
        localRepository = getMavenSettingsLocalRepository(settingsFile);
        if (trace)
            log.trace("Falling back to M2 settings.xml [" + settingsFile + "]; found value=" + localRepository);
        if (localRepository == null) {
            // fall back to the default location
            localRepository = new File(userHome, DEFAULT_DIR).getAbsolutePath();
            if (trace)
                log.trace("No custom setting found; using default M2 local repository=" + localRepository);

        }
    }

    repositoryHome = localRepository;
    log.info("Local Maven2 repository used: [" + repositoryHome + "]");
}

From source file:org.apache.hadoop.yarn.client.GroupMembershipProxyService.java

private void updateFromActiveNodeList() {
    while (!anList.isEmpty()) {
        List<ActiveNode> activeNodes = anList.getActiveNodes();

        final ActiveNode nextNode = activeNodes.get(random.nextInt(activeNodes.size()));
        try {//from   w ww  .ja  v a  2s.c o m

            UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
            if (ugi.getRealUser() != null) {
                ugi = ugi.getRealUser();
            }
            LiveRMsResponse response = ugi.doAs(new PrivilegedAction<LiveRMsResponse>() {
                @Override
                public LiveRMsResponse run() {
                    try {
                        GroupMembership proxy = oldProxies.get(nextNode.getRpcServerAddressForClients());
                        if (proxy == null) {
                            proxy = new GroupMembershipPBClientImpl(1, nextNode.getRpcServerAddressForClients(),
                                    conf);
                            oldProxies.put(nextNode.getRpcServerAddressForClients(), proxy);
                        }

                        return (LiveRMsResponse) proxy.getLiveRMList();
                    } catch (IOException ex) {
                        LOG.warn(ex, ex);
                    } catch (YarnException ex) {
                        LOG.warn(ex, ex);
                    }
                    return null;
                }
            });
            if (response == null) {
                activeNodes.remove(nextNode);
                anList = new SortedActiveRMList(activeNodes);
                continue;
            }
            anList = response.getLiveRMsList();
            return;
        } catch (IOException e) {
            LOG.error(e, e);
        }
    }
    updateFromConfigFile();
}

From source file:edu.ku.brc.dbsupport.SchemaUpdateService.java

/**
 * Returns the instance of the AppContextMgr.
 * @return the instance of the AppContextMgr.
 *//*w w  w. j  a va  2 s. co  m*/
public static SchemaUpdateService getInstance() {
    if (instance != null) {
        return instance;

    }
    // else
    String factoryNameStr = AccessController.doPrivileged(new PrivilegedAction<String>() {
        public String run() {
            return System.getProperty(factoryName);
        }
    });

    if (factoryNameStr != null) {
        try {
            instance = (SchemaUpdateService) Class.forName(factoryNameStr).newInstance();
            return instance;

        } catch (Exception e) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(SchemaUpdateService.class, e);
            InternalError error = new InternalError(
                    "Can't instantiate AppContextMgr factory " + factoryNameStr); //$NON-NLS-1$
            error.initCause(e);
            throw error;
        }
    }
    return null;
}

From source file:org.mobicents.slee.runtime.sbb.SbbObjectPoolFactory.java

public void destroyObject(Object sbb) throws java.lang.Exception {

    if (doTraceLogs) {
        logger.trace("destroyObject() for " + sbbComponent);
    }//from   w ww  . j a  va  2s . c o m

    SbbObject sbbObject = (SbbObject) sbb;
    final ClassLoader oldClassLoader = SleeContainerUtils.getCurrentThreadClassLoader();

    try {
        //unsetSbbContext must be called with the context classloader
        //of the entities sbbDescriptor as with other sbb invocatiions.
        Thread.currentThread().setContextClassLoader(sbbComponent.getClassLoader());
        if (sbbObject.getState() != SbbObjectState.DOES_NOT_EXIST) {
            sbbObject.unsetSbbContext();
        }
    } finally {
        if (System.getSecurityManager() != null)
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
                public Object run() {
                    Thread.currentThread().setContextClassLoader(oldClassLoader);
                    return null;
                }
            });
        else
            Thread.currentThread().setContextClassLoader(oldClassLoader);

    }

    sbbObject.setState(SbbObjectState.DOES_NOT_EXIST);

}

From source file:org.javascool.webjavac.Gateway.java

/**
 * Exec a compiled Runnable.// w  w w  .j a  va  2  s  .co  m
 *
 * @param location Which class
 * @return The System.out and System.err threads
 * @see FileManager#load(String)
 */
public String exec(final String location) throws Exception {
    assertSafeUsage();
    try {
        return AccessController.doPrivileged(new PrivilegedAction<String>() {
            private String result = "";

            public String run() {
                Runnable clazz = Java2Class.load(location);
                try {
                    clazz.run();
                } catch (Throwable e) {
                    return e.toString();
                }
                return "";
            }

        });
    } catch (Exception e) {
        popException(e);
        throw e;
    }
}

From source file:com.stratuscom.harvester.Utils.java

public static void logGrantsToClass(final Logger log, final Level level, final Class c) {
    AccessController.doPrivileged(new PrivilegedAction<Object>() {
        public Object run() {
            ClassLoader cl = c.getClassLoader();
            DynamicPolicyProvider dpp = (DynamicPolicyProvider) Policy.getPolicy();
            Permission[] perms = dpp.getGrants(c, null);
            log.log(level, MessageNames.GRANTS_TO_CLASS_ARE, new Object[] { c.getName(), Utils.format(perms) });
            return null;
        }/*ww w .  j a  v  a  2 s  . c  om*/
    });
}

From source file:org.apache.ws.security.util.Loader.java

/**
 * Get the class loader of the class argument
 * <p/>/*from ww w . j  a  v a2 s  . com*/
 *
 * @return the class loader of the argument
 */
public static ClassLoader getClassLoader(final Class clazz) {
    return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            return clazz.getClassLoader();
        }
    });
}

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  ava 2  s  .com

    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.apache.ranger.services.hive.client.HiveClient.java

public List<String> getDatabaseList(String databaseMatching, final List<String> databaseList)
        throws HadoopException {
    final String dbMatching = databaseMatching;
    final List<String> dbList = databaseList;
    List<String> dblist = Subject.doAs(getLoginSubject(), new PrivilegedAction<List<String>>() {
        public List<String> run() {
            List<String> ret = null;
            try {
                ret = getDBList(dbMatching, dbList);
            } catch (HadoopException he) {
                LOG.error("<== HiveClient getDatabaseList() :Unable to get the Database List", he);
                throw he;
            }/*from  w  w w  . j  a v  a  2s.  c  o m*/
            return ret;
        }
    });
    return dblist;
}

From source file:org.apache.flink.yarn.ApplicationMaster.java

public static void main(String[] args) throws Exception {
    final String yarnClientUsername = System.getenv(Client.ENV_CLIENT_USERNAME);
    LOG.info("YARN daemon runs as '" + UserGroupInformation.getCurrentUser().getShortUserName() + "' setting"
            + " user to execute Flink ApplicationMaster/JobManager to '" + yarnClientUsername + "'");
    UserGroupInformation ugi = UserGroupInformation.createRemoteUser(yarnClientUsername);
    for (Token<? extends TokenIdentifier> toks : UserGroupInformation.getCurrentUser().getTokens()) {
        ugi.addToken(toks);/*from  www .  ja  va 2  s .  c  o  m*/
    }
    ugi.doAs(new PrivilegedAction<Object>() {
        @Override
        public Object run() {
            try {
                new ApplicationMaster().run();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    });
}