Source for java.security.AccessControlContext

   1: /* AccessControlContext.java --- Access Control Context Class
   2:    Copyright (C) 1999, 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package java.security;
  39: 
  40: import java.util.HashSet;
  41: 
  42: /**
  43:  * AccessControlContext makes system resource access decsion 
  44:  * based on permission rights.  
  45:  *
  46:  * It is used for a specific context and has only one method
  47:  * checkPermission. It is similar to AccessController except
  48:  * that it makes decsions based on the current context instead
  49:  * of the the current thread.
  50:  *
  51:  * It is created by call AccessController.getContext method.
  52:  *
  53:  * @author Mark Benvenuto
  54:  * @since 1.2
  55:  */
  56: public final class AccessControlContext
  57: {
  58:   private final ProtectionDomain[] protectionDomains;
  59:   private final DomainCombiner combiner;
  60: 
  61:   /**
  62:    * Construct a new AccessControlContext with the specified
  63:    * ProtectionDomains. <code>context</code> must not be 
  64:    * null and duplicates will be removed.
  65:    *
  66:    * @param context The ProtectionDomains to use
  67:    */
  68:   public AccessControlContext(ProtectionDomain[] context)
  69:   {
  70:     HashSet domains = new HashSet (context.length);
  71:     for (int i = 0; i < context.length; i++)
  72:       domains.add (context[i]);
  73:     protectionDomains = (ProtectionDomain[])
  74:       domains.toArray (new ProtectionDomain[domains.size()]);
  75:     combiner = null;
  76:   }
  77: 
  78:   /**
  79:    * Construct a new AccessControlContext with the specified
  80:    * ProtectionDomains and DomainCombiner
  81:    *
  82:    * @since 1.3
  83:    */
  84:   public AccessControlContext(AccessControlContext acc,
  85:                   DomainCombiner combiner)
  86:   {
  87:     // XXX check permission to call this.
  88:     AccessControlContext acc2 = AccessController.getContext();
  89:     protectionDomains = combiner.combine (acc2.protectionDomains,
  90:                                           acc.protectionDomains);
  91:     this.combiner = combiner;
  92:   }
  93: 
  94:   AccessControlContext (ProtectionDomain[] domains, AccessControlContext acc,
  95:                         DomainCombiner combiner)
  96:   {
  97:     protectionDomains = combiner.combine (domains, acc.protectionDomains);
  98:     this.combiner = combiner;
  99:   }
 100: 
 101:   /**
 102:    * Returns the Domain Combiner associated with the AccessControlContext
 103:    *
 104:    * @return the DomainCombiner
 105:    */
 106:   public DomainCombiner getDomainCombiner()
 107:   {
 108:     return combiner;
 109:   }
 110: 
 111:   /**
 112:    * Determines whether or not the specific permission is granted
 113:    * depending on the context it is within. 
 114:    *
 115:    * @param perm a permission to check
 116:    *
 117:    * @throws AccessControlException if the permssion is not permitted
 118:    */
 119:   public void checkPermission(Permission perm) throws AccessControlException
 120:   {
 121:     if (protectionDomains.length == 0)
 122:       throw new AccessControlException ("permission not granted");
 123:     for (int i = 0; i < protectionDomains.length; i++)
 124:       if (!protectionDomains[i].implies(perm))
 125:         throw new AccessControlException ("permission not granted");
 126:   }
 127: 
 128:   /**
 129:    * Checks if two AccessControlContexts are equal.
 130:    *
 131:    * It first checks if obj is an AccessControlContext class, and
 132:    * then checks if each ProtectionDomain matches.
 133:    *
 134:    * @param obj The object to compare this class to
 135:    *
 136:    * @return true if equal, false otherwise
 137:    */
 138:   public boolean equals(Object obj)
 139:   {
 140:     if (obj instanceof AccessControlContext)
 141:       {
 142:     AccessControlContext acc = (AccessControlContext) obj;
 143: 
 144:     if (acc.protectionDomains.length != protectionDomains.length)
 145:       return false;
 146: 
 147:         int i, j;
 148:         for (i = 0; i < protectionDomains.length; i++)
 149:           {
 150:             for (j = 0; j < acc.protectionDomains.length; j++)
 151:               {
 152:                 if (acc.protectionDomains[j].equals (protectionDomains[i]))
 153:                   break;
 154:               }
 155:             if (j == acc.protectionDomains.length)
 156:               return false;
 157:           }
 158:         return true;
 159:       }
 160:     return false;
 161:   }
 162: 
 163:   /**
 164:    * Computes a hash code of this class
 165:    *
 166:    * @return a hash code representing this class
 167:    */
 168:   public int hashCode()
 169:   {
 170:     int h = 0;
 171:     for (int i = 0; i < protectionDomains.length; i++)
 172:       h ^= protectionDomains[i].hashCode();
 173: 
 174:     return h;
 175:   }
 176: }