1 11 package org.eclipse.jdt.internal.launching.environments; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.NullProgressMonitor; 24 import org.eclipse.core.runtime.Path; 25 import org.eclipse.jdt.core.IAccessRule; 26 import org.eclipse.jdt.core.IClasspathContainer; 27 import org.eclipse.jdt.core.IClasspathEntry; 28 import org.eclipse.jdt.core.IJavaModel; 29 import org.eclipse.jdt.core.IJavaProject; 30 import org.eclipse.jdt.core.JavaCore; 31 import org.eclipse.jdt.core.JavaModelException; 32 import org.eclipse.jdt.internal.launching.LaunchingPlugin; 33 import org.eclipse.jdt.launching.IVMInstall; 34 import org.eclipse.jdt.launching.IVMInstallChangedListener; 35 import org.eclipse.jdt.launching.JavaRuntime; 36 import org.eclipse.jdt.launching.LibraryLocation; 37 import org.eclipse.jdt.launching.PropertyChangeEvent; 38 import org.eclipse.jdt.launching.environments.IAccessRuleParticipant; 39 import org.eclipse.jdt.launching.environments.IExecutionEnvironment; 40 41 import com.ibm.icu.text.MessageFormat; 42 43 48 class ExecutionEnvironment implements IExecutionEnvironment { 49 50 53 private IVMInstallChangedListener fListener = new IVMInstallChangedListener() { 54 55 58 public void defaultVMInstallChanged(IVMInstall previous, IVMInstall current) {} 59 60 63 public void vmAdded(IVMInstall newVm) {} 64 65 68 public void vmChanged(PropertyChangeEvent event) { 69 if (event.getSource() != null) { 70 fParticipantMap.remove(event.getSource()); 71 fRuleCache.remove(event.getSource()); 72 } 73 } 74 75 78 public void vmRemoved(IVMInstall removedVm) { 79 fParticipantMap.remove(removedVm); 80 fRuleCache.remove(removedVm); 81 } 82 }; 83 84 85 88 private IConfigurationElement fElement; 89 90 93 private IAccessRuleParticipant fRuleParticipant; 94 95 98 private Set fStrictlyCompatible = new HashSet (); 99 100 103 private List fCompatibleVMs = new ArrayList (); 104 105 108 private IVMInstall fDefault = null; 109 110 113 private IAccessRuleParticipant[] fParticipants = null; 114 115 120 private Map fParticipantMap = new HashMap (); 121 122 128 private Map fRuleCache = new HashMap (); 129 130 133 private static final IPath ALL_PATTERN = new Path("**/*"); 135 139 ExecutionEnvironment(IConfigurationElement element) { 140 fElement = element; 141 String attribute = fElement.getAttribute(EnvironmentsManager.RULE_PARTICIPANT_ELEMENT); 142 if (attribute != null) { 143 fRuleParticipant = new AccessRuleParticipant(fElement); 144 } 145 JavaRuntime.addVMInstallChangedListener(fListener); 146 } 147 148 151 private void init() { 152 EnvironmentsManager.getDefault().initializeCompatibilities(); 153 } 154 155 158 public String getId() { 159 return fElement.getAttribute("id"); } 161 162 165 public String getDescription() { 166 return fElement.getAttribute("description"); } 168 169 172 public IVMInstall[] getCompatibleVMs() { 173 init(); 174 return (IVMInstall[]) fCompatibleVMs.toArray(new IVMInstall[fCompatibleVMs.size()]); 175 } 176 177 180 public boolean isStrictlyCompatible(IVMInstall vm) { 181 init(); 182 return fStrictlyCompatible.contains(vm); 183 } 184 185 188 public IVMInstall getDefaultVM() { 189 init(); 190 return fDefault; 191 } 192 193 196 public void setDefaultVM(IVMInstall vm) { 197 init(); 198 if (vm != null && !fCompatibleVMs.contains(vm)) { 199 throw new IllegalArgumentException (MessageFormat.format(EnvironmentMessages.EnvironmentsManager_0, new String []{getId()})); 200 } 201 if (vm != null && vm.equals(fDefault)) { 202 return; 203 } 204 fDefault = vm; 205 EnvironmentsManager.getDefault().updateDefaultVMs(); 206 rebindClasspathContainers(); 208 } 209 210 213 private void rebindClasspathContainers() { 214 IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 215 if (model != null) { 216 try { 217 List updates = new ArrayList (); 218 IJavaProject[] javaProjects = model.getJavaProjects(); 219 IPath path = JavaRuntime.newJREContainerPath(this); 220 for (int i = 0; i < javaProjects.length; i++) { 221 IJavaProject project = javaProjects[i]; 222 IClasspathEntry[] rawClasspath = project.getRawClasspath(); 223 for (int j = 0; j < rawClasspath.length; j++) { 224 IClasspathEntry entry = rawClasspath[j]; 225 if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { 226 if (entry.getPath().equals(path)) { 227 updates.add(project); 228 } 229 } 230 } 231 } 232 if (!updates.isEmpty()) { 233 JavaCore.setClasspathContainer(path, 234 (IJavaProject[]) updates.toArray(new IJavaProject[updates.size()]), 235 new IClasspathContainer[updates.size()], 236 new NullProgressMonitor()); 237 } 238 } catch (JavaModelException e) { 239 LaunchingPlugin.log(e); 240 } 241 } 242 } 243 244 251 void add(IVMInstall vm, boolean strictlyCompatible) { 252 if (fCompatibleVMs.contains(vm)) { 253 return; 254 } 255 fCompatibleVMs.add(vm); 256 if (strictlyCompatible) { 257 fStrictlyCompatible.add(vm); 258 } 259 } 260 261 265 void remove(IVMInstall vm) { 266 fCompatibleVMs.remove(vm); 267 fStrictlyCompatible.remove(vm); 268 } 269 270 274 void initDefaultVM(IVMInstall vm) { 275 fDefault = vm; 276 } 277 278 281 public IAccessRule[][] getAccessRules(IVMInstall vm, LibraryLocation[] libraries, IJavaProject project) { 282 IAccessRuleParticipant[] participants = getParticipants(); 283 Map rulesByParticipant = collectRulesByParticipant(participants, vm, libraries, project); 284 synchronized (this) { 285 Map cachedRules = (Map ) fParticipantMap.get(vm); 286 if (cachedRules == null || !cachedRules.equals(rulesByParticipant)) { 287 List [] libLists = new List [libraries.length]; for (int i = 0; i < libLists.length; i++) { 289 libLists[i] = new ArrayList (); 290 } 291 for (int i = 0; i < participants.length; i++) { 292 IAccessRuleParticipant participant = participants[i]; 293 addRules((IAccessRule[][]) rulesByParticipant.get(participant), libLists); 294 } 295 IAccessRule[][] allRules = new IAccessRule[libraries.length][]; 296 for (int i = 0; i < libLists.length; i++) { 297 allRules[i] = (IAccessRule[]) libLists[i].toArray(new IAccessRule[libLists[i].size()]); 298 } 299 fParticipantMap.put(vm, rulesByParticipant); 300 fRuleCache.put(vm, allRules); 301 return allRules; 302 } else { 303 return (IAccessRule[][]) fRuleCache.get(vm); 304 } 305 } 306 } 307 308 315 private synchronized IAccessRuleParticipant[] getParticipants() { 316 if (fParticipants == null) { 317 IAccessRuleParticipant[] participants = EnvironmentsManager.getDefault().getAccessRuleParticipants(); 319 if (fRuleParticipant != null) { 320 IAccessRuleParticipant[] copy = new IAccessRuleParticipant[participants.length + 1]; 322 System.arraycopy(participants, 0, copy, 0, participants.length); 323 copy[participants.length] = fRuleParticipant; 324 participants = copy; 325 } 326 fParticipants = participants; 327 } 328 return fParticipants; 329 } 330 331 338 private Map collectRulesByParticipant(IAccessRuleParticipant[] participants, IVMInstall vm, LibraryLocation[] libraries, IJavaProject project) { 339 Map map = new HashMap (); 340 for (int i = 0; i < participants.length; i++) { 341 map.put(participants[i], participants[i].getAccessRules(this, vm, libraries, project)); 343 } 344 return map; 345 } 346 347 351 private void addRules(IAccessRule[][] accessRules, List [] collect) { 352 for (int i = 0; i < accessRules.length; i++) { 353 IAccessRule[] libRules = accessRules[i]; 354 List list = collect[i]; 355 if (!list.isEmpty()) { 357 IAccessRule lastRule = (IAccessRule) list.get(list.size() - 1); 358 if(lastRule.getPattern().equals(ALL_PATTERN)) { 359 continue; 360 } 361 } 362 for (int j = 0; j < libRules.length; j++) { 363 list.add(libRules[j]); 364 } 365 } 366 } 367 368 } 369 | Popular Tags |