KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > MainLookup


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.startup;
21
22 import org.openide.modules.ModuleInfo;
23 import org.openide.util.Lookup;
24 import org.openide.util.lookup.AbstractLookup;
25 import org.openide.util.lookup.InstanceContent;
26 import org.openide.util.lookup.Lookups;
27 import org.openide.util.lookup.ProxyLookup;
28
29 /** The default lookup for the system.
30  */

31 public final class MainLookup extends ProxyLookup {
32     private static boolean started = false;
33     /** currently effective ClassLoader */
34     private static ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
35     /** inner access to dynamic lookup service for this top mangager */
36     private static InstanceContent instanceContent = new InstanceContent ();
37     /** dynamic lookup service for this top mangager */
38     private static Lookup instanceLookup = new AbstractLookup (instanceContent);
39
40     /** Someone called NbTopManager.get().
41      * That means that subsequent calls to lookup on ModuleInfo
42      * need not try to get it again.
43      */

44     public static void startedNbTopManager() {
45         started = true;
46     }
47     
48     /** Checks whether everything is started.
49      */

50     static boolean isStarted() {
51         return started;
52     }
53
54     /** Initialize the lookup to delegate to NbTopManager.
55     */

56     public MainLookup () {
57         super (new Lookup[] {
58                    // #14722: pay attention also to META-INF/services/class.Name resources:
59
Lookups.metaInfServices(classLoader),
60                    Lookups.singleton(classLoader),
61                    Lookup.EMPTY, // will be moduleLookup
62
instanceLookup
63                });
64     }
65
66     /** Called when a system classloader changes.
67      */

68     public static final void systemClassLoaderChanged (ClassLoader JavaDoc nue) {
69         if (!(Lookup.getDefault() instanceof MainLookup)) {
70             // May be called from MockServices.setServices even though we are not main lookup.
71
return;
72         }
73         if (classLoader != nue) {
74             classLoader = nue;
75             MainLookup l = (MainLookup)Lookup.getDefault();
76             Lookup[] delegates = l.getLookups();
77             Lookup[] newDelegates = delegates.clone();
78             // Replace classloader.
79
newDelegates[0] = Lookups.metaInfServices(classLoader);
80             newDelegates[1] = Lookups.singleton(classLoader);
81             l.setLookups(newDelegates);
82         } else {
83             moduleClassLoadersUp();
84         }
85     }
86
87     /** Called when modules are about to be turned on.
88      */

89     public static final void moduleClassLoadersUp() {
90         MainLookup l = (MainLookup)Lookup.getDefault();
91         Lookup[] newDelegates = null;
92         Lookup[] delegates = l.getLookups();
93         newDelegates = delegates.clone();
94         newDelegates[0] = Lookups.metaInfServices(classLoader);
95         l.setLookups(newDelegates);
96     }
97
98     /** Called when Lookup<ModuleInfo> is ready from the ModuleManager.
99      * @see "#28465"
100      */

101     public static final void moduleLookupReady(Lookup moduleLookup) {
102         MainLookup l = (MainLookup)Lookup.getDefault();
103         Lookup[] newDelegates = l.getLookups().clone();
104         newDelegates[2] = moduleLookup;
105         l.setLookups(newDelegates);
106     }
107
108     /** When all module classes are accessible thru systemClassLoader, this
109      * method is called to initialize the FolderLookup.
110      */

111
112     public static final void modulesClassPathInitialized () {
113         //System.err.println("mCPI");
114
//StartLog.logStart ("NbTopManager$MainLookup: initialization of FolderLookup"); // NOI18N
115

116         // replace the lookup by new one
117
Lookup lookup = Lookup.getDefault ();
118         StartLog.logProgress ("Got Lookup"); // NOI18N
119

120         ((MainLookup)lookup).doInitializeLookup ();
121     }
122
123     //
124
//
125
//
126

127     /** Register new instance.
128      */

129     public static void register (Object JavaDoc obj) {
130         instanceContent.add (obj);
131     }
132     
133     /** Register new instance.
134      * @param obj source
135      * @param conv convertor which postponing an instantiation
136      */

137     public static <T,R> void register(T obj, InstanceContent.Convertor<T,R> conv) {
138         instanceContent.add(obj, conv);
139     }
140     
141     /** Unregisters the service.
142      */

143     public static void unregister (Object JavaDoc obj) {
144         instanceContent.remove (obj);
145     }
146     /** Unregisters the service registered with a convertor.
147      */

148     public static <T,R> void unregister (T obj, InstanceContent.Convertor<T,R> conv) {
149         instanceContent.remove (obj, conv);
150     }
151     
152     
153     
154     
155
156     private final void doInitializeLookup () {
157         //System.err.println("doInitializeLookup");
158

159         // extend the lookup
160
Lookup[] arr = new Lookup[] {
161             getLookups()[0], // metaInfServicesLookup
162
getLookups()[1], // ClassLoader lookup
163
getLookups()[2], // ModuleInfo lookup
164
instanceLookup,
165             CoreBridge.conditionallyLookupCacheLoad (),
166         };
167         StartLog.logProgress ("prepared other Lookups"); // NOI18N
168

169         setLookups (arr);
170         StartLog.logProgress ("Lookups set"); // NOI18N
171

172         CoreBridge.lookupInitialized();
173     //StartLog.logEnd ("NbTopManager$MainLookup: initialization of FolderLookup"); // NOI18N
174
}
175
176     public void storeCache() throws java.io.IOException JavaDoc {
177         Lookup[] ls = getLookups();
178         if (ls.length == 5) {
179             // modulesClassPathInitialized has been called, so store folder lookup
180
CoreBridge.getDefault ().lookupCacheStore (ls[4]);
181         }
182     }
183
184     protected void beforeLookup(Lookup.Template templ) {
185         Class JavaDoc type = templ.getType();
186
187         // Force module system to be initialize by looking up ModuleInfo.
188
// Good for unit tests, etc.
189
if (!started && (type == ModuleInfo.class || type == org.netbeans.Module.class)) {
190             Main.getModuleSystem ();
191         }
192
193         super.beforeLookup(templ);
194     }
195 }
196     
197
Popular Tags