Uploaded image for project: 'WildFly'
  1. WildFly
  2. WFLY-10706

URL.getContent() returns VirtualFile instead of ImageProducer

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Done
    • Affects Version/s: 13.0.0.Final
    • Fix Version/s: 14.0.0.Final
    • Component/s: VFS
    • Labels:
      None
    • Environment:

      Windows XP

    • Steps to Reproduce:
      Hide

      Here are an example class and a jsp to reproduce the problem:

      LoadImageTest.java
      package example.loadimage.fail;
      import java.awt.Image;
      import java.beans.SimpleBeanInfo;
      public class LoadImageTest extends SimpleBeanInfo {
      	public Image image;
      
      	public LoadImageTest(){
      		image = loadImage2("icon.png");
      	}
      	public static void main(String[] args) {
      		LoadImageTest test = new LoadImageTest();
      		System.out.println("Is image null? " + (test.image == null));
      	}
      	// Note: This method was pulled out of the JDK src file for SimpleBeanInfo.java.
      	// This allows for easier debugging, you can actually see the local variables.
      	public java.awt.Image loadImage2(final String resourceName) {
      		try {
      			final Class c = getClass();
      			java.awt.image.ImageProducer ip = (java.awt.image.ImageProducer)
      			java.security.AccessController.doPrivileged(
      			new java.security.PrivilegedAction() {
      				public Object run() {
      					java.net.URL url;
      					if ((url = c.getResource(resourceName)) == null) {
      						return null;
      					} else {
      						try {
      							return url.getContent();
      						} catch (java.io.IOException ioe) {
      							return null;
      						}
      					}
      				}
      			});
      			if (ip == null)
      			return null;
      			java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
      			return tk.createImage(ip);
      		} catch (Exception ex) {
      			// will end up here because the PrivilegedAction returns something that cannot be cast
      			// to an ImageProducer when this is run in JBoss AS 6.0.0.FINAL. In JBoss the returned
      			// object will be a VirtualFile instead of an ImageProducer.
      			return null;
      		}
      	}
      }
      
      <%@page import=" example.loadimage.fail.LoadImageTest"%>
      <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Insert title here</title>
      </head>
      <body>
      <%
      out.print("Testing<br />");
      LoadImageTest test = new LoadImageTest();
      out.print("Is test.image null? " + (test.image == null) + "<br />");
      %>
      </body>
      </html>
      
      Show
      Here are an example class and a jsp to reproduce the problem: LoadImageTest.java package example.loadimage.fail; import java.awt.Image; import java.beans.SimpleBeanInfo; public class LoadImageTest extends SimpleBeanInfo { public Image image; public LoadImageTest(){ image = loadImage2( "icon.png" ); } public static void main( String [] args) { LoadImageTest test = new LoadImageTest(); System .out.println( "Is image null ? " + (test.image == null )); } // Note: This method was pulled out of the JDK src file for SimpleBeanInfo.java. // This allows for easier debugging, you can actually see the local variables. public java.awt.Image loadImage2( final String resourceName) { try { final Class c = getClass(); java.awt.image.ImageProducer ip = (java.awt.image.ImageProducer) java.security.AccessController.doPrivileged( new java.security.PrivilegedAction() { public Object run() { java.net.URL url; if ((url = c.getResource(resourceName)) == null ) { return null ; } else { try { return url.getContent(); } catch (java.io.IOException ioe) { return null ; } } } }); if (ip == null ) return null ; java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit(); return tk.createImage(ip); } catch (Exception ex) { // will end up here because the PrivilegedAction returns something that cannot be cast // to an ImageProducer when this is run in JBoss AS 6.0.0.FINAL. In JBoss the returned // object will be a VirtualFile instead of an ImageProducer. return null ; } } } <%@page import= " example.loadimage.fail.LoadImageTest" %> <%@ page language= "java" contentType= "text/html; charset=ISO-8859-1" pageEncoding= "ISO-8859-1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=ISO-8859-1" > <title> Insert title here </title> </head> <body> <% out.print( "Testing <br /> " ); LoadImageTest test = new LoadImageTest(); out.print( "Is test.image null? " + (test.image == null) + " <br /> " ); %> </body> </html>
    • Workaround:
      Workaround Exists
    • Workaround Description:
      Hide

      Read the content directly such as with ImageIO.read(url.openStream().getSource()

      import java.awt.Image;
      import javax.imageio.ImageIO;
      import java.io.InputStream;
      import java.beans.SimpleBeanInfo;
      
                  URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
                  InputStream is = null;
                  try {
                     is = url.openStream();
                     return ImageIO.read(is).getSource();
                  } catch(Exception e) {
                     e.printStackTrace();
                     return null;
                  } finally {
                     try { is.close(); } catch(Exception e) { } 
                  }
      
      Show
      Read the content directly such as with ImageIO.read(url.openStream().getSource() import java.awt.Image; import javax.imageio.ImageIO; import java.io.InputStream; import java.beans.SimpleBeanInfo; URL url = Thread .currentThread().getContextClassLoader().getResource(resourceName); InputStream is = null ; try { is = url.openStream(); return ImageIO.read(is).getSource(); } catch (Exception e) { e.printStackTrace(); return null ; } finally { try { is.close(); } catch (Exception e) { } }
    • Bugzilla Update:
      Perform

      Description

      URL.getContent() behaves differently within JBoss than it does in a normal JVM. In our case, when a jar is used in a normal JVM, URL.getContent() returns an ImageProducer. When the same jar is used from within JBoss, a VirtualFile is returned which then causes a ClassCastException.

      Note: This worked fine in JBoss 4.2.3.GA.

        Gliffy Diagrams

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  thofman Tomas Hofman
                  Reporter:
                  thofman Tomas Hofman
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  2 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: