1 /* 2 * $Id$ 3 * 4 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. 5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6 * 7 * This code is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 only, as 9 * published by the Free Software Foundation. Oracle designates this 10 * particular file as subject to the "Classpath" exception as provided 11 * by Oracle in the LICENSE file that accompanied this code. 12 * 13 * This code is distributed in the hope that it will be useful, but WITHOUT 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 * version 2 for more details (a copy is included in the LICENSE file that 17 * accompanied this code). 18 * 19 * You should have received a copy of the GNU General Public License version 20 * 2 along with this work; if not, write to the Free Software Foundation, 21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22 * 23 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 24 * or visit www.oracle.com if you need additional information or have any 25 * questions. 26 */ 27 package com.sun.javatest.agent; 28 29 import java.io.ByteArrayInputStream; 30 import java.io.InputStream; 31 32 class AgentClassLoader extends ClassLoader { 33 AgentClassLoader(Agent.Task parent) { 34 this.parent = parent; 35 } 36 37 public synchronized Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException { 38 39 // check the cache first 40 Class<?> c = findLoadedClass(className); 41 42 // not found in the cache? 43 if (c == null) { 44 try { 45 ClassLoader cl = getClass().getClassLoader(); 46 if (cl != null) { 47 // if this class has a class loader, defer to that, 48 // (and assume it will call findSystemClass if necessary) 49 c = cl.loadClass(className); 50 } 51 else { 52 // this class must be loaded via system class loader, 53 // so go use that one 54 c = findSystemClass(className); 55 } 56 } 57 catch (ClassNotFoundException e) { 58 AgentRemoteClassData data = parent.getClassData(className); 59 c = defineClass(className, data.getByteData(), 0, data.getByteData().length); 60 } 61 } 62 63 if (resolve) 64 resolveClass(c); 65 66 return c; 67 } 68 69 public synchronized InputStream getResourceAsStream(String resourceName) { 70 // check local classpath first 71 // the resource should already be absolute, if we've got here 72 // through getClass().getResourceAsStream() 73 InputStream in = getClass().getResourceAsStream(resourceName); 74 if (in == null) { 75 try { 76 // if not found here, try remote load from Agent Manager 77 byte[] data = parent.getResourceData(resourceName); 78 in = new ByteArrayInputStream(data); 79 } 80 catch (Exception e) { 81 // ignore 82 } 83 } 84 return in; 85 } 86 87 private Agent.Task parent; 88 }