[r=kirillk]
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ide / startup / impl / StartupManagerImpl.java
blobaa45db575630907cb438982e81f6bc80a5f3953d
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.ide.startup.impl;
18 import com.intellij.ide.caches.CacheUpdater;
19 import com.intellij.ide.startup.StartupManagerEx;
20 import com.intellij.openapi.application.Application;
21 import com.intellij.openapi.application.ApplicationManager;
22 import com.intellij.openapi.diagnostic.Logger;
23 import com.intellij.openapi.progress.ProcessCanceledException;
24 import com.intellij.openapi.progress.ProgressIndicator;
25 import com.intellij.openapi.progress.ProgressManager;
26 import com.intellij.openapi.project.*;
27 import com.intellij.openapi.vfs.VirtualFileManager;
28 import com.intellij.util.containers.CollectionFactory;
29 import com.intellij.util.io.storage.HeavyProcessLatch;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.TestOnly;
33 import javax.swing.*;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.LinkedList;
37 import java.util.List;
39 public class StartupManagerImpl extends StartupManagerEx {
40 private static final Logger LOG = Logger.getInstance("#com.intellij.ide.startup.impl.StartupManagerImpl");
42 private final List<Runnable> myPreStartupActivities = Collections.synchronizedList(new ArrayList<Runnable>());
43 private final List<Runnable> myStartupActivities = new ArrayList<Runnable>();
44 private final List<Runnable> myPostStartupActivities = Collections.synchronizedList(new ArrayList<Runnable>());
46 private final List<CacheUpdater> myCacheUpdaters = new LinkedList<CacheUpdater>();
48 private volatile boolean myPreStartupActivitiesPassed = false;
49 private volatile boolean myStartupActivitiesRunning = false;
50 private volatile boolean myStartupActivitiesPassed = false;
51 private volatile boolean myPostStartupActivitiesPassed = false;
53 private final Project myProject;
55 public StartupManagerImpl(Project project) {
56 myProject = project;
59 @Override
60 public void registerPreStartupActivity(@NotNull Runnable runnable) {
61 LOG.assertTrue(!myPreStartupActivitiesPassed, "Registering pre startup activity that will never be run");
62 myPreStartupActivities.add(runnable);
65 @Override
66 public void registerStartupActivity(Runnable runnable) {
67 LOG.assertTrue(!myStartupActivitiesPassed, "Registering startup activity that will never be run");
68 myStartupActivities.add(runnable);
71 @Override
72 public synchronized void registerPostStartupActivity(Runnable runnable) {
73 LOG.assertTrue(!myPostStartupActivitiesPassed, "Registering post-startup activity that will never be run");
74 myPostStartupActivities.add(runnable);
77 @Override
78 public void registerCacheUpdater(CacheUpdater updater) {
79 LOG.assertTrue(!myStartupActivitiesPassed, CacheUpdater.class.getSimpleName() + " must be registered before startup activity finished");
80 myCacheUpdaters.add(updater);
83 @Override
84 public boolean startupActivityRunning() {
85 return myStartupActivitiesRunning;
88 @Override
89 public boolean startupActivityPassed() {
90 return myStartupActivitiesPassed;
93 @Override
94 public boolean postStartupActivityPassed() {
95 return myPostStartupActivitiesPassed;
98 public void runStartupActivities() {
99 ApplicationManager.getApplication().runReadAction(new Runnable() {
100 public void run() {
101 assert !HeavyProcessLatch.INSTANCE.isRunning();
102 HeavyProcessLatch.INSTANCE.processStarted();
103 try {
104 runActivities(myPreStartupActivities);
105 myPreStartupActivitiesPassed = true;
107 myStartupActivitiesRunning = true;
108 runActivities(myStartupActivities);
110 myStartupActivitiesRunning = false;
111 myStartupActivitiesPassed = true;
113 startCacheUpdate();
115 finally {
116 HeavyProcessLatch.INSTANCE.processFinished();
122 public synchronized void runPostStartupActivities() {
123 Application app = ApplicationManager.getApplication();
124 app.assertIsDispatchThread();
126 if (myPostStartupActivitiesPassed) return;
128 final List<Runnable> dumbAware = CollectionFactory.arrayList();
129 final List<Runnable> nonDumbAware = CollectionFactory.arrayList();
131 for (Runnable each : myPostStartupActivities) {
132 if (each instanceof DumbAware) {
133 dumbAware.add(each);
135 else {
136 nonDumbAware.add(each);
140 runActivities(dumbAware);
141 DumbService.getInstance(myProject).runWhenSmart(new Runnable() {
142 public void run() {
143 if (myProject.isDisposed()) return;
144 runActivities(nonDumbAware);
146 myPostStartupActivitiesPassed = true;
147 myPostStartupActivities.clear();
151 if (!app.isUnitTestMode()) {
152 VirtualFileManager.getInstance().refresh(!app.isHeadlessEnvironment());
156 private void startCacheUpdate() {
157 try {
158 DumbServiceImpl.getInstance(myProject).queueCacheUpdate(myCacheUpdaters);
160 catch (ProcessCanceledException e) {
161 throw e;
163 catch (Throwable e) {
164 LOG.error(e);
168 private static void runActivities(final List<Runnable> activities) {
169 final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
170 while (!activities.isEmpty()) {
171 final Runnable runnable = activities.remove(0);
172 if (indicator != null) indicator.checkCanceled();
174 try {
175 runnable.run();
177 catch (ProcessCanceledException e) {
178 throw e;
180 catch (Throwable ex) {
181 LOG.error(ex);
186 public void runWhenProjectIsInitialized(final Runnable action) {
187 final Runnable runnable;
189 final Application application = ApplicationManager.getApplication();
190 if (action instanceof DumbAware) {
191 runnable = new DumbAwareRunnable() {
192 public void run() {
193 application.runWriteAction(action);
197 else {
198 runnable = new Runnable() {
199 public void run() {
200 application.runWriteAction(action);
205 if (myProject.isInitialized() || (application.isUnitTestMode() && myPostStartupActivitiesPassed)) {
206 // in tests which simulate project opening, post-startup activities could have been run already. Then we should act as if the project was initialized
207 if (application.isDispatchThread()) {
208 runnable.run();
210 else {
211 SwingUtilities.invokeLater(new Runnable() {
212 public void run() {
213 if (!myProject.isDisposed()) {
214 runnable.run();
220 else {
221 registerPostStartupActivity(runnable);
225 @TestOnly
226 public void prepareForNextTest() {
227 myPreStartupActivities.clear();
228 myStartupActivities.clear();
229 myPostStartupActivities.clear();
230 myCacheUpdaters.clear();
232 myPreStartupActivitiesPassed = false;
233 myStartupActivitiesRunning = false;
234 myStartupActivitiesPassed = false;
235 myPostStartupActivitiesPassed = false;
238 @TestOnly
239 public void checkCleared() {
240 try {
241 assert myStartupActivities.isEmpty() : "Activities: " + myStartupActivities;
242 assert myPostStartupActivities.isEmpty() : "Post Activities: " + myPostStartupActivities;
243 assert myPreStartupActivities.isEmpty() : "Pre Activities: " + myPreStartupActivities;
245 finally {
246 prepareForNextTest();