< prev index next >

## src/java.base/share/classes/javax/security/auth/Subject.java

Print this page

       1 /*
       2  * Copyright (c) 1998, 2020, 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
     932             while (pubCIterator.hasNext()) {
     933                 hashCode ^= getCredHashCode(pubCIterator.next());
     934             }
     935         }
     936         return hashCode;
     937     }
     938
     939     /**
     940      * get a credential's hashcode
     941      */
     942     private int getCredHashCode(Object o) {
     943         try {
     944             return o.hashCode();
     945         } catch (IllegalStateException ise) {
     946             return o.getClass().toString().hashCode();
     947         }
     948     }
     949
     950     /**
     951      * Writes this object out to a stream (i.e., serializes it).

     952      */
     953     @java.io.Serial
     954     private void writeObject(java.io.ObjectOutputStream oos)
     955                 throws java.io.IOException {
     956         synchronized(principals) {
     957             oos.defaultWriteObject();
     958         }
     959     }
     960
     961     /**
     962      * Reads this object from a stream (i.e., deserializes it)

     963      */
     964     @SuppressWarnings("unchecked")
     965     @java.io.Serial
     966     private void readObject(java.io.ObjectInputStream s)
     967                 throws java.io.IOException, ClassNotFoundException {
     968
     969         ObjectInputStream.GetField gf = s.readFields();
     970
     971         readOnly = gf.get("readOnly", false);
     972
     973         Set<Principal> inputPrincs = (Set<Principal>)gf.get("principals", null);
     974
     975         Objects.requireNonNull(inputPrincs,
     976                 ResourcesMgr.getString("invalid.null.input.s."));
     977
     978         // Rewrap the principals into a SecureSet
     979         try {
     980             LinkedList<Principal> principalList = collectionNullClean(inputPrincs);
     981             principals = Collections.synchronizedSet(new SecureSet<>
     982                                 (this, PRINCIPAL_SET, principalList));
    1391         public int hashCode() {
    1392             int h = 0;
    1393             Iterator<E> i = iterator();
    1394             while (i.hasNext()) {
    1395                 E obj = i.next();
    1396                 if (obj != null) {
    1397                     h += obj.hashCode();
    1398                 }
    1399             }
    1400             return h;
    1401         }
    1402
    1403         /**
    1404          * Writes this object out to a stream (i.e., serializes it).
    1405          *
    1406          * @serialData If this is a private credential set,
    1407          *      a security check is performed to ensure that
    1408          *      the caller has permission to access each credential
    1409          *      in the set.  If the security check passes,
    1410          *      the set is serialized.

    1411          */
    1412         @java.io.Serial
    1413         private void writeObject(java.io.ObjectOutputStream oos)
    1414                 throws java.io.IOException {
    1415
    1416             if (which == Subject.PRIV_CREDENTIAL_SET) {
    1417                 // check permissions before serializing
    1418                 Iterator<E> i = iterator();
    1419                 while (i.hasNext()) {
    1420                     i.next();
    1421                 }
    1422             }
    1423             ObjectOutputStream.PutField fields = oos.putFields();
    1424             fields.put("this$0", subject);
    1425             fields.put("elements", elements);
    1426             fields.put("which", which);
    1427             oos.writeFields();
    1428         }
    1429

    1430         @SuppressWarnings("unchecked")
    1431         @java.io.Serial
    1432         private void readObject(ObjectInputStream ois)
    1433             throws IOException, ClassNotFoundException
    1434         {
    1435             ObjectInputStream.GetField fields = ois.readFields();
    1436             subject = (Subject) fields.get("this$0", null);
    1437             which = fields.get("which", 0);
    1438
    1439             LinkedList<E> tmp = (LinkedList<E>) fields.get("elements", null);
    1440
    1441             elements = Subject.collectionNullClean(tmp);
    1442         }
    1443
    1444     }
    1445
    1446     /**
    1447      * This class implements a {@code Set} which returns only
    1448      * members that are an instance of a specified Class.
    1449      */
       1 /*
       2  * Copyright (c) 1998, 2021, 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
     932             while (pubCIterator.hasNext()) {
     933                 hashCode ^= getCredHashCode(pubCIterator.next());
     934             }
     935         }
     936         return hashCode;
     937     }
     938
     939     /**
     940      * get a credential's hashcode
     941      */
     942     private int getCredHashCode(Object o) {
     943         try {
     944             return o.hashCode();
     945         } catch (IllegalStateException ise) {
     946             return o.getClass().toString().hashCode();
     947         }
     948     }
     949
     950     /**
     951      * Writes this object out to a stream (i.e., serializes it).
     952      *
     953      * @param  oos the {@code ObjectOutputStream} to which data is written
     954      * @throws IOException if an I/O error occurs
     955      */
     956     @java.io.Serial
     957     private void writeObject(java.io.ObjectOutputStream oos)
     958                 throws java.io.IOException {
     959         synchronized(principals) {
     960             oos.defaultWriteObject();
     961         }
     962     }
     963
     964     /**
     965      * Reads this object from a stream (i.e., deserializes it)
     966      *
     967      * @param  s the {@code ObjectInputStream} from which data is read
     968      * @throws IOException if an I/O error occurs
     969      * @throws ClassNotFoundException if a serialized class cannot be loaded
     970      */
     971     @SuppressWarnings("unchecked")
     972     @java.io.Serial
     973     private void readObject(java.io.ObjectInputStream s)
     974                 throws java.io.IOException, ClassNotFoundException {
     975
     976         ObjectInputStream.GetField gf = s.readFields();
     977
     978         readOnly = gf.get("readOnly", false);
     979
     980         Set<Principal> inputPrincs = (Set<Principal>)gf.get("principals", null);
     981
     982         Objects.requireNonNull(inputPrincs,
     983                 ResourcesMgr.getString("invalid.null.input.s."));
     984
     985         // Rewrap the principals into a SecureSet
     986         try {
     987             LinkedList<Principal> principalList = collectionNullClean(inputPrincs);
     988             principals = Collections.synchronizedSet(new SecureSet<>
     989                                 (this, PRINCIPAL_SET, principalList));
    1398         public int hashCode() {
    1399             int h = 0;
    1400             Iterator<E> i = iterator();
    1401             while (i.hasNext()) {
    1402                 E obj = i.next();
    1403                 if (obj != null) {
    1404                     h += obj.hashCode();
    1405                 }
    1406             }
    1407             return h;
    1408         }
    1409
    1410         /**
    1411          * Writes this object out to a stream (i.e., serializes it).
    1412          *
    1413          * @serialData If this is a private credential set,
    1414          *      a security check is performed to ensure that
    1415          *      the caller has permission to access each credential
    1416          *      in the set.  If the security check passes,
    1417          *      the set is serialized.
    1418          *
    1419          * @param  oos the {@code ObjectOutputStream} to which data is written
    1420          * @throws IOException if an I/O error occurs
    1421          */
    1422         @java.io.Serial
    1423         private void writeObject(java.io.ObjectOutputStream oos)
    1424                 throws java.io.IOException {
    1425
    1426             if (which == Subject.PRIV_CREDENTIAL_SET) {
    1427                 // check permissions before serializing
    1428                 Iterator<E> i = iterator();
    1429                 while (i.hasNext()) {
    1430                     i.next();
    1431                 }
    1432             }
    1433             ObjectOutputStream.PutField fields = oos.putFields();
    1434             fields.put("this$0", subject);
    1435             fields.put("elements", elements);
    1436             fields.put("which", which);
    1437             oos.writeFields();
    1438         }
    1439
    1440         /**
    1441          * Restores the state of this object from the stream.
    1442          *
    1443          * @param  ois the {@code ObjectInputStream} from which data is read
    1444          * @throws IOException if an I/O error occurs
    1445          * @throws ClassNotFoundException if a serialized class cannot be loaded
    1446          */
    1447         @SuppressWarnings("unchecked")
    1448         @java.io.Serial
    1449         private void readObject(ObjectInputStream ois)
    1450             throws IOException, ClassNotFoundException
    1451         {
    1452             ObjectInputStream.GetField fields = ois.readFields();
    1453             subject = (Subject) fields.get("this$0", null);
    1454             which = fields.get("which", 0);
    1455
    1456             LinkedList<E> tmp = (LinkedList<E>) fields.get("elements", null);
    1457
    1458             elements = Subject.collectionNullClean(tmp);
    1459         }
    1460
    1461     }
    1462
    1463     /**
    1464      * This class implements a {@code Set} which returns only
    1465      * members that are an instance of a specified Class.
    1466      */

< prev index next >