1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import com.ibm.icu.text.MessageFormat; 17 import java.util.ArrayList ; 18 import java.util.Enumeration ; 19 import java.util.Map ; 20 import java.util.zip.ZipEntry ; 21 import java.util.zip.ZipException ; 22 import java.util.zip.ZipFile ; 23 24 import org.eclipse.core.resources.IProject; 25 import org.eclipse.core.resources.IResource; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IPath; 28 import org.eclipse.core.runtime.IProgressMonitor; 29 import org.eclipse.core.runtime.NullProgressMonitor; 30 import org.eclipse.core.runtime.Path; 31 import org.eclipse.core.runtime.Platform; 32 import org.eclipse.debug.core.DebugPlugin; 33 import org.eclipse.debug.core.ILaunch; 34 import org.eclipse.debug.core.ILaunchConfiguration; 35 import org.eclipse.jdt.core.IClasspathEntry; 36 import org.eclipse.jdt.core.IJavaProject; 37 import org.eclipse.jdt.core.JavaCore; 38 import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate; 39 import org.eclipse.jdt.launching.ExecutionArguments; 40 import org.eclipse.jdt.launching.IVMInstall; 41 import org.eclipse.jdt.launching.IVMRunner; 42 import org.eclipse.jdt.launching.JavaRuntime; 43 import org.eclipse.jdt.launching.VMRunnerConfiguration; 44 import org.eclipse.osgi.service.environment.Constants; 45 import org.eclipse.osgi.service.resolver.BundleDescription; 46 import org.eclipse.pde.core.plugin.IFragmentModel; 47 import org.eclipse.pde.core.plugin.IPluginLibrary; 48 import org.eclipse.pde.core.plugin.IPluginModelBase; 49 import org.eclipse.pde.internal.core.ClasspathUtilCore; 50 import org.eclipse.pde.internal.core.PDECore; 51 import org.eclipse.pde.internal.core.PluginModelManager; 52 import org.eclipse.pde.internal.core.TargetPlatform; 53 import org.eclipse.pde.internal.core.util.CoreUtility; 54 import org.eclipse.pde.internal.ui.PDEPlugin; 55 import org.osgi.framework.Version; 56 57 public class SWTLaunchConfiguration extends 58 AbstractJavaLaunchConfigurationDelegate { 59 60 private boolean fShouldDelete; 61 62 public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException { 63 if (monitor == null) { 64 monitor = new NullProgressMonitor(); 65 } 66 fShouldDelete = true; 67 68 monitor.beginTask(MessageFormat.format("{0}...", new String []{configuration.getName()}), 3); if (monitor.isCanceled()) { 71 return; 72 } 73 74 String mainTypeName = verifyMainTypeName(configuration); 75 76 IVMInstall vm = verifyVMInstall(configuration); 77 78 IVMRunner runner = vm.getVMRunner(mode); 79 if (runner == null) { 80 monitor.setCanceled(true); 81 } 82 83 File workingDir = verifyWorkingDirectory(configuration); 84 String workingDirName = null; 85 if (workingDir != null) { 86 workingDirName = workingDir.getAbsolutePath(); 87 } 88 89 String [] envp= DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration); 91 92 String pgmArgs = getProgramArguments(configuration); 94 String vmArgs = getVMArguments(configuration); 95 ExecutionArguments execArgs = new ExecutionArguments(vmArgs, pgmArgs); 96 97 BundleDescription[] fragments = findFragments(); 99 100 Map vmAttributesMap = getVMSpecificAttributesMap(configuration); 102 103 String [] classpath = getClasspath(fragments, configuration); 105 106 VMRunnerConfiguration runConfig = new VMRunnerConfiguration(mainTypeName, classpath); 108 runConfig.setProgramArguments(execArgs.getProgramArgumentsArray()); 109 runConfig.setEnvironment(envp); 110 runConfig.setVMArguments(getVMArguments(fragments, execArgs)); 111 runConfig.setWorkingDirectory(workingDirName); 112 runConfig.setVMSpecificAttributesMap(vmAttributesMap); 113 114 runConfig.setBootClassPath(getBootpath(configuration)); 116 117 if (monitor.isCanceled()) { 119 return; 120 } 121 122 prepareStopInMain(configuration); 124 125 monitor.worked(1); 127 128 setDefaultSourceLocator(launch, configuration); 130 monitor.worked(1); 131 132 runner.run(runConfig, launch, monitor); 134 135 if (monitor.isCanceled()) { 137 return; 138 } 139 140 monitor.done(); 141 } 142 143 private String [] getVMArguments(BundleDescription[] fragments, ExecutionArguments execArgs) { 144 if (fragments.length == 0) 145 return execArgs.getVMArgumentsArray(); 146 147 String location = getNativeLibrariesLocations(fragments); 148 if (location == null) 149 return execArgs.getVMArgumentsArray(); 150 151 String [] vmArgs = execArgs.getVMArgumentsArray(); 152 for (int i = vmArgs.length - 1; i >= 0; i--) { 153 if (vmArgs[i].startsWith("-Djava.library.path")) { vmArgs[i] += File.pathSeparatorChar + location; 155 return vmArgs; 156 } 157 } 158 String [] all = new String [vmArgs.length + 1]; 159 all[0] = "-Djava.library.path=" + location; System.arraycopy(vmArgs, 0, all, 1, vmArgs.length); 161 return all; 162 } 163 164 private String getNativeLibrariesLocations(BundleDescription[] bundles) { 165 StringBuffer buffer = new StringBuffer (); 166 for (int i = 0; i < bundles.length; i++) { 167 String location = getNativeLibrariesLocation(bundles[i]); 168 if (location != null) { 169 if (buffer.length() > 0) 170 buffer.append(File.pathSeparatorChar); 171 buffer.append(location); 172 } 173 } 174 return buffer.length() == 0 ? null : buffer.toString(); 175 } 176 177 protected static BundleDescription[] findFragments() { 178 IPluginModelBase model = PDECore.getDefault().getModelManager().findModel("org.eclipse.swt"); if (model != null && model.isEnabled()) { 180 BundleDescription desc = model.getBundleDescription(); 181 if (desc.getContainingState() != null) 182 return desc.getFragments(); 183 } 184 return new BundleDescription[0]; 185 } 186 187 private String getNativeLibrariesLocation(BundleDescription fragment) { 188 if (!fragment.isResolved()) 189 return null; 190 Version version = fragment.getVersion(); 191 if (version.getMajor() < 3 || version.getMinor() < 1) 192 return getLegacyNativeLibrariesLocation(fragment); 193 194 File file = new File (fragment.getLocation()); 195 return file.isDirectory() ? fragment.getLocation() : getExtractionLocation(file); 196 } 197 198 private String getExtractionLocation(File file) { 199 long timestamp = file.lastModified() ^ file.getAbsolutePath().hashCode(); 200 File metadata = PDEPlugin.getDefault().getStateLocation().toFile(); 201 File cache = new File (metadata, Long.toString(timestamp) + ".swt"); if (!cache.exists()){ 203 if (fShouldDelete) { 204 deleteStaleCache(metadata); 205 fShouldDelete = false; 206 } 207 cache.mkdirs(); 208 extractZipFile(file, cache); 209 } 210 return cache.getAbsolutePath(); 211 } 212 213 private void deleteStaleCache(File metadata) { 214 if (!metadata.exists()) 215 return; 216 217 File [] children = metadata.listFiles(); 218 if (children == null) 219 return; 220 for (int i = 0; i < children.length; i++) { 221 if (children[i].isDirectory() && children[i].getName().endsWith(".swt")) { CoreUtility.deleteContent(children[i]); 223 } 224 } 225 } 226 227 private void extractZipFile(File fragment, File destination) { 228 ZipFile zipFile = null; 229 try { 230 zipFile = new ZipFile (fragment); 231 for (Enumeration zipEntries = zipFile.entries(); zipEntries.hasMoreElements();) { 232 ZipEntry zipEntry = (ZipEntry ) zipEntries.nextElement(); 233 if (zipEntry.isDirectory()) 234 continue; 235 if (isInterestingFile(zipEntry.getName())) { 236 InputStream in = null; 237 try { 238 in = zipFile.getInputStream(zipEntry); 239 if (in != null) { 240 File file = new File (destination, zipEntry.getName()); 241 CoreUtility.readFile(in, file); 242 if (!Platform.getOS().equals(Constants.OS_WIN32)) 243 Runtime.getRuntime().exec(new String [] {"chmod", "755", file.getAbsolutePath()}).waitFor(); } 245 } catch (IOException e) { 246 } catch (InterruptedException e) { 247 } finally { 248 try { 249 if (in != null) 250 in.close(); 251 } catch (IOException e1) { 252 } 253 } 254 } 255 } 256 } catch (ZipException e) { 257 } catch (IOException e) { 258 } finally { 259 try { 260 if (zipFile != null) 261 zipFile.close(); 262 } catch (IOException e) { 263 } 264 } 265 } 266 267 private boolean isInterestingFile(String name) { 268 Path path = new Path(name); 269 if (path.segmentCount() > 1) 270 return false; 271 return name.endsWith(".dll") || name.endsWith(".jnilib") || name.endsWith(".sl") || name.endsWith(".a") || name.indexOf(".so") != -1; } 277 278 private String getLegacyNativeLibrariesLocation(BundleDescription fragment) { 279 StringBuffer buffer = new StringBuffer (); 280 IPath path = new Path(fragment.getLocation()); 281 buffer.append(path.removeTrailingSeparator().toString()); 282 buffer.append(IPath.SEPARATOR); 283 buffer.append("os"); buffer.append(IPath.SEPARATOR); 285 buffer.append(TargetPlatform.getOS()); 286 buffer.append(IPath.SEPARATOR); 287 buffer.append(TargetPlatform.getOSArch()); 288 return buffer.toString(); 289 } 290 291 private String [] getClasspath(BundleDescription[] fragments, ILaunchConfiguration configuration) throws CoreException { 292 String [] entries = getClasspath(configuration); 293 294 PluginModelManager manager = PDECore.getDefault().getModelManager(); 295 ArrayList extra = new ArrayList (); 296 for (int i = 0; i < fragments.length; i++) { 297 IFragmentModel fragment = manager.findFragmentModel(fragments[i].getSymbolicName()); 298 if (fragment == null) 299 continue; 300 301 IResource resource = fragment.getUnderlyingResource(); 302 if (resource != null) { 303 IProject project = resource.getProject(); 304 if (project.hasNature(JavaCore.NATURE_ID)) { 305 IJavaProject jProject = JavaCore.create(project); 306 extra.add(JavaRuntime.newProjectRuntimeClasspathEntry(jProject).getPath()); 307 IClasspathEntry[] classEntries = jProject.getRawClasspath(); 308 for (int j = 0; j < classEntries.length; j++) { 309 int kind = classEntries[j].getEntryKind(); 310 if (kind == IClasspathEntry.CPE_LIBRARY) { 311 extra.add(JavaRuntime.newArchiveRuntimeClasspathEntry(classEntries[j].getPath()).getLocation()); 312 } 313 } 314 } 315 } else { 316 IPluginLibrary[] libraries = fragment.getFragment().getLibraries(); 317 String location = fragment.getInstallLocation(); 318 for (int j = 0; j < libraries.length; j++) { 319 String name = ClasspathUtilCore.expandLibraryName(libraries[j].getName()); 320 extra.add(new Path(location).append(name).toOSString()); 321 } 322 } 323 } 324 if (extra.size() > 0) { 325 String [] all = new String [entries.length + extra.size()]; 326 System.arraycopy(entries, 0, all, 0, entries.length); 327 for (int i = 0; i < extra.size(); i++) { 328 all[i+entries.length] = extra.get(i).toString(); 329 } 330 return all; 331 } 332 return entries; 333 } 334 } 335 | Popular Tags |