Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0eb8df3647231c7e7ddb27f75f932da6480aa9a7 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*******************************************************************************
 * Copyright (c) 2008-2010 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.core.embedder;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.core.runtime.preferences.InstanceScope;

import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.internal.MavenPluginActivator;
import org.eclipse.m2e.core.internal.launch.AbstractMavenRuntime;
import org.eclipse.m2e.core.internal.launch.ClasspathEntry;
import org.eclipse.m2e.core.internal.launch.MavenEmbeddedRuntime;
import org.eclipse.m2e.core.internal.launch.MavenExternalRuntime;
import org.eclipse.m2e.core.internal.launch.MavenWorkspaceRuntime;
import org.eclipse.m2e.core.internal.preferences.MavenPreferenceConstants;


/**
 * Maven runtime manager
 * 
 * @author Eugene Kuleshov
 * @author Jason van Zyl
 */
public class MavenRuntimeManager {

  public static final String DEFAULT = "DEFAULT"; //$NON-NLS-1$

  public static final String EMBEDDED = "EMBEDDED"; //$NON-NLS-1$

  public static final String WORKSPACE = "WORKSPACE"; //$NON-NLS-1$

  private final IEclipsePreferences[] preferencesLookup = new IEclipsePreferences[2];

  private final IPreferencesService preferenceStore;

  public MavenRuntimeManager() {
    this.preferenceStore = Platform.getPreferencesService();
    this.preferencesLookup[0] = InstanceScope.INSTANCE.getNode(IMavenConstants.PLUGIN_ID);
    this.preferencesLookup[1] = DefaultScope.INSTANCE.getNode(IMavenConstants.PLUGIN_ID);
  }

  /**
   * @deprecated this method does nothing
   */
  public void setEmbeddedRuntime(MavenRuntime embeddedRuntime) {
  }

  /**
   * @deprecated this method does nothing
   */
  @Deprecated
  public void setWorkspaceRuntime(MavenRuntime workspaceRuntime) {
  }

  public MavenRuntime getDefaultRuntime() {
    String selected = preferenceStore.get(MavenPreferenceConstants.P_DEFAULT_RUNTIME, null, preferencesLookup);
    if(selected == null) {
      return getEmbeddedRuntime();
    }
    MavenRuntime runtime = getRuntimeByName(selected);
    return runtime != null && runtime.isAvailable() ? runtime : getEmbeddedRuntime();
  }

  private MavenEmbeddedRuntime getEmbeddedRuntime() {
    return new MavenEmbeddedRuntime(MavenPluginActivator.getDefault().getBundle());
  }

  /**
   * @deprecated use {@link #getRuntimeByName(String)}
   */
  public MavenRuntime getRuntime(String location) {
    if(location == null || location.length() == 0 || DEFAULT.equals(location)) {
      return getDefaultRuntime();
    }
    for(MavenRuntime runtime : getRuntimes().values()) {
      if(location.equals(runtime.getLocation())) {
        return runtime;
      }
    }

    return null;
  }

  /**
   * @since 1.5
   */
  public MavenRuntime getRuntimeByName(String name) {
    return getRuntimes().get(name);
  }

  public List<MavenRuntime> getMavenRuntimes() {
    List<MavenRuntime> mavenRuntimes = new ArrayList<MavenRuntime>();
    for(MavenRuntime mavenRuntime : getRuntimes().values()) {
      if(mavenRuntime.isAvailable()) {
        mavenRuntimes.add(mavenRuntime);
      }
    }
    return mavenRuntimes;
  }

  public void reset() {
    preferencesLookup[0].remove(MavenPreferenceConstants.P_RUNTIMES);
    preferencesLookup[0].remove(MavenPreferenceConstants.P_DEFAULT_RUNTIME);
    removeRuntimePreferences();
    flush();
  }

  public void setDefaultRuntime(MavenRuntime runtime) {
    if(runtime == null) {
      preferencesLookup[0].remove(MavenPreferenceConstants.P_DEFAULT_RUNTIME);
    } else {
      preferencesLookup[0].put(MavenPreferenceConstants.P_DEFAULT_RUNTIME, runtime.getName());
    }
    flush();
  }

  private void flush() {
    try {
      preferencesLookup[0].flush();
    } catch(BackingStoreException ex) {
      // TODO do nothing
    }
  }

