Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 361505

Summary: Declarative Services using EMF suppress Choose Workspace Dialog
Product: [Modeling] EMF Reporter: Gregor Pfeifer <gregor.pfeifer>
Component: CoreAssignee: Ed Merks <Ed.Merks>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: ekke, john.arthorne
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Attachments:
Description Flags
ZIP to reproduce the problem
none
Patch with proposed solution none

Description Gregor Pfeifer CLA 2011-10-20 04:11:06 EDT
Build Identifier: 20110615-0604

Problem
-------
If an OSGi Bundle use EMF and is started before org.eclipse.ui is started then the Choose Workspace Dialog will not appear, because EcorePlugin>>start calls Platform.getWorkspace().getRoot().

Details
-------
My  Plugin/OSGi Bundle requires EMF but does not requires org.eclipse.ui.

My Bundle will be started by org.eclipse.equinox.ds before org.eclipse.ui.

Since my Bundle requires org.eclipse.emf.core the bundle org.eclipse.emf.core will be started and EcorePlugin>>start will be called.

EcorePlugins>>start calls Platform.getWorkspace().getRoot() which will create a Workspace, if there is no Workpace available.

The Bundle org.eclipse.ui starts after org.eclipse.emf.core. Since a workspace is already created by org.eclipse.emf.core no Choose Workspace Dialog will appear.

Proposed Solution
-----------------
EcorePlugins already checks if “org.eclipse.core.resources” is available. Extent this mechanism and check if “org.eclipse.ui” is available, too. If “org.eclipse.ui” is available initialize workspaceRoot after the Eclipse Workbench is initialized. See attached patch-gpf-01.txt.   

Reproducible: Always

Steps to Reproduce:
See attached ds.zip. In the Bundle ds.provided you will find a launch configuration (launch/ds.provider.ide.launch). Using this launch configuration no Choose Workspace Dialog will appear. If you disable the two bundle ds.provided and ds.consumer the Choose Workspace Dialog will appear.
Comment 1 Gregor Pfeifer CLA 2011-10-20 04:12:40 EDT
Created attachment 205588 [details]
ZIP to reproduce the problem
Comment 2 Gregor Pfeifer CLA 2011-10-20 04:14:08 EDT
Created attachment 205589 [details]
Patch with proposed solution
Comment 3 Ed Merks CLA 2011-10-20 05:29:14 EDT
Sorry, I don't understand any of this so I'm reluctant to make changes I don't understand.  It appears I'd be forcing the UI to start as well, which seems bad because nothing here relies on that and the next guy might complain that they want something even further downstream to be started before the UI or that they don't want the UI started at all, even though it's available.

Hopefully John could comment on this. After all, E4 uses EMF and hasn't had this problem.  Maybe this problem should be fixed in the UI or maybe the workspace needs to have an extension point the UI uses to ensure that what the UI wants to happen happens when the workspace starts.  I don't know...
Comment 4 Gregor Pfeifer CLA 2011-10-20 08:51:25 EDT
The proposed solution is an extension to the current code and is very similar to the existing code.

The existing code in EMFPlugin checks if org.eclipse.core.resources is available:

public static final boolean IS_RESOURCES_BUNDLE_AVAILABLE;
static
  {
  // code to initialize  IS_RESOURCES_BUNDLE_AVAILABLE
  ...
  }
  
The proposed solution checks in addition if org.eclipse.ui is available:

public static final boolean IS_ECLIPSE_UI_AVAILABLE;
static
  {
  // code to initialize  IS_ECLIPSE_UI_AVAILABLE (similar to existing code)
  ....
  }


The existing code calls ResourcesPlugin.getWorkspace().getRoot() when "org.eclipse.core.resources" is available 

      if (IS_RESOURCES_BUNDLE_AVAILABLE) {
	workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
      }
	  
The proposed solution add an additional check:

       if (IS_RESOURCES_BUNDLE_AVAILABLE {
         if (IS_ECLIPSE_UI_AVAILABLE) {
           // If org.eclipse.ui is available
           // set workSpaceRoot after the workbench is initialized
           new WorkbenchTracker(context).open();
         } else {
           workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
         } 	  
       }


The WorkbenchTracker subclasses org.osgi.util.tracker.ServiceTracker and will never ever force any UI to start. It even has no dependency to "org.eclipse.ui". The WorkbenchTracker will be called when the Eclipse Workbench is initialized and will then initialize the static variable workspaceRoot (workspaceRoot = ResourcesPlugin.getWorkspace().getRoot())
Comment 5 Gregor Pfeifer CLA 2011-10-20 09:25:30 EDT
Just discovered:
Same problem occurs with Riena EMF Dynamic Views

https://bugs.eclipse.org/bugs/show_bug.cgi?id=288063
Comment 6 Ed Merks CLA 2011-10-20 09:31:33 EDT
Might it be simpler to move the code:

  private static IWorkspaceRoot workspaceRoot = null;

  /**
   * Returns the workspace root, or <code>null</code>, if the runtime environment is stand-alone.
   * @return the workspace root, or <code>null</code>.
   */
  public static IWorkspaceRoot getWorkspaceRoot()
  {
    if (workspaceRoot == null && IS_RESOURCES_BUNDLE_AVAILABLE && System.getProperty("org.eclipse.emf.ecore.plugin.EcorePlugin.doNotLoadResourcesPlugin") == null)
    {
      workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    }
    return workspaceRoot;
  }

This way the workspace is created on demand by anyone who needs it.  I'd feel more comfortable with that because I understand it better and it's harder to imagine anyone even being able to notice...
Comment 7 Gregor Pfeifer CLA 2011-10-21 03:29:12 EDT
Yes, this is much simpler. Since in my environment no bundle calls EcorePlugin>>getWorkspaceRoot during Eclipse startup it solves my problem.
Thank you!
Comment 8 Ed Merks CLA 2011-10-25 04:03:45 EDT
The change was committed to CVS for EMF 2.8.
Comment 9 Ed Merks CLA 2011-11-22 05:26:46 EST
The changes are available in builds.
Comment 10 Stoyan Boshev CLA 2013-11-27 14:56:14 EST
*** Bug 288063 has been marked as a duplicate of this bug. ***