Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
42a3d813
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
42a3d813
编写于
7月 19, 2013
作者:
O
okutsu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8001029: Add new date/time capability
Reviewed-by: mchung, hawtin
上级
825e6979
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
107 addition
and
100 deletion
+107
-100
src/share/classes/java/util/TimeZone.java
src/share/classes/java/util/TimeZone.java
+40
-100
test/java/util/TimeZone/SetDefaultSecurityTest.java
test/java/util/TimeZone/SetDefaultSecurityTest.java
+67
-0
未找到文件。
src/share/classes/java/util/TimeZone.java
浏览文件 @
42a3d813
/*
* Copyright (c) 1996, 201
2
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 201
3
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
...
...
@@ -39,13 +39,9 @@
package
java.util
;
import
java.io.Serializable
;
import
java.lang.ref.SoftReference
;
import
java.security.AccessController
;
import
java.security.PrivilegedAction
;
import
java.time.ZoneId
;
import
java.util.concurrent.ConcurrentHashMap
;
import
sun.misc.JavaAWTAccess
;
import
sun.misc.SharedSecrets
;
import
sun.security.action.GetPropertyAction
;
import
sun.util.calendar.ZoneInfo
;
import
sun.util.calendar.ZoneInfoFile
;
...
...
@@ -596,11 +592,26 @@ abstract public class TimeZone implements Serializable, Cloneable {
private
static
native
String
getSystemGMTOffsetID
();
/**
* Gets the default <code>TimeZone</code> for this host.
* The source of the default <code>TimeZone</code>
* may vary with implementation.
* @return a default <code>TimeZone</code>.
* @see #setDefault
* Gets the default {@code TimeZone} of the Java virtual machine. If the
* cached default {@code TimeZone} is available, its clone is returned.
* Otherwise, the method takes the following steps to determine the default
* time zone.
*
* <p><ul>
* <li>Use the {@code user.timezone} property value as the default
* time zone ID if it's available.</li>
* <li>Detect the platform time zone ID. The source of the
* platform time zone and ID mapping may vary with implementation.</li>
* <li>Use {@code GMT} as the last resort if the given or detected
* time zone ID is unknown.</li>
* </ul>
*
* <p>The default {@code TimeZone} created from the ID is cached,
* and its clone is returned. The {@code user.timezone} property
* value is set to the ID upon return.
*
* @return the default {@code TimeZone}
* @see #setDefault(TimeZone)
*/
public
static
TimeZone
getDefault
()
{
return
(
TimeZone
)
getDefaultRef
().
clone
();
...
...
@@ -611,14 +622,11 @@ abstract public class TimeZone implements Serializable, Cloneable {
* method doesn't create a clone.
*/
static
TimeZone
getDefaultRef
()
{
TimeZone
defaultZone
=
getDefaultInAppContext
()
;
TimeZone
defaultZone
=
defaultTimeZone
;
if
(
defaultZone
==
null
)
{
defaultZone
=
defaultTimeZone
;
if
(
defaultZone
==
null
)
{
// Need to initialize the default time zone.
defaultZone
=
setDefaultZone
();
assert
defaultZone
!=
null
;
}
// Need to initialize the default time zone.
defaultZone
=
setDefaultZone
();
assert
defaultZone
!=
null
;
}
// Don't clone here.
return
defaultZone
;
...
...
@@ -676,95 +684,27 @@ abstract public class TimeZone implements Serializable, Cloneable {
return
tz
;
}
private
static
boolean
hasPermission
()
{
boolean
hasPermission
=
true
;
SecurityManager
sm
=
System
.
getSecurityManager
();
if
(
sm
!=
null
)
{
try
{
sm
.
checkPermission
(
new
PropertyPermission
(
"user.timezone"
,
"write"
));
}
catch
(
SecurityException
e
)
{
hasPermission
=
false
;
}
}
return
hasPermission
;
}
/**
* Sets the <code>TimeZone</code> that is
* returned by the <code>getDefault</code> method. If <code>zone</code>
* is null, reset the default to the value it had originally when the
* VM first started.
* @param zone the new default time zone
* Sets the {@code TimeZone} that is returned by the {@code getDefault}
* method. {@code zone} is cached. If {@code zone} is null, the cached
* default {@code TimeZone} is cleared. This method doesn't change the value
* of the {@code user.timezone} property.
*
* @param zone the new default {@code TimeZone}, or null
* @throws SecurityException if the security manager's {@code checkPermission}
* denies {@code PropertyPermission("user.timezone",
* "write")}
* @see #getDefault
* @see PropertyPermission
*/
public
static
void
setDefault
(
TimeZone
zone
)
{
if
(
hasPermission
())
{
synchronized
(
TimeZone
.
class
)
{
defaultTimeZone
=
zone
;
setDefaultInAppContext
(
null
);
}
}
else
{
setDefaultInAppContext
(
zone
);
}
}
/**
* Returns the default TimeZone in an AppContext if any AppContext
* has ever used. null is returned if any AppContext hasn't been
* used or if the AppContext doesn't have the default TimeZone.
*
* Note that javaAWTAccess may be null if sun.awt.AppContext class hasn't
* been loaded. If so, it implies that AWTSecurityManager is not our
* SecurityManager and we can use a local static variable.
* This works around a build time issue.
*/
private
static
TimeZone
getDefaultInAppContext
()
{
// JavaAWTAccess provides access implementation-private methods without using reflection.
JavaAWTAccess
javaAWTAccess
=
SharedSecrets
.
getJavaAWTAccess
();
if
(
javaAWTAccess
==
null
)
{
return
mainAppContextDefault
;
}
else
{
if
(!
javaAWTAccess
.
isDisposed
())
{
TimeZone
tz
=
(
TimeZone
)
javaAWTAccess
.
get
(
TimeZone
.
class
);
if
(
tz
==
null
&&
javaAWTAccess
.
isMainAppContext
())
{
return
mainAppContextDefault
;
}
else
{
return
tz
;
}
}
}
return
null
;
}
/**
* Sets the default TimeZone in the AppContext to the given
* tz. null is handled special: do nothing if any AppContext
* hasn't been used, remove the default TimeZone in the
* AppContext otherwise.
*
* Note that javaAWTAccess may be null if sun.awt.AppContext class hasn't
* been loaded. If so, it implies that AWTSecurityManager is not our
* SecurityManager and we can use a local static variable.
* This works around a build time issue.
*/
private
static
void
setDefaultInAppContext
(
TimeZone
tz
)
{
// JavaAWTAccess provides access implementation-private methods without using reflection.
JavaAWTAccess
javaAWTAccess
=
SharedSecrets
.
getJavaAWTAccess
();
if
(
javaAWTAccess
==
null
)
{
mainAppContextDefault
=
tz
;
}
else
{
if
(!
javaAWTAccess
.
isDisposed
())
{
javaAWTAccess
.
put
(
TimeZone
.
class
,
tz
);
if
(
javaAWTAccess
.
isMainAppContext
())
{
mainAppContextDefault
=
null
;
}
}
SecurityManager
sm
=
System
.
getSecurityManager
();
if
(
sm
!=
null
)
{
sm
.
checkPermission
(
new
PropertyPermission
(
"user.timezone"
,
"write"
));
}
defaultTimeZone
=
zone
;
}
/**
...
...
test/java/util/TimeZone/SetDefaultSecurityTest.java
0 → 100644
浏览文件 @
42a3d813
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8001029
* @summary Make sure that TimeZone.setDefault throws a SecurityException if the
* security manager doesn't permit.
* @run main/othervm SetDefaultSecurityTest
*/
import
java.util.SimpleTimeZone
;
import
java.util.TimeZone
;
public
class
SetDefaultSecurityTest
{
static
final
TimeZone
NOWHERE
=
new
SimpleTimeZone
(
Integer
.
MAX_VALUE
,
"Nowhere"
);
public
static
void
main
(
String
[]
args
)
{
TimeZone
defaultZone
=
TimeZone
.
getDefault
();
// Make sure that TimeZone.setDefault works for trusted code
TimeZone
.
setDefault
(
NOWHERE
);
if
(!
NOWHERE
.
equals
(
TimeZone
.
getDefault
()))
{
new
RuntimeException
(
"TimeZone.setDefault doesn't work for trusted code."
);
}
// Restore defaultZone
TimeZone
.
setDefault
(
defaultZone
);
if
(!
defaultZone
.
equals
(
TimeZone
.
getDefault
()))
{
new
RuntimeException
(
"TimeZone.setDefault doesn't restore defaultZone."
);
}
// Install a SecurityManager.
System
.
setSecurityManager
(
new
SecurityManager
());
try
{
TimeZone
.
setDefault
(
NOWHERE
);
throw
new
RuntimeException
(
"TimeZone.setDefault doesn't throw a SecurityException."
);
}
catch
(
SecurityException
se
)
{
// OK
}
TimeZone
tz
=
TimeZone
.
getDefault
();
if
(!
defaultZone
.
equals
(
tz
))
{
throw
new
RuntimeException
(
"Default TimeZone changed: "
+
tz
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录