KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > LaunchValidationOperation


1 /*******************************************************************************
2  * Copyright (c) 2007 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.pde.internal.ui.launcher;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Dictionary JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.util.zip.ZipEntry JavaDoc;
23 import java.util.zip.ZipFile JavaDoc;
24
25 import org.eclipse.core.resources.IWorkspaceRunnable;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.debug.core.ILaunchConfiguration;
29 import org.eclipse.jdt.launching.IVMInstall;
30 import org.eclipse.jdt.launching.JavaRuntime;
31 import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
32 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
33 import org.eclipse.osgi.service.resolver.State;
34 import org.eclipse.pde.core.plugin.IPluginModelBase;
35 import org.eclipse.pde.core.plugin.PluginRegistry;
36 import org.eclipse.pde.internal.core.BundleValidationOperation;
37 import org.eclipse.pde.internal.core.TargetPlatformHelper;
38 import org.osgi.framework.Constants;
39
40
41 public abstract class LaunchValidationOperation implements IWorkspaceRunnable {
42     
43
44     private BundleValidationOperation fOperation;
45     protected ILaunchConfiguration fLaunchConfiguration;
46
47     public LaunchValidationOperation(ILaunchConfiguration configuration) {
48         fLaunchConfiguration = configuration;
49     }
50     
51     public void run(IProgressMonitor monitor) throws CoreException {
52         fOperation = new BundleValidationOperation(getModels(), getPlatformProperties());
53         fOperation.run(monitor);
54     }
55         
56     protected abstract IPluginModelBase[] getModels() throws CoreException;
57     
58     protected Dictionary JavaDoc[] getPlatformProperties() throws CoreException {
59         IExecutionEnvironment[] envs = getMatchingEnvironments();
60         if (envs.length == 0)
61             return new Dictionary JavaDoc[] {TargetPlatformHelper.getTargetEnvironment()};
62         
63         // add java profiles for those EE's that have a .profile file in the current system bundle
64
ArrayList JavaDoc result = new ArrayList JavaDoc(envs.length);
65         for (int i = 0; i < envs.length; i++) {
66             Properties JavaDoc profileProps = getJavaProfileProperties(envs[i].getId());
67             if (profileProps != null) {
68                 Dictionary JavaDoc props = TargetPlatformHelper.getTargetEnvironment();
69                 String JavaDoc systemPackages = profileProps.getProperty(Constants.FRAMEWORK_SYSTEMPACKAGES);
70                 if (systemPackages != null)
71                     props.put(Constants.FRAMEWORK_SYSTEMPACKAGES, systemPackages);
72                 String JavaDoc ee = profileProps.getProperty(Constants.FRAMEWORK_EXECUTIONENVIRONMENT);
73                 if (ee != null)
74                     props.put(Constants.FRAMEWORK_EXECUTIONENVIRONMENT, ee);
75                 result.add(props);
76             }
77         }
78         if (result.size() > 0)
79             return (Dictionary JavaDoc[])result.toArray(new Dictionary JavaDoc[result.size()]);
80         return new Dictionary JavaDoc[] {TargetPlatformHelper.getTargetEnvironment()};
81
82     }
83     
84     private IExecutionEnvironment[] getMatchingEnvironments() throws CoreException {
85         IVMInstall install = VMHelper.getVMInstall(fLaunchConfiguration);
86         if (install == null)
87             return new IExecutionEnvironment[0];
88         
89         IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
90         IExecutionEnvironment[] envs = manager.getExecutionEnvironments();
91         List JavaDoc result = new ArrayList JavaDoc(envs.length);
92         for (int i = 0; i < envs.length; i++) {
93             IExecutionEnvironment env = envs[i];
94             IVMInstall[] compatible = env.getCompatibleVMs();
95             for (int j = 0; j < compatible.length; j++) {
96                 if (compatible[j].equals(install)) {
97                     result.add(env);
98                     break;
99                 }
100             }
101         }
102         return (IExecutionEnvironment[])result.toArray(new IExecutionEnvironment[result.size()]);
103     }
104     
105     private Properties JavaDoc getJavaProfileProperties(String JavaDoc ee) {
106         IPluginModelBase model = PluginRegistry.findModel("system.bundle"); //$NON-NLS-1$
107
if (model == null)
108             return null;
109         
110         File JavaDoc location = new File JavaDoc(model.getInstallLocation());
111         String JavaDoc filename = ee.replace('/', '_') + ".profile"; //$NON-NLS-1$
112
InputStream JavaDoc is = null;
113         ZipFile JavaDoc zipFile = null;
114         try {
115             // find the input stream to the profile properties file
116
if (location.isDirectory()) {
117                 File JavaDoc file = new File JavaDoc(location, filename);
118                 if (file.exists())
119                     is = new FileInputStream JavaDoc(file);
120             } else {
121                 zipFile = null;
122                 try {
123                     zipFile = new ZipFile JavaDoc(location, ZipFile.OPEN_READ);
124                     ZipEntry JavaDoc entry = zipFile.getEntry(filename);
125                     if (entry != null)
126                         is = zipFile.getInputStream(entry);
127                 } catch (IOException JavaDoc e) {
128                     // nothing to do
129
}
130             }
131             if (is != null) {
132                 Properties JavaDoc profile = new Properties JavaDoc();
133                 profile.load(is);
134                 return profile;
135             }
136         } catch (IOException JavaDoc e) {
137             // nothing to do
138
} finally {
139             if (is != null)
140                 try {
141                     is.close();
142                 } catch (IOException JavaDoc e) {
143                     // nothing to do
144
}
145             if (zipFile != null)
146                 try {
147                     zipFile.close();
148                 } catch (IOException JavaDoc e) {
149                     // nothing to do
150
}
151         }
152         return null;
153     }
154     
155     public boolean hasErrors() {
156         return fOperation.hasErrors();
157     }
158     
159     public Map JavaDoc getInput() {
160         return fOperation.getResolverErrors();
161     }
162     
163     public boolean isEmpty() {
164         return fOperation.getState().getHighestBundleId() == -1;
165     }
166     
167     protected State getState() {
168         return fOperation.getState();
169     }
170
171 }
172
Popular Tags