  public void setRuntimes(List<MavenRuntime> runtimes) {
    removeRuntimePreferences();
    StringBuilder sb = new StringBuilder();
    for(MavenRuntime runtime : runtimes) {
      if(runtime.isEditable()) {
        AbstractMavenRuntime impl = (AbstractMavenRuntime) runtime;
        if(sb.length() > 0) {
          sb.append('|');
        }
        sb.append(runtime.getName());
        if(!impl.isLegacy()) {
          Preferences runtimeNode = getRuntimePreferences(runtime.getName(), true);
          runtimeNode.put("location", runtime.getLocation());
          String extensions = encodeClasspath(impl.getExtensions());
          if(extensions != null) {
            runtimeNode.put("extensions", extensions);
          } else {
            runtimeNode.remove("extensions");
          }
        }
      }
    }
    preferencesLookup[0].put(MavenPreferenceConstants.P_RUNTIMES, sb.toString());
    flush();
  }

  private void removeRuntimePreferences() {
    try {
      if(preferencesLookup[0].nodeExists(MavenPreferenceConstants.P_RUNTIMES_NODE)) {
        preferencesLookup[0].node(MavenPreferenceConstants.P_RUNTIMES_NODE).removeNode();
      }
    } catch(BackingStoreException ex) {
      // assume the node does not exist
    }
  }

  private Preferences getRuntimePreferences(String name, boolean create) {
    Preferences runtimesNode = preferencesLookup[0].node(MavenPreferenceConstants.P_RUNTIMES_NODE);
    try {
      if(runtimesNode.nodeExists(name) || create) {
        return runtimesNode.node(name);
      }
    } catch(BackingStoreException ex) {
      // assume the node does not exist
    }
    return null;
  }

  private String encodeClasspath(List<ClasspathEntry> classpath) {
    if(classpath == null || classpath.isEmpty()) {
      return null;
    }
    StringBuilder sb = new StringBuilder();
    for(ClasspathEntry cpe : classpath) {
      if(sb.length() > 0) {
        sb.append('|');
      }
      sb.append(cpe.toExternalForm());
    }
    return sb.toString();
  }

  private List<ClasspathEntry> decodeClasspath(String string) {
    if(string == null || string.isEmpty()) {
      return null;
    }
    List<ClasspathEntry> result = new ArrayList<ClasspathEntry>();
    for(String entry : string.split("\\|")) {
      ClasspathEntry decoded = ClasspathEntry.fromExternalForm(entry);
      if(decoded != null) {
        result.add(decoded);
      }
    }
    return result;
  }

  private Map<String, AbstractMavenRuntime> getRuntimes() {
    Map<String, AbstractMavenRuntime> runtimes = new LinkedHashMap<String, AbstractMavenRuntime>();
    runtimes.put(EMBEDDED, getEmbeddedRuntime());
    runtimes.put(WORKSPACE, new MavenWorkspaceRuntime(MavenPluginActivator.getDefault().getMavenProjectManager()));

    String runtimesPreference = preferenceStore.get(MavenPreferenceConstants.P_RUNTIMES, null, preferencesLookup);
    if(runtimesPreference != null && runtimesPreference.length() > 0) {
      for(String name : runtimesPreference.split("\\|")) { //$NON-NLS-1$
        Preferences preferences = getRuntimePreferences(name, false);
        AbstractMavenRuntime runtime;
        if(preferences == null) {
          runtime = (AbstractMavenRuntime) createExternalRuntime(name);
        } else {
          runtime = createRuntime(name, preferences);
        }
        runtimes.put(runtime.getName(), runtime);
      }
    }

    return runtimes;
  }

  private AbstractMavenRuntime createRuntime(String name, Preferences preferences) {
    String location = preferences.get("location", null);
    MavenExternalRuntime runtime = new MavenExternalRuntime(name, location);
    runtime.setExtensions(decodeClasspath(preferences.get("extensions", null)));
    return runtime;
  }

  /**
   * @deprecated as of version 1.5, m2e does not provide public API to create MavenRuntime instances
   */
  public static MavenRuntime createExternalRuntime(String location) {
    return new MavenExternalRuntime(location);
  }

  /**
   * @deprecated global setting file is only used to determine localRepository location, which does not make much sense
   */
  public String getGlobalSettingsFile() {
    String globalSettings = preferenceStore.get(MavenPreferenceConstants.P_GLOBAL_SETTINGS_FILE, null,
        preferencesLookup);
    return globalSettings.trim().length() == 0 ? null : globalSettings;
  }

}

Back to the top