Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2a16b8b0905c6e8f24a1cfad96b9a2c6ee63f97b (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
/*******************************************************************************
 * Copyright (c) 2004, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.make.internal.core.scannerconfig2;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.cdt.make.core.scannerconfig.IScannerConfigBuilderInfo2;
import org.eclipse.cdt.make.core.scannerconfig.IScannerConfigBuilderInfo2Set;
import org.eclipse.cdt.make.core.scannerconfig.InfoContext;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Preferences;

/**
 * Manages profiles per project
 * 
 * @author vhirsl
 */
public final class ScannerConfigProfileManager {
	public static final String SI_PROFILE_SIMPLE_ID = "ScannerConfigurationDiscoveryProfile";	//$NON-NLS-1$
	public static final String PER_PROJECT_PROFILE_ID = MakeCorePlugin.getUniqueIdentifier() + ".GCCStandardMakePerProjectProfile"; //$NON-NLS-1$
	public static final String NULL_PROFILE_ID = "";//$NON-NLS-1$
	public static final String DEFAULT_SI_PROFILE_ID = NULL_PROFILE_ID;
	
	private final Map<IProject, Map<InfoContext, Object>> projectToProfileInstanceMap;
	private List<String> profileIds;
	private List<String> contextAwareProfileIds;
	private final Object fLock = new Object();

	/**
	 * Singleton pattern
	 */
	private ScannerConfigProfileManager() {
		projectToProfileInstanceMap = new HashMap<IProject, Map<InfoContext, Object>>();
	}
	private static final ScannerConfigProfileManager instance = new ScannerConfigProfileManager();

	public static ScannerConfigProfileManager getInstance() {
		return instance;
	}

	private String getProfileId(IProject project, InfoContext context) {
		String profileId;
		try {
			IScannerConfigBuilderInfo2Set container = createScannerConfigBuildInfo2Set(project);
			IScannerConfigBuilderInfo2 buildInfo = container.getInfo(context);
			if(buildInfo == null)
				buildInfo = container.getInfo(new InfoContext(project));
			profileId = buildInfo.getSelectedProfileId();
		} catch (CoreException e) {
			MakeCorePlugin.log(e);
			profileId = DEFAULT_SI_PROFILE_ID;
		}
		return profileId;
	}

	/**
	 * For projects that do not have profile id specified in .project file.
	 * For example managed projects.
	 */
	public void addProfile(IProject project, ScannerConfigProfile profile) {
		addProfile(project, new InfoContext(project), profile);
	}

	public void addProfile(IProject project, InfoContext context, ScannerConfigProfile profile) {
		getProfileMap(project, true).put(context, profile);
	}
	
	private Map<InfoContext, Object> getProfileMap(IProject project, boolean create){
		synchronized (fLock) {
			Map<InfoContext, Object> map = projectToProfileInstanceMap.get(project);
			if(map == null && create){
				map = new HashMap<InfoContext, Object>();
				projectToProfileInstanceMap.put(project, map);
			}
			return Collections.synchronizedMap(map);
		}
	}
	
	public void handleProjectRemoved(IProject project){
		synchronized (fLock) {
			projectToProfileInstanceMap.remove(project);
		}
	}
	
	/**
	 * @param profileId - if null, get the one associated with the project
	 * @return the scannerConfigProfile instance for a project.
	 */
	public SCProfileInstance getSCProfileInstance(IProject project, String profileId) {
		return getSCProfileInstance(project, new InfoContext(project), profileId);
	}

	public SCProfileInstance getSCProfileInstance(IProject project, InfoContext context, String profileId) {

		// if not specified read from .project file
        if (profileId == NULL_PROFILE_ID) {
            profileId = getProfileId(project, context);
        }
		synchronized (fLock) {
	        // is the project's profile already loaded?
	        Map<InfoContext, Object> map = getProfileMap(project, true);
	        SoftReference<SCProfileInstance> profileInstanceReference = (SoftReference<SCProfileInstance>) map.get(context);
	        SCProfileInstance profileInstance = profileInstanceReference != null ? profileInstanceReference.get() : null;
	        
	        if (profileInstance == null || !profileInstance.getProfile().getId().equals(profileId)) {
	            profileInstance = new SCProfileInstance(project, context, getSCProfileConfiguration(profileId));
	            map.put(context, new SoftReference<SCProfileInstance>(profileInstance));
	        }
	        return profileInstance;
		}
	}

    public SCProfileInstance getSCProfileInstance(String profileId) {
        SCProfileInstance profileInstance = null;
        if (profileId != NULL_PROFILE_ID) {
            profileInstance = new SCProfileInstance(null, getSCProfileConfiguration(profileId));
        }
        return profileInstance;
    }

	/**
	 * @param profileId - if null, get the default one
	 * @return Returns the scannerConfigProfile for a project.
	 */
	public ScannerConfigProfile getSCProfileConfiguration(String profileId) {
		profileId = (profileId == NULL_PROFILE_ID) ? getDefaultSIProfileId() : profileId;
		return new ScannerConfigProfile(profileId);
	}

	/**
	 * @return a list of available scanner config profile id's.
	 */
	public List<String> getProfileIds() {
		synchronized (fLock) {
			if (profileIds == null) {
				profileIds = new ArrayList<String>();
				IExtensionPoint extension = Platform.getExtensionRegistry().
						getExtensionPoint(MakeCorePlugin.PLUGIN_ID, ScannerConfigProfileManager.SI_PROFILE_SIMPLE_ID);
				if (extension != null) {
					IExtension[] extensions = extension.getExtensions();
					for (int i = 0; i < extensions.length; ++i) {
						String rProfileId = extensions[i].getUniqueIdentifier();
						profileIds.add(rProfileId);
					}
				}
			}
		}
		return Collections.unmodifiableList(profileIds);	
	}
	
	/**
	 * @return the list of profile IDs supported for this context
	 */
	public List<String> getProfileIds(InfoContext context){
		if(context.isDefaultContext() || context.getProject() == null)
			return getProfileIds();
		
		synchronized (fLock) {
			if(contextAwareProfileIds == null){
				contextAwareProfileIds = new ArrayList<String>();
				List<String> all = getProfileIds();

				for(int i = 0; i < all.size(); i++){
					String id = all.get(i);
					ScannerConfigProfile profile = getSCProfileConfiguration(id);
					if(profile.supportsContext())
						contextAwareProfileIds.add(id);
				}
			}
		}
		return Collections.unmodifiableList(contextAwareProfileIds);
	}

	/**
	 * @return default profile id
	 */
	public static String getDefaultSIProfileId() {
		return DEFAULT_SI_PROFILE_ID;
	}
	
	/**
     * Set selectedProfile to profileId 
	 */
    public static IScannerConfigBuilderInfo2 createScannerConfigBuildInfo2(IProject project, String profileId) throws CoreException {
		return ScannerConfigInfoFactory2.create(project, profileId);
	}

    /**
     * Use stored selectedProfile
     */
    public static IScannerConfigBuilderInfo2 createScannerConfigBuildInfo2(IProject project) throws CoreException {
        return ScannerConfigInfoFactory2.create(project, ScannerConfigProfileManager.NULL_PROFILE_ID);
    }
    
    public static IScannerConfigBuilderInfo2Set createScannerConfigBuildInfo2Set(IProject project) throws CoreException {
        return ScannerConfigInfoFactory2.createInfoSet(project, ScannerConfigProfileManager.NULL_PROFILE_ID);
    }

    public static IScannerConfigBuilderInfo2Set createScannerConfigBuildInfo2Set(IProject project, String profileId) throws CoreException {
        return ScannerConfigInfoFactory2.createInfoSet(project, profileId);
    }

    public static IScannerConfigBuilderInfo2Set createScannerConfigBuildInfo2Set(Preferences prefs, boolean useDefaults) throws CoreException {
    	return ScannerConfigInfoFactory2.createInfoSet(prefs, ScannerConfigProfileManager.NULL_PROFILE_ID, useDefaults);
    }

    public static IScannerConfigBuilderInfo2Set createScannerConfigBuildInfo2Set(Preferences prefs, String profileId, boolean useDefaults) throws CoreException {
        return ScannerConfigInfoFactory2.createInfoSet(prefs, profileId, useDefaults);
    }

	/**
     * Set selectedProfile to profileId 
	 */
    public static IScannerConfigBuilderInfo2 createScannerConfigBuildInfo2(Preferences prefs, String profileId, boolean useDefaults) {
		return ScannerConfigInfoFactory2.create(prefs, profileId, useDefaults);
	}

    /**
     * Use stored selectedProfile
     */
    public static IScannerConfigBuilderInfo2 createScannerConfigBuildInfo2(Preferences prefs, boolean useDefaults) {
        return ScannerConfigInfoFactory2.create(prefs, ScannerConfigProfileManager.NULL_PROFILE_ID, useDefaults);
    }

}

Back to the top