Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f379d26b4e94a6a0519bdeaaf896e05103f2ab8a (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
/*******************************************************************************
 * Copyright (c) 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.internal.project;

import org.osgi.service.prefs.BackingStoreException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;

import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.project.ResolverConfiguration;

/**
 * @TODO anyone can think of a better name?
 */
public class ResolverConfigurationIO {
  private static final Logger log = LoggerFactory.getLogger(ResolverConfigurationIO.class);

  /**
   * Configuration version project preference key.
   */
  private static final String P_VERSION = "version"; //$NON-NLS-1$

  /**
   * Workspace dependency resolution project preference key. Boolean, <code>true</code> means workspace dependency
   * resolution is enabled.
   */
  private static final String P_RESOLVE_WORKSPACE_PROJECTS = "resolveWorkspaceProjects"; //$NON-NLS-1$

  /**
   * Active profiles project preference key. Value is comma-separated list of enabled profiles.
   */
  private static final String P_ACTIVE_PROFILES = "activeProfiles"; //$NON-NLS-1$

  /**
   * Current configuration version value. See {@link #P_VERSION}
   */
  private static final String VERSION = "1"; //$NON-NLS-1$
  
  public static boolean saveResolverConfiguration(IProject project, ResolverConfiguration configuration) {
    IScopeContext projectScope = new ProjectScope(project);
    IEclipsePreferences projectNode = projectScope.getNode(IMavenConstants.PLUGIN_ID);
    if(projectNode != null) {
      projectNode.put(P_VERSION, VERSION);

      projectNode.putBoolean(P_RESOLVE_WORKSPACE_PROJECTS, configuration.shouldResolveWorkspaceProjects());

      projectNode.put(P_ACTIVE_PROFILES, configuration.getActiveProfiles());

      try {
        projectNode.flush();
        return true;
      } catch(BackingStoreException ex) {
        log.error("Failed to save resolver configuration", ex);
      }
    }

    return false;
  }

  public static ResolverConfiguration readResolverConfiguration(IProject project) {
    IScopeContext projectScope = new ProjectScope(project);
    IEclipsePreferences projectNode = projectScope.getNode(IMavenConstants.PLUGIN_ID);
    if(projectNode == null) {
      return new ResolverConfiguration();
    }

    String version = projectNode.get(P_VERSION, null);
    if(version == null) { // migrate from old config
      // return LegacyBuildPathManager.getResolverConfiguration(project);
      return new ResolverConfiguration();
    }

    ResolverConfiguration configuration = new ResolverConfiguration();
    configuration.setResolveWorkspaceProjects(projectNode.getBoolean(P_RESOLVE_WORKSPACE_PROJECTS, false));

    configuration.setActiveProfiles(projectNode.get(P_ACTIVE_PROFILES, "")); //$NON-NLS-1$
    return configuration;
  }

}

Back to the top