### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core.tests.model Index: src/org/eclipse/jdt/core/tests/model/JavaProjectTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaProjectTests.java,v retrieving revision 1.86 diff -u -r1.86 JavaProjectTests.java --- src/org/eclipse/jdt/core/tests/model/JavaProjectTests.java 20 Aug 2007 14:59:45 -0000 1.86 +++ src/org/eclipse/jdt/core/tests/model/JavaProjectTests.java 23 Aug 2007 14:18:18 -0000 @@ -26,7 +26,6 @@ import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; import org.eclipse.jdt.internal.core.JavaModelManager; import org.eclipse.jdt.internal.core.PackageFragmentRoot; -import org.eclipse.jdt.internal.core.UserLibrary; import org.eclipse.jdt.internal.core.UserLibraryManager; import org.eclipse.jdt.internal.core.util.Util; @@ -1523,8 +1522,7 @@ userEntries[1] = JavaCore.newLibraryEntry(path, null, null, pathRules, extraAttributes, false); // Create user library - UserLibrary library = new UserLibrary(userEntries, false); - UserLibraryManager.setUserLibrary("TEST", library, null); + JavaModelManager.getUserLibraryManager().setUserLibrary("TEST", userEntries, false); // Verify it has been written in preferences IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences(); #P org.eclipse.jdt.core Index: model/org/eclipse/jdt/internal/core/UserLibraryClasspathContainerInitializer.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/UserLibraryClasspathContainerInitializer.java,v retrieving revision 1.7 diff -u -r1.7 UserLibraryClasspathContainerInitializer.java --- model/org/eclipse/jdt/internal/core/UserLibraryClasspathContainerInitializer.java 17 Aug 2007 11:23:07 -0000 1.7 +++ model/org/eclipse/jdt/internal/core/UserLibraryClasspathContainerInitializer.java 23 Aug 2007 14:18:21 -0000 @@ -24,16 +24,36 @@ public class UserLibraryClasspathContainerInitializer extends ClasspathContainerInitializer { /* (non-Javadoc) - * @see org.eclipse.jdt.core.ClasspathContainerInitializer#initialize(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject) + * @see org.eclipse.jdt.core.ClasspathContainerInitializer#canUpdateClasspathContainer(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject) + */ + public boolean canUpdateClasspathContainer(IPath containerPath, IJavaProject project) { + return isUserLibraryContainer(containerPath); + } + + /* (non-Javadoc) + * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getComparisonID(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject) + */ + public Object getComparisonID(IPath containerPath, IJavaProject project) { + return containerPath; + } + + /** + * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getDescription(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject) */ + public String getDescription(IPath containerPath, IJavaProject project) { + if (isUserLibraryContainer(containerPath)) { + return containerPath.segment(1); + } + return super.getDescription(containerPath, project); + } + public void initialize(IPath containerPath, IJavaProject project) throws CoreException { if (isUserLibraryContainer(containerPath)) { String userLibName = containerPath.segment(1); - - UserLibrary entries = UserLibraryManager.getUserLibrary(userLibName); - if (entries != null) { + UserLibrary userLibrary = JavaModelManager.getUserLibraryManager().getUserLibrary(userLibName); + if (userLibrary != null) { UserLibraryClasspathContainer container = new UserLibraryClasspathContainer(userLibName); - JavaCore.setClasspathContainer(containerPath, new IJavaProject[] { project }, new IClasspathContainer[] { container }, null); + JavaCore.setClasspathContainer(containerPath, new IJavaProject[] { project }, new IClasspathContainer[] { container }, null); } else if (JavaModelManager.CP_RESOLVE_VERBOSE) { verbose_no_user_library_found(project, userLibName); } @@ -46,43 +66,26 @@ return path != null && path.segmentCount() == 2 && JavaCore.USER_LIBRARY_CONTAINER_ID.equals(path.segment(0)); } - /* (non-Javadoc) - * @see org.eclipse.jdt.core.ClasspathContainerInitializer#canUpdateClasspathContainer(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject) - */ - public boolean canUpdateClasspathContainer(IPath containerPath, IJavaProject project) { - return isUserLibraryContainer(containerPath); - } - /** * @see org.eclipse.jdt.core.ClasspathContainerInitializer#requestClasspathContainerUpdate(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject, org.eclipse.jdt.core.IClasspathContainer) */ public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project, IClasspathContainer containerSuggestion) throws CoreException { if (isUserLibraryContainer(containerPath)) { - String name= containerPath.segment(1); + String name = containerPath.segment(1); if (containerSuggestion != null) { - UserLibrary library= new UserLibrary(containerSuggestion.getClasspathEntries(), containerSuggestion.getKind() == IClasspathContainer.K_SYSTEM); - UserLibraryManager.setUserLibrary(name, library, null); // should use a real progress monitor + JavaModelManager.getUserLibraryManager().setUserLibrary(name, containerSuggestion.getClasspathEntries(), containerSuggestion.getKind() == IClasspathContainer.K_SYSTEM); } else { - UserLibraryManager.setUserLibrary(name, null, null); // should use a real progress monitor + JavaModelManager.getUserLibraryManager().removeUserLibrary(name); } + // update of affected projects was done as a consequence of setUserLibrary() or removeUserLibrary() } } - /** - * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getDescription(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject) - */ - public String getDescription(IPath containerPath, IJavaProject project) { - if (isUserLibraryContainer(containerPath)) { - return containerPath.segment(1); - } - return super.getDescription(containerPath, project); - } - - /* (non-Javadoc) - * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getComparisonID(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject) - */ - public Object getComparisonID(IPath containerPath, IJavaProject project) { - return containerPath; + private void verbose_no_user_library_found(IJavaProject project, String userLibraryName) { + Util.verbose( + "UserLibrary INIT - FAILED (no user library found)\n" + //$NON-NLS-1$ + " project: " + project.getElementName() + '\n' + //$NON-NLS-1$ + " userLibraryName: " + userLibraryName); //$NON-NLS-1$ } private void verbose_not_a_user_library(IJavaProject project, IPath containerPath) { @@ -91,12 +94,4 @@ " project: " + project.getElementName() + '\n' + //$NON-NLS-1$ " container path: " + containerPath); //$NON-NLS-1$ } - - private void verbose_no_user_library_found(IJavaProject project, String userLibraryName) { - Util.verbose( - "UserLibrary INIT - FAILED (no user library found)\n" + //$NON-NLS-1$ - " project: " + project.getElementName() + '\n' + //$NON-NLS-1$ - " userLibraryName: " + userLibraryName); //$NON-NLS-1$ - } - } Index: model/org/eclipse/jdt/internal/core/UserLibrary.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/UserLibrary.java,v retrieving revision 1.11 diff -u -r1.11 UserLibrary.java --- model/org/eclipse/jdt/internal/core/UserLibrary.java 10 May 2006 18:03:48 -0000 1.11 +++ model/org/eclipse/jdt/internal/core/UserLibrary.java 23 Aug 2007 14:18:21 -0000 @@ -98,18 +98,18 @@ return hashCode; } - /* package */ String serialize() throws IOException { + public static String serialize(IClasspathEntry[] entries, boolean isSystemLibrary) throws IOException { ByteArrayOutputStream s = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8"); //$NON-NLS-1$ XMLWriter xmlWriter = new XMLWriter(writer, null/*use the workspace line delimiter*/, true/*print XML version*/); HashMap library = new HashMap(); library.put(TAG_VERSION, String.valueOf(CURRENT_VERSION)); - library.put(TAG_SYSTEMLIBRARY, String.valueOf(this.isSystemLibrary)); + library.put(TAG_SYSTEMLIBRARY, String.valueOf(isSystemLibrary)); xmlWriter.printTag(TAG_USERLIBRARY, library, true, true, false); - for (int i = 0; i < this.entries.length; ++i) { - ClasspathEntry cpEntry = (ClasspathEntry) this.entries[i]; + for (int i = 0, length = entries.length; i < length; ++i) { + ClasspathEntry cpEntry = (ClasspathEntry) entries[i]; HashMap archive = new HashMap(); archive.put(TAG_PATH, cpEntry.getPath().toString()); @@ -145,7 +145,7 @@ return s.toString("UTF8");//$NON-NLS-1$ } - /* package */ static UserLibrary createFromString(Reader reader) throws IOException { + public static UserLibrary createFromString(Reader reader) throws IOException { Element cpElement; try { DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Index: model/org/eclipse/jdt/internal/core/UserLibraryClasspathContainer.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/UserLibraryClasspathContainer.java,v retrieving revision 1.5 diff -u -r1.5 UserLibraryClasspathContainer.java --- model/org/eclipse/jdt/internal/core/UserLibraryClasspathContainer.java 10 May 2006 18:03:46 -0000 1.5 +++ model/org/eclipse/jdt/internal/core/UserLibraryClasspathContainer.java 23 Aug 2007 14:18:21 -0000 @@ -15,6 +15,7 @@ import org.eclipse.jdt.core.IClasspathContainer; import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.internal.core.util.Util; /** * @@ -23,14 +24,10 @@ private String name; - public UserLibraryClasspathContainer(String libName) { - this.name= libName; + public UserLibraryClasspathContainer(String name) { + this.name = name; } - private UserLibrary getUserLibrary() { - return UserLibraryManager.getUserLibrary(this.name); - } - /* (non-Javadoc) * @see org.eclipse.jdt.core.IClasspathContainer#getClasspathEntries() */ @@ -40,7 +37,6 @@ return library.getEntries(); } return new IClasspathEntry[0]; - } /* (non-Javadoc) @@ -67,4 +63,18 @@ public IPath getPath() { return new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(this.name); } + + private UserLibrary getUserLibrary() { + UserLibrary userLibrary = JavaModelManager.getUserLibraryManager().getUserLibrary(this.name); + if (userLibrary == null && JavaModelManager.CP_RESOLVE_VERBOSE) { + verbose_no_user_library_found(this.name); + } + return userLibrary; + } + + private void verbose_no_user_library_found(String userLibraryName) { + Util.verbose( + "UserLibrary INIT - FAILED (no user library found)\n" + //$NON-NLS-1$ + " userLibraryName: " + userLibraryName); //$NON-NLS-1$ + } } Index: model/org/eclipse/jdt/internal/core/JavaModelManager.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaModelManager.java,v retrieving revision 1.374 diff -u -r1.374 JavaModelManager.java --- model/org/eclipse/jdt/internal/core/JavaModelManager.java 18 Aug 2007 18:02:32 -0000 1.374 +++ model/org/eclipse/jdt/internal/core/JavaModelManager.java 23 Aug 2007 14:18:21 -0000 @@ -1213,6 +1213,7 @@ */ private ThreadLocal zipFiles = new ThreadLocal(); + private UserLibraryManager userLibraryManager; /** * Update the classpath variable cache @@ -1952,6 +1953,20 @@ return workingLocation.append("state.dat").toFile(); //$NON-NLS-1$ } + public static UserLibraryManager getUserLibraryManager() { + JavaModelManager modelManager = getJavaModelManager(); + if (modelManager.userLibraryManager == null) { + UserLibraryManager libraryManager = new UserLibraryManager(); + synchronized(modelManager) { + if (modelManager.userLibraryManager == null) { // ensure another library manager was not set while creating the instance above + modelManager.userLibraryManager = libraryManager; + modelManager.getInstancePreferences().addPreferenceChangeListener(libraryManager); + } + } + } + return modelManager.userLibraryManager; + } + /* * Returns all the working copies which have the given owner. * Adds the working copies of the primary owner if specified. @@ -4203,6 +4218,10 @@ // Stop listening to content-type changes Platform.getContentTypeManager().removeContentTypeChangeListener(this); + + // Stop listening to user library changes + if (this.userLibraryManager != null) + getInstancePreferences().removePreferenceChangeListener(this.userLibraryManager); if (this.indexManager != null){ // no more indexing this.indexManager.shutdown(); Index: model/org/eclipse/jdt/internal/core/UserLibraryManager.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/UserLibraryManager.java,v retrieving revision 1.12 diff -u -r1.12 UserLibraryManager.java --- model/org/eclipse/jdt/internal/core/UserLibraryManager.java 19 Mar 2007 18:34:03 -0000 1.12 +++ model/org/eclipse/jdt/internal/core/UserLibraryManager.java 23 Aug 2007 14:18:21 -0000 @@ -16,14 +16,9 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; -import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Path; -import org.eclipse.core.runtime.SubProgressMonitor; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.jdt.core.IClasspathContainer; import org.eclipse.jdt.core.IClasspathEntry; @@ -36,240 +31,157 @@ /** * */ -public class UserLibraryManager { +public class UserLibraryManager implements IEclipsePreferences.IPreferenceChangeListener { public final static String CP_USERLIBRARY_PREFERENCES_PREFIX = JavaCore.PLUGIN_ID+".userLibrary."; //$NON-NLS-1$ - public final static String CP_ENTRY_IGNORE = "####"; //$NON-NLS-1$ - private static Map UserLibraries; - private static ThreadLocal InitializingLibraries = new ThreadLocal(); - private static final boolean logProblems= false; - private static IEclipsePreferences.IPreferenceChangeListener listener= new IEclipsePreferences.IPreferenceChangeListener() { + private Map userLibraries; - public void preferenceChange(IEclipsePreferences.PreferenceChangeEvent event) { - String key= event.getKey(); - if (key.startsWith(CP_USERLIBRARY_PREFERENCES_PREFIX)) { - try { - recreatePersistedUserLibraryEntry(key, (String) event.getNewValue(), false, true); - } catch (JavaModelException e) { - if (logProblems) { - Util.log(e, "Exception while rebinding user library '"+ key.substring(CP_USERLIBRARY_PREFERENCES_PREFIX.length()) +"'."); //$NON-NLS-1$ //$NON-NLS-2$ - } - - } - } - } - }; - - private UserLibraryManager() { - // do not instantiate + public UserLibraryManager() { + initialize(); } - /** - * Returns the names of all defined user libraries. The corresponding classpath container path - * is the name appended to the CONTAINER_ID. - * @return Return an array containing the names of all known user defined. - */ - public static String[] getUserLibraryNames() { - Set set= getLibraryMap().keySet(); - return (String[]) set.toArray(new String[set.size()]); - } - - /** + /* * Gets the library for a given name or null if no such library exists. - * @param name The name of the library - * @return The library registered for the given name or null. - */ - public static UserLibrary getUserLibrary(String name) { - return (UserLibrary) getLibraryMap().get(name); - } - - /** - * Registers user libraries for given names. If a library for the given name already exists, its value will be updated. - * This call will also rebind all related classpath container. - * @param newNames The names to register the libraries for - * @param newLibs The libraries to register - * @param monitor A progress monitor used when rebinding the classpath containers - * @throws JavaModelException */ - public static void setUserLibraries(String[] newNames, UserLibrary[] newLibs, IProgressMonitor monitor) throws JavaModelException { - Assert.isTrue(newNames.length == newLibs.length, "names and libraries should have the same length"); //$NON-NLS-1$ - - if (monitor == null) { - monitor= new NullProgressMonitor(); - } - - try { - monitor.beginTask("", newNames.length); //$NON-NLS-1$ - int last= newNames.length - 1; - for (int i= 0; i < newLibs.length; i++) { - internalSetUserLibrary(newNames[i], newLibs[i], i == last, true, new SubProgressMonitor(monitor, 1)); - } - } finally { - monitor.done(); - } + public synchronized UserLibrary getUserLibrary(String libName) { + return (UserLibrary) this.userLibraries.get(libName); } - /** - * Registers a user library for a given name. If a library for the given name already exists, its value will be updated. - * This call will also rebind all related classpath container. - * @param name The name to register the library for - * @param library The library to register - * @param monitor A progress monitor used when rebinding the classpath containers - * @throws JavaModelException + /* + * Returns the names of all defined user libraries. The corresponding classpath container path + * is the name appended to the CONTAINER_ID. */ - public static void setUserLibrary(String name, UserLibrary library, IProgressMonitor monitor) throws JavaModelException { - internalSetUserLibrary(name, library, true, true, monitor); + public synchronized String[] getUserLibraryNames() { + Set set = this.userLibraries.keySet(); + return (String[]) set.toArray(new String[set.size()]); } - static Map getLibraryMap() { - if (UserLibraries == null) { - HashMap libraries = (HashMap) InitializingLibraries.get(); - if (libraries != null) - return libraries; - try { - InitializingLibraries.set(libraries = new HashMap()); - // load variables and containers from preferences into cache - IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences(); - instancePreferences.addPreferenceChangeListener(listener); - - // only get variable from preferences not set to their default - try { - String[] propertyNames = instancePreferences.keys(); - for (int i = 0; i < propertyNames.length; i++) { - String propertyName = propertyNames[i]; - if (propertyName.startsWith(CP_USERLIBRARY_PREFERENCES_PREFIX)) { - try { - String propertyValue = instancePreferences.get(propertyName, null); - if (propertyValue != null) - recreatePersistedUserLibraryEntry(propertyName,propertyValue, false, false); - } catch (JavaModelException e) { - // won't happen: no rebinding - } - } + private void initialize() { + this.userLibraries = new HashMap(); + IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences(); + String[] propertyNames; + try { + propertyNames = instancePreferences.keys(); + } catch (BackingStoreException e) { + Util.log(e, "Exception while initializing user libraries"); //$NON-NLS-1$ + return; + } + + boolean preferencesNeedFlush = false; + for (int i = 0, length = propertyNames.length; i < length; i++) { + String propertyName = propertyNames[i]; + if (propertyName.startsWith(CP_USERLIBRARY_PREFERENCES_PREFIX)) { + String propertyValue = instancePreferences.get(propertyName, null); + if (propertyValue != null) { + String libName= propertyName.substring(CP_USERLIBRARY_PREFERENCES_PREFIX.length()); + StringReader reader = new StringReader(propertyValue); + UserLibrary library; + try { + library = UserLibrary.createFromString(reader); + } catch (IOException e) { + Util.log(e, "Exception while initializing user library " + libName); //$NON-NLS-1$ + instancePreferences.remove(propertyName); + preferencesNeedFlush = true; + continue; } - } catch (BackingStoreException e) { - // nothing to do in this case + this.userLibraries.put(libName, library); } - UserLibraries = libraries; - } finally { - InitializingLibraries.set(null); } } - return UserLibraries; - } - - static void recreatePersistedUserLibraryEntry(String propertyName, String savedString, boolean save, boolean rebind) throws JavaModelException { - String libName= propertyName.substring(CP_USERLIBRARY_PREFERENCES_PREFIX.length()); - if (savedString == null || savedString.equals(CP_ENTRY_IGNORE)) { - internalSetUserLibrary(libName, null, save, rebind, null); - } else { + if (preferencesNeedFlush) { try { - StringReader reader = new StringReader(savedString); - UserLibrary library= UserLibrary.createFromString(reader); - internalSetUserLibrary(libName, library, save, rebind, null); - } catch (IOException e) { - if (logProblems) { - Util.log(e, "Exception while retrieving user library '"+ propertyName +"', library will be removed."); //$NON-NLS-1$ //$NON-NLS-2$ - } - internalSetUserLibrary(libName, null, save, rebind, null); + instancePreferences.flush(); + } catch (BackingStoreException e) { + Util.log(e, "Exception while flusing instance preferences"); //$NON-NLS-1$ } } } - - - static void internalSetUserLibrary(String name, UserLibrary library, boolean save, boolean rebind, IProgressMonitor monitor) throws JavaModelException { - if (library == null) { - Object previous= getLibraryMap().remove(name); - if (previous == null) { - return; // no change - } - } else { - Object previous= getLibraryMap().put(name, library); - if (library.equals(previous)) { - return; // no change - } - } - - IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences(); - String containerKey = CP_USERLIBRARY_PREFERENCES_PREFIX+name; - String containerString = CP_ENTRY_IGNORE; - if (library != null) { + public void preferenceChange(IEclipsePreferences.PreferenceChangeEvent event) { + String key = event.getKey(); + if (key.startsWith(CP_USERLIBRARY_PREFERENCES_PREFIX)) { + String libName = key.substring(CP_USERLIBRARY_PREFERENCES_PREFIX.length()); try { - containerString= library.serialize(); + // find affected projects + IPath containerPath = new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(libName); + IJavaProject[] allJavaProjects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects(); + ArrayList affectedProjects = new ArrayList(); + for (int i= 0; i < allJavaProjects.length; i++) { + IJavaProject javaProject = allJavaProjects[i]; + IClasspathEntry[] entries= javaProject.getRawClasspath(); + for (int j= 0; j < entries.length; j++) { + IClasspathEntry entry = entries[j]; + if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { + if (containerPath.equals(entry.getPath())) { + affectedProjects.add(javaProject); + break; + } + } + } + } + + // decode user library + String encodedUserLibrary = (String) event.getNewValue(); + UserLibrary userLibrary = encodedUserLibrary == null ? null : UserLibrary.createFromString(new StringReader(encodedUserLibrary)); + + // update user libraries map + if (userLibrary != null) { + this.userLibraries.put(libName, userLibrary); + } else { + this.userLibraries.remove(libName); + } + + // update affected projects + int length = affectedProjects.size(); + if (length == 0) + return; + IJavaProject[] projects = new IJavaProject[length]; + affectedProjects.toArray(projects); + IClasspathContainer[] containers = new IClasspathContainer[length]; + if (userLibrary != null) { + UserLibraryClasspathContainer container = new UserLibraryClasspathContainer(libName); + for (int i = 0; i < length; i++) { + containers[i] = container; + } + } + JavaCore.setClasspathContainer(containerPath, projects, containers, null); } catch (IOException e) { - // could not encode entry: leave it as CP_ENTRY_IGNORE + Util.log(e, "Exception while decoding user library '"+ libName +"'."); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (JavaModelException e) { + Util.log(e, "Exception while setting user library '"+ libName +"'."); //$NON-NLS-1$ //$NON-NLS-2$ } } - instancePreferences.removePreferenceChangeListener(listener); + } + + public synchronized void removeUserLibrary(String libName) { + IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences(); + String propertyName = CP_USERLIBRARY_PREFERENCES_PREFIX+libName; + instancePreferences.remove(propertyName); try { - instancePreferences.put(containerKey, containerString); - if (save) { - try { - instancePreferences.flush(); - } catch (BackingStoreException e) { - // nothing to do in this case - } - } - if (rebind) { - rebindClasspathEntries(name, library==null, monitor); - } - - } finally { - instancePreferences.addPreferenceChangeListener(listener); + instancePreferences.flush(); + } catch (BackingStoreException e) { + Util.log(e, "Exception while removing user library " + libName); //$NON-NLS-1$ } + // this.userLibraries was updated during the PreferenceChangeEvent (see preferenceChange(...)) } - - private static void rebindClasspathEntries(String name, boolean remove, IProgressMonitor monitor) throws JavaModelException { + + public synchronized void setUserLibrary(String libName, IClasspathEntry[] entries, boolean isSystemLibrary) { + IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences(); + String propertyName = CP_USERLIBRARY_PREFERENCES_PREFIX+libName; try { - if (monitor != null) { - monitor.beginTask("", 1); //$NON-NLS-1$ - } - IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); - IJavaProject[] projects= JavaCore.create(root).getJavaProjects(); - IPath containerPath= new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(name); - - ArrayList affectedProjects= new ArrayList(); - - for (int i= 0; i < projects.length; i++) { - IJavaProject project= projects[i]; - IClasspathEntry[] entries= project.getRawClasspath(); - for (int k= 0; k < entries.length; k++) { - IClasspathEntry curr= entries[k]; - if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { - if (containerPath.equals(curr.getPath())) { - affectedProjects.add(project); - break; - } - } - } - } - if (!affectedProjects.isEmpty()) { - IJavaProject[] affected= (IJavaProject[]) affectedProjects.toArray(new IJavaProject[affectedProjects.size()]); - IClasspathContainer[] containers= new IClasspathContainer[affected.length]; - if (!remove) { - // Previously, containers array only contained a null value. Then, user library classpath entry was first removed - // and then added a while after when post change delta event on .classpath file was fired... - // Unfortunately, in some cases, this event was fired a little bit too late and missed the refresh of Package Explorer - // (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=61872) - // So now, instanciate a new user library classpath container instead which allow to refresh its content immediately - // as there's no classpath entry removal... - // Note that it works because equals(Object) method is not overridden for UserLibraryClasspathContainer. - // If it was, the update wouldn't happen while setting classpath container - // @see javaCore.setClasspathContainer(IPath, IJavaProject[], IClasspathContainer[], IProgressMonitor) - UserLibraryClasspathContainer container= new UserLibraryClasspathContainer(name); - containers[0] = container; - } - JavaCore.setClasspathContainer(containerPath, affected, containers, monitor == null ? null : new SubProgressMonitor(monitor, 1)); - } - } finally { - if (monitor != null) { - monitor.done(); - } + String propertyValue = UserLibrary.serialize(entries, isSystemLibrary); + instancePreferences.put(propertyName, propertyValue); // sends out a PreferenceChangeEvent (see preferenceChange(...)) + } catch (IOException e) { + Util.log(e, "Exception while serializing user library " + libName); //$NON-NLS-1$ + return; } + try { + instancePreferences.flush(); + } catch (BackingStoreException e) { + Util.log(e, "Exception while saving user library " + libName); //$NON-NLS-1$ + } + // this.userLibraries was updated during the PreferenceChangeEvent (see preferenceChange(...)) } - - } Index: model/org/eclipse/jdt/core/JavaCore.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java,v retrieving revision 1.578 diff -u -r1.578 JavaCore.java --- model/org/eclipse/jdt/core/JavaCore.java 18 Aug 2007 18:02:59 -0000 1.578 +++ model/org/eclipse/jdt/core/JavaCore.java 23 Aug 2007 14:18:20 -0000 @@ -3219,7 +3219,7 @@ * @since 3.0 */ public static String[] getUserLibraryNames() { - return UserLibraryManager.getUserLibraryNames(); + return JavaModelManager.getUserLibraryManager().getUserLibraryNames(); } /**