1 /*
  2  * Copyright (c) 2024, 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.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8296244
 27  * @enablePreview
 28  * @summary Implement Subject.current and Subject.callAs using scoped values.
 29  *      Need enablePreview to use StructuredTaskScope.
 30  * @run main/othervm -Djava.security.manager=allow CallAsWithScopedValue false
 31  * @run main/othervm -Djava.security.manager=disallow CallAsWithScopedValue true
 32  */
 33 import com.sun.security.auth.UserPrincipal;
 34 
 35 import javax.security.auth.Subject;
 36 import java.util.Map;
 37 import java.util.Objects;
 38 import java.util.concurrent.ConcurrentHashMap;
 39 import java.util.concurrent.StructuredTaskScope;
 40 
 41 public class CallAsWithScopedValue {
 42 
 43     private static Map results = new ConcurrentHashMap<Integer,Boolean>();
 44 
 45     public static void main(String[] args) throws Exception {
 46 
 47         boolean usv = Boolean.parseBoolean(args[0]);
 48 
 49         Subject subject = new Subject();
 50         subject.getPrincipals().add(new UserPrincipal("Duke"));
 51 
 52         // Always observable in the same thread
 53         Subject.callAs(subject, () -> check(0, Subject.current(), "Duke"));
 54 
 55         // Observable in a new platform thread in ACC mode, but not in the SV mode
 56         Subject.callAs(subject, () -> {
 57             Thread.ofPlatform().start(() -> check(1, Subject.current(), usv ? null : "Duke")).join();
 58             return null;
 59         });
 60 
 61         // Never observable in a new virtual thread
 62         Subject.callAs(subject, () -> {
 63             Thread.ofVirtual().start(() -> check(2, Subject.current(), null)).join();
 64             return null;
 65         });
 66 
 67         // Observable in structured concurrency in SV mode, but not in ACC mode
 68         Subject.callAs(subject, () -> {
 69             try (var scope = new StructuredTaskScope<>()) {
 70                 scope.fork(() -> check(3, Subject.current(), usv ? "Duke" : null));
 71                 scope.join();
 72             }
 73             return null;
 74         });
 75 
 76         // Suggested way to pass the current subject into arbitrary
 77         // threads. Grab one using current() and explicitly pass it
 78         // into the new thread.
 79         Subject.callAs(subject, () -> {
 80             Subject current = Subject.current();
 81             Thread.ofPlatform().start(() -> {
 82                 Subject.callAs(current, () -> check(4, Subject.current(), "Duke"));
 83             }).join();
 84             return null;
 85         });
 86 
 87         if (results.size() != 5 || results.containsValue(false)) {
 88             System.out.println(results);
 89             throw new RuntimeException("Failed");
 90         }
 91     }
 92 
 93     static String check(int type, Subject current, String expected) {
 94         String actual;
 95         if (current == null) {
 96             actual = null;
 97         } else {
 98             var set = current.getPrincipals(UserPrincipal.class);
 99             actual = set.isEmpty()
100                     ? null
101                     : set.iterator().next().getName();
102         }
103         results.put(type, Objects.equals(actual, expected));
104         return actual;
105     }
106 }