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