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.exoplatform.services.ftp.FtpServerImpl.java

public FtpServerImpl(FtpConfig configuration, CommandService commandService,
        RepositoryService repositoryService) throws Exception {
    this.configuration = configuration;
    this.repositoryService = repositoryService;

    InputStream commandStream = SecurityHelper.doPrivilegedAction(new PrivilegedAction<InputStream>() {
        public InputStream run() {
            return getClass().getResourceAsStream(COMMAND_PATH);
        }/*ww w.  j a v a  2  s  .c o  m*/
    });

    commandService.putCatalog(commandStream);
    commandCatalog = commandService.getCatalog(FtpConst.FTP_COMMAND_CATALOG);
}

From source file:org.solmix.runtime.support.spring.SpringContainerFactory.java

/**
 * @param strings/*w  w  w .  jav a 2  s .  co m*/
 * @param includeDefaults
 * @return
 */
public Container createContainer(String[] cfgFiles, boolean includeDefaults) {
    try {
        final Resource r = ContainerApplicationContext
                .findResource(ContainerApplicationContext.DEFAULT_USER_CFG_FILE);
        boolean exists = true;
        if (r != null) {
            exists = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {

                @Override
                public Boolean run() {
                    return r.exists();
                }
            });
        }
        if (parent == null && includeDefaults && (r == null || !exists)) {
            return new ContainerFactoryImpl().createContainer();
        }
        ConfigurableApplicationContext cac = createApplicationContext(cfgFiles, includeDefaults, parent);
        return completeCreating(cac);
    } catch (BeansException ex) {
        throw new java.lang.RuntimeException(ex);
    }
}

From source file:com.kenai.redminenb.repository.RedmineManagerFactoryHelper.java

public static Transport getTransportFromManager(final RedmineManager rm) {
    return AccessController.doPrivileged(new PrivilegedAction<Transport>() {

        @Override//from w  ww.j  av a  2 s  .  c o m
        public Transport run() {
            try {
                Field transportField = RedmineManager.class.getDeclaredField("transport");
                transportField.setAccessible(true);
                return (Transport) transportField.get(rm);
            } catch (NoSuchFieldException | SecurityException | IllegalArgumentException
                    | IllegalAccessException ex) {
                throw new RuntimeException("Failed to get transport from redmine manager", ex);
            }
        }
    });
}

From source file:com.agimatec.validation.jsr303.ConstraintDefaults.java

private Map<String, Class[]> loadDefaultConstraints(String resource) {
    Properties constraintProperties = new Properties();
    final ClassLoader classloader = getClassLoader();
    InputStream stream = classloader.getResourceAsStream(resource);
    if (stream != null) {
        try {/*from  www. ja v  a2s.  com*/
            constraintProperties.load(stream);
        } catch (IOException e) {
            log.error("cannot load " + resource, e);
        }
    } else {
        log.warn("cannot find " + resource);
    }
    Map<String, Class[]> loadedConstraints = new HashMap();
    for (Map.Entry entry : constraintProperties.entrySet()) {

        StringTokenizer tokens = new StringTokenizer((String) entry.getValue(), ", ");
        LinkedList classes = new LinkedList();
        while (tokens.hasMoreTokens()) {
            final String eachClassName = tokens.nextToken();

            Class constraintValidatorClass = SecureActions.run(new PrivilegedAction<Class>() {
                public Class run() {
                    try {
                        return Class.forName(eachClassName, true, classloader);
                    } catch (ClassNotFoundException e) {
                        log.error("Cannot find class " + eachClassName, e);
                        return null;
                    }
                }
            });

            if (constraintValidatorClass != null)
                classes.add(constraintValidatorClass);

        }
        loadedConstraints.put((String) entry.getKey(), (Class[]) classes.toArray(new Class[classes.size()]));

    }
    return loadedConstraints;
}

From source file:ja.centre.util.io.nio.MappedByteBufferWrapper.java

