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.apache.stanbol.commons.sphinx.impl.ModelProviderImpl.java

/**
 * //from   ww  w  .  j av a2 s .co m
 * @param modelDataStream {@link InputStream} of the Model, received from {@link DataFileProvider} Service
 * @param resourceName Model File Name
 * @param path path to copy the @resourceName i.e. /tmp
 * @throws PrivilegedActionException
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private void createTempResource(final InputStream modelDataStream, String resourceName, String path)
        throws PrivilegedActionException {
    final File resource = new File(path + "/" + resourceName);

    AccessController.doPrivileged(new PrivilegedAction() {
        @Override
        public Object run() {
            try {
                FileUtils.copyInputStreamToFile(modelDataStream, resource);
            } catch (IOException e) {
                log.debug("Unable to copy Resource {} to temp", resource.getAbsolutePath());
            }
            return null;
        }
    });
}

From source file:org.apache.axis2.util.Loader.java

/**
 * Gets the thread context class loader.
 *
 * @return Returns ClassLoader./*  ww  w  .  j av a 2s. c  o m*/
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
static private ClassLoader getTCL() throws IllegalAccessException, InvocationTargetException {
    return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
        public ClassLoader run() {
            return Thread.currentThread().getContextClassLoader();
        }
    });
}

From source file:org.eclipse.gemini.blueprint.config.internal.adapter.OsgiServiceRegistrationListenerAdapter.java

public void registered(final Object service, final Map serviceProperties) {
    boolean trace = log.isTraceEnabled();

    if (trace)// ww w.  jav  a2  s  . c o  m
        log.trace("Invoking registered method with props=" + serviceProperties);

    if (!initialized)
        retrieveTarget();

    boolean isSecurityEnabled = System.getSecurityManager() != null;
    AccessControlContext acc = null;
    if (isSecurityEnabled) {
        acc = SecurityUtils.getAccFrom(beanFactory);
    }

    // first call interface method (if it exists)
    if (isListener) {
        if (trace)
            log.trace("Invoking listener interface methods");

        try {
            if (isSecurityEnabled) {
                AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                    public Object run() throws Exception {
                        ((OsgiServiceRegistrationListener) target).registered(service, serviceProperties);
                        return null;
                    }
                }, acc);
            } else {
                ((OsgiServiceRegistrationListener) target).registered(service, serviceProperties);
            }
        } catch (Exception ex) {
            if (ex instanceof PrivilegedActionException) {
                ex = ((PrivilegedActionException) ex).getException();
            }
            log.warn("Standard registered method on [" + target.getClass().getName() + "] threw exception", ex);
        }
    }

    if (isSecurityEnabled) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            public Object run() {
                CustomListenerAdapterUtils.invokeCustomMethods(target, registrationMethods, service,
                        serviceProperties);
                return null;
            }
        }, acc);
    } else {
        CustomListenerAdapterUtils.invokeCustomMethods(target, registrationMethods, service, serviceProperties);
    }
}

From source file:org.eclipse.gemini.blueprint.extender.internal.blueprint.event.EventAdminDispatcher.java

public void grace(final BlueprintEvent event) {
    if (dispatcher != null) {
        try {//from   w  w w .  ja v a  2 s . c om
            if (System.getSecurityManager() != null) {
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
                    public Object run() {
                        dispatcher.grace(event);
                        return null;
                    }
                });
            } else {
                dispatcher.grace(event);
            }
        } catch (Throwable th) {
            log.warn("Cannot dispatch event " + event, th);
        }
    }
}

From source file:SecuritySupport.java

String getSystemProperty(final String propName) {
    return (String) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            return System.getProperty(propName);
        }/*from w  ww  .  ja  v a 2  s. c  om*/
    });
}

From source file:org.rhq.enterprise.client.LocalClient.java

@Override
public RepoManagerRemote getRepoManager() {
    return AccessController.doPrivileged(new PrivilegedAction<RepoManagerRemote>() {
        @Override/*  w w w.  jav  a 2 s  .  c o m*/
        public RepoManagerRemote run() {
            return getProxy(LookupUtil.getRepoManagerLocal(), RepoManagerRemote.class);
        }
    });
}

From source file:org.eclipse.gemini.blueprint.io.internal.resolver.PackageAdminResolver.java

private PackageAdmin getPackageAdmin() {

    return AccessController.doPrivileged(new PrivilegedAction<PackageAdmin>() {

        public PackageAdmin run() {
            ServiceReference ref = bundleContext.getServiceReference(PackageAdmin.class.getName());
            if (ref == null)
                throw new IllegalStateException(PackageAdmin.class.getName() + " service is required");
            // don't do any proxying since PackageAdmin is normally a framework service
            // we can assume for now that it will always be available
            return (PackageAdmin) bundleContext.getService(ref);
        }/*from w w  w.  j a v a2  s.  c om*/
    });
}

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

/**
 * Move a file or directory/*from   www . j av  a  2s  . c o  m*/
 *
 * @param location The path to file or directory
 * @param to       The new path
 * @return true On success false If the file or folder don't exist
 */
public boolean move(final String location, final String to) throws Exception {
    assertSafeUsage();
    try {
        return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
            public Boolean run() {
                try {
                    if (location != null) {
                        if (!new File(location).exists() || new File(to).exists()) {
                            throw new IllegalArgumentException("Can't move " + location
                                    + " because location path do not exist or something exists in the 'to' path");
                        }
                        new File(location).renameTo(new File(to));
                    } else {
                        throw new IllegalArgumentException("location is null");
                    }
                } catch (Exception e) {
                    return false;
                }
                return true;
            }
        });
    } catch (Exception e) {
        popException(e);
        throw e;
    }
}

