This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 247930
Collapse All | Expand All

(-)a/openide.util.lookup/apichanges.xml (+29 lines)
Lines 49-54 Link Here
49
    <apidef name="lookup">Lookup API</apidef>
49
    <apidef name="lookup">Lookup API</apidef>
50
</apidefs>
50
</apidefs>
51
<changes>
51
<changes>
52
    <change id="lookups.execute">
53
        <api name="lookup"/>
54
        <summary>A way to control Lookup.getDefault</summary>
55
        <version major="8" minor="30"/>
56
        <date day="22" month="10" year="2014"/>
57
        <author login="jtulach"/>
58
        <compatibility
59
            addition="yes"
60
            binary="compatible" deletion="no" deprecation="no"
61
            modification="no" semantic="incompatible" source="compatible"
62
        >
63
            The clients of <a href="@TOP@/org/openide/util/Lookup.html#getDefault()">
64
            Lookup.getDefault()</a> are mostly unaffected by this change,
65
            just they need to be ready for the fact that the return 
66
            value of the method may no longer be fixed one, but can mutate
67
            over time.
68
        </compatibility>
69
        <description>
70
            <p>
71
                One can use <a href="@TOP@/org/openide/util/lookup/Lookups.html#execute(org.openide.util.Lookup, java.lang.Runnable)">
72
                Lookups.execute(yourLookup, yourRunnable)
73
                </a> to temporarily influence return value from
74
                <a href="@TOP@/org/openide/util/Lookup.html#getDefault()">
75
                Lookup.getDefault()</a>. 
76
            </p>
77
        </description>
78
        <class package="org.openide.util.lookup" name="Lookups"/>
79
        <issue number="247930"/>
80
    </change>
52
    <change id="named.service.definition">
81
    <change id="named.service.definition">
53
        <api name="lookup"/>
82
        <api name="lookup"/>
54
        <summary>Easy and robust way to register named services</summary>
83
        <summary>Easy and robust way to register named services</summary>
