AccessController.doPrivileged [英] AccessController.doPrivileged

查看:525
本文介绍了AccessController.doPrivileged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一些旧的code在做什么。究竟是这条线做的,为什么我需要这种方式?

I am trying to figure out what some legacy code is doing. What exactly is this line doing, and why would I need it this way?

String lineSeparator = (String) java.security.AccessController.doPrivileged(
       new sun.security.action.GetPropertyAction("line.separator"));

我发现它在记录器实现上运行的Weblogic 8.有没有启用特殊的安全政策,据我所知网络/ EJB应用程序。 (我不喜欢从太阳进口*包,所以我想摆脱这行 - 。)

I found it in the logger implementation of the web/ejb application running on Weblogic 8. There are no special security policies enabled as far as I know. (I do not like imports from sun.* packages, so I want to get rid of this line ;-)

推荐答案

这是刚开的系统属性。检索系统属性要求它调用code可能没有权限。在 doPrivileged的断言调用类的特权,它是如何叫不管。显然, doPrivileged的是你需要小心的东西。

It is just getting a system property. Retrieving system properties requires permissions which the calling code may not have. The doPrivileged asserts the privileges of the calling class irrespective of how it was called. Clearly, doPrivileged is something you need to be careful with.

引用的code是等价的:

The code quoted is the equivalent of:

String lineSeparator = java.security.AccessController.doPrivileged(
    new java.security.PrivilegedAction<String>() {
        public String run() {
            return System.getProperty("line.separator");
        }
    }
 );

(难道你不喜欢Java的语法的简洁性?)

(Don't you just love the conciseness of Java's syntax?)

如果没有声明特权,这可以被改写为:

Without asserting privileges, this can be rewritten as:

String lineSeparator = System.getProperty("line.separator");

这篇关于AccessController.doPrivileged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