private void clean(final MappedByteBuffer buffer) {
    AccessController.doPrivileged(new PrivilegedAction<Object>() {
        public Object run() {
            try {
                Method cleanerMethod = buffer.getClass().getMethod("cleaner");
                cleanerMethod.setAccessible(true);
                sun.misc.Cleaner cleaner = (sun.misc.Cleaner) cleanerMethod.invoke(buffer);
                cleaner.clean();/*from   w  w  w  . j a  v a 2s  .co m*/
            } catch (IllegalAccessException e) {
                States.shouldNeverReachHere(e);
            } catch (NoSuchMethodException e) {
                States.shouldNeverReachHere(e);
            } catch (InvocationTargetException e) {
                States.shouldNeverReachHere(e);
            }
            return null;
        }
    });
}

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

/**
 * Write a File on a location.//  ww w.  j  a  va  2  s  .c o m
 * Replace the FileWriter API for text files only (code, plain text e.g.)
 *
 * @param location Where have I got to write your file ?
 * @param what     The content to write into the file
 * @return true if all is ok and the file is safe false otherwise
 * @see FileManager#save(String, String)
 */
public boolean save(final String location, final String what) throws Exception {
    assertSafeUsage();
    try {
        return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
            public Boolean run() {
                try {
                    FileManager.save(location, what);
                    return true;
                } catch (Exception e) {
                    return false;
                }
            }
        });
    } catch (Exception e) {
        popException(e);
        throw e;
    }
}

From source file:com.hortonworks.streamline.streams.storm.common.StormRestAPIClient.java

private Map doGetRequest(String requestUrl) {
    try {/* w w  w.ja va2  s  .  c  o  m*/
        LOG.debug("GET request to Storm cluster: " + requestUrl);
        return Subject.doAs(subject, new PrivilegedAction<Map>() {
            @Override
            public Map run() {
                return JsonClientUtil.getEntity(client.target(requestUrl), STORM_REST_API_MEDIA_TYPE,
                        Map.class);
            }
        });
    } catch (RuntimeException ex) {
        // JsonClientUtil wraps exception, so need to compare
        if (ex.getCause() instanceof javax.ws.rs.ProcessingException) {
            if (ex.getCause().getCause() instanceof IOException) {
                throw new StormNotReachableException("Exception while requesting " + requestUrl, ex);
            }
        } else if (ex.getCause() instanceof WebApplicationException) {
            throw WrappedWebApplicationException.of((WebApplicationException) ex.getCause());
        }

        throw ex;
    }
}

From source file:com.googlecode.arit.jmx.LeakDetector.java

public void afterPropertiesSet() throws Exception {
    timer = AccessController.doPrivileged(new PrivilegedAction<Timer>() {
        public Timer run() {
            timer = new Timer("LeakDetectorTimer");
            timer.schedule(new TimerTask() {
                @Override/*from   ww w  . j a va  2  s .com*/
                public void run() {
                    try {
                        runDetection();
                    } catch (Throwable ex) {
                        log.error("Leak detection failed", ex);
                    }
                }
            }, 0, 60000);
            return timer;
        }
    });
}

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

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

From source file:org.apache.ranger.audit.provider.kafka.KafkaAuditProvider.java

@Override
public void init(Properties props) {
    LOG.info("init() called");
    super.init(props);

    topic = MiscUtil.getStringProperty(props, AUDIT_KAFKA_TOPIC_NAME);
    if (topic == null || topic.isEmpty()) {
        topic = "ranger_audits";
    }/*  ww w  . j a  v a2 s  .c  o m*/

    try {
        if (!initDone) {
            String brokerList = MiscUtil.getStringProperty(props, AUDIT_KAFKA_BROKER_LIST);
            if (brokerList == null || brokerList.isEmpty()) {
                brokerList = "localhost:9092";
            }

            final Map<String, Object> kakfaProps = new HashMap<String, Object>();
            kakfaProps.put("metadata.broker.list", brokerList);
            kakfaProps.put("serializer.class", "kafka.serializer.StringEncoder");
            // kakfaProps.put("partitioner.class",
            // "example.producer.SimplePartitioner");
            kakfaProps.put("request.required.acks", "1");

            LOG.info("Connecting to Kafka producer using properties:" + kakfaProps.toString());

            producer = MiscUtil.executePrivilegedAction(new PrivilegedAction<Producer<String, String>>() {
                @Override
                public Producer<String, String> run() {
                    Producer<String, String> producer = new KafkaProducer<String, String>(kakfaProps);
                    return producer;
                };
            });

            initDone = true;
        }
    } catch (Throwable t) {
        LOG.fatal("Error initializing kafka:", t);
    }
}