From source file:org.apache.ranger.biz.KmsKeyMgr.java

@SuppressWarnings("unchecked")
public VXKmsKeyList searchKeys(HttpServletRequest request, String repoName) throws Exception {
    String providers[] = null;// w  w  w.j  a  v  a 2  s.co  m
    try {
        providers = getKMSURL(repoName);
    } catch (Exception e) {
        logger.error("getKey(" + repoName + ") failed", e);
    }
    List<VXKmsKey> vXKeys = new ArrayList<VXKmsKey>();
    VXKmsKeyList vxKmsKeyList = new VXKmsKeyList();
    List<String> keys = null;
    String connProvider = null;
    boolean isKerberos = false;
    try {
        isKerberos = checkKerberos();
    } catch (Exception e1) {
        logger.error("checkKerberos(" + repoName + ") failed", e1);
    }
    if (providers != null) {
        for (int i = 0; i < providers.length; i++) {
            Client c = getClient();
            String currentUserLoginId = ContextUtil.getCurrentUserLoginId();
            String keyLists = KMS_KEY_LIST_URI.replaceAll(Pattern.quote("${userName}"), currentUserLoginId);
            connProvider = providers[i];
            String uri = providers[i] + (providers[i].endsWith("/") ? keyLists : ("/" + keyLists));
            if (!isKerberos) {
                uri = uri.concat("?user.name=" + currentUserLoginId);
            } else {
                uri = uri.concat("?doAs=" + currentUserLoginId);
            }
            final WebResource r = c.resource(uri);
            try {
                String response = null;
                if (!isKerberos) {
                    response = r.accept(MediaType.APPLICATION_JSON_TYPE).type(MediaType.APPLICATION_JSON_TYPE)
                            .get(String.class);
                } else {
                    Subject sub = getSubjectForKerberos(repoName);
                    response = Subject.doAs(sub, new PrivilegedAction<String>() {
                        @Override
                        public String run() {
                            return r.accept(MediaType.APPLICATION_JSON_TYPE)
                                    .type(MediaType.APPLICATION_JSON_TYPE).get(String.class);
                        }
                    });
                }
                Gson gson = new GsonBuilder().create();
                logger.debug(" Search Key RESPONSE: [" + response + "]");
                keys = gson.fromJson(response, List.class);
                Collections.sort(keys);
                VXKmsKeyList vxKmsKeyList2 = new VXKmsKeyList();
                List<VXKmsKey> vXKeys2 = new ArrayList<VXKmsKey>();
                for (String name : keys) {
                    VXKmsKey key = new VXKmsKey();
                    key.setName(name);
                    vXKeys2.add(key);
                }
                vxKmsKeyList2.setVXKeys(vXKeys2);
                vxKmsKeyList = getFilteredKeyList(request, vxKmsKeyList2);
                break;
            } catch (Exception e) {
                if (e instanceof UniformInterfaceException || i == providers.length - 1)
                    throw e;
                else
                    continue;
            }
        }
    }
    //details
    if (vxKmsKeyList != null && vxKmsKeyList.getVXKeys() != null && vxKmsKeyList.getVXKeys().size() > 0) {
        List<VXKmsKey> lstKMSKey = vxKmsKeyList.getVXKeys();
        int startIndex = restErrorUtil.parseInt(request.getParameter("startIndex"), 0,
                "Invalid value for parameter startIndex", MessageEnums.INVALID_INPUT_DATA, null, "startIndex");
        startIndex = startIndex < 0 ? 0 : startIndex;

        int pageSize = restErrorUtil.parseInt(request.getParameter("pageSize"), 0,
                "Invalid value for parameter pageSize", MessageEnums.INVALID_INPUT_DATA, null, "pageSize");
        pageSize = pageSize < 0 ? 0 : pageSize;

        vxKmsKeyList.setResultSize(lstKMSKey.size());
        vxKmsKeyList.setTotalCount(lstKMSKey.size());
        if ((startIndex + pageSize) <= lstKMSKey.size()) {
            lstKMSKey = lstKMSKey.subList(startIndex, (startIndex + pageSize));
        } else {
            startIndex = startIndex >= lstKMSKey.size() ? 0 : startIndex;
            lstKMSKey = lstKMSKey.subList(startIndex, lstKMSKey.size());
        }
        if (CollectionUtils.isNotEmpty(lstKMSKey)) {
            for (VXKmsKey kmsKey : lstKMSKey) {
                if (kmsKey != null) {
                    VXKmsKey key = getKeyFromUri(connProvider, kmsKey.getName(), isKerberos, repoName);
                    vXKeys.add(key);
                }
            }
        }
        vxKmsKeyList.setStartIndex(startIndex);
        vxKmsKeyList.setPageSize(pageSize);
    }
    if (vxKmsKeyList != null) {
        vxKmsKeyList.setVXKeys(vXKeys);
    }
    return vxKmsKeyList;
}

From source file:org.rhq.enterprise.client.LocalClient.java

@Override
public ConfigurationManagerRemote getConfigurationManager() {
    return AccessController.doPrivileged(new PrivilegedAction<ConfigurationManagerRemote>() {
        @Override//from   w  w w . j  a va2 s . c  o  m
        public ConfigurationManagerRemote run() {
            return getProxy(LookupUtil.getConfigurationManager(), ConfigurationManagerRemote.class);
        }
    });
}