001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core;
043:
044: import java.awt.EventQueue;
045: import java.awt.Frame;
046: import java.awt.Toolkit;
047: import java.awt.event.ComponentAdapter;
048: import java.awt.event.ComponentEvent;
049: import java.io.IOException;
050: import java.lang.reflect.Method;
051: import java.util.logging.Level;
052: import java.util.logging.Logger;
053: import javax.swing.SwingUtilities;
054: import javax.swing.Timer;
055: import org.netbeans.TopSecurityManager;
056: import org.netbeans.core.startup.InstalledFileLocatorImpl;
057: import org.netbeans.core.startup.Main;
058: import org.netbeans.core.startup.ModuleSystem;
059: import org.netbeans.core.startup.Splash;
060: import org.netbeans.core.startup.StartLog;
061: import org.openide.awt.StatusDisplayer;
062: import org.openide.util.Lookup;
063: import org.openide.util.NbBundle;
064: import org.openide.windows.WindowManager;
065:
066: /**
067: * Most of the NetBeans startup logic that is not closely tied to the GUI.
068: * The meat of the startup sequence is in {@link #run}.
069: */
070: public class NonGui extends NbTopManager implements Runnable,
071: org.netbeans.core.startup.RunLevel {
072: private static int count;
073:
074: public NonGui() {
075: assert count++ == 0 : "Only one instance allowed"; // NOI18N
076: }
077:
078: /** Everything is interactive */
079: public boolean isInteractive(int il) {
080: return true;
081: }
082:
083: /** Initialization of the manager.
084: */
085: public void run() {
086: // -----------------------------------------------------------------------------------------------------
087: // 10. Loader pool loading
088: try {
089: LoaderPoolNode.load();
090: } catch (IOException ioe) {
091: Logger.getLogger(NonGui.class.getName()).log(Level.WARNING,
092: null, ioe);
093: }
094: StartLog.logProgress("LoaderPool loaded"); // NOI18N
095: Splash.getInstance().increment(10);
096:
097: LoaderPoolNode.installationFinished();
098: StartLog.logProgress("LoaderPool notified"); // NOI18N
099: Splash.getInstance().increment(10);
100:
101: // install java.net.ProxySelector
102: java.net.ProxySelector.setDefault(new NbProxySelector());
103:
104: //---------------------------------------------------------------------------------------------------------
105: // initialize main window AFTER the setup wizard is finished
106:
107: initializeMainWindow();
108: StartLog.logProgress("Main window initialized"); // NOI18N
109: Splash.getInstance().increment(1);
110:
111: // -----------------------------------------------------------------------------------------------------
112: // 8. Advance Policy
113:
114: // set security manager
115: SecurityManager secman = new TopSecurityManager();
116:
117: System.setSecurityManager(secman);
118: TopSecurityManager
119: .makeSwingUseSpecialClipboard(Lookup
120: .getDefault()
121: .lookup(
122: org.openide.util.datatransfer.ExClipboard.class));
123:
124: // install java.net.Authenticator
125: java.net.Authenticator.setDefault(new NbAuthenticator());
126:
127: StartLog.logProgress("Security managers installed"); // NOI18N
128: Splash.getInstance().increment(1);
129: }
130:
131: /** Method to initialize the main window.
132: */
133: protected void initializeMainWindow() {
134: if (!org.netbeans.core.startup.CLIOptions.isGui()) {
135: return;
136: }
137:
138: StartLog.logStart("Main window initialization"); //NOI18N
139:
140: TimeableEventQueue.initialize();
141:
142: // -----------------------------------------------------------------------------------------------------
143: // 11. Initialization of main window
144: StatusDisplayer.getDefault()
145: .setStatusText(
146: NbBundle.getMessage(NonGui.class,
147: "MSG_MainWindowInit"));
148:
149: // force to initialize timer
150: // sometimes happened that the timer thread was initialized under
151: // a TaskThreadGroup
152: // such task never ends or, if killed, timer is over
153: Timer timerInit = new Timer(0,
154: new java.awt.event.ActionListener() {
155: public void actionPerformed(
156: java.awt.event.ActionEvent ev) {
157: }
158: });
159: timerInit.setRepeats(false);
160: timerInit.start();
161: Splash.getInstance().increment(10);
162: StartLog.logProgress("Timer initialized"); // NOI18N
163:
164: // -----------------------------------------------------------------------------------------------------
165: // 13. Initialize Shortcuts
166: ShortcutsFolder.initShortcuts();
167: Splash.getInstance().increment(1);
168: StartLog.logProgress("Shortcuts initialized"); // NOI18N
169:
170: // -----------------------------------------------------------------------------------------------------
171: // 14. Open main window
172: StatusDisplayer.getDefault()
173: .setStatusText(
174: NbBundle.getMessage(NonGui.class,
175: "MSG_WindowShowInit"));
176:
177: // Starts GUI components to be created and shown on screen.
178: // I.e. main window + current workspace components.
179:
180: // Access winsys from AWT thread only. In this case main thread wouldn't harm, just to be kosher.
181: SwingUtilities.invokeLater(new Runnable() {
182:
183: public void run() {
184: StartLog.logProgress("Window system initialization");
185: if (System.getProperty("netbeans.warmup.skip") == null
186: && System.getProperty("netbeans.close") == null) {
187: final Frame mainWindow = WindowManager.getDefault()
188: .getMainWindow();
189:
190: mainWindow
191: .addComponentListener(new ComponentAdapter() {
192:
193: public void componentShown(
194: ComponentEvent evt) {
195: mainWindow
196: .removeComponentListener(this );
197: WarmUpSupport.warmUp();
198: }
199: });
200: }
201: NbTopManager.WindowSystem windowSystem = (NbTopManager.WindowSystem) Lookup
202: .getDefault().lookup(
203: NbTopManager.WindowSystem.class);
204:
205: if (windowSystem != null) {
206: windowSystem.load();
207: StartLog.logProgress("Window system loaded");
208: if (StartLog.willLog()) {
209: waitForMainWindowPaint();
210: }
211: windowSystem.show();
212: } else {
213: Logger
214: .getLogger(NonGui.class.getName())
215: .log(
216: Level.WARNING,
217: null,
218: new NullPointerException(
219: "\n\n\nWindowSystem is not supplied!!!\\n\n"));
220: }
221: StartLog.logProgress("Window system shown");
222: if (!StartLog.willLog()) {
223: maybeDie(null);
224: }
225: }
226: });
227: StartLog.logEnd("Main window initialization"); //NOI18N
228: }
229:
230: private static void waitForMainWindowPaint() {
231: // Waits for notification about processed paint event for main window
232: // require modified java.awt.EventQueue to run succesfully
233: Runnable r = new Runnable() {
234: public void run() {
235: try {
236: Class clz = Class
237: .forName("org.netbeans.performance.test.guitracker.LoggingRepaintManager"); // NOI18N
238: Method m = clz.getMethod("measureStartup",
239: new Class[] {}); // NOI18N
240: Object o = m.invoke(null);
241: endOfStartupMeasuring(o);
242: } catch (ClassNotFoundException e) {
243: StartLog.logProgress(e.toString());
244: } catch (NoSuchMethodException e) {
245: StartLog.logProgress(e.toString());
246: // } catch (InterruptedException e) {
247: // StartLog.logProgress(e.toString());
248: } catch (IllegalAccessException e) {
249: StartLog.logProgress(e.toString());
250: } catch (java.lang.reflect.InvocationTargetException e) {
251: StartLog.logProgress(e.toString());
252: }
253: }
254: };
255: new Thread(r).start();
256: }
257:
258: private static void endOfStartupMeasuring(Object o) {
259: StartLog.logProgress("Startup memory and time measured"); // NOI18N
260: maybeDie(o);
261: }
262:
263: private static void maybeDie(Object o) {
264: // finish starting
265: if (System.getProperty("netbeans.kill") != null) {
266: org.netbeans.TopSecurityManager.exit(5);
267: }
268:
269: // close IDE
270: if (System.getProperty("netbeans.close") != null) {
271: if (Boolean.getBoolean("netbeans.warm.close")) {
272: // Do other stuff related to startup, to measure the effect.
273: // Useful for performance testing.
274: new WarmUpSupport().run(); // synchronous
275: }
276: if (o != null)
277: StartLog.logMeasuredStartupTime(((Long) o).longValue());
278: org.openide.LifecycleManager.getDefault().exit();
279: }
280: }
281:
282: /** Exits from the VM.
283: */
284: static void doExit(int code) {
285: TopSecurityManager.exit(code);
286: }
287:
288: /** Get the module subsystem. */
289: public ModuleSystem getModuleSystem() {
290: return Main.getModuleSystem();
291: }
292:
293: }
|