src/share/classes/java/net/URLClassLoader.java

Print this page
rev 10449 : [mq]: cds
rev 10450 : mq
   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 390             // Package found, so check package sealing.
 391             if (pkg.isSealed()) {
 392                 // Verify that code source URL is the same.
 393                 if (!pkg.isSealed(url)) {
 394                     throw new SecurityException(
 395                         "sealing violation: package " + pkgname + " is sealed");
 396                 }
 397             } else {
 398                 // Make sure we are not attempting to seal the package
 399                 // at this code source URL.
 400                 if ((man != null) && isSealed(pkgname, man)) {
 401                     throw new SecurityException(
 402                         "sealing violation: can't seal package " + pkgname +
 403                         ": already loaded");
 404                 }
 405             }
 406         }
 407         return pkg;
 408     }
 409 
 410     /*
 411      * Defines a Class using the class bytes obtained from the specified
 412      * Resource. The resulting Class must be resolved before it can be
 413      * used.
 414      */
 415     private Class<?> defineClass(String name, Resource res) throws IOException {
 416         long t0 = System.nanoTime();
 417         int i = name.lastIndexOf('.');
 418         URL url = res.getCodeSourceURL();
 419         if (i != -1) {
 420             String pkgname = name.substring(0, i);
 421             // Check if package already loaded.
 422             Manifest man = res.getManifest();
 423             if (getAndVerifyPackage(pkgname, man, url) == null) {
 424                 try {
 425                     if (man != null) {
 426                         definePackage(pkgname, man, url);
 427                     } else {
 428                         definePackage(pkgname, null, null, null, null, null, null, null);
 429                     }
 430                 } catch (IllegalArgumentException iae) {
 431                     // parallel-capable class loaders: re-verify in case of a
 432                     // race condition
 433                     if (getAndVerifyPackage(pkgname, man, url) == null) {
 434                         // Should never happen
 435                         throw new AssertionError("Cannot find package " +
 436                                                  pkgname);
 437                     }
 438                 }
 439             }




















 440         }
 441         // Now read the class bytes and define the class
 442         java.nio.ByteBuffer bb = res.getByteBuffer();
 443         if (bb != null) {
 444             // Use (direct) ByteBuffer:
 445             CodeSigner[] signers = res.getCodeSigners();
 446             CodeSource cs = new CodeSource(url, signers);
 447             sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
 448             return defineClass(name, bb, cs);
 449         } else {
 450             byte[] b = res.getBytes();
 451             // must read certificates AFTER reading bytes.
 452             CodeSigner[] signers = res.getCodeSigners();
 453             CodeSource cs = new CodeSource(url, signers);
 454             sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
 455             return defineClass(name, b, 0, b.length, cs);
 456         }
 457     }
 458 
 459     /**


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 390             // Package found, so check package sealing.
 391             if (pkg.isSealed()) {
 392                 // Verify that code source URL is the same.
 393                 if (!pkg.isSealed(url)) {
 394                     throw new SecurityException(
 395                         "sealing violation: package " + pkgname + " is sealed");
 396                 }
 397             } else {
 398                 // Make sure we are not attempting to seal the package
 399                 // at this code source URL.
 400                 if ((man != null) && isSealed(pkgname, man)) {
 401                     throw new SecurityException(
 402                         "sealing violation: can't seal package " + pkgname +
 403                         ": already loaded");
 404                 }
 405             }
 406         }
 407         return pkg;
 408     }
 409 
 410     // Also called by VM to define Package for classes loaded from the CDS
 411     // archive
 412     private void definePackageInternal(String pkgname, Manifest man, URL url)
 413     {









 414         if (getAndVerifyPackage(pkgname, man, url) == null) {
 415             try {
 416                 if (man != null) {
 417                     definePackage(pkgname, man, url);
 418                 } else {
 419                     definePackage(pkgname, null, null, null, null, null, null, null);
 420                 }
 421             } catch (IllegalArgumentException iae) {
 422                 // parallel-capable class loaders: re-verify in case of a
 423                 // race condition
 424                 if (getAndVerifyPackage(pkgname, man, url) == null) {
 425                     // Should never happen
 426                     throw new AssertionError("Cannot find package " +
 427                                              pkgname);
 428                 }
 429             }
 430         }
 431     }
 432 
 433     /*
 434      * Defines a Class using the class bytes obtained from the specified
 435      * Resource. The resulting Class must be resolved before it can be
 436      * used.
 437      *
 438      * NOTE: the logic used here has been duplicated in the VM native code
 439      * (search for invocation of definePackageInternal in the HotSpot sources).
 440      * If this is changed, the VM code also need to be modified.
 441      */
 442     private Class<?> defineClass(String name, Resource res) throws IOException {
 443         long t0 = System.nanoTime();
 444         int i = name.lastIndexOf('.');
 445         URL url = res.getCodeSourceURL();
 446         if (i != -1) {
 447             String pkgname = name.substring(0, i);
 448             // Check if package already loaded.
 449             Manifest man = res.getManifest();
 450             definePackageInternal(pkgname, man, url);
 451         }
 452         // Now read the class bytes and define the class
 453         java.nio.ByteBuffer bb = res.getByteBuffer();
 454         if (bb != null) {
 455             // Use (direct) ByteBuffer:
 456             CodeSigner[] signers = res.getCodeSigners();
 457             CodeSource cs = new CodeSource(url, signers);
 458             sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
 459             return defineClass(name, bb, cs);
 460         } else {
 461             byte[] b = res.getBytes();
 462             // must read certificates AFTER reading bytes.
 463             CodeSigner[] signers = res.getCodeSigners();
 464             CodeSource cs = new CodeSource(url, signers);
 465             sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
 466             return defineClass(name, b, 0, b.length, cs);
 467         }
 468     }
 469 
 470     /**