1 11 package org.eclipse.jdt.internal.core; 12 13 import java.io.IOException ; 14 import java.io.StringReader ; 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 import java.util.Set ; 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 23 import org.eclipse.jdt.core.IClasspathContainer; 24 import org.eclipse.jdt.core.IClasspathEntry; 25 import org.eclipse.jdt.core.IJavaProject; 26 import org.eclipse.jdt.core.JavaCore; 27 import org.eclipse.jdt.core.JavaModelException; 28 import org.eclipse.jdt.internal.core.util.Util; 29 import org.osgi.service.prefs.BackingStoreException; 30 31 34 public class UserLibraryManager implements IEclipsePreferences.IPreferenceChangeListener { 35 36 public final static String CP_USERLIBRARY_PREFERENCES_PREFIX = JavaCore.PLUGIN_ID+".userLibrary."; 38 private Map userLibraries; 39 40 public UserLibraryManager() { 41 initialize(); 42 } 43 44 47 public synchronized UserLibrary getUserLibrary(String libName) { 48 return (UserLibrary) this.userLibraries.get(libName); 49 } 50 51 55 public synchronized String [] getUserLibraryNames() { 56 Set set = this.userLibraries.keySet(); 57 return (String []) set.toArray(new String [set.size()]); 58 } 59 60 private void initialize() { 61 this.userLibraries = new HashMap (); 62 IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences(); 63 String [] propertyNames; 64 try { 65 propertyNames = instancePreferences.keys(); 66 } catch (BackingStoreException e) { 67 Util.log(e, "Exception while initializing user libraries"); return; 69 } 70 71 boolean preferencesNeedFlush = false; 72 for (int i = 0, length = propertyNames.length; i < length; i++) { 73 String propertyName = propertyNames[i]; 74 if (propertyName.startsWith(CP_USERLIBRARY_PREFERENCES_PREFIX)) { 75 String propertyValue = instancePreferences.get(propertyName, null); 76 if (propertyValue != null) { 77 String libName= propertyName.substring(CP_USERLIBRARY_PREFERENCES_PREFIX.length()); 78 StringReader reader = new StringReader (propertyValue); 79 UserLibrary library; 80 try { 81 library = UserLibrary.createFromString(reader); 82 } catch (IOException e) { 83 Util.log(e, "Exception while initializing user library " + libName); instancePreferences.remove(propertyName); 85 preferencesNeedFlush = true; 86 continue; 87 } 88 this.userLibraries.put(libName, library); 89 } 90 } 91 } 92 if (preferencesNeedFlush) { 93 try { 94 instancePreferences.flush(); 95 } catch (BackingStoreException e) { 96 Util.log(e, "Exception while flusing instance preferences"); } 98 } 99 } 100 101 public void preferenceChange(IEclipsePreferences.PreferenceChangeEvent event) { 102 String key = event.getKey(); 103 if (key.startsWith(CP_USERLIBRARY_PREFERENCES_PREFIX)) { 104 String libName = key.substring(CP_USERLIBRARY_PREFERENCES_PREFIX.length()); 105 try { 106 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 String encodedUserLibrary = (String ) event.getNewValue(); 126 UserLibrary userLibrary = encodedUserLibrary == null ? null : UserLibrary.createFromString(new StringReader (encodedUserLibrary)); 127 128 if (userLibrary != null) { 130 this.userLibraries.put(libName, userLibrary); 131 } else { 132 this.userLibraries.remove(libName); 133 } 134 135 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); 149 } catch (IOException e) { 150 Util.log(e, "Exception while decoding user library '"+ libName +"'."); } catch (JavaModelException e) { 152 Util.log(e, "Exception while setting user library '"+ libName +"'."); } 154 } 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); 161 try { 162 instancePreferences.flush(); 163 } catch (BackingStoreException e) { 164 Util.log(e, "Exception while removing user library " + libName); } 166 } 168 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; 172 try { 173 String propertyValue = UserLibrary.serialize(entries, isSystemLibrary); 174 instancePreferences.put(propertyName, propertyValue); } catch (IOException e) { 176 Util.log(e, "Exception while serializing user library " + libName); return; 178 } 179 try { 180 instancePreferences.flush(); 181 } catch (BackingStoreException e) { 182 Util.log(e, "Exception while saving user library " + libName); } 184 } 186 187 } 188 | Popular Tags |