United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 6466058 OceanTheme causes swing components to serialize with sun.* classes
6466058 : OceanTheme causes swing components to serialize with sun.* classes

Details
Type:
Bug
Submit Date:
2006-08-31
Status:
Closed
Updated Date:
2011-03-08
Project Name:
JDK
Resolved Date:
2011-03-08
Component:
client-libs
OS:
linux
Sub-Component:
javax.swing
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:
7

Related Reports
Backport:
Backport:

Sub Tasks

Description
FULL PRODUCT VERSION :
java version "1.5.0_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_08-b03)
Java HotSpot(TM) Client VM (build 1.5.0_08-b03, mixed mode, sharing)

java version "1.6.0-rc"
Java(TM) SE Runtime Environment (build 1.6.0-rc-b92)
Java HotSpot(TM) Client VM (build 1.6.0-rc-b92, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Linux localhost.localdomain 2.6.11-1.1369_FC4 #1 Thu Jun 2 22:55:56 EDT 2005 i686 i686 i386 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
When serializing Swing components (tested with JFrame and JPanel) with the Ocean theme, classes from sun.swing.* are serialised. sun.* classes cannot be deserialised. This will cause an AccessControlException in untrusted code.

Examining the serializer data, the class sun.swing.PrintColorUIResource appears to be present. This class appears in javax.swing.plaf.metal.OceanTheme.CONTROL_TEXT_COLOR and OCEAN_BLACK. Using the -Dswing.metalTheme=steel command line option switches to the Steel theme and prevents the exception. I believe Component.doSwingSerialization should remove PL&F artifacts during serialization.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
The program below set a security manager, serialises a JPanel and then desrialises it. Run as:

java OceanSun

  To run without Ocean (and hence the exception):

java  -Dswing.metalTheme=steel OceanSun


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The program should run and exit without error.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.security.AccessControlException: access denied (java.lang.RuntimePermission accessClassInPackage.sun.swing)
        at java.security.AccessControlContext.checkPermission(Unknown Source)
        at java.security.AccessController.checkPermission(Unknown Source)
        at java.lang.SecurityManager.checkPermission(Unknown Source)
        at java.lang.SecurityManager.checkPackageAccess(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at java.io.ObjectInputStream.resolveClass(Unknown Source)
        at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
        at java.io.ObjectInputStream.readClassDesc(Unknown Source)
        at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
        at java.io.ObjectInputStream.readObject0(Unknown Source)
        at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
        at java.io.ObjectInputStream.defaultReadObject(Unknown Source)
        at java.awt.Component.readObject(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
        at java.io.ObjectInputStream.readSerialData(Unknown Source)
        at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
        at java.io.ObjectInputStream.readObject0(Unknown Source)
        at java.io.ObjectInputStream.readObject(Unknown Source)
        at OceanSun.main(OceanSun.java:15)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.*;
import javax.swing.*;

class OceanSun {
    public static void main(String[] args) throws Exception {
        System.setSecurityManager(new SecurityManager());
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        JPanel frame = new JPanel();
        out.writeObject(frame);
        out.close();
        ObjectInputStream in = new ObjectInputStream(
            new ByteArrayInputStream(byteOut.toByteArray())
        );
        JPanel readFrame = (JPanel)in.readObject();
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Don't use Ocean when serializing components. It may be possible to overwrite the relevant Color properties on the Component.

                                    

Comments
EVALUATION

Contribution forum : https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=15307
                                     
2006-09-06
EVALUATION

Since PrintColorUIResource resides in the sun.swing package, access can be disallowed to it by a security manager. When access is disallowed, deserialization of any object with reference to a PrintColorUIResource fails. Since PrintColorUIResource is used only by Swing's look and feels, and we know that UI supplied colors are replaced after deserialization when the UI is re-installed, the only important aspect of the PrintColorUIResource that needs to be persisted is the fact that it is a ColorUIResource. As such, we can avoid the problem by giving PrintColorUIResource a writeReplace() method that replaces the problematic PrintColorUIResource with a plain ColorUIResource during serialization.

Note: As a result of this method, it is not possible to write a PrintColorUIResource to a stream and then read back a PrintColorUIResource. This is acceptable since we
don't have a requirement for that in Swing.
                                     
2006-09-05
EVALUATION

Introduced by the fix to 6201884. Need to solve this.
                                     
2006-08-31



Hardware and Software, Engineered to Work Together