View | Details | Raw Unified | Return to bug 183117 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/model/JavaProjectTests.java (-3 / +1 lines)
Lines 26-32 Link Here
26
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
26
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
27
import org.eclipse.jdt.internal.core.JavaModelManager;
27
import org.eclipse.jdt.internal.core.JavaModelManager;
28
import org.eclipse.jdt.internal.core.PackageFragmentRoot;
28
import org.eclipse.jdt.internal.core.PackageFragmentRoot;
29
import org.eclipse.jdt.internal.core.UserLibrary;
30
import org.eclipse.jdt.internal.core.UserLibraryManager;
29
import org.eclipse.jdt.internal.core.UserLibraryManager;
31
import org.eclipse.jdt.internal.core.util.Util;
30
import org.eclipse.jdt.internal.core.util.Util;
32
31
Lines 1523-1530 Link Here
1523
	userEntries[1] = JavaCore.newLibraryEntry(path, null, null, pathRules, extraAttributes, false);
1522
	userEntries[1] = JavaCore.newLibraryEntry(path, null, null, pathRules, extraAttributes, false);
1524
	
1523
	
1525
	// Create user library
1524
	// Create user library
1526
	UserLibrary library = new UserLibrary(userEntries, false);
1525
	JavaModelManager.getUserLibraryManager().setUserLibrary("TEST", userEntries, false);
1527
	UserLibraryManager.setUserLibrary("TEST", library, null);
1528
	
1526
	
1529
	// Verify it has been written in preferences
1527
	// Verify it has been written in preferences
1530
	IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences();
1528
	IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences();