(-)a/openide.util.lookup/manifest.mf (-1 / +1 lines)
Lines 1-5 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.openide.util.lookup
2
OpenIDE-Module: org.openide.util.lookup
3
OpenIDE-Module-Localizing-Bundle: org/openide/util/lookup/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/openide/util/lookup/Bundle.properties
4
OpenIDE-Module-Specification-Version: 8.26
4
OpenIDE-Module-Specification-Version: 8.30
5
5
(-)e7499d50abe0 (+65 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2014 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2014 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.openide.util;
43
44
import org.openide.util.Lookup;
45
46
public class GlobalLookup {
47
    private static ThreadLocal<Lookup> CURRENT = new ThreadLocal<Lookup>();
48
    
49
    private GlobalLookup() {
50
    }
51
52
    public static void execute(Lookup defaultLookup, Runnable r) {
53
        Lookup prev = CURRENT.get();
54
        try {
55
            CURRENT.set(defaultLookup);
56
            r.run();
57
        } finally {
58
            CURRENT.set(prev);
59
        }
60
    }
61
    
62
    public static Lookup current() {
63
        return CURRENT.get();
64
    }
65
}
(-)a/openide.util.lookup/src/org/openide/util/Lookup.java (+6 lines)
Lines 52-57 Link Here
52
import java.util.Set;
52
import java.util.Set;
53
import java.util.logging.Level;
53
import java.util.logging.Level;
54
import java.util.logging.Logger;
54
import java.util.logging.Logger;
55
import org.netbeans.modules.openide.util.GlobalLookup;
55
import org.openide.util.lookup.Lookups;
56
import org.openide.util.lookup.Lookups;
56
import org.openide.util.lookup.ProxyLookup;
57
import org.openide.util.lookup.ProxyLookup;
57
import org.openide.util.lookup.ServiceProvider;
58
import org.openide.util.lookup.ServiceProvider;
Lines 110-115 Link Here
110
     * @see ServiceProvider
111
     * @see ServiceProvider
111
     */
112
     */
112
    public static synchronized Lookup getDefault() {
113
    public static synchronized Lookup getDefault() {
114
        Lookup current = GlobalLookup.current();
115
        if (current != null) {
116
            return current;
117
        }
118
        
113
        if (defaultLookup != null) {
119
        if (defaultLookup != null) {
114
            return defaultLookup;
120
            return defaultLookup;
115
        }
121
        }
(-)a/openide.util.lookup/src/org/openide/util/lookup/Lookups.java (+14 lines)
Lines 45-50 Link Here
45
package org.openide.util.lookup;
45
package org.openide.util.lookup;
46
46
47
import java.util.Arrays;
47
import java.util.Arrays;
48
import org.netbeans.modules.openide.util.GlobalLookup;
48
import org.openide.util.Lookup;
49
import org.openide.util.Lookup;
49
import org.openide.util.lookup.implspi.NamedServicesProvider;
50
import org.openide.util.lookup.implspi.NamedServicesProvider;
50
51
Lines 279-284 Link Here
279
    public static <T> Lookup.Item<T> lookupItem(T instance, String id) {
280
    public static <T> Lookup.Item<T> lookupItem(T instance, String id) {
280
        return new LookupItem<T>(instance, id);
281
        return new LookupItem<T>(instance, id);
281
    }
282
    }
283
    
284
    /** Temporarily (while the <code>code</code> is running) changes value
285
     * of {@link Lookup#getDefault()} to here-in provided lookup. Useful in a
286
     * multi user environment where different users and their requests should
287
     * be associated with different content of default lookup.
288
     * 
289
     * @param defaultLookup the lookup to be come default while code is running
290
     * @param code the code to execute (synchronously) before the method returns
291
     * @since 8.30
292
     */
293
    public static void execute(Lookup defaultLookup, Runnable code) {
294
        GlobalLookup.execute(defaultLookup, code);
295
    }
282
296
283
    private static class LookupItem<T> extends Lookup.Item<T> {
297
    private static class LookupItem<T> extends Lookup.Item<T> {
284
        private String id;
298
        private String id;
(-)e7499d50abe0 (+70 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2014 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2014 Sun Microsystems, Inc.
41
 */
42
package org.openide.util.lookup;
43
44
import org.netbeans.junit.NbTestCase;
45
import org.openide.util.Lookup;
46
47
public class LookupGetDefaultTest extends NbTestCase {
48
49
    public LookupGetDefaultTest(String name) {
50
        super(name);
51
    }
52
    
53
    public void testCanChangeDefaultLookup() throws Exception {
54
        Lookup prev = Lookup.getDefault();
55
        final Lookup my = new AbstractLookup();
56
        final Thread myThread = Thread.currentThread();
57
        final boolean[] ok = { false };
58
        
59
        Lookups.execute(my, new Runnable() {
60
            @Override
61
            public void run() {
62
                assertSame("Default lookup has been changed", my, Lookup.getDefault());
63
                assertSame("We are being executed in the same thread", myThread, Thread.currentThread());
64
                ok[0] = true;
65
            }
66
        });
67
        assertTrue("In my lookup code executed OK", ok[0]);
68
        assertEquals("Current lookup back to normal", prev, Lookup.getDefault());
69
    }
70
}
(-)a/openide.util/apichanges.xml (+19 lines)
Lines 51-56 Link Here
51
    <apidef name="actions">Actions API</apidef>
51
    <apidef name="actions">Actions API</apidef>
52
</apidefs>
52
</apidefs>
53
<changes>
53
<changes>
54
    <change id="lookups.execute.and.rp">
55
        <api name="util"/>
56
        <summary>RequestProcessor tasks remember correct lookup</summary>
57
        <version major="8" minor="41"/>
58
        <date year="2014" month="10" day="22"/>
59
        <author login="jtulach"/>
60
        <compatibility addition="yes" binary="compatible" source="compatible"/>
61
        <description>
62
            <p>
63
            <a href="@TOP@/org/openide/util/RequestProcessor.html">RequestProcessor</a>
64
            is now <a href="@org-openide-util-lookup@/org/openide/util/lookup/Lookups.html#execute(org.openide.util.Lookup, java.lang.Runnable)">
65
            Lookups.execute</a> aware and makes sure the executed task
66
            uses the same default lookup as at the time of its 
67
            initial creation.
68
            </p>
69
        </description>
70
        <class package="org.openide.util" name="RequestProcessor"/>
71
        <issue number="247930"/>
72
    </change>
54
    <change id="disabled-action-beep">
73
    <change id="disabled-action-beep">
55
        <api name="util"/>
74
        <api name="util"/>
56
        <summary>Platform dependent sound when invoking a disabled action.</summary>
75
        <summary>Platform dependent sound when invoking a disabled action.</summary>
(-)a/openide.util/manifest.mf (-1 / +1 lines)
Lines 1-5 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.openide.util
2
OpenIDE-Module: org.openide.util
3
OpenIDE-Module-Localizing-Bundle: org/openide/util/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/openide/util/Bundle.properties
4
OpenIDE-Module-Specification-Version: 8.40
4
OpenIDE-Module-Specification-Version: 8.41
5
5
(-)a/openide.util/src/org/openide/util/RequestProcessor.java (-3 / +13 lines)
Lines 79-84 Link Here
79
import java.util.concurrent.atomic.AtomicReference;
79
import java.util.concurrent.atomic.AtomicReference;
80
import java.util.logging.Level;
80
import java.util.logging.Level;
81
import java.util.logging.Logger;
81
import java.util.logging.Logger;
82
import org.openide.util.lookup.Lookups;
82
83
83
/** Request processor is {@link Executor} (since version 7.16) capable to
84
/** Request processor is {@link Executor} (since version 7.16) capable to
84
 * perform asynchronous requests in a dedicated thread pool.
85
 * perform asynchronous requests in a dedicated thread pool.
Lines 629-635 Link Here
629
        item.enqueued = true;
630
        item.enqueued = true;
630
    }
631
    }
631
632
632
    Task askForWork(Processor worker, String debug) {
633
    Task askForWork(Processor worker, String debug, Lookup[] lkp) {
633
        if (getQueue().isEmpty() || (stopped && !finishAwaitingTasks)) { // no more work in this burst, return him
634
        if (getQueue().isEmpty() || (stopped && !finishAwaitingTasks)) { // no more work in this burst, return him
634
            processors.remove(worker);
635
            processors.remove(worker);
635
            Processor.put(worker, debug);
636
            Processor.put(worker, debug);
Lines 639-644 Link Here
639
            Item i = getQueue().first();
640
            Item i = getQueue().first();
640
            getQueue().remove(i);
641
            getQueue().remove(i);
641
            Task t = i.getTask();
642
            Task t = i.getTask();
643
            lkp[0] = i.current;
642
            i.clear(worker);
644
            i.clear(worker);
643
645
644
            return t;
646
            return t;
Lines 1728-1733 Link Here
1728
        private static int counter;
1730
        private static int counter;
1729
        private final RequestProcessor owner;
1731
        private final RequestProcessor owner;
1730
        private final int cnt;
1732
        private final int cnt;
1733
        final Lookup current;
1731
        Object action;
1734
        Object action;
1732
        boolean enqueued;
1735
        boolean enqueued;
1733
        String message;
1736
        String message;
Lines 1738-1743 Link Here
1738
            action = task;
1741
            action = task;
1739
            owner = rp;
1742
            owner = rp;
1740
            cnt = counter++;
1743
            cnt = counter++;
1744
            current = Lookup.getDefault();
1741
        }
1745
        }
1742
1746
1743
        final Task getTask() {
1747
        final Task getTask() {
Lines 2015-2023 Link Here
2015
2019
2016
                // while we have something to do
2020
                // while we have something to do
2017
                for (;;) {
2021
                for (;;) {
2022
                    Lookup[] lkp = new Lookup[1];
2018
                    // need the same sync as interruptTask
2023
                    // need the same sync as interruptTask
2019
                    synchronized (current.processorLock) {
2024
                    synchronized (current.processorLock) {
2020
                        todo = current.askForWork(this, debug);
2025
                        todo = current.askForWork(this, debug, lkp);
2021
                        if (todo == null) {
2026
                        if (todo == null) {
2022
                            break;
2027
                            break;
2023
                        }
2028
                        }
Lines 2030-2036 Link Here
2030
                            em.log(Level.FINE, "  Executing {0}", todo); // NOI18N
2035
                            em.log(Level.FINE, "  Executing {0}", todo); // NOI18N
2031
                        }
2036
                        }
2032
                        registerParallel(todo, current);
2037
                        registerParallel(todo, current);
2033
                        todo.run();
2038
                        if (lkp[0] == null || lkp[0] == Lookup.getDefault()) {
2039
                            todo.run();
2040
                        } else {
2041
                            Lookups.execute(lkp[0], todo);
2042
                        }
2043
                        lkp[0] = null;
2034
2044
2035
                        if (loggable) {
2045
                        if (loggable) {
2036
                            em.log(Level.FINE, "  Execution finished in {0}", getName()); // NOI18N
2046
                            em.log(Level.FINE, "  Execution finished in {0}", getName()); // NOI18N
(-)e7499d50abe0 (+83 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2014 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2014 Sun Microsystems, Inc.
41
 */
42
package org.openide.util;
43
44
import java.util.concurrent.TimeUnit;
45
import org.netbeans.junit.NbTestCase;
46
import org.openide.util.Lookup;
47
import org.openide.util.lookup.AbstractLookup;
48
import org.openide.util.lookup.InstanceContent;
49
import org.openide.util.lookup.Lookups;
50
51
public class RequestProcessorLookupGetDefaultTest extends NbTestCase {
52
53
    public RequestProcessorLookupGetDefaultTest(String name) {
54
        super(name);
55
    }
56
    
57
    public void testChangeOfDefaultLookupAppliedToRPTask() throws Exception {
58
        Lookup prev = Lookup.getDefault();
59
        final Lookup my = new AbstractLookup(new InstanceContent());
60
        final Thread myThread = Thread.currentThread();
61
        final RequestProcessor.Task[] task = { null };
62
        final boolean[] ok = { false };
63
        
64
        Lookups.execute(my, new Runnable() {
65
            @Override
66
            public void run() {
67
                assertSame("Default lookup has been changed", my, Lookup.getDefault());
68
69
                if (task[0] == null) {
70
                    assertSame("We are being executed in the same thread", myThread, Thread.currentThread());
71
                    // once again in the RP
72
                    task[0] = RequestProcessor.getDefault().post(this, 500);
73
                } else {
74
                    ok[0] = true;
75
                }
76
            }
77
        });
78
        assertNotNull("In my lookup code executed OK", task[0]);
79
        assertEquals("Current lookup back to normal", prev, Lookup.getDefault());
80
        task[0].waitFinished();
81
        assertTrue("Even RP task had the right lookup", ok[0]);
82
    }
83
}

Return to bug 247930