Search in sources :

Example 1 with ITargetPlatformService

use of org.eclipse.pde.core.target.ITargetPlatformService 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)

Example 2 with ITargetPlatformService

use of org.eclipse.pde.core.target.ITargetPlatformService in project xtext-eclipse by eclipse.

the class TargetPlatformUtil method setTargetPlatform.

/**
 * Sets the target platform for tests (to be used in tycho mainly)
 * @param context any class of the test bundle to be able to determine the test bundle
 * @since 2.14
 */
public static void setTargetPlatform(Class<?> context) throws Exception {
    if (isPdeLaunch()) {
        return;
    }
    Bundle currentBundle = FrameworkUtil.getBundle(context);
    ITargetPlatformService tpService = TargetPlatformService.getDefault();
    ITargetDefinition targetDef = tpService.newTarget();
    targetDef.setName("Tycho platform");
    Bundle[] bundles = FrameworkUtil.getBundle(Platform.class).getBundleContext().getBundles();
    List<ITargetLocation> bundleContainers = new ArrayList<ITargetLocation>();
    Set<File> dirs = new HashSet<File>();
    for (Bundle bundle : bundles) {
        if (bundle.equals(currentBundle)) {
            // errors during our tests.
            continue;
        }
        EquinoxBundle bundleImpl = (EquinoxBundle) bundle;
        Generation generation = (Generation) bundleImpl.getModule().getCurrentRevision().getRevisionInfo();
        File file = generation.getBundleFile().getBaseFile();
        File folder = file.getParentFile();
        if ((file.isFile() || Platform.inDevelopmentMode()) && !dirs.contains(folder)) {
            dirs.add(folder);
            bundleContainers.add(tpService.newDirectoryLocation(folder.getAbsolutePath()));
        }
    }
    targetDef.setTargetLocations(bundleContainers.toArray(new ITargetLocation[bundleContainers.size()]));
    targetDef.setArch(Platform.getOSArch());
    targetDef.setOS(Platform.getOS());
    targetDef.setWS(Platform.getWS());
    targetDef.setNL(Platform.getNL());
    // targetDef.setJREContainer()
    tpService.saveTargetDefinition(targetDef);
    Job job = new LoadTargetDefinitionJob(targetDef);
    job.schedule();
    job.join();
}
Also used : EquinoxBundle(org.eclipse.osgi.internal.framework.EquinoxBundle) EquinoxBundle(org.eclipse.osgi.internal.framework.EquinoxBundle) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ITargetLocation(org.eclipse.pde.core.target.ITargetLocation) ITargetPlatformService(org.eclipse.pde.core.target.ITargetPlatformService) Generation(org.eclipse.osgi.storage.BundleInfo.Generation) ITargetDefinition(org.eclipse.pde.core.target.ITargetDefinition) LoadTargetDefinitionJob(org.eclipse.pde.core.target.LoadTargetDefinitionJob) Job(org.eclipse.core.runtime.jobs.Job) LoadTargetDefinitionJob(org.eclipse.pde.core.target.LoadTargetDefinitionJob) File(java.io.File) HashSet(java.util.HashSet)

Example 3 with ITargetPlatformService

use of org.eclipse.pde.core.target.ITargetPlatformService in project xtext-eclipse by eclipse.

the class TargetPlatformUtil method setTargetPlatform.

/**
 * Sets the target platform for tests (to be used in tycho mainly)
 * @param context any class of the test bundle to be able to determine the test bundle
 * @since 2.14
 */
public static void setTargetPlatform(Class<?> context) throws Exception {
    if (isPdeLaunch()) {
        return;
    }
    Bundle currentBundle = FrameworkUtil.getBundle(context);
    ITargetPlatformService tpService = TargetPlatformService.getDefault();
    ITargetDefinition targetDef = tpService.newTarget();
    targetDef.setName("Tycho platform");
    Bundle[] bundles = FrameworkUtil.getBundle(Platform.class).getBundleContext().getBundles();
    List<ITargetLocation> bundleContainers = new ArrayList<ITargetLocation>();
    Set<File> dirs = new HashSet<File>();
    for (Bundle bundle : bundles) {
        if (bundle.equals(currentBundle)) {
            // errors during our tests.
            continue;
        }
        EquinoxBundle bundleImpl = (EquinoxBundle) bundle;
        Generation generation = (Generation) bundleImpl.getModule().getCurrentRevision().getRevisionInfo();
        File file = generation.getBundleFile().getBaseFile();
        File folder = file.getParentFile();
        if (!dirs.contains(folder)) {
            dirs.add(folder);
            bundleContainers.add(tpService.newDirectoryLocation(folder.getAbsolutePath()));
        }
    }
    targetDef.setTargetLocations(bundleContainers.toArray(new ITargetLocation[bundleContainers.size()]));
    targetDef.setArch(Platform.getOSArch());
    targetDef.setOS(Platform.getOS());
    targetDef.setWS(Platform.getWS());
    targetDef.setNL(Platform.getNL());
    // targetDef.setJREContainer()
    tpService.saveTargetDefinition(targetDef);
    Job job = new LoadTargetDefinitionJob(targetDef);
    job.schedule();
    job.join();
}
Also used : EquinoxBundle(org.eclipse.osgi.internal.framework.EquinoxBundle) EquinoxBundle(org.eclipse.osgi.internal.framework.EquinoxBundle) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ITargetLocation(org.eclipse.pde.core.target.ITargetLocation) ITargetPlatformService(org.eclipse.pde.core.target.ITargetPlatformService) Generation(org.eclipse.osgi.storage.BundleInfo.Generation) ITargetDefinition(org.eclipse.pde.core.target.ITargetDefinition) LoadTargetDefinitionJob(org.eclipse.pde.core.target.LoadTargetDefinitionJob) Job(org.eclipse.core.runtime.jobs.Job) LoadTargetDefinitionJob(org.eclipse.pde.core.target.LoadTargetDefinitionJob) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

File (java.io.File)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ITargetDefinition (org.eclipse.pde.core.target.ITargetDefinition)3 ITargetLocation (org.eclipse.pde.core.target.ITargetLocation)3 ITargetPlatformService (org.eclipse.pde.core.target.ITargetPlatformService)3 Bundle (org.osgi.framework.Bundle)3 Job (org.eclipse.core.runtime.jobs.Job)2 EquinoxBundle (org.eclipse.osgi.internal.framework.EquinoxBundle)2 Generation (org.eclipse.osgi.storage.BundleInfo.Generation)2 LoadTargetDefinitionJob (org.eclipse.pde.core.target.LoadTargetDefinitionJob)2 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 ITargetHandle (org.eclipse.pde.core.target.ITargetHandle)1