Sun, 15 Dec 2013 23:35:45 +0100
8025152: Enhance activation set up
8028388: 9 jaxws tests failed in nightly build with java.lang.ClassCastException
Summary: fix also reviewed by Bill Shannon, Alexander Fomin
Reviewed-by: dfuchs, hawtin, mgrebac
Contributed-by: bill.shannon@oracle.com
ohair@286 | 1 | /* |
alanb@368 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.ws.encoding; |
ohair@286 | 27 | |
ohair@286 | 28 | import javax.activation.DataContentHandler; |
ohair@286 | 29 | import javax.activation.ActivationDataFlavor; |
ohair@286 | 30 | import javax.activation.DataSource; |
ohair@286 | 31 | import javax.imageio.ImageIO; |
ohair@286 | 32 | import javax.imageio.ImageWriter; |
ohair@286 | 33 | import javax.imageio.stream.ImageOutputStream; |
ohair@286 | 34 | import java.awt.*; |
ohair@286 | 35 | import java.awt.image.BufferedImage; |
ohair@286 | 36 | import java.awt.datatransfer.DataFlavor; |
ohair@286 | 37 | import java.util.logging.Logger; |
ohair@286 | 38 | import java.util.Iterator; |
ohair@286 | 39 | import java.io.IOException; |
ohair@286 | 40 | import java.io.BufferedInputStream; |
ohair@286 | 41 | import java.io.OutputStream; |
ohair@286 | 42 | import java.util.Arrays; |
ohair@286 | 43 | |
ohair@286 | 44 | /** |
ohair@286 | 45 | * @author Jitendra Kotamraju |
ohair@286 | 46 | */ |
ohair@286 | 47 | |
ohair@286 | 48 | public class ImageDataContentHandler extends Component |
ohair@286 | 49 | implements DataContentHandler { |
ohair@286 | 50 | |
ohair@286 | 51 | private static final Logger log = Logger.getLogger(ImageDataContentHandler.class.getName()); |
ohair@286 | 52 | private final DataFlavor[] flavor; |
ohair@286 | 53 | |
ohair@286 | 54 | public ImageDataContentHandler() { |
ohair@286 | 55 | String[] mimeTypes = ImageIO.getReaderMIMETypes(); |
ohair@286 | 56 | flavor = new DataFlavor[mimeTypes.length]; |
ohair@286 | 57 | for(int i=0; i < mimeTypes.length; i++) { |
ohair@286 | 58 | flavor[i] = new ActivationDataFlavor(Image.class, mimeTypes[i], "Image"); |
ohair@286 | 59 | } |
ohair@286 | 60 | } |
ohair@286 | 61 | |
ohair@286 | 62 | /** |
ohair@286 | 63 | * Returns an array of DataFlavor objects indicating the flavors the |
ohair@286 | 64 | * data can be provided in. The array should be ordered according to |
ohair@286 | 65 | * preference for providing the data (from most richly descriptive to |
ohair@286 | 66 | * least descriptive). |
ohair@286 | 67 | * |
ohair@286 | 68 | * @return The DataFlavors. |
ohair@286 | 69 | */ |
ohair@286 | 70 | public DataFlavor[] getTransferDataFlavors() { |
ohair@286 | 71 | return Arrays.copyOf(flavor, flavor.length); |
ohair@286 | 72 | } |
ohair@286 | 73 | |
ohair@286 | 74 | /** |
ohair@286 | 75 | * Returns an object which represents the data to be transferred. |
ohair@286 | 76 | * The class of the object returned is defined by the representation class |
ohair@286 | 77 | * of the flavor. |
ohair@286 | 78 | * |
ohair@286 | 79 | * @param df The DataFlavor representing the requested type. |
ohair@286 | 80 | * @param ds The DataSource representing the data to be converted. |
ohair@286 | 81 | * @return The constructed Object. |
ohair@286 | 82 | */ |
ohair@286 | 83 | public Object getTransferData(DataFlavor df, DataSource ds) |
ohair@286 | 84 | throws IOException { |
ohair@286 | 85 | for (DataFlavor aFlavor : flavor) { |
ohair@286 | 86 | if (aFlavor.equals(df)) { |
ohair@286 | 87 | return getContent(ds); |
ohair@286 | 88 | } |
ohair@286 | 89 | } |
ohair@286 | 90 | return null; |
ohair@286 | 91 | } |
ohair@286 | 92 | |
ohair@286 | 93 | /** |
ohair@286 | 94 | * Return an object representing the data in its most preferred form. |
ohair@286 | 95 | * Generally this will be the form described by the first DataFlavor |
ohair@286 | 96 | * returned by the <code>getTransferDataFlavors</code> method. |
ohair@286 | 97 | * |
ohair@286 | 98 | * @param ds The DataSource representing the data to be converted. |
ohair@286 | 99 | * @return The constructed Object. |
ohair@286 | 100 | */ |
ohair@286 | 101 | public Object getContent(DataSource ds) throws IOException { |
ohair@286 | 102 | return ImageIO.read(new BufferedInputStream(ds.getInputStream())); |
ohair@286 | 103 | } |
ohair@286 | 104 | |
ohair@286 | 105 | /** |
ohair@286 | 106 | * Convert the object to a byte stream of the specified MIME type |
ohair@286 | 107 | * and write it to the output stream. |
ohair@286 | 108 | * |
ohair@286 | 109 | * @param obj The object to be converted. |
ohair@286 | 110 | * @param type The requested MIME type of the resulting byte stream. |
ohair@286 | 111 | * @param os The output stream into which to write the converted |
ohair@286 | 112 | * byte stream. |
ohair@286 | 113 | */ |
ohair@286 | 114 | |
ohair@286 | 115 | public void writeTo(Object obj, String type, OutputStream os) |
ohair@286 | 116 | throws IOException { |
ohair@286 | 117 | |
ohair@286 | 118 | try { |
ohair@286 | 119 | BufferedImage bufImage; |
ohair@286 | 120 | if (obj instanceof BufferedImage) { |
ohair@286 | 121 | bufImage = (BufferedImage)obj; |
ohair@286 | 122 | } else if (obj instanceof Image) { |
ohair@286 | 123 | bufImage = render((Image)obj); |
ohair@286 | 124 | } else { |
ohair@286 | 125 | throw new IOException( |
ohair@286 | 126 | "ImageDataContentHandler requires Image object, " |
ohair@286 | 127 | + "was given object of type " |
ohair@286 | 128 | + obj.getClass().toString()); |
ohair@286 | 129 | } |
ohair@286 | 130 | ImageWriter writer = null; |
ohair@286 | 131 | Iterator<ImageWriter> i = ImageIO.getImageWritersByMIMEType(type); |
ohair@286 | 132 | if (i.hasNext()) { |
ohair@286 | 133 | writer = i.next(); |
ohair@286 | 134 | } |
ohair@286 | 135 | if (writer != null) { |
ohair@286 | 136 | ImageOutputStream stream = ImageIO.createImageOutputStream(os); |
ohair@286 | 137 | writer.setOutput(stream); |
ohair@286 | 138 | writer.write(bufImage); |
ohair@286 | 139 | writer.dispose(); |
ohair@286 | 140 | stream.close(); |
ohair@286 | 141 | } else { |
ohair@286 | 142 | throw new IOException("Unsupported mime type:"+ type); |
ohair@286 | 143 | } |
ohair@286 | 144 | } catch (Exception e) { |
ohair@286 | 145 | throw new IOException("Unable to encode the image to a stream " |
ohair@286 | 146 | + e.getMessage()); |
ohair@286 | 147 | } |
ohair@286 | 148 | } |
ohair@286 | 149 | |
ohair@286 | 150 | |
ohair@286 | 151 | private BufferedImage render(Image img) throws InterruptedException { |
ohair@286 | 152 | |
ohair@286 | 153 | MediaTracker tracker = new MediaTracker(this); |
ohair@286 | 154 | tracker.addImage(img, 0); |
ohair@286 | 155 | tracker.waitForAll(); |
ohair@286 | 156 | BufferedImage bufImage = new BufferedImage(img.getWidth(null), |
ohair@286 | 157 | img.getHeight(null), BufferedImage.TYPE_INT_RGB); |
ohair@286 | 158 | Graphics g = bufImage.createGraphics(); |
ohair@286 | 159 | g.drawImage(img, 0, 0, null); |
ohair@286 | 160 | g.dispose(); |
ohair@286 | 161 | return bufImage; |
ohair@286 | 162 | } |
ohair@286 | 163 | |
ohair@286 | 164 | } |