985N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
985N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
985N/A *
985N/A * This code is free software; you can redistribute it and/or modify it
985N/A * under the terms of the GNU General Public License version 2 only, as
985N/A * published by the Free Software Foundation.
985N/A *
985N/A * This code is distributed in the hope that it will be useful, but WITHOUT
985N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
985N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
985N/A * version 2 for more details (a copy is included in the LICENSE file that
985N/A * accompanied this code).
985N/A *
985N/A * You should have received a copy of the GNU General Public License version
985N/A * 2 along with this work; if not, write to the Free Software Foundation,
985N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
985N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
985N/A */
985N/A
985N/Apackage testapp;
985N/A
985N/Aimport java.io.ByteArrayInputStream;
985N/Aimport java.io.File;
985N/Aimport java.io.IOException;
985N/Aimport java.io.InputStream;
985N/Aimport java.util.HashMap;
985N/Aimport java.util.concurrent.ConcurrentLinkedQueue;
985N/Aimport javax.imageio.stream.FileCacheImageInputStream;
985N/Aimport javax.imageio.stream.ImageInputStream;
985N/A
985N/Apublic class Main {
985N/A
985N/A public static void main(String[] args) {
985N/A Main o = new Main("testapp.some.class", null);
985N/A o.launch(null);
985N/A }
985N/A
985N/A private final String uniqClassName;
985N/A private final ConcurrentLinkedQueue<Throwable> problems;
985N/A
985N/A public Main(String uniq, ConcurrentLinkedQueue<Throwable> p) {
985N/A uniqClassName = uniq;
985N/A problems = p;
985N/A }
985N/A
985N/A public void launch(HashMap<String, ImageInputStream> refs) {
985N/A System.out.printf("%s: current context class loader: %s\n",
985N/A uniqClassName,
985N/A Thread.currentThread().getContextClassLoader());
985N/A try {
985N/A byte[] data = new byte[1024];
985N/A ByteArrayInputStream bais = new ByteArrayInputStream(data);
985N/A MyImageInputStream iis = new MyImageInputStream(bais,
985N/A uniqClassName,
985N/A problems);
985N/A if (refs != null) {
985N/A System.out.printf("%s: added to strong store\n",
985N/A uniqClassName);
985N/A refs.put(uniqClassName, iis);
985N/A }
985N/A iis.read();
985N/A //leave stream open : let's shutdown hook work!
985N/A } catch (IOException e) {
985N/A problems.add(e);
985N/A }
985N/A }
985N/A
985N/A private static class MyImageInputStream extends FileCacheImageInputStream {
985N/A private final String uniqClassName;
985N/A private ConcurrentLinkedQueue<Throwable> problems;
985N/A public MyImageInputStream(InputStream is, String uniq,
985N/A ConcurrentLinkedQueue<Throwable> p) throws IOException
985N/A {
985N/A super(is, new File("tmp"));
985N/A uniqClassName = uniq;
985N/A problems = p;
985N/A }
985N/A
985N/A @Override
985N/A public void close() throws IOException {
985N/A Test t = new Test();
985N/A try {
985N/A t.doTest(uniqClassName);
985N/A } catch (Throwable e) {
985N/A problems.add(e);
985N/A }
985N/A
985N/A super.close();
985N/A
985N/A problems = null;
985N/A }
985N/A }
985N/A}
985N/A
985N/Aclass Test {
985N/A public void doTest(String uniqClassName) throws ClassNotFoundException {
985N/A System.out.printf("%s: Current thread: %s\n", uniqClassName,
985N/A Thread.currentThread());
985N/A
985N/A ClassLoader thisCL = this.getClass().getClassLoader();
985N/A Class uniq = thisCL.loadClass(uniqClassName);
985N/A
985N/A System.out.printf("%s: test is done!\n",uniqClassName);
985N/A }
985N/A}