KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > JREPreferenceModifyListener


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.launching;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import javax.xml.parsers.ParserConfigurationException JavaDoc;
23 import javax.xml.transform.TransformerException JavaDoc;
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 /**
35  * Manages import of installed JREs. Merges valid imported JREs with existing JREs.
36  *
37  * @since 3.1
38  */

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 JavaDoc jresXML = node.get(JavaRuntime.PREF_VM_XML, null);
46                 if (jresXML != null) {
47                     VMDefinitionsContainer vms = new VMDefinitionsContainer();
48                     String JavaDoc pref = LaunchingPlugin.getDefault().getPluginPreferences().getString(JavaRuntime.PREF_VM_XML);
49                     // names -> existing vm's
50
Map JavaDoc names = new HashMap JavaDoc();
51                     Set JavaDoc ids = new HashSet JavaDoc();
52                     if (pref.length() > 0) {
53                         try {
54                             VMDefinitionsContainer container = VMDefinitionsContainer.parseXMLIntoContainer(new ByteArrayInputStream JavaDoc(pref.getBytes("UTF8"))); //$NON-NLS-1$
55
List JavaDoc validVMList = container.getValidVMList();
56                             Iterator JavaDoc 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 JavaDoc e) {
66                             LaunchingPlugin.log(e);
67                             return false;
68                         }
69                     }
70                     // merge valid VMs with existing VMs
71
try {
72                         ByteArrayInputStream JavaDoc inputStream = new ByteArrayInputStream JavaDoc(jresXML.getBytes("UTF8")); //$NON-NLS-1$
73
VMDefinitionsContainer container = VMDefinitionsContainer.parseXMLIntoContainer(inputStream);
74                         List JavaDoc validVMList = container.getValidVMList();
75                         Iterator JavaDoc 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                                 // vm with same name already exists - replace with imported VM
81
vms.removeVM(existing);
82                                 ids.remove(existing.getId());
83                             }
84                             boolean collision = ids.contains(vm.getId());
85                             if (collision) {
86                                 // conflicting id, create a new one with unique id
87
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                         // update default VM if it exists
97
String JavaDoc 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 JavaDoc e) {
108                         LaunchingPlugin.log(e);
109                         return false;
110                     }
111                     try {
112                         String JavaDoc xml = vms.getAsXML();
113                         node.put(JavaRuntime.PREF_VM_XML, xml);
114                     } catch (ParserConfigurationException JavaDoc e) {
115                         LaunchingPlugin.log(e);
116                         return false;
117                     } catch (IOException JavaDoc e) {
118                         LaunchingPlugin.log(e);
119                         return false;
120                     } catch (TransformerException JavaDoc 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             // force VMs to be initialized before we import the new VMs
135
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