< prev index next >

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

Print this page




  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 
  32 
  33 /**
  34  * A custom ClassLoader to load the concrete LoggerFinder class
  35  * with all permissions.
  36  *
  37  * @author danielfuchs
  38  */
  39 public class CustomSystemClassLoader extends ClassLoader {
  40 
  41 
  42     Class<?> loggerFinderClass = null;
  43 
  44     public CustomSystemClassLoader() {
  45         super();
  46     }
  47     public CustomSystemClassLoader(ClassLoader parent) {
  48         super(parent);
  49     }
  50 
  51     private Class<?> defineFinderClass(String name)
  52         throws ClassNotFoundException {


  53         final Object obj = getClassLoadingLock(name);
  54         synchronized(obj) {

  55             if (loggerFinderClass != null) return loggerFinderClass;
  56 
  57             URL url = this.getClass().getProtectionDomain().getCodeSource().getLocation();
  58             File file = new File(url.getPath(), name+".class");
  59             if (file.canRead()) {
  60                 try {
  61                     byte[] b = Files.readAllBytes(file.toPath());
  62                     Permissions perms = new Permissions();
  63                     perms.add(new AllPermission());
  64                     loggerFinderClass = defineClass(
  65                             name, b, 0, b.length, new ProtectionDomain(
  66                             this.getClass().getProtectionDomain().getCodeSource(),
  67                             perms));
  68                     System.out.println("Loaded " + name);

  69                     return loggerFinderClass;
  70                 } catch (Throwable ex) {
  71                     ex.printStackTrace();
  72                     throw new ClassNotFoundException(name, ex);
  73                 }
  74             } else {
  75                 throw new ClassNotFoundException(name,
  76                         new IOException(file.toPath() + ": can't read"));
  77             }
  78         }
  79     }
  80 
  81     @Override
  82     public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
  83         if (name.endsWith("$LogProducerFinder")) {
  84             Class<?> c = defineFinderClass(name);
  85             if (resolve) {
  86                 resolveClass(c);
  87             }
  88             return c;
  89         }
  90         return super.loadClass(name, resolve);
  91     }
  92 
  93     @Override
  94     protected Class<?> findClass(String name) throws ClassNotFoundException {
  95         if (name.endsWith("$$LogProducerFinder")) {
  96             return defineFinderClass(name);
  97         }
  98         return super.findClass(name);
  99     }
 100 
 101 }


  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.Map;
  32 import java.util.concurrent.ConcurrentHashMap;
  33 
  34 
  35 /**
  36  * A custom ClassLoader to load the concrete LoggerFinder class
  37  * with all permissions.
  38  *
  39  * @author danielfuchs
  40  */
  41 public class CustomSystemClassLoader extends ClassLoader {
  42 
  43 
  44     final ConcurrentHashMap<String, Class<?>> classes = new ConcurrentHashMap<>();
  45 
  46     public CustomSystemClassLoader() {
  47         super();
  48     }
  49     public CustomSystemClassLoader(ClassLoader parent) {
  50         super(parent);
  51     }
  52 
  53     private Class<?> defineFinderClass(String name)
  54         throws ClassNotFoundException {
  55         Class<?> loggerFinderClass = classes.get(name);
  56         if (loggerFinderClass != null) return loggerFinderClass;
  57         final Object obj = getClassLoadingLock(name);
  58         synchronized(obj) {
  59             loggerFinderClass = classes.get(name);
  60             if (loggerFinderClass != null) return loggerFinderClass;
  61 
  62             URL url = this.getClass().getProtectionDomain().getCodeSource().getLocation();
  63             File file = new File(url.getPath(), name+".class");
  64             if (file.canRead()) {
  65                 try {
  66                     byte[] b = Files.readAllBytes(file.toPath());
  67                     Permissions perms = new Permissions();
  68                     perms.add(new AllPermission());
  69                     loggerFinderClass = defineClass(
  70                             name, b, 0, b.length, new ProtectionDomain(
  71                             this.getClass().getProtectionDomain().getCodeSource(),
  72                             perms));
  73                     System.out.println("Loaded " + name);
  74                     classes.put(name, loggerFinderClass);
  75                     return loggerFinderClass;
  76                 } catch (Throwable ex) {
  77                     ex.printStackTrace();
  78                     throw new ClassNotFoundException(name, ex);
  79                 }
  80             } else {
  81                 throw new ClassNotFoundException(name,
  82                         new IOException(file.toPath() + ": can't read"));
  83             }
  84         }
  85     }
  86 
  87     @Override
  88     public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
  89         if (name.equals("LogProducerFinder") || name.startsWith("LogProducerFinder$")) {
  90             Class<?> c = defineFinderClass(name);
  91             if (resolve) {
  92                 resolveClass(c);
  93             }
  94             return c;
  95         }
  96         return super.loadClass(name, resolve);
  97     }
  98 
  99     @Override
 100     protected Class<?> findClass(String name) throws ClassNotFoundException {
 101         if (name.equals("LogProducerFinder") || name.startsWith("LogProducerFinder$")) {
 102             return defineFinderClass(name);
 103         }
 104         return super.findClass(name);
 105     }
 106 
 107 }
< prev index next >