This is an automated email from the ASF dual-hosted git repository. lkishalmi pushed a commit to branch release120 in repository https://gitbox.apache.org/repos/asf/netbeans.git
commit 7e9d23bed3ce7c7108ebb63a4d239fb54253a082 Author: Jaroslav Tulach <[email protected]> AuthorDate: Wed Jun 17 06:16:09 2020 +0200 Verify NbBundle and Lookup (with ActiveQueue) can be used in Thread-less sandbox --- .../openide/util/lookup/implspi/ActiveQueue.java | 9 ++-- .../ActiveQueueWithSecurityManagerTest.java | 54 ++++++++++++++++++++++ .../src/org/openide/util/TimedSoftReference.java | 2 +- .../util/NbBundleWithSecurityManager.properties | 18 ++++++++ .../util/NbBundleWithSecurityManagerTest.java | 52 +++++++++++++++++++++ 5 files changed, 131 insertions(+), 4 deletions(-) diff --git a/platform/openide.util.lookup/src/org/openide/util/lookup/implspi/ActiveQueue.java b/platform/openide.util.lookup/src/org/openide/util/lookup/implspi/ActiveQueue.java index 6ebfa33..dd57b50 100644 --- a/platform/openide.util.lookup/src/org/openide/util/lookup/implspi/ActiveQueue.java +++ b/platform/openide.util.lookup/src/org/openide/util/lookup/implspi/ActiveQueue.java @@ -72,6 +72,7 @@ public final class ActiveQueue { } private static final class Daemon extends Thread { + private static boolean initialized; private static Daemon running; public Daemon() { @@ -79,17 +80,19 @@ public final class ActiveQueue { } static synchronized void ping() { - if (running == null) { - Daemon t = new Daemon(); + if (!initialized) { try { + Daemon t = new Daemon(); t.setPriority(Thread.MIN_PRIORITY); t.setDaemon(true); t.start(); LOGGER.fine("starting thread"); + running = t; } catch (SecurityException ex) { LOGGER.log(Level.FINE, "cannot start thread", ex); + } finally { + initialized = true; } - running = t; } } diff --git a/platform/openide.util.lookup/test/unit/src/org/openide/util/lookup/implspi/ActiveQueueWithSecurityManagerTest.java b/platform/openide.util.lookup/test/unit/src/org/openide/util/lookup/implspi/ActiveQueueWithSecurityManagerTest.java new file mode 100644 index 0000000..bd9c35b --- /dev/null +++ b/platform/openide.util.lookup/test/unit/src/org/openide/util/lookup/implspi/ActiveQueueWithSecurityManagerTest.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.openide.util.lookup.implspi; + +import java.lang.ref.ReferenceQueue; +import java.security.Permission; +import org.junit.Test; +import static org.junit.Assert.*; + +public class ActiveQueueWithSecurityManagerTest { + + public ActiveQueueWithSecurityManagerTest() { + } + + @Test + public void testQueue() { + final Thread[] raised = { null }; + System.setSecurityManager(new SecurityManager() { + @Override + public void checkAccess(Thread t) { + if (t.getName().startsWith("Active")) { + raised[0] = t; + throw new SecurityException(); + } + } + + @Override + public void checkPermission(Permission perm) { + } + }); + ReferenceQueue<Object> result = ActiveQueue.queue(); + System.setSecurityManager(null); + assertNotNull("The thread has been prevented from being started", raised[0]); + assertEquals("Active Reference Queue Daemon", raised[0].getName()); + assertNotNull("ActiveQueue can be created in spite of that", result); + } + +} diff --git a/platform/openide.util/src/org/openide/util/TimedSoftReference.java b/platform/openide.util/src/org/openide/util/TimedSoftReference.java index 5539935..5f24879 100644 --- a/platform/openide.util/src/org/openide/util/TimedSoftReference.java +++ b/platform/openide.util/src/org/openide/util/TimedSoftReference.java @@ -62,7 +62,7 @@ final class TimedSoftReference<T> extends SoftReference<T> implements Runnable { * @param m a map in which this reference may serve as a value * @param k the key whose value in <code>m</code> may be this reference */ - public TimedSoftReference(T o, Map m, Object k) { + TimedSoftReference(T o, Map m, Object k) { super(o, BaseUtilities.activeReferenceQueue()); this.o = o; this.m = m; diff --git a/platform/openide.util/test/unit/src/org/openide/util/NbBundleWithSecurityManager.properties b/platform/openide.util/test/unit/src/org/openide/util/NbBundleWithSecurityManager.properties new file mode 100644 index 0000000..4e88e51 --- /dev/null +++ b/platform/openide.util/test/unit/src/org/openide/util/NbBundleWithSecurityManager.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +HELLO=World diff --git a/platform/openide.util/test/unit/src/org/openide/util/NbBundleWithSecurityManagerTest.java b/platform/openide.util/test/unit/src/org/openide/util/NbBundleWithSecurityManagerTest.java new file mode 100644 index 0000000..4c87889 --- /dev/null +++ b/platform/openide.util/test/unit/src/org/openide/util/NbBundleWithSecurityManagerTest.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.openide.util; + +import java.security.Permission; +import java.util.ResourceBundle; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import org.junit.Test; + +public class NbBundleWithSecurityManagerTest { + @Test + public void testQueue() { + final Thread[] raised = { null }; + System.setSecurityManager(new SecurityManager() { + @Override + public void checkAccess(Thread t) { + if (t.getClass().getName().startsWith("java.util.logging")) { + return; + } + raised[0] = t; + throw new SecurityException(); + } + + @Override + public void checkPermission(Permission perm) { + } + }); + ResourceBundle bundle = NbBundle.getBundle("org.openide.util.NbBundleWithSecurityManager"); + assertNotNull("Bundle found", bundle); + assertEquals("World", bundle.getString("HELLO")); + + System.setSecurityManager(null); + assertNotNull("The thread has been prevented from being started", raised[0]); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
