< prev index next >

test/jdk/java/lang/System/LoggerFinder/internal/BaseDefaultLoggerFinderTest/CustomSystemClassLoader.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.IOException;
  26 import java.net.URL;
  27 import java.nio.file.Files;
  28 import java.security.AllPermission;
  29 import java.security.Permissions;
  30 import java.security.ProtectionDomain;
  31 import java.util.Arrays;
  32 import java.util.HashMap;
  33 import java.util.List;
  34 import java.util.Map;

  35 
  36 
  37 /**
  38  * A custom ClassLoader to load the concrete LoggerFinder class
  39  * with all permissions. The CustomSystemClassLoader class must be
  40  * in the BCL, otherwise when system classes - such as
  41  * ZoneDateTime try to load their resource bundle a MissingResourceBundle
  42  * caused by a SecurityException may be thrown, as the CustomSystemClassLoader
  43  * code base will be found in the stack called by doPrivileged.
  44  *
  45  * @author danielfuchs
  46  */
  47 public class CustomSystemClassLoader extends ClassLoader {
  48 
  49 
  50     final List<String> finderClassNames =
  51             Arrays.asList("BaseDefaultLoggerFinderTest$BaseLoggerFinder");
  52     final Map<String, Class<?>> finderClasses = new HashMap<>();
  53     Class<?> testLoggerFinderClass;
  54 
  55     public CustomSystemClassLoader() {
  56         super();
  57     }
  58     public CustomSystemClassLoader(ClassLoader parent) {
  59         super(parent);
  60     }
  61 
  62     private Class<?> defineFinderClass(String name)
  63         throws ClassNotFoundException {



  64         final Object obj = getClassLoadingLock(name);
  65         synchronized(obj) {
  66             if (finderClasses.get(name) != null) return finderClasses.get(name);


  67             if (testLoggerFinderClass == null) {
  68                 // Hack: we  load testLoggerFinderClass to get its code source.
  69                 //       we can't use this.getClass() since we are in the boot.
  70                 testLoggerFinderClass = super.loadClass("BaseDefaultLoggerFinderTest$TestLoggerFinder");
  71             }
  72             URL url = testLoggerFinderClass.getProtectionDomain().getCodeSource().getLocation();
  73             File file = new File(url.getPath(), name+".class");
  74             if (file.canRead()) {
  75                 try {
  76                     byte[] b = Files.readAllBytes(file.toPath());
  77                     Permissions perms = new Permissions();
  78                     perms.add(new AllPermission());
  79                     Class<?> finderClass = defineClass(
  80                             name, b, 0, b.length, new ProtectionDomain(
  81                             this.getClass().getProtectionDomain().getCodeSource(),
  82                             perms));
  83                     System.out.println("Loaded " + name);
  84                     finderClasses.put(name, finderClass);
  85                     return finderClass;
  86                 } catch (Throwable ex) {
  87                     ex.printStackTrace();
  88                     throw new ClassNotFoundException(name, ex);
  89                 }
  90             } else {
  91                 throw new ClassNotFoundException(name,
  92                         new IOException(file.toPath() + ": can't read"));
  93             }
  94         }
  95     }
  96 




  97     @Override
  98     public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
  99         if (finderClassNames.contains(name)) {
 100             Class<?> c = defineFinderClass(name);
 101             if (resolve) {
 102                 resolveClass(c);
 103             }
 104             return c;
 105         }
 106         return super.loadClass(name, resolve);
 107     }
 108 
 109     @Override
 110     protected Class<?> findClass(String name) throws ClassNotFoundException {
 111         if (finderClassNames.contains(name)) {
 112             return defineFinderClass(name);
 113         }
 114         return super.findClass(name);
 115     }
 116 
 117 }
   1 /*
   2  * Copyright (c) 2015, 2018, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.IOException;
  26 import java.net.URL;
  27 import java.nio.file.Files;
  28 import java.security.AllPermission;
  29 import java.security.Permissions;
  30 import java.security.ProtectionDomain;
  31 import java.util.Arrays;
  32 import java.util.HashMap;
  33 import java.util.List;
  34 import java.util.Map;
  35 import java.util.concurrent.ConcurrentHashMap;
  36 
  37 
  38 /**
  39  * A custom ClassLoader to load the concrete LoggerFinder class
  40  * with all permissions. The CustomSystemClassLoader class must be
  41  * in the BCL, otherwise when system classes - such as
  42  * ZoneDateTime try to load their resource bundle a MissingResourceBundle
  43  * caused by a SecurityException may be thrown, as the CustomSystemClassLoader
  44  * code base will be found in the stack called by doPrivileged.
  45  *
  46  * @author danielfuchs
  47  */
  48 public class CustomSystemClassLoader extends ClassLoader {
  49 
  50 
  51     final List<String> finderClassNames =
  52             Arrays.asList("BaseLoggerFinder");
  53     final Map<String, Class<?>> finderClasses = new ConcurrentHashMap<>();
  54     Class<?> testLoggerFinderClass;
  55 
  56     public CustomSystemClassLoader() {
  57         super();
  58     }
  59     public CustomSystemClassLoader(ClassLoader parent) {
  60         super(parent);
  61     }
  62 
  63     private Class<?> defineFinderClass(String name)
  64         throws ClassNotFoundException {
  65         Class<?> finderClass =  finderClasses.get(name);
  66         if (finderClass != null) return finderClass;
  67 
  68         final Object obj = getClassLoadingLock(name);
  69         synchronized(obj) {
  70             finderClasses.get(name);
  71             if (finderClass != null) return finderClass;
  72 
  73             if (testLoggerFinderClass == null) {
  74                 // Hack: we  load testLoggerFinderClass to get its code source.
  75                 //       we can't use this.getClass() since we are in the boot.
  76                 testLoggerFinderClass = super.loadClass("BaseDefaultLoggerFinderTest$TestLoggerFinder");
  77             }
  78             URL url = testLoggerFinderClass.getProtectionDomain().getCodeSource().getLocation();
  79             File file = new File(url.getPath(), name+".class");
  80             if (file.canRead()) {
  81                 try {
  82                     byte[] b = Files.readAllBytes(file.toPath());
  83                     Permissions perms = new Permissions();
  84                     perms.add(new AllPermission());
  85                     finderClass = defineClass(
  86                             name, b, 0, b.length, new ProtectionDomain(
  87                             this.getClass().getProtectionDomain().getCodeSource(),
  88                             perms));
  89                     System.out.println("Loaded " + name);
  90                     finderClasses.put(name, finderClass);
  91                     return finderClass;
  92                 } catch (Throwable ex) {
  93                     ex.printStackTrace();
  94                     throw new ClassNotFoundException(name, ex);
  95                 }
  96             } else {
  97                 throw new ClassNotFoundException(name,
  98                         new IOException(file.toPath() + ": can't read"));
  99             }
 100         }
 101     }
 102 
 103     private static boolean matches(String prefix, String name) {
 104         return prefix.equals(name) || name.startsWith(prefix + "$");
 105     }
 106 
 107     @Override
 108     public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
 109         if (finderClassNames.stream().anyMatch(n -> matches(n, name))) {
 110             Class<?> c = defineFinderClass(name);
 111             if (resolve) {
 112                 resolveClass(c);
 113             }
 114             return c;
 115         }
 116         return super.loadClass(name, resolve);
 117     }
 118 
 119     @Override
 120     protected Class<?> findClass(String name) throws ClassNotFoundException {
 121         if (finderClassNames.stream().anyMatch(n -> matches(n, name))) {
 122             return defineFinderClass(name);
 123         }
 124         return super.findClass(name);
 125     }
 126 
 127 }
< prev index next >