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 try (var scope = new StructuredTaskScope<>()) { 69 scope.fork(() -> check(3, Subject.current(), usv ? "Duke" : null)); 70 scope.join(); 71 } 72 return null; 73 }); 74 75 // Suggested way to pass the current subject into arbitrary 76 // threads. Grab one using current() and explicitly pass it 77 // into the new thread. 78 Subject.callAs(subject, () -> { 79 Subject current = Subject.current(); 80 Thread.ofPlatform().start(() -> { 81 Subject.callAs(current, () -> check(4, Subject.current(), "Duke")); 82 }).join(); 83 return null; 84 }); 85 86 if (results.size() != 5 || results.containsValue(false)) { 87 System.out.println(results); 88 throw new RuntimeException("Failed"); 89 } 90 } 91 92 static String check(int type, Subject current, String expected) { 93 String actual; 94 if (current == null) { 95 actual = null; 96 } else { 97 var set = current.getPrincipals(UserPrincipal.class); 98 actual = set.isEmpty() 99 ? null 100 : set.iterator().next().getName(); 101 } 102 results.put(type, Objects.equals(actual, expected)); 103 return actual; 104 } 105 }