(-)model/org/eclipse/jdt/internal/core/UserLibraryClasspathContainerInitializer.java (-39 / +34 lines)
Lines 24-39 Link Here
24
public class UserLibraryClasspathContainerInitializer extends ClasspathContainerInitializer {
24
public class UserLibraryClasspathContainerInitializer extends ClasspathContainerInitializer {
25
25
26
	/* (non-Javadoc)
26
	/* (non-Javadoc)
27
	 * @see org.eclipse.jdt.core.ClasspathContainerInitializer#initialize(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
27
	 * @see org.eclipse.jdt.core.ClasspathContainerInitializer#canUpdateClasspathContainer(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
28
	 */
29
	public boolean canUpdateClasspathContainer(IPath containerPath, IJavaProject project) {
30
		return isUserLibraryContainer(containerPath);
31
	}
32
33
	/* (non-Javadoc)
34
	 * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getComparisonID(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
35
	 */
36
	public Object getComparisonID(IPath containerPath, IJavaProject project) {
37
		return containerPath;
38
	}
39
	
40
	/**
41
	 * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getDescription(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
28
	 */
42
	 */
43
	public String getDescription(IPath containerPath, IJavaProject project) {
44
		if (isUserLibraryContainer(containerPath)) {
45
			return containerPath.segment(1);
46
		}
47
		return super.getDescription(containerPath, project);
48
	}
49
29
	public void initialize(IPath containerPath, IJavaProject project) throws CoreException {
50
	public void initialize(IPath containerPath, IJavaProject project) throws CoreException {
30
		if (isUserLibraryContainer(containerPath)) {
51
		if (isUserLibraryContainer(containerPath)) {
31
			String userLibName = containerPath.segment(1);
52
			String userLibName = containerPath.segment(1);
32
						
53
			UserLibrary userLibrary = JavaModelManager.getUserLibraryManager().getUserLibrary(userLibName);
33
			UserLibrary entries = UserLibraryManager.getUserLibrary(userLibName);
54
			if (userLibrary != null) {
34
			if (entries != null) {
35
				UserLibraryClasspathContainer container = new UserLibraryClasspathContainer(userLibName);
55
				UserLibraryClasspathContainer container = new UserLibraryClasspathContainer(userLibName);
36
				JavaCore.setClasspathContainer(containerPath, new IJavaProject[] { project }, 	new IClasspathContainer[] { container }, null);
56
				JavaCore.setClasspathContainer(containerPath, new IJavaProject[] { project }, new IClasspathContainer[] { container }, null);
37
			} else if (JavaModelManager.CP_RESOLVE_VERBOSE) {
57
			} else if (JavaModelManager.CP_RESOLVE_VERBOSE) {
38
				verbose_no_user_library_found(project, userLibName);
58
				verbose_no_user_library_found(project, userLibName);
39
			}
59
			}
Lines 46-88 Link Here
46
		return path != null && path.segmentCount() == 2 && JavaCore.USER_LIBRARY_CONTAINER_ID.equals(path.segment(0));
66
		return path != null && path.segmentCount() == 2 && JavaCore.USER_LIBRARY_CONTAINER_ID.equals(path.segment(0));
47
	}
67
	}
48
	
68
	
49
	/* (non-Javadoc)
50
	 * @see org.eclipse.jdt.core.ClasspathContainerInitializer#canUpdateClasspathContainer(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
51
	 */
52
	public boolean canUpdateClasspathContainer(IPath containerPath, IJavaProject project) {
53
		return isUserLibraryContainer(containerPath);
54
	}
55
56
	/**
69
	/**
57
	 * @see org.eclipse.jdt.core.ClasspathContainerInitializer#requestClasspathContainerUpdate(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject, org.eclipse.jdt.core.IClasspathContainer)
70
	 * @see org.eclipse.jdt.core.ClasspathContainerInitializer#requestClasspathContainerUpdate(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject, org.eclipse.jdt.core.IClasspathContainer)
58
	 */
71
	 */
59
	public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project, IClasspathContainer containerSuggestion) throws CoreException {
72
	public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project, IClasspathContainer containerSuggestion) throws CoreException {
60
		if (isUserLibraryContainer(containerPath)) {
73
		if (isUserLibraryContainer(containerPath)) {
61
			String name= containerPath.segment(1);
74
			String name = containerPath.segment(1);
62
			if (containerSuggestion != null) {
75
			if (containerSuggestion != null) {
63
				UserLibrary library= new UserLibrary(containerSuggestion.getClasspathEntries(), containerSuggestion.getKind() == IClasspathContainer.K_SYSTEM);
76
				JavaModelManager.getUserLibraryManager().setUserLibrary(name, containerSuggestion.getClasspathEntries(), containerSuggestion.getKind() == IClasspathContainer.K_SYSTEM);
64
				UserLibraryManager.setUserLibrary(name, library, null); // should use a real progress monitor
65
			} else {
77
			} else {
66
				UserLibraryManager.setUserLibrary(name, null, null); // should use a real progress monitor
78
				JavaModelManager.getUserLibraryManager().removeUserLibrary(name);
67
			}
79
			}
80
			// update of affected projects was done as a consequence of putUserLibrary()
68
		}
81
		}
69
	}
82
	}
70
83
71
	/**
84
	private void verbose_no_user_library_found(IJavaProject project, String userLibraryName) {
72
	 * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getDescription(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
85
		Util.verbose(
73
	 */
86
			"UserLibrary INIT - FAILED (no user library found)\n" + //$NON-NLS-1$
74
	public String getDescription(IPath containerPath, IJavaProject project) {
87
			"	project: " + project.getElementName() + '\n' + //$NON-NLS-1$
75
		if (isUserLibraryContainer(containerPath)) {
88
			"	userLibraryName: " + userLibraryName); //$NON-NLS-1$
76
			return containerPath.segment(1);
77
		}
78
		return super.getDescription(containerPath, project);
79
	}
80
81
	/* (non-Javadoc)
82
	 * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getComparisonID(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
83
	 */
84
	public Object getComparisonID(IPath containerPath, IJavaProject project) {
85
		return containerPath;
86
	}
89
	}
87
	
90
	
88
	private void verbose_not_a_user_library(IJavaProject project, IPath containerPath) {
91
	private void verbose_not_a_user_library(IJavaProject project, IPath containerPath) {
Lines 91-102 Link Here
91
			"	project: " + project.getElementName() + '\n' + //$NON-NLS-1$
94
			"	project: " + project.getElementName() + '\n' + //$NON-NLS-1$
92
			"	container path: " + containerPath); //$NON-NLS-1$
95
			"	container path: " + containerPath); //$NON-NLS-1$
93
	}
96
	}
94
95
	private void verbose_no_user_library_found(IJavaProject project, String userLibraryName) {
96
		Util.verbose(
97
			"UserLibrary INIT - FAILED (no user library found)\n" + //$NON-NLS-1$
98
			"	project: " + project.getElementName() + '\n' + //$NON-NLS-1$
99
			"	userLibraryName: " + userLibraryName); //$NON-NLS-1$
100
	}
101
	
