Search in sources :

Example 1 with ITargetHandle

use of org.eclipse.pde.core.target.ITargetHandle in project dsl-devkit by dsldevkit.

the class TargetDefinitionSetup method initializeTargetPlatform.

/**
 * If necessary, creates and loads a new target platform. Waits until the new platform has been loaded.
 *
 * @throws IOException
 *           if locating bundle file fails
 */
public static void initializeTargetPlatform() throws IOException {
    // The work-around is creating a new target platform adding the parent folders of the bundles
    try {
        final ITargetPlatformService targetPlatformService = TargetPlatformService.getDefault();
        final ITargetHandle workspaceTargetHandle = targetPlatformService.getWorkspaceTargetHandle();
        if (workspaceTargetHandle == null || !TARGET_PLATFORM_NAME.equals(workspaceTargetHandle.getTargetDefinition().getName())) {
            final ITargetDefinition targetDefinition = targetPlatformService.newTarget();
            targetDefinition.setName(TARGET_PLATFORM_NAME);
            final List<ITargetLocation> bundleContainers = new ArrayList<ITargetLocation>();
            final Set<String> dirs = new HashSet<String>();
            for (final Bundle bundle : Platform.getBundle("org.eclipse.core.runtime").getBundleContext().getBundles()) {
                // $NON-NLS-1$
                final File file = FileLocator.getBundleFile(bundle);
                final File folder = file.getParentFile();
                final String path = folder.getAbsolutePath();
                if (dirs.add(path)) {
                    ITargetLocation newDirectoryLocation = targetPlatformService.newDirectoryLocation(path);
                    bundleContainers.add(newDirectoryLocation);
                }
            }
            targetDefinition.setTargetLocations(bundleContainers.toArray(new ITargetLocation[bundleContainers.size()]));
            targetDefinition.setArch(Platform.getOSArch());
            targetDefinition.setOS(Platform.getOS());
            targetDefinition.setWS(Platform.getWS());
            targetDefinition.setNL(Platform.getNL());
            targetPlatformService.saveTargetDefinition(targetDefinition);
            final CountDownLatch platformLoaded = new CountDownLatch(1);
            LoadTargetDefinitionJob.load(targetDefinition, new JobChangeAdapter() {

                @Override
                public void done(final IJobChangeEvent event) {
                    platformLoaded.countDown();
                }
            });
            platformLoaded.await();
        }
    } catch (final CoreException coreException) {
    // Nothing
    } catch (InterruptedException e) {
        // Restore interrupted status
        Thread.currentThread().interrupt();
    }
}
Also used : Bundle(org.osgi.framework.Bundle) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) ArrayList(java.util.ArrayList) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) CountDownLatch(java.util.concurrent.CountDownLatch) ITargetLocation(org.eclipse.pde.core.target.ITargetLocation) ITargetPlatformService(org.eclipse.pde.core.target.ITargetPlatformService) ITargetDefinition(org.eclipse.pde.core.target.ITargetDefinition) CoreException(org.eclipse.core.runtime.CoreException) File(java.io.File) ITargetHandle(org.eclipse.pde.core.target.ITargetHandle) HashSet(java.util.HashSet)

Aggregations

File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CoreException (org.eclipse.core.runtime.CoreException)1 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)1 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)1 ITargetDefinition (org.eclipse.pde.core.target.ITargetDefinition)1 ITargetHandle (org.eclipse.pde.core.target.ITargetHandle)1 ITargetLocation (org.eclipse.pde.core.target.ITargetLocation)1 ITargetPlatformService (org.eclipse.pde.core.target.ITargetPlatformService)1 Bundle (org.osgi.framework.Bundle)1