1 11 package org.eclipse.jdt.internal.launching; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.IOException ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import javax.xml.parsers.ParserConfigurationException ; 23 import javax.xml.transform.TransformerException ; 24 25 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 26 import org.eclipse.core.runtime.preferences.IPreferenceNodeVisitor; 27 import org.eclipse.core.runtime.preferences.PreferenceModifyListener; 28 import org.eclipse.jdt.launching.IVMInstall; 29 import org.eclipse.jdt.launching.IVMInstallType; 30 import org.eclipse.jdt.launching.JavaRuntime; 31 import org.eclipse.jdt.launching.VMStandin; 32 import org.osgi.service.prefs.BackingStoreException; 33 34 39 public class JREPreferenceModifyListener extends PreferenceModifyListener { 40 41 class Visitor implements IPreferenceNodeVisitor { 42 43 public boolean visit(IEclipsePreferences node) throws BackingStoreException { 44 if (node.name().equals(LaunchingPlugin.getUniqueIdentifier())) { 45 String jresXML = node.get(JavaRuntime.PREF_VM_XML, null); 46 if (jresXML != null) { 47 VMDefinitionsContainer vms = new VMDefinitionsContainer(); 48 String pref = LaunchingPlugin.getDefault().getPluginPreferences().getString(JavaRuntime.PREF_VM_XML); 49 Map names = new HashMap (); 51 Set ids = new HashSet (); 52 if (pref.length() > 0) { 53 try { 54 VMDefinitionsContainer container = VMDefinitionsContainer.parseXMLIntoContainer(new ByteArrayInputStream (pref.getBytes("UTF8"))); List validVMList = container.getValidVMList(); 56 Iterator iterator = validVMList.iterator(); 57 while (iterator.hasNext()) { 58 IVMInstall vm = (IVMInstall) iterator.next(); 59 names.put(vm.getName(), vm); 60 ids.add(vm.getId()); 61 vms.addVM(vm); 62 } 63 vms.setDefaultVMInstallCompositeID(container.getDefaultVMInstallCompositeID()); 64 vms.setDefaultVMInstallConnectorTypeID(container.getDefaultVMInstallConnectorTypeID()); 65 } catch (IOException e) { 66 LaunchingPlugin.log(e); 67 return false; 68 } 69 } 70 try { 72 ByteArrayInputStream inputStream = new ByteArrayInputStream (jresXML.getBytes("UTF8")); VMDefinitionsContainer container = VMDefinitionsContainer.parseXMLIntoContainer(inputStream); 74 List validVMList = container.getValidVMList(); 75 Iterator iterator = validVMList.iterator(); 76 while (iterator.hasNext()) { 77 IVMInstall vm = (IVMInstall) iterator.next(); 78 IVMInstall existing = (IVMInstall) names.get(vm.getName()); 79 if (existing != null) { 80 vms.removeVM(existing); 82 ids.remove(existing.getId()); 83 } 84 boolean collision = ids.contains(vm.getId()); 85 if (collision) { 86 long unique = System.currentTimeMillis(); 88 IVMInstallType vmType = vm.getVMInstallType(); 89 while (vmType.findVMInstall(String.valueOf(unique)) != null) { 90 unique++; 91 } 92 vm = new VMStandin(vm, String.valueOf(unique)); 93 } 94 vms.addVM(vm); 95 } 96 String defaultVMInstallCompositeID = container.getDefaultVMInstallCompositeID(); 98 validVMList = vms.getValidVMList(); 99 iterator = validVMList.iterator(); 100 while (iterator.hasNext()) { 101 IVMInstall vm = (IVMInstall)iterator.next(); 102 if (JavaRuntime.getCompositeIdFromVM(vm).equals(defaultVMInstallCompositeID)) { 103 vms.setDefaultVMInstallCompositeID(defaultVMInstallCompositeID); 104 break; 105 } 106 } 107 } catch (IOException e) { 108 LaunchingPlugin.log(e); 109 return false; 110 } 111 try { 112 String xml = vms.getAsXML(); 113 node.put(JavaRuntime.PREF_VM_XML, xml); 114 } catch (ParserConfigurationException e) { 115 LaunchingPlugin.log(e); 116 return false; 117 } catch (IOException e) { 118 LaunchingPlugin.log(e); 119 return false; 120 } catch (TransformerException e) { 121 LaunchingPlugin.log(e); 122 return false; 123 } 124 } 125 return false; 126 } 127 return true; 128 } 129 130 } 131 132 public IEclipsePreferences preApply(IEclipsePreferences node) { 133 try { 134 JavaRuntime.getVMInstallTypes(); 136 node.accept(new Visitor()); 137 } catch (BackingStoreException e) { 138 LaunchingPlugin.log(e); 139 } 140 return node; 141 } 142 143 144 } 145 | Popular Tags |