102
}
97
}
(-)model/org/eclipse/jdt/internal/core/UserLibrary.java (-5 / +5 lines)
Lines 98-115 Link Here
98
		return hashCode;
98
		return hashCode;
99
	}
99
	}
100
	
100
	
101
	/* package */  String serialize() throws IOException {
101
	public static String serialize(IClasspathEntry[] entries, boolean isSystemLibrary) throws IOException {
102
		ByteArrayOutputStream s = new ByteArrayOutputStream();
102
		ByteArrayOutputStream s = new ByteArrayOutputStream();
103
		OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8"); //$NON-NLS-1$
103
		OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8"); //$NON-NLS-1$
104
		XMLWriter xmlWriter = new XMLWriter(writer, null/*use the workspace line delimiter*/, true/*print XML version*/);
104
		XMLWriter xmlWriter = new XMLWriter(writer, null/*use the workspace line delimiter*/, true/*print XML version*/);
105
		
105
		
106
		HashMap library = new HashMap();
106
		HashMap library = new HashMap();
107
		library.put(TAG_VERSION, String.valueOf(CURRENT_VERSION));
107
		library.put(TAG_VERSION, String.valueOf(CURRENT_VERSION));
108
		library.put(TAG_SYSTEMLIBRARY, String.valueOf(this.isSystemLibrary));
108
		library.put(TAG_SYSTEMLIBRARY, String.valueOf(isSystemLibrary));
109
		xmlWriter.printTag(TAG_USERLIBRARY, library, true, true, false);
109
		xmlWriter.printTag(TAG_USERLIBRARY, library, true, true, false);
110
		
110
		
111
		for (int i = 0; i < this.entries.length; ++i) {
111
		for (int i = 0, length = entries.length; i < length; ++i) {
112
			ClasspathEntry cpEntry = (ClasspathEntry) this.entries[i];
112
			ClasspathEntry cpEntry = (ClasspathEntry) entries[i];
113
		
113
		
114
			HashMap archive = new HashMap();
114
			HashMap archive = new HashMap();
115
			archive.put(TAG_PATH, cpEntry.getPath().toString());
115
			archive.put(TAG_PATH, cpEntry.getPath().toString());
Lines 145-151 Link Here
145
		return s.toString("UTF8");//$NON-NLS-1$
145
		return s.toString("UTF8");//$NON-NLS-1$
146
	}
146
	}
147
	
147
	
148
	/* package */ static UserLibrary createFromString(Reader reader) throws IOException {
148
	public static UserLibrary createFromString(Reader reader) throws IOException {
149
		Element cpElement;
149
		Element cpElement;
150
		try {
150
		try {
151
			DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
151
			DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
(-)model/org/eclipse/jdt/internal/core/UserLibraryClasspathContainer.java (-7 / +17 lines)
Lines 15-20 Link Here
15
import org.eclipse.jdt.core.IClasspathContainer;
15
import org.eclipse.jdt.core.IClasspathContainer;
16
import org.eclipse.jdt.core.IClasspathEntry;
16
import org.eclipse.jdt.core.IClasspathEntry;
17
import org.eclipse.jdt.core.JavaCore;
17
import org.eclipse.jdt.core.JavaCore;
18
import org.eclipse.jdt.internal.core.util.Util;
18
19
19
/**
20
/**
20
 *
21
 *
Lines 23-36 Link Here
23
	
24
	
24
	private String name;
25
	private String name;
25
	
26
	
26
	public UserLibraryClasspathContainer(String libName) {
27
	public UserLibraryClasspathContainer(String name) {
27
		this.name= libName;
28
		this.name = name;
28
	}
29
	}
29
	
30
	
30
	private UserLibrary getUserLibrary() {
31
		return UserLibraryManager.getUserLibrary(this.name);
32
	}
33
34
	/* (non-Javadoc)
31
	/* (non-Javadoc)
35
	 * @see org.eclipse.jdt.core.IClasspathContainer#getClasspathEntries()
32
	 * @see org.eclipse.jdt.core.IClasspathContainer#getClasspathEntries()
36
	 */
33
	 */
Lines 40-46 Link Here
40
			return library.getEntries();
37
			return library.getEntries();
41
		}
38
		}
42
		return new IClasspathEntry[0];
39
		return new IClasspathEntry[0];
43
		
44
	}
40
	}
45
41
46
	/* (non-Javadoc)
42
	/* (non-Javadoc)
Lines 67-70 Link Here
67
	public IPath getPath() {
63
	public IPath getPath() {
68
		return new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(this.name);
64
		return new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(this.name);
69
	}
65
	}
66
	
67
	private UserLibrary getUserLibrary() {
68
		UserLibrary userLibrary = JavaModelManager.getUserLibraryManager().getUserLibrary(this.name);
69
		if (userLibrary == null && JavaModelManager.CP_RESOLVE_VERBOSE) {
70
			verbose_no_user_library_found(this.name);
71
		}
72
		return userLibrary;
73
	}
74
75
	private void verbose_no_user_library_found(String userLibraryName) {
76
		Util.verbose(
77
			"UserLibrary INIT - FAILED (no user library found)\n" + //$NON-NLS-1$
78
			"	userLibraryName: " + userLibraryName); //$NON-NLS-1$
79
	}
70
}
80
}
(-)model/org/eclipse/jdt/internal/core/JavaModelManager.java (+17 lines)
Lines 1213-1218 Link Here
1213
	 */
1213
	 */
1214
	private ThreadLocal zipFiles = new ThreadLocal();
1214
	private ThreadLocal zipFiles = new ThreadLocal();
1215
	
1215
	
1216
	private UserLibraryManager userLibraryManager;
1216
	
1217
	
1217
	/**
1218
	/**
1218
	 * Update the classpath variable cache
1219
	 * Update the classpath variable cache
Lines 1952-1957 Link Here
1952
		return workingLocation.append("state.dat").toFile(); //$NON-NLS-1$
1953
		return workingLocation.append("state.dat").toFile(); //$NON-NLS-1$
1953
	}
1954
	}
1954
	
1955
	
1956
	public static UserLibraryManager getUserLibraryManager() {
1957
		JavaModelManager modelManager = getJavaModelManager();
1958
		if (modelManager.userLibraryManager == null) {
1959
			UserLibraryManager libraryManager = new UserLibraryManager();
1960
			synchronized(modelManager) {
1961
				modelManager.userLibraryManager = libraryManager;
1962
				modelManager.getInstancePreferences().addPreferenceChangeListener(libraryManager);
1963
			}
1964
		}
1965
		return modelManager.userLibraryManager;
1966
	}
1967
	
1955
	/*
1968
	/*
1956
	 * Returns all the working copies which have the given owner.
1969
	 * Returns all the working copies which have the given owner.
1957
	 * Adds the working copies of the primary owner if specified.
1970
	 * Adds the working copies of the primary owner if specified.
Lines 4203-4208 Link Here
4203
		
4216
		
4204
		// Stop listening to content-type changes
4217
		// Stop listening to content-type changes
4205
		Platform.getContentTypeManager().removeContentTypeChangeListener(this);
4218
		Platform.getContentTypeManager().removeContentTypeChangeListener(this);
4219
		
4220
		// Stop listening to user library changes
4221
		if (this.userLibraryManager != null)
4222
			getInstancePreferences().removePreferenceChangeListener(this.userLibraryManager);
4206
	
4223
	
4207
		if (this.indexManager != null){ // no more indexing
4224
		if (this.indexManager != null){ // no more indexing
4208
			this.indexManager.shutdown();
4225
			this.indexManager.shutdown();
(-)model/org/eclipse/jdt/internal/core/UserLibraryManager.java (-208 / +120 lines)
Lines 16-29 Link Here
16
import java.util.HashMap;
16
import java.util.HashMap;
17
import java.util.Map;
17
import java.util.Map;
18
import java.util.Set;
18
import java.util.Set;
19
import org.eclipse.core.resources.IWorkspaceRoot;
20
import org.eclipse.core.resources.ResourcesPlugin;
19
import org.eclipse.core.resources.ResourcesPlugin;
21
import org.eclipse.core.runtime.Assert;
22
import org.eclipse.core.runtime.IPath;
20
import org.eclipse.core.runtime.IPath;
23
import org.eclipse.core.runtime.IProgressMonitor;
24
import org.eclipse.core.runtime.NullProgressMonitor;
25
import org.eclipse.core.runtime.Path;
21
import org.eclipse.core.runtime.Path;
26
import org.eclipse.core.runtime.SubProgressMonitor;
27
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
22
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
28
import org.eclipse.jdt.core.IClasspathContainer;
23
import org.eclipse.jdt.core.IClasspathContainer;
29
import org.eclipse.jdt.core.IClasspathEntry;
24
import org.eclipse.jdt.core.IClasspathEntry;
Lines 36-275 Link Here
36
/**
31
/**
37
 *
32
 *
38
 */
33
 */
39
public class UserLibraryManager {
34
public class UserLibraryManager implements IEclipsePreferences.IPreferenceChangeListener {
40
	
35
	
41
	public final static String CP_USERLIBRARY_PREFERENCES_PREFIX = JavaCore.PLUGIN_ID+".userLibrary."; //$NON-NLS-1$
36
	public final static String CP_USERLIBRARY_PREFERENCES_PREFIX = JavaCore.PLUGIN_ID+".userLibrary."; //$NON-NLS-1$
42
	public final static String CP_ENTRY_IGNORE = "##<cp entry ignore>##"; //$NON-NLS-1$
43
37
44
	private static Map UserLibraries;
38
	private Map userLibraries;
45
	private static ThreadLocal InitializingLibraries = new ThreadLocal();
46
	private static final boolean logProblems= false;
47
	private static IEclipsePreferences.IPreferenceChangeListener listener= new IEclipsePreferences.IPreferenceChangeListener() {
48
39
49
		public void preferenceChange(IEclipsePreferences.PreferenceChangeEvent event) {
40
	public UserLibraryManager() {
50
			String key= event.getKey();
41
		initialize();
51
			if (key.startsWith(CP_USERLIBRARY_PREFERENCES_PREFIX)) {
52
				try {
53
					recreatePersistedUserLibraryEntry(key, (String) event.getNewValue(), false, true);
54
				} catch (JavaModelException e) {
55
					if (logProblems) {
56
						Util.log(e, "Exception while rebinding user library '"+ key.substring(CP_USERLIBRARY_PREFERENCES_PREFIX.length()) +"'."); //$NON-NLS-1$ //$NON-NLS-2$
57
					}
58
					
59
				}
60
			}
61
		}
62
	};
63
	
64
	private UserLibraryManager() {
65
		// do not instantiate
66
	}
42
	}
67
		
43
		
68
	/**
44
	/*
69
	 * Returns the names of all defined user libraries. The corresponding classpath container path
70
	 * is the name appended to the CONTAINER_ID.  
71
	 * @return Return an array containing the names of all known user defined.
72
	 */
73
	public static String[] getUserLibraryNames() {
74
		Set set= getLibraryMap().keySet();
75
		return (String[]) set.toArray(new String[set.size()]);
76
	}
77
	
78
	/**
79
	 * Gets the library for a given name or <code>null</code> if no such library exists.
45
	 * Gets the library for a given name or <code>null</code> if no such library exists.
80
	 * @param name The name of the library
81
	 * @return The library registered for the given name or <code>null</code>.
82
	 */
83
	public static UserLibrary getUserLibrary(String name) {
84
		return (UserLibrary) getLibraryMap().get(name);
85
	}
86
87
	/**
88
	 * Registers user libraries for given names. If a library for the given name already exists, its value will be updated.
89
	 * This call will also rebind all related classpath container. 
90
	 * @param newNames The names to register the libraries for
91
	 * @param newLibs The libraries to register
92
	 * @param monitor A progress monitor used when rebinding the classpath containers
93
	 * @throws JavaModelException
94
	 */
46
	 */
95
	public static void setUserLibraries(String[] newNames, UserLibrary[] newLibs, IProgressMonitor monitor) throws JavaModelException {
47
	public synchronized UserLibrary getUserLibrary(String libName) {
96
		Assert.isTrue(newNames.length == newLibs.length, "names and libraries should have the same length"); //$NON-NLS-1$
48
		return (UserLibrary) this.userLibraries.get(libName);
97
		
98
		if (monitor == null) {
99
			monitor= new NullProgressMonitor();
100
		}
101
		
102
		try {
103
			monitor.beginTask("", newNames.length);	//$NON-NLS-1$
104
			int last= newNames.length - 1;
105
			for (int i= 0; i < newLibs.length; i++) {
106
				internalSetUserLibrary(newNames[i], newLibs[i], i == last, true, new SubProgressMonitor(monitor, 1));
107
			}
108
		} finally {
109
			monitor.done();
110
		}
111
	}
49
	}
112
	
50
	
113
	/**
51
	/*
114
	 * Registers a user library for a given name. If a library for the given name already exists, its value will be updated.
52
	 * Returns the names of all defined user libraries. The corresponding classpath container path
115
	 * This call will also rebind all related classpath container. 
53
	 * is the name appended to the CONTAINER_ID.  
116
	 * @param name The name to register the library for
117
	 * @param library The library to register
118
	 * @param monitor A progress monitor used when rebinding the classpath containers
119
	 * @throws JavaModelException
120
	 */
54
	 */
121
	public static void setUserLibrary(String name, UserLibrary library, IProgressMonitor monitor) throws JavaModelException {
55
	public synchronized String[] getUserLibraryNames() {
122
		internalSetUserLibrary(name, library, true, true, monitor);
56
		Set set = this.userLibraries.keySet();
57
		return (String[]) set.toArray(new String[set.size()]);
123
	}
58
	}
124
	
59
	
125
	static Map getLibraryMap() {
60
	private void initialize() {
126
		if (UserLibraries == null) {
61
		this.userLibraries = new HashMap();
127
			HashMap libraries = (HashMap) InitializingLibraries.get();
62
		IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences();
128
			if (libraries != null)
63
		String[] propertyNames;
129
				return libraries;
64
		try {
130
			try {
65
			propertyNames = instancePreferences.keys();
131
				InitializingLibraries.set(libraries = new HashMap());
66
		} catch (BackingStoreException e) {
132
				// load variables and containers from preferences into cache
67
			Util.log(e, "Exception while initializing user libraries"); //$NON-NLS-1$
133
				IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences();
68
			return;
134
				instancePreferences.addPreferenceChangeListener(listener);
69
		}
135
	
70
136
				// only get variable from preferences not set to their default
71
		boolean preferencesNeedFlush = false;
137
				try {
72
		for (int i = 0, length = propertyNames.length; i < length; i++) {
138
					String[] propertyNames = instancePreferences.keys();
73
			String propertyName = propertyNames[i];
139
					for (int i = 0; i < propertyNames.length; i++) {
74
			if (propertyName.startsWith(CP_USERLIBRARY_PREFERENCES_PREFIX)) {
140
						String propertyName = propertyNames[i];
75
				String propertyValue = instancePreferences.get(propertyName, null);
141
						if (propertyName.startsWith(CP_USERLIBRARY_PREFERENCES_PREFIX)) {
76
				if (propertyValue != null) {
142
							try {
77
					String libName= propertyName.substring(CP_USERLIBRARY_PREFERENCES_PREFIX.length());
143
								String propertyValue = instancePreferences.get(propertyName, null);
78
					StringReader reader = new StringReader(propertyValue);
144
								if (propertyValue != null)
79
					UserLibrary library;
145
									recreatePersistedUserLibraryEntry(propertyName,propertyValue, false, false);
80
					try {
146
							} catch (JavaModelException e) {
81
						library = UserLibrary.createFromString(reader);
147
								// won't happen: no rebinding
82
					} catch (IOException e) {
148
							}
83
						Util.log(e, "Exception while initializing user library " + libName); //$NON-NLS-1$
149
						}
84
						instancePreferences.remove(propertyName);
85
						preferencesNeedFlush = true;
86
						continue;
150
					}
87
					}
151
				} catch (BackingStoreException e) {
88
					this.userLibraries.put(libName, library);
152
					// nothing to do in this case
153
				}
89
				}
154
				UserLibraries = libraries;
155
			} finally {
156
				InitializingLibraries.set(null);
157
			}
90
			}
158
		}
91
		}
159
		return UserLibraries;
92
		if (preferencesNeedFlush) {
160
	}
161
	
162
	static void recreatePersistedUserLibraryEntry(String propertyName, String savedString, boolean save, boolean rebind) throws JavaModelException {
163
		String libName= propertyName.substring(CP_USERLIBRARY_PREFERENCES_PREFIX.length());
164
		if (savedString == null || savedString.equals(CP_ENTRY_IGNORE)) {
165
			internalSetUserLibrary(libName, null, save, rebind, null);
166
		} else {
167
			try {
93
			try {
168
				StringReader reader = new StringReader(savedString);
94
				instancePreferences.flush();
169
				UserLibrary library= UserLibrary.createFromString(reader);
95
			} catch (BackingStoreException e) {
170
				internalSetUserLibrary(libName, library, save, rebind, null);
96
				Util.log(e, "Exception while flusing instance preferences"); //$NON-NLS-1$
171
			} catch (IOException e) {
172
				if (logProblems) {
173
					Util.log(e, "Exception while retrieving user library '"+ propertyName +"', library will be removed."); //$NON-NLS-1$ //$NON-NLS-2$
174
				}
175
				internalSetUserLibrary(libName, null, save, rebind, null);
176
			}
97
			}
177
		}
98
		}
178
	}
99
	}
179
100
180
101
	public void preferenceChange(IEclipsePreferences.PreferenceChangeEvent event) {
181
102
		String key = event.getKey();
182
	static void internalSetUserLibrary(String name, UserLibrary library, boolean save, boolean rebind, IProgressMonitor monitor) throws JavaModelException {
103
		if (key.startsWith(CP_USERLIBRARY_PREFERENCES_PREFIX)) {
183
		if (library == null) {
104
			String libName = key.substring(CP_USERLIBRARY_PREFERENCES_PREFIX.length());
184
			Object previous= getLibraryMap().remove(name);
185
			if (previous == null) {
186
				return; // no change
187
			}
188
		} else {
189
			Object previous= getLibraryMap().put(name, library);
190
			if (library.equals(previous)) {
191
				return; // no change
192
			}
193
		}
194
		
195
		IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences();
196
		String containerKey = CP_USERLIBRARY_PREFERENCES_PREFIX+name;
197
		String containerString = CP_ENTRY_IGNORE;
198
		if (library != null) {
199
			try {
105
			try {
200
				containerString= library.serialize();
106
				// find affected projects
107
				IPath containerPath = new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(libName);
108
				IJavaProject[] allJavaProjects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
109
				ArrayList affectedProjects = new ArrayList();
110
				for (int i= 0; i < allJavaProjects.length; i++) {
111
					IJavaProject javaProject = allJavaProjects[i];
112
					IClasspathEntry[] entries= javaProject.getRawClasspath();
113
					for (int j= 0; j < entries.length; j++) {
114
						IClasspathEntry entry = entries[j];
115
						if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
116
							if (containerPath.equals(entry.getPath())) {
117
								affectedProjects.add(javaProject);
118
								break;
119
							}				
120
						}
121
					}
122
				}
123
				
124
				// decode user library
125
				String encodedUserLibrary = (String) event.getNewValue();
126
				UserLibrary userLibrary = encodedUserLibrary == null ? null : UserLibrary.createFromString(new StringReader(encodedUserLibrary));
127
				
128
				// update user libraries map
129
				if (userLibrary != null) {
130
					this.userLibraries.put(libName, userLibrary);
131
				} else {
132
					this.userLibraries.remove(libName);
133
				}
134
				
135
				// update affected projects
136
				int length = affectedProjects.size();
137
				if (length == 0)
138
					return;
139
				IJavaProject[] projects = new IJavaProject[length];
140
				affectedProjects.toArray(projects);
141
				IClasspathContainer[] containers = new IClasspathContainer[length];
142
				if (userLibrary != null) {
143
					UserLibraryClasspathContainer container = new UserLibraryClasspathContainer(libName);
144
					for (int i = 0; i < length; i++) {
145
						containers[i] = container;
146
					}
147
				}
148
				JavaCore.setClasspathContainer(containerPath, projects, containers, null);
201
			} catch (IOException e) {
149
			} catch (IOException e) {
202
				// could not encode entry: leave it as CP_ENTRY_IGNORE
150
				Util.log(e, "Exception while decoding user library '"+ libName +"'."); //$NON-NLS-1$ //$NON-NLS-2$
151
			} catch (JavaModelException e) {
152
				Util.log(e, "Exception while setting user library '"+ libName +"'."); //$NON-NLS-1$ //$NON-NLS-2$
203
			}
153
			}
204
		}
154
		}
205
		instancePreferences.removePreferenceChangeListener(listener);
155
	}
156
	
157
	public synchronized void removeUserLibrary(String libName)  {
158
		IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences();
159
		String propertyName = CP_USERLIBRARY_PREFERENCES_PREFIX+libName;
160
		instancePreferences.remove(propertyName);
206
		try {
161
		try {
207
			instancePreferences.put(containerKey, containerString);
162
			instancePreferences.flush();
208
			if (save) {
163
		} catch (BackingStoreException e) {
209
				try {
164
			Util.log(e, "Exception while removing user library " + libName); //$NON-NLS-1$
210
					instancePreferences.flush();
211
				} catch (BackingStoreException e) {
212
					// nothing to do in this case
213
				}
214
			}
215
			if (rebind) {
216
				rebindClasspathEntries(name, library==null, monitor);
217
			}
218
			
219
		} finally {
220
			instancePreferences.addPreferenceChangeListener(listener);
221
		}
165
		}
166
		// this.userLibraries was updated during the PreferenceChangeEvent (see preferenceChange(...))
222
	}
167
	}
223
168
	
224
	private static void rebindClasspathEntries(String name, boolean remove, IProgressMonitor monitor) throws JavaModelException {
169
	public synchronized void setUserLibrary(String libName, IClasspathEntry[] entries, boolean isSystemLibrary)  {
170
		IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences();
171
		String propertyName = CP_USERLIBRARY_PREFERENCES_PREFIX+libName;
225
		try {
172
		try {
226
			if (monitor != null) {
173
			String propertyValue = UserLibrary.serialize(entries, isSystemLibrary);
227
				monitor.beginTask("", 1); //$NON-NLS-1$
174
			instancePreferences.put(propertyName, propertyValue); // sends out a PreferenceChangeEvent (see preferenceChange(...))
228
			}
175
		} catch (IOException e) {
229
			IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
176
			Util.log(e, "Exception while serializing user library " + libName); //$NON-NLS-1$
230
			IJavaProject[] projects= JavaCore.create(root).getJavaProjects();
177
			return;
231
			IPath containerPath= new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(name);
232
			
233
			ArrayList affectedProjects= new ArrayList();
234
			
235
			for (int i= 0; i < projects.length; i++) {
236
				IJavaProject project= projects[i];
237
				IClasspathEntry[] entries= project.getRawClasspath();
238
				for (int k= 0; k < entries.length; k++) {
239
					IClasspathEntry curr= entries[k];
240
					if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
241
						if (containerPath.equals(curr.getPath())) {
242
							affectedProjects.add(project);
243
							break;
244
						}				
245
					}
246
				}
247
			}
248
			if (!affectedProjects.isEmpty()) {
249
				IJavaProject[] affected= (IJavaProject[]) affectedProjects.toArray(new IJavaProject[affectedProjects.size()]);
250
				IClasspathContainer[] containers= new IClasspathContainer[affected.length];
251
				if (!remove) {
252
					// Previously, containers array only contained a null value. Then, user library classpath entry was first removed
253
					// and then added a while after when post change delta event on .classpath file was fired...
254
					// Unfortunately, in some cases, this event was fired a little bit too late and missed the refresh of Package Explorer
255
					// (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=61872)
256
					// So now, instanciate a new user library classpath container instead which allow to refresh its content immediately
257
					// as there's no classpath entry removal...
258
					// Note that it works because equals(Object) method is not overridden for UserLibraryClasspathContainer.
259
					// If it was, the update wouldn't happen while setting classpath container
260
					// @see javaCore.setClasspathContainer(IPath, IJavaProject[], IClasspathContainer[], IProgressMonitor)
261
					UserLibraryClasspathContainer container= new UserLibraryClasspathContainer(name);
262
					containers[0] = container;
263
				}
264
				JavaCore.setClasspathContainer(containerPath, affected, containers, monitor == null ? null : new SubProgressMonitor(monitor, 1));
265
			} 
266
		} finally {
267
			if (monitor != null) {
268
				monitor.done();
269
			}
270
		}
178
		}
179
		try {
180
			instancePreferences.flush();
181
		} catch (BackingStoreException e) {
182
			Util.log(e, "Exception while saving user library " + libName); //$NON-NLS-1$
183
		}
184
		// this.userLibraries was updated during the PreferenceChangeEvent (see preferenceChange(...))
271
	}
185
	}
272
273
274
	
186
	
275
}
187
}
(-)model/org/eclipse/jdt/core/JavaCore.java (-1 / +1 lines)
Lines 3219-3225 Link Here
3219
	 * @since 3.0
3219
	 * @since 3.0
3220
	 */
3220
	 */
3221
	public static String[] getUserLibraryNames() {
3221
	public static String[] getUserLibraryNames() {
3222
		 return UserLibraryManager.getUserLibraryNames();
3222
		 return JavaModelManager.getUserLibraryManager().getUserLibraryNames();
3223
	}
3223
	}
3224
3224
3225
	/**
3225
	/**

Return to bug 183117