home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / rmi / RMISecurityManager.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  2.7 KB  |  90 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)RMISecurityManager.java    1.23 98/07/15
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.rmi;
  16.  
  17. import java.security.*;
  18.  
  19. /**
  20.  * <code>RMISecurityManager</code> provides an example security manager for
  21.  * use by RMI applications that use downloaded code.  RMI's class loader will
  22.  * not download any classes from remote locations if no security manager has
  23.  * been set.  <code>RMISecurityManager</code> does not apply to applets, which
  24.  * run under the protection of their browser's security manager.
  25.  *
  26.  * <p>To use the <code>RMISecurityManager</code> in your application , add
  27.  * the following statement to your code (it needs to be executed before RMI
  28.  * can download code from remote hosts, so it most likely needs to appear
  29.  * in the <code>main</code> of your application):
  30.  *
  31.  * <pre>
  32.  * System.setSecurityManager(new RMISecurityManager());
  33.  * </pre>
  34.  *
  35.  * <p>The <code>RMISecurityManager</code> overrides several
  36.  * of <code>java.lang.SecurityManager</code>'s methods that deal with
  37.  * thread or package access.
  38.  *
  39.  * @version 1.23, 07/15/98
  40.  * @author  Roger Riggs
  41.  * @author  Peter Jones
  42.  * @since JDK1.1
  43.  */
  44. public class RMISecurityManager extends SecurityManager {
  45.  
  46.     /**
  47.      * Constructs a new <code>RMISecurityManager</code>.
  48.      * @since JDK1.1
  49.      */
  50.     public RMISecurityManager() {
  51.     }
  52.  
  53.     /**
  54.      * Throws a <code>SecurityException</code> if the 
  55.      * calling thread is not allowed to access the package specified by 
  56.      * the argument. 
  57.      * <p>
  58.      * This method is used by the <code>loadClass</code> method of class 
  59.      * loaders. 
  60.      *
  61.      * @param      pkg   the package name.
  62.      * @exception  SecurityException  if the caller does not have
  63.      *             permission to access the specified package.
  64.      * @see        java.lang.ClassLoader#loadClass(java.lang.String, boolean)
  65.      */
  66.     public void checkPackageAccess(final String pkgname) {
  67.     final boolean[] check = { false };
  68.     AccessController.doPrivileged(new PrivilegedAction(){
  69.         public Object run() {
  70.         int i;
  71.         String pkg = pkgname;
  72.         do {
  73.             String prop = "package.restrict.access." + pkg;
  74.             if (Boolean.getBoolean(prop)) {
  75.             check[0] = true;
  76.             break;
  77.             }
  78.             if ((i = pkg.lastIndexOf('.')) != -1) {
  79.             pkg = pkg.substring(0, i);
  80.             }
  81.         } while (i != -1);
  82.         return null;
  83.         }
  84.     });
  85.  
  86.         if (check[0])
  87.         super.checkPackageAccess(pkgname);
  88.     }
  89. }
  90.