SecureClassLoader.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* SecureClassLoader.java --- A Secure Class Loader
  2. Copyright (C) 1999 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.security;
  32. /**
  33. A Secure Class Loader for loading classes with additional
  34. support for specifying code source and permissions when
  35. they are retrieved by the system policy handler.
  36. @since JDK 1.2
  37. @author Mark Benvenuto
  38. */
  39. public class SecureClassLoader extends ClassLoader
  40. {
  41. protected SecureClassLoader(ClassLoader parent)
  42. {
  43. super(parent);
  44. SecurityManager sm = System.getSecurityManager();
  45. if(sm != null)
  46. sm.checkCreateClassLoader();
  47. }
  48. protected SecureClassLoader()
  49. {
  50. SecurityManager sm = System.getSecurityManager();
  51. if(sm != null)
  52. sm.checkCreateClassLoader();
  53. }
  54. /**
  55. Creates a class using an array of bytes and a
  56. CodeSource.
  57. @param name the name to give the class. null if unknown.
  58. @param b the data representing the classfile, in classfile format.
  59. @param off the offset into the data where the classfile starts.
  60. @param len the length of the classfile data in the array.
  61. @param cs the CodeSource for the class or null when unknown.
  62. @return the class that was defined and optional CodeSource.
  63. @exception ClassFormatError if the byte array is not in proper classfile format.
  64. */
  65. protected final Class defineClass(String name, byte[] b, int off, int len,
  66. CodeSource cs)
  67. {
  68. // FIXME: Need to cache ProtectionDomains according to 1.3 docs.
  69. if (cs != null)
  70. {
  71. ProtectionDomain protectionDomain
  72. = new ProtectionDomain(cs, getPermissions(cs));
  73. return super.defineClass(name, b, off, len, protectionDomain);
  74. }
  75. else
  76. return super.defineClass(name, b, off, len);
  77. }
  78. /**
  79. Returns a PermissionCollection for the specified CodeSource.
  80. The default implmentation invokes
  81. java.security.Policy.getPermissions.
  82. This method is called by defineClass that takes a CodeSource
  83. arguement to build a proper ProtectionDomain for the class
  84. being defined.
  85. */
  86. protected PermissionCollection getPermissions(CodeSource cs)
  87. {
  88. Policy policy = Policy.getPolicy();
  89. return policy.getPermissions(cs);
  90. }
  91. }