-
Notifications
You must be signed in to change notification settings - Fork 227
Prompt user when switching to a locked workspace in Eclipse #3518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,9 +35,12 @@ | |
| import java.io.InputStream; | ||
| import java.io.StringReader; | ||
| import java.io.StringWriter; | ||
| import java.net.MalformedURLException; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.net.URL; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
|
|
@@ -49,6 +52,7 @@ | |
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Properties; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
@@ -80,6 +84,7 @@ | |
| import org.eclipse.core.runtime.SafeRunner; | ||
| import org.eclipse.core.runtime.Status; | ||
| import org.eclipse.core.runtime.SubMonitor; | ||
| import org.eclipse.core.runtime.URIUtil; | ||
| import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; | ||
| import org.eclipse.core.runtime.jobs.Job; | ||
| import org.eclipse.core.runtime.preferences.ConfigurationScope; | ||
|
|
@@ -114,6 +119,7 @@ | |
| import org.eclipse.e4.ui.workbench.modeling.EModelService; | ||
| import org.eclipse.e4.ui.workbench.modeling.EPartService; | ||
| import org.eclipse.e4.ui.workbench.modeling.ISaveHandler; | ||
| import org.eclipse.e4.ui.workbench.swt.internal.copy.WorkbenchSWTMessages; | ||
| import org.eclipse.emf.ecore.EObject; | ||
| import org.eclipse.emf.ecore.resource.Resource; | ||
| import org.eclipse.emf.ecore.util.EcoreUtil; | ||
|
|
@@ -2699,7 +2705,8 @@ private static String buildCommandLine(String workspace) { | |
| * as the workspace location. | ||
| * | ||
| * @param workspacePath the new workspace location | ||
| * @return {@link IApplication#EXIT_OK} or {@link IApplication#EXIT_RELAUNCH} | ||
| * @return {@link IApplication#EXIT_OK} or {@link IApplication#EXIT_RELAUNCH} or | ||
| * <code>null</code> | ||
| */ | ||
| @SuppressWarnings("restriction") | ||
| public static Object setRestartArguments(String workspacePath) { | ||
|
|
@@ -2714,6 +2721,16 @@ public static Object setRestartArguments(String workspacePath) { | |
| if (command_line == null) { | ||
| return IApplication.EXIT_OK; | ||
| } | ||
| Path selectedWorkspace = Path.of(workspacePath); | ||
| try { | ||
| String workspaceLock = getWorkspaceLockDetails(selectedWorkspace.toUri().toURL()); | ||
| if (workspaceLock != null && !workspaceLock.isEmpty()) { | ||
| showWorkspaceLockedDialog(workspacePath, workspaceLock); | ||
| return null; | ||
SougandhS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } catch (MalformedURLException e) { | ||
| return null; | ||
| } | ||
|
|
||
| System.setProperty(Workbench.PROP_EXIT_CODE, IApplication.EXIT_RELAUNCH.toString()); | ||
| System.setProperty(IApplicationContext.EXIT_DATA_PROPERTY, command_line); | ||
|
|
@@ -3684,4 +3701,72 @@ public void runWithInitialAutoScaleValue(Runnable runnable) { | |
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Extract the lock details of the selected workspace if it is locked by another | ||
| * Eclipse application | ||
| * | ||
| * @param workspaceUrl the <code>URL</code> of selected workspace | ||
| * @return <code>String</code> details of lock owned workspace, | ||
| * <code>null or Empty</code> if not locked | ||
| */ | ||
| @SuppressWarnings("restriction") | ||
| public static String getWorkspaceLockDetails(URL workspaceUrl) { | ||
| Path lockFile = getLockInfoFile(workspaceUrl); | ||
| if (lockFile != null && Files.exists(lockFile)) { | ||
| StringBuilder lockDetails = new StringBuilder(); | ||
| Properties properties = new Properties(); | ||
| try (InputStream is = Files.newInputStream(lockFile)) { | ||
| properties.load(is); | ||
| String prop = properties.getProperty("user"); //$NON-NLS-1$ | ||
| if (prop != null) { | ||
| lockDetails.append(NLS.bind(WorkbenchSWTMessages.IDEApplication_workspaceLockOwner, prop)); | ||
| } | ||
| prop = properties.getProperty("host"); //$NON-NLS-1$ | ||
| if (prop != null) { | ||
| lockDetails.append(NLS.bind(WorkbenchSWTMessages.IDEApplication_workspaceLockHost, prop)); | ||
| } | ||
| prop = properties.getProperty("display"); //$NON-NLS-1$ | ||
| if (prop != null) { | ||
| lockDetails.append(NLS.bind(WorkbenchSWTMessages.IDEApplication_workspaceLockDisplay, prop)); | ||
| } | ||
| prop = properties.getProperty("process-id"); //$NON-NLS-1$ | ||
| if (prop != null) { | ||
| lockDetails.append(NLS.bind(WorkbenchSWTMessages.IDEApplication_workspaceLockPID, prop)); | ||
| } | ||
|
|
||
| } catch (IOException e) { | ||
| WorkbenchPlugin.log(e); | ||
| } | ||
| return lockDetails.toString(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the lock file. | ||
| * | ||
| * @param workspaceUrl the <code>URL</code> of selected workspace | ||
| * @return the path to the <code>.lock_info</code> file within the specified | ||
| * workspace, or <code> null</code> if the workspace URL cannot be | ||
| * converted to a valid URI | ||
| */ | ||
| public static Path getLockInfoFile(URL workspaceUrl) { | ||
| Path lockFile = Path.of(".metadata", ".lock_info"); //$NON-NLS-1$ //$NON-NLS-2$ | ||
| try { | ||
| return Path.of(URIUtil.toURI(workspaceUrl)).resolve(lockFile); | ||
| } catch (URISyntaxException e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("restriction") | ||
| public static void showWorkspaceLockedDialog(String workspacePath, String workspaceLock) { | ||
| String lockMessage = NLS.bind(WorkbenchSWTMessages.IDEApplication_workspaceCannotLockMessage2, workspacePath); | ||
| String wsLockedError = lockMessage + System.lineSeparator() + System.lineSeparator() | ||
| + NLS.bind(WorkbenchSWTMessages.IDEApplication_workspaceLockMessage, workspaceLock); | ||
|
|
||
| MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've already commented before, if the workspace selection dialog is opened, there is NO active workbench window, not a single window is opened! I would recommend to move all this workspace locking code to extra class and add a bold statement on the class javadoc that it is used in both workbench / window - free and workbench - created use cases. |
||
| WorkbenchSWTMessages.IDEApplication_workspaceCannotLockTitle, wsLockedError); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the returning constants is self explanatory, what does "return null" mean for the caller? NO exit? Don't care? Leave me alone with my problems?
Just imagine you would see this javadoc and not see the implementation. What you would think about this API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will add few more details then