< prev index next >

src/java.base/share/classes/java/security/Permissions.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.security;
  27 
  28 import java.io.InvalidObjectException;
  29 import java.io.IOException;
  30 import java.io.ObjectInputStream;
  31 import java.io.ObjectOutputStream;
  32 import java.io.ObjectStreamField;
  33 import java.io.Serializable;
  34 import java.util.Enumeration;
  35 import java.util.HashMap;
  36 import java.util.Hashtable;
  37 import java.util.Iterator;
  38 import java.util.List;
  39 import java.util.Map;
  40 import java.util.NoSuchElementException;





  41 import java.util.concurrent.ConcurrentHashMap;






  42 
  43 /**
  44  * This class represents a heterogeneous collection of Permissions. That is,
  45  * it contains different types of Permission objects, organized into
  46  * PermissionCollections. For example, if any
  47  * {@code java.io.FilePermission} objects are added to an instance of
  48  * this class, they are all stored in a single
  49  * PermissionCollection. It is the PermissionCollection returned by a call to
  50  * the {@code newPermissionCollection} method in the FilePermission class.
  51  * Similarly, any {@code java.lang.RuntimePermission} objects are
  52  * stored in the PermissionCollection returned by a call to the
  53  * {@code newPermissionCollection} method in the
  54  * RuntimePermission class. Thus, this class represents a collection of
  55  * PermissionCollections.
  56  *
  57  * <p>When the {@code add} method is called to add a Permission, the
  58  * Permission is stored in the appropriate PermissionCollection. If no such
  59  * collection exists yet, the Permission object's class is determined and the
  60  * {@code newPermissionCollection} method is called on that class to create
  61  * the PermissionCollection and add it to the Permissions object. If


 374      */
 375     private void readObject(ObjectInputStream in) throws IOException,
 376     ClassNotFoundException {
 377         // Don't call defaultReadObject()
 378 
 379         // Read in serialized fields
 380         ObjectInputStream.GetField gfields = in.readFields();
 381 
 382         // Get allPermission
 383         allPermission = (PermissionCollection) gfields.get("allPermission", null);
 384 
 385         // Get permissions
 386         // writeObject writes a Hashtable<Class<?>, PermissionCollection> for
 387         // the perms key, so this cast is safe, unless the data is corrupt.
 388         @SuppressWarnings("unchecked")
 389         Hashtable<Class<?>, PermissionCollection> perms =
 390             (Hashtable<Class<?>, PermissionCollection>)gfields.get("perms", null);
 391         permsMap = new ConcurrentHashMap<>(perms.size()*2);
 392         permsMap.putAll(perms);
 393 
 394         // Check that Class is mapped to PermissionCollection containing
 395         // Permissions of the same class
 396         for (Map.Entry<Class<?>, PermissionCollection> e : perms.entrySet()) {
 397             Class<?> k = e.getKey();
 398             PermissionCollection v = e.getValue();
 399             Enumeration<Permission> en = v.elements();
 400             while (en.hasMoreElements()) {
 401                 Permission p = en.nextElement();
 402                 if (!k.equals(p.getClass())) {
 403                     throw new InvalidObjectException("Permission with class " +
 404                         k + " incorrectly mapped to PermissionCollection " +
 405                         "containing Permission with " + p.getClass());
 406                 }
 407             }
 408         }
 409 
 410         // Set hasUnresolved
 411         UnresolvedPermissionCollection uc =
 412         (UnresolvedPermissionCollection) permsMap.get(UnresolvedPermission.class);
 413         hasUnresolved = (uc != null && uc.elements().hasMoreElements());
 414     }
 415 }
 416 
 417 final class PermissionsEnumerator implements Enumeration<Permission> {
 418 
 419     // all the perms
 420     private Iterator<PermissionCollection> perms;
 421     // the current set
 422     private Enumeration<Permission> permset;
 423 
 424     PermissionsEnumerator(Iterator<PermissionCollection> e) {
 425         perms = e;
 426         permset = getNextEnumWithMore();
 427     }
 428 
 429     // No need to synchronize; caller should sync on object as required


 582 
 583     /*
 584      * Reads in a Hashtable of Permission/Permission and saves them in the
 585      * permsMap field.
 586      */
 587     private void readObject(ObjectInputStream in) throws IOException,
 588     ClassNotFoundException {
 589         // Don't call defaultReadObject()
 590 
 591         // Read in serialized fields
 592         ObjectInputStream.GetField gfields = in.readFields();
 593 
 594         // Get permissions
 595         // writeObject writes a Hashtable<Class<?>, PermissionCollection> for
 596         // the perms key, so this cast is safe, unless the data is corrupt.
 597         @SuppressWarnings("unchecked")
 598         Hashtable<Permission, Permission> perms =
 599                 (Hashtable<Permission, Permission>)gfields.get("perms", null);
 600         permsMap = new ConcurrentHashMap<>(perms.size()*2);
 601         permsMap.putAll(perms);
 602 
 603         // check that the Permission key and value are the same object
 604         for (Map.Entry<Permission, Permission> e : perms.entrySet()) {
 605             Permission k = e.getKey();
 606             Permission v = e.getValue();
 607             if (k != v) {
 608                 throw new InvalidObjectException("Permission (" + k +
 609                     ") incorrectly mapped to Permission (" + v + ")");
 610             }
 611         }
 612     }
 613 }
   1 /*
   2  * Copyright (c) 1997, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.security;
  27 






  28 import java.util.Enumeration;

  29 import java.util.Hashtable;



  30 import java.util.NoSuchElementException;
  31 import java.util.Map;
  32 import java.util.HashMap;
  33 import java.util.List;
  34 import java.util.Iterator;
  35 import java.util.Collections;
  36 import java.util.concurrent.ConcurrentHashMap;
  37 import java.io.Serializable;
  38 import java.io.ObjectStreamField;
  39 import java.io.ObjectOutputStream;
  40 import java.io.ObjectInputStream;
  41 import java.io.IOException;
  42 
  43 
  44 /**
  45  * This class represents a heterogeneous collection of Permissions. That is,
  46  * it contains different types of Permission objects, organized into
  47  * PermissionCollections. For example, if any
  48  * {@code java.io.FilePermission} objects are added to an instance of
  49  * this class, they are all stored in a single
  50  * PermissionCollection. It is the PermissionCollection returned by a call to
  51  * the {@code newPermissionCollection} method in the FilePermission class.
  52  * Similarly, any {@code java.lang.RuntimePermission} objects are
  53  * stored in the PermissionCollection returned by a call to the
  54  * {@code newPermissionCollection} method in the
  55  * RuntimePermission class. Thus, this class represents a collection of
  56  * PermissionCollections.
  57  *
  58  * <p>When the {@code add} method is called to add a Permission, the
  59  * Permission is stored in the appropriate PermissionCollection. If no such
  60  * collection exists yet, the Permission object's class is determined and the
  61  * {@code newPermissionCollection} method is called on that class to create
  62  * the PermissionCollection and add it to the Permissions object. If


 375      */
 376     private void readObject(ObjectInputStream in) throws IOException,
 377     ClassNotFoundException {
 378         // Don't call defaultReadObject()
 379 
 380         // Read in serialized fields
 381         ObjectInputStream.GetField gfields = in.readFields();
 382 
 383         // Get allPermission
 384         allPermission = (PermissionCollection) gfields.get("allPermission", null);
 385 
 386         // Get permissions
 387         // writeObject writes a Hashtable<Class<?>, PermissionCollection> for
 388         // the perms key, so this cast is safe, unless the data is corrupt.
 389         @SuppressWarnings("unchecked")
 390         Hashtable<Class<?>, PermissionCollection> perms =
 391             (Hashtable<Class<?>, PermissionCollection>)gfields.get("perms", null);
 392         permsMap = new ConcurrentHashMap<>(perms.size()*2);
 393         permsMap.putAll(perms);
 394 
















 395         // Set hasUnresolved
 396         UnresolvedPermissionCollection uc =
 397         (UnresolvedPermissionCollection) permsMap.get(UnresolvedPermission.class);
 398         hasUnresolved = (uc != null && uc.elements().hasMoreElements());
 399     }
 400 }
 401 
 402 final class PermissionsEnumerator implements Enumeration<Permission> {
 403 
 404     // all the perms
 405     private Iterator<PermissionCollection> perms;
 406     // the current set
 407     private Enumeration<Permission> permset;
 408 
 409     PermissionsEnumerator(Iterator<PermissionCollection> e) {
 410         perms = e;
 411         permset = getNextEnumWithMore();
 412     }
 413 
 414     // No need to synchronize; caller should sync on object as required


 567 
 568     /*
 569      * Reads in a Hashtable of Permission/Permission and saves them in the
 570      * permsMap field.
 571      */
 572     private void readObject(ObjectInputStream in) throws IOException,
 573     ClassNotFoundException {
 574         // Don't call defaultReadObject()
 575 
 576         // Read in serialized fields
 577         ObjectInputStream.GetField gfields = in.readFields();
 578 
 579         // Get permissions
 580         // writeObject writes a Hashtable<Class<?>, PermissionCollection> for
 581         // the perms key, so this cast is safe, unless the data is corrupt.
 582         @SuppressWarnings("unchecked")
 583         Hashtable<Permission, Permission> perms =
 584                 (Hashtable<Permission, Permission>)gfields.get("perms", null);
 585         permsMap = new ConcurrentHashMap<>(perms.size()*2);
 586         permsMap.putAll(perms);










 587     }
 588 }
< prev index next >