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 CallAsWithScopedValue true 31 */ 32 import com.sun.security.auth.UserPrincipal; 33 34 import javax.security.auth.Subject; 35 import java.util.Map; 36 import java.util.Objects; 37 import java.util.concurrent.ConcurrentHashMap; 38 import java.util.concurrent.StructuredTaskScope; 39 40 public class CallAsWithScopedValue { 41 42 private static Map results = new ConcurrentHashMap<Integer,Boolean>(); 43 44 public static void main(String[] args) throws Exception { 45 46 boolean usv = Boolean.parseBoolean(args[0]); 47 48 Subject subject = new Subject(); 49 subject.getPrincipals().add(new UserPrincipal("Duke")); 50 51 // Always observable in the same thread 52 Subject.callAs(subject, () -> check(0, Subject.current(), "Duke")); 53 54 // Observable in a new platform thread in ACC mode, but not in the SV mode 55 Subject.callAs(subject, () -> { 56 Thread.ofPlatform().start(() -> check(1, Subject.current(), usv ? null : "Duke")).join(); 57 return null; 58 }); 59 60 // Never observable in a new virtual thread 61 Subject.callAs(subject, () -> { 62 Thread.ofVirtual().start(() -> check(2, Subject.current(), null)).join(); 63 return null; 64 }); 65 66 // Observable in structured concurrency in SV mode, but not in ACC mode 67 Subject.callAs(subject, () -> { 68 var joiner = StructuredTaskScope.Joiner.awaitAll(); 69 try (var scope = StructuredTaskScope.open(joiner)) { 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 }