001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.launcher;
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.IOException;
015: import java.io.InputStream;
016: import java.util.ArrayList;
017: import java.util.Dictionary;
018: import java.util.List;
019: import java.util.Map;
020: import java.util.Properties;
021: import java.util.zip.ZipEntry;
022: import java.util.zip.ZipFile;
023:
024: import org.eclipse.core.resources.IWorkspaceRunnable;
025: import org.eclipse.core.runtime.CoreException;
026: import org.eclipse.core.runtime.IProgressMonitor;
027: import org.eclipse.debug.core.ILaunchConfiguration;
028: import org.eclipse.jdt.launching.IVMInstall;
029: import org.eclipse.jdt.launching.JavaRuntime;
030: import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
031: import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
032: import org.eclipse.osgi.service.resolver.State;
033: import org.eclipse.pde.core.plugin.IPluginModelBase;
034: import org.eclipse.pde.core.plugin.PluginRegistry;
035: import org.eclipse.pde.internal.core.BundleValidationOperation;
036: import org.eclipse.pde.internal.core.TargetPlatformHelper;
037: import org.osgi.framework.Constants;
038:
039: public abstract class LaunchValidationOperation implements
040: IWorkspaceRunnable {
041:
042: private BundleValidationOperation fOperation;
043: protected ILaunchConfiguration fLaunchConfiguration;
044:
045: public LaunchValidationOperation(ILaunchConfiguration configuration) {
046: fLaunchConfiguration = configuration;
047: }
048:
049: public void run(IProgressMonitor monitor) throws CoreException {
050: fOperation = new BundleValidationOperation(getModels(),
051: getPlatformProperties());
052: fOperation.run(monitor);
053: }
054:
055: protected abstract IPluginModelBase[] getModels()
056: throws CoreException;
057:
058: protected Dictionary[] getPlatformProperties() throws CoreException {
059: IExecutionEnvironment[] envs = getMatchingEnvironments();
060: if (envs.length == 0)
061: return new Dictionary[] { TargetPlatformHelper
062: .getTargetEnvironment() };
063:
064: // add java profiles for those EE's that have a .profile file in the current system bundle
065: ArrayList result = new ArrayList(envs.length);
066: for (int i = 0; i < envs.length; i++) {
067: Properties profileProps = getJavaProfileProperties(envs[i]
068: .getId());
069: if (profileProps != null) {
070: Dictionary props = TargetPlatformHelper
071: .getTargetEnvironment();
072: String systemPackages = profileProps
073: .getProperty(Constants.FRAMEWORK_SYSTEMPACKAGES);
074: if (systemPackages != null)
075: props.put(Constants.FRAMEWORK_SYSTEMPACKAGES,
076: systemPackages);
077: String ee = profileProps
078: .getProperty(Constants.FRAMEWORK_EXECUTIONENVIRONMENT);
079: if (ee != null)
080: props.put(Constants.FRAMEWORK_EXECUTIONENVIRONMENT,
081: ee);
082: result.add(props);
083: }
084: }
085: if (result.size() > 0)
086: return (Dictionary[]) result.toArray(new Dictionary[result
087: .size()]);
088: return new Dictionary[] { TargetPlatformHelper
089: .getTargetEnvironment() };
090:
091: }
092:
093: private IExecutionEnvironment[] getMatchingEnvironments()
094: throws CoreException {
095: IVMInstall install = VMHelper
096: .getVMInstall(fLaunchConfiguration);
097: if (install == null)
098: return new IExecutionEnvironment[0];
099:
100: IExecutionEnvironmentsManager manager = JavaRuntime
101: .getExecutionEnvironmentsManager();
102: IExecutionEnvironment[] envs = manager
103: .getExecutionEnvironments();
104: List result = new ArrayList(envs.length);
105: for (int i = 0; i < envs.length; i++) {
106: IExecutionEnvironment env = envs[i];
107: IVMInstall[] compatible = env.getCompatibleVMs();
108: for (int j = 0; j < compatible.length; j++) {
109: if (compatible[j].equals(install)) {
110: result.add(env);
111: break;
112: }
113: }
114: }
115: return (IExecutionEnvironment[]) result
116: .toArray(new IExecutionEnvironment[result.size()]);
117: }
118:
119: private Properties getJavaProfileProperties(String ee) {
120: IPluginModelBase model = PluginRegistry
121: .findModel("system.bundle"); //$NON-NLS-1$
122: if (model == null)
123: return null;
124:
125: File location = new File(model.getInstallLocation());
126: String filename = ee.replace('/', '_') + ".profile"; //$NON-NLS-1$
127: InputStream is = null;
128: ZipFile zipFile = null;
129: try {
130: // find the input stream to the profile properties file
131: if (location.isDirectory()) {
132: File file = new File(location, filename);
133: if (file.exists())
134: is = new FileInputStream(file);
135: } else {
136: zipFile = null;
137: try {
138: zipFile = new ZipFile(location, ZipFile.OPEN_READ);
139: ZipEntry entry = zipFile.getEntry(filename);
140: if (entry != null)
141: is = zipFile.getInputStream(entry);
142: } catch (IOException e) {
143: // nothing to do
144: }
145: }
146: if (is != null) {
147: Properties profile = new Properties();
148: profile.load(is);
149: return profile;
150: }
151: } catch (IOException e) {
152: // nothing to do
153: } finally {
154: if (is != null)
155: try {
156: is.close();
157: } catch (IOException e) {
158: // nothing to do
159: }
160: if (zipFile != null)
161: try {
162: zipFile.close();
163: } catch (IOException e) {
164: // nothing to do
165: }
166: }
167: return null;
168: }
169:
170: public boolean hasErrors() {
171: return fOperation.hasErrors();
172: }
173:
174: public Map getInput() {
175: return fOperation.getResolverErrors();
176: }
177:
178: public boolean isEmpty() {
179: return fOperation.getState().getHighestBundleId() == -1;
180: }
181:
182: protected State getState() {
183: return fOperation.getState();
184: }
185:
186: }
|