Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6964500

Persistence Service does not work with Applets

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P3 P3
    • 7-client
    • 6u7
    • deploy
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.6.0_07"
      Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
      Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Browsers: IE8, Firefox

      A DESCRIPTION OF THE PROBLEM :
      The persistence service denies access to codebase when used within an Applet.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See the attached code. Which includes the HTML and the JNLP files.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Data to be stored and retrieved locally.
      ACTUAL -
      No data stored or retrieved

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.net.MalformedURLException: Access to persistent storage denied for URL http://5lls221/DesktopHeatmaps/
      at com.sun.jnlp.PersistenceServiceImpl.throwAccessDenied(Unknown Source)
      at com.sun.jnlp.PersistenceServiceImpl.checkAccess(Unknown Source)
      at com.sun.jnlp.PersistenceServiceImpl.get(Unknown Source)
      at test.PersistenceTest.restoreSettings(PersistenceTest.java:84)
      at test.PersistenceTest.startUp(PersistenceTest.java:76)
      at test.PersistenceTest.init(PersistenceTest.java:157)
      at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Java Code:
      -------------------------------------------------
      package test;

      import java.awt.BorderLayout;
      import java.awt.Dimension;
      import java.awt.Point;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.io.*;
      import java.net.MalformedURLException;
      import java.net.URL;

      import javax.jnlp.*;
      import javax.swing.*;
      import javax.swing.border.EmptyBorder;

      /** A test of the PersistenceService of the web-start API.
      @author Andrew Thompson
      @version 2007/1/12
       */
      public class PersistenceTest extends JApplet {

        JTextArea document;
        PersistenceService persistenceService = null;
        URL codebase = null;
        JCheckBox plaf;

        /** Assemble the GUI. */
        //PersistenceTest() {
        // super("JotPad");
        // setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        // this.addWindowListener(new WindowAdapter() {
        // public void windowClosing(WindowEvent we) {
        // storeSettings();
        // System.exit(0);
        // }
        // });
        //}
        private void startUp () {
          try {
            BasicService basicService =
              (BasicService)ServiceManager.
              lookup("javax.jnlp.BasicService");
            codebase = basicService.getCodeBase();
            persistenceService = (PersistenceService)ServiceManager.
            lookup("javax.jnlp.PersistenceService");
          } catch(UnavailableServiceException use) {
            use.printStackTrace();
            System.exit(0);
          }
          JPanel main = new JPanel( new BorderLayout(5,5) );

          document = new JTextArea();
          document.setLineWrap(true);
          document.setWrapStyleWord(true);
          JScrollPane scrollpane = new JScrollPane(
              document,
              ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
              ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
          scrollpane.setPreferredSize(new Dimension(300,350));
          main.add(scrollpane, BorderLayout.CENTER);

          plaf = new JCheckBox("System Look'n'Feel", false);
          plaf.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
              updateLookAndFeel( plaf.isSelected() );
            }
          } );
          main.add( plaf, BorderLayout.SOUTH );

          main.setBorder( new EmptyBorder(8,8,8,8) );
          getContentPane().add(main);

          // pack();
          // setLocationRelativeTo(null);

          restoreSettings();
        }

        public void restoreSettings() {
          try {
            long fileSize = 16000;

            FileContents appSettings = null;
            appSettings = persistenceService.get(codebase);
            ObjectInputStream ois = new
            ObjectInputStream( appSettings.getInputStream() );
            ApplicationCache cache = (ApplicationCache)ois.readObject();
            setSize( cache.getSize() );
            setLocation( cache.getLocation() );
            plaf.setSelected( cache.isSystemPLAF() );
            updateLookAndFeel( cache.isSystemPLAF() );
            document.setText( cache.getText() );
            ois.close();
          } catch(FileNotFoundException fnfe) {
            // create the cache
            try {
              long size = persistenceService.create(codebase, 16000);
              System.out.println( "Cache created - size: " + size );
            } catch(MalformedURLException murle) {
              System.err.println( "Application codebase is not a valid URL?!" );
              murle.printStackTrace();
            } catch(IOException ioe) {
              ioe.printStackTrace();
            }
          } catch (Exception e) {
            e.printStackTrace();
          }
        }

        public void storeSettings() {
          try {
            ApplicationCache cache = new ApplicationCache();
            cache.setSize( getSize() );
            cache.setLocation( getLocation() );
            cache.setSystemPLAF( plaf.isSelected() );
            cache.setText( document.getText() );
            FileContents fc = persistenceService.get(codebase);
            ObjectOutputStream oos = new ObjectOutputStream(fc.getOutputStream(true));
            oos.writeObject( cache );
            oos.flush();
            oos.close();
          } catch(Exception e) {
            e.printStackTrace();
          }
        }

        /** Update the PLAF to either the X-plat, or system specific versions. */
        public void updateLookAndFeel(boolean xPlat) {
          try {
            if (xPlat) {
              UIManager.setLookAndFeel(
                  UIManager.getSystemLookAndFeelClassName() );
            } else {
              UIManager.setLookAndFeel(
                  UIManager.getCrossPlatformLookAndFeelClassName() );
            }

            SwingUtilities.updateComponentTreeUI( this );
            //pack();
            validate();
          } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
          }
        }

        /** Construct the GUI and display it. If the user double clicked
          a file to start the application, begin measures to load that file. */
        public static void main(String[] args) {
          PersistenceTest pt = new PersistenceTest();
          pt.setVisible(true);
        }
        @Override
        public void init () {
          // TODO Auto-generated method stub
          System.out.println("init");
          super.init();
          startUp();
          System.out.println("started up");
        }
        @Override
        public void start () {
          // TODO Auto-generated method stub
          System.out.println("start");
          super.start();
        }
        @Override
        public void stop () {
          // TODO Auto-generated method stub
          super.stop();
          storeSettings();
        }

      }

      class ApplicationCache implements Serializable {
        public String text;
        public boolean systemPLAF;
        public Point location;
        public Dimension size;

        public String getText() {
          return text;
        }

        public void setText(String text) {
          this.text = text;
        }

        public boolean isSystemPLAF() {
          return systemPLAF;
        }

        public void setSystemPLAF(boolean systemPLAF) {
          this.systemPLAF = systemPLAF;
        }

        public Point getLocation() {
          return location;
        }

        public void setLocation(Point location) {
          this.location = location;
        }

        public Dimension getSize() {
          return size;
        }

        public void setSize(Dimension size) {
          this.size = size;
        }

        @Override
        public String toString() {
          return "ApplicationCache:\nSys. PLAF: " +
          systemPLAF +
          "\nLocation: " + location +
          "\nSize: " + size +
          "\nText: " + text;
        }
      }
      ------------------------------------------------------------------------------------------
      HTML:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html>
          <head>
              <title>The Impermanence of All Things</title>
              <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
          </head>
          <body>
              
              <script src="http://www.java.com/js/deployJava.js"></script>
               <script>
                  var attributes = {code:'test.PersistenceTest',
      archive:'app/persistserv.jar',
      width:'300', height:'800' } ;
                  <!-- customize splash screen display options -->
                  var parameters = {jnlp_href: 'app/persistserv.jnlp',
                                      boxbgcolor: 'cyan',
                                      boxborder: 'true',
                                      centerimage: 'true'
                  } ;
                  deployJava.runApplet(attributes, parameters, '1.6');
               </script>
              <p>The applet's splash screen (or loading screen) has been customized.</p>
          </body>
      </html>
      --------------------------------------------------------------------------------
      JNLP
      <?xml version='1.0' encoding='UTF-8' ?>
      <jnlp spec='1.0'
            codebase='http://5llS221/DesktopHeatmaps&#39;
            href='app/persistserv.jnlp'>
        <information>
          <title>JotPad</title>
          <vendor>Andrew Thompson</vendor>
          <description kind='one-line'>
            Test of the web-start PersistenceService
          </description>
          <shortcut online='false'>
            <desktop/>
          </shortcut>
        </information>
        <resources>
          <j2se version='1.2+' />
          <jar href='app/persistserv.jar' main='true' />
        </resources>
        <applet-desc name='PersistApplet'
      main-class='test.PersistenceTest'
              width='300'
      height='800'
      />
      </jnlp>
      ---------- END SOURCE ----------

      SUPPORT :
      YES

            herrick Andy Herrick (Inactive)
            igor Igor Nekrestyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: