Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: de62f6adb9c545b59ec5e76991e074d8a38701a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*******************************************************************************
 * Copyright (c) 2011 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.internal.discovery.startup;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.m2e.core.core.IMavenConstants;
import org.eclipse.m2e.core.ui.internal.UpdateConfigurationJob;
import org.eclipse.m2e.internal.discovery.DiscoveryActivator;
import org.eclipse.m2e.internal.discovery.Messages;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.internal.IPreferenceConstants;
import org.eclipse.ui.internal.Workbench;
import org.eclipse.ui.internal.util.PrefUtil;
import org.eclipse.ui.statushandlers.StatusManager;


@SuppressWarnings("restriction")
public class UpdateConfigurationStartup implements IStartup {

  private static final String PROJECT_PREF = DiscoveryActivator.PLUGIN_ID + ".pref.projects"; //$NON-NLS-1$

  public void earlyStartup() {
    IProject[] projects = getSavedProjects();
    if(projects != null && projects.length > 0) {
      updateConfiguration(projects);
    }
    disableStartup();
  }

  /*
   * Enables the early startup for this bundle
   */
  public static void enableStartup() {
    saveMarkedProjects();
    addEarlyStartup();
  }

  /*
   * Enable early startup for this bundle, also add the list of projects to those that should be configured
   */
  public static void enableStartup(Collection<String> knownProjects) {
    if(knownProjects != null) {
      Set<String> projects = new HashSet<String>(knownProjects);
      for(IProject project : getMarkedProjects()) {
        projects.add(project.getName());
      }
      saveProjects(projects);
    } else {
      saveMarkedProjects();
    }
    addEarlyStartup();
  }

  /*
   * Disables the early startup for this bundle 
   */
  public static void disableStartup() {
    clearSavedProjects();
    removeEarlyStartup();
  }

  /*
   * Configure projects
   */
  public static void updateConfiguration() {
    Collection<IProject> projects = getMarkedProjects();
    new UpdateConfigurationJob(projects.toArray(new IProject[projects.size()])).schedule();
  }

  private static void updateConfiguration(IProject[] projects) {
    new UpdateConfigurationJob(projects).schedule();
  }

  private static void addEarlyStartup() {
    String[] disabledEarlyActivation = Workbench.getInstance().getDisabledEarlyActivatedPlugins();

    // If we aren't disabled, nothing to do
    if(!isDisabled(disabledEarlyActivation)) {
      return;
    }

    String[] disabledPlugins = new String[disabledEarlyActivation.length - 1];
    int index = 0;
    for(String plugin : disabledEarlyActivation) {
      if(!DiscoveryActivator.PLUGIN_ID.equals(plugin)) {
        disabledPlugins[index] = plugin;
      }
    }
    setEarlyActivationPreference(disabledPlugins);
  }

  private static void removeEarlyStartup() {
    String[] disabledEarlyActivation = Workbench.getInstance().getDisabledEarlyActivatedPlugins();

    // Determine if we're already disabled
    if(isDisabled(disabledEarlyActivation)) {
      return;
    }

    String[] disabledPlugins = new String[disabledEarlyActivation.length + 1];
    System.arraycopy(disabledEarlyActivation, 0, disabledPlugins, 0, disabledEarlyActivation.length);
    disabledPlugins[disabledPlugins.length - 1] = DiscoveryActivator.PLUGIN_ID;

    setEarlyActivationPreference(disabledPlugins);
  }

  private static boolean isDisabled(String[] disabledEarlyActivation) {
    for(String item : disabledEarlyActivation) {
      if(DiscoveryActivator.PLUGIN_ID.equals(item)) {
        return true;
      }
    }
    return false;
  }

  private static void setEarlyActivationPreference(String[] disabledPlugins) {// Add ourself to disabled
    StringBuffer preference = new StringBuffer();
    for(String item : disabledPlugins) {
      preference.append(item).append(IPreferenceConstants.SEPARATOR);
    }

    IPreferenceStore store = PrefUtil.getInternalPreferenceStore();
    store.putValue(IPreferenceConstants.PLUGINS_NOT_ACTIVATED_ON_STARTUP, preference.toString());
    PrefUtil.savePrefs();
  }

  /*
   * Get projects we saved previously  
   */
  public static IProject[] getSavedProjects() {
    String[] projectNames = DiscoveryActivator.getDefault().getPreferenceStore().getString(PROJECT_PREF)
        .split(String.valueOf(IPreferenceConstants.SEPARATOR));
    List<IProject> projects = new ArrayList<IProject>(projectNames.length);
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    for(String projectName : projectNames) {
      if(projectName.length() > 0) {
        IProject project = root.getProject(projectName);
        if(project != null) {
          projects.add(project);
        }
      }
    }
    return projects.toArray(new IProject[projects.size()]);
  }

  /*
   * Save a list of projects which have configuration markers
   */
  public static void saveMarkedProjects() {
    StringBuilder sb = new StringBuilder();
    for(IProject project : getMarkedProjects()) {
      sb.append(project.getName()).append(IPreferenceConstants.SEPARATOR);
    }
    DiscoveryActivator.getDefault().getPreferenceStore().putValue(PROJECT_PREF, sb.toString());
  }

  /*
   * Save a list of projects which have configuration markers
   */
  public static void saveProjects(Collection<String> projects) {
    StringBuilder sb = new StringBuilder();
    for(String project : projects) {
      sb.append(project).append(IPreferenceConstants.SEPARATOR);
    }
    DiscoveryActivator.getDefault().getPreferenceStore().putValue(PROJECT_PREF, sb.toString());
  }

  private static Collection<IProject> getMarkedProjects() {
    List<IProject> projects = new ArrayList<IProject>();
    MultiStatus status = new MultiStatus(DiscoveryActivator.PLUGIN_ID, 0,
        Messages.UpdateConfigurationStartup_MarkerError, null);
    for(IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
      try {
        if(project.findMarkers(IMavenConstants.MARKER_LIFECYCLEMAPPING_ID, true, IResource.DEPTH_ONE).length > 0) {
          projects.add(project);
        }
      } catch(CoreException e) {
        status.add(e.getStatus());
      }
    }
    if(status.getChildren().length > 0) {
      StatusManager.getManager().handle(status);
    }
    return projects;
  }

  /*
   * Empty the list of saved projects
   */
  public static void clearSavedProjects() {
    DiscoveryActivator.getDefault().getPreferenceStore().putValue(PROJECT_PREF, ""); //$NON-NLS-1$
  }
}

Back to the top