Skip to content

Commit 279a4ef

Browse files
committed
Prompt user when switching to a locked workspace in Eclipse
Prompts user on attempting to switch to a workspace that is currently locked by another Eclipse instance, a prompt dialog is now shown informing the user about the lock and preventing Eclipse from exiting
1 parent ef09467 commit 279a4ef

File tree

1 file changed

+51
-1
lines changed
  • bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal

1 file changed

+51
-1
lines changed

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/Workbench.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@
3535
import java.io.InputStream;
3636
import java.io.StringReader;
3737
import java.io.StringWriter;
38+
import java.net.MalformedURLException;
3839
import java.net.URI;
3940
import java.net.URISyntaxException;
4041
import java.net.URL;
42+
import java.nio.file.Files;
43+
import java.nio.file.Path;
4144
import java.util.ArrayList;
4245
import java.util.Arrays;
4346
import java.util.Collection;
@@ -80,6 +83,7 @@
8083
import org.eclipse.core.runtime.SafeRunner;
8184
import org.eclipse.core.runtime.Status;
8285
import org.eclipse.core.runtime.SubMonitor;
86+
import org.eclipse.core.runtime.URIUtil;
8387
import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
8488
import org.eclipse.core.runtime.jobs.Job;
8589
import org.eclipse.core.runtime.preferences.ConfigurationScope;
@@ -114,6 +118,7 @@
114118
import org.eclipse.e4.ui.workbench.modeling.EModelService;
115119
import org.eclipse.e4.ui.workbench.modeling.EPartService;
116120
import org.eclipse.e4.ui.workbench.modeling.ISaveHandler;
121+
import org.eclipse.e4.ui.workbench.swt.internal.copy.WorkbenchSWTMessages;
117122
import org.eclipse.emf.ecore.EObject;
118123
import org.eclipse.emf.ecore.resource.Resource;
119124
import org.eclipse.emf.ecore.util.EcoreUtil;
@@ -2699,7 +2704,8 @@ private static String buildCommandLine(String workspace) {
26992704
* as the workspace location.
27002705
*
27012706
* @param workspacePath the new workspace location
2702-
* @return {@link IApplication#EXIT_OK} or {@link IApplication#EXIT_RELAUNCH}
2707+
* @return {@link IApplication#EXIT_OK} or {@link IApplication#EXIT_RELAUNCH} or
2708+
* <code>null</code>
27032709
*/
27042710
@SuppressWarnings("restriction")
27052711
public static Object setRestartArguments(String workspacePath) {
@@ -2714,12 +2720,56 @@ public static Object setRestartArguments(String workspacePath) {
27142720
if (command_line == null) {
27152721
return IApplication.EXIT_OK;
27162722
}
2723+
Path selectedWorkspace = Path.of(workspacePath);
2724+
try {
2725+
if (isWorkspaceLocked(selectedWorkspace.toUri().toURL())) {
2726+
MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
2727+
WorkbenchSWTMessages.IDEApplication_workspaceCannotLockTitle,
2728+
WorkbenchSWTMessages.IDEApplication_workspaceCannotLockMessage);
2729+
return null;
2730+
}
2731+
} catch (MalformedURLException e) {
2732+
return null;
2733+
}
27172734

27182735
System.setProperty(Workbench.PROP_EXIT_CODE, IApplication.EXIT_RELAUNCH.toString());
27192736
System.setProperty(IApplicationContext.EXIT_DATA_PROPERTY, command_line);
27202737
return IApplication.EXIT_RELAUNCH;
27212738
}
27222739

2740+
/**
2741+
* Check if the selected workspace is already being used by another eclipse
2742+
* application
2743+
*
2744+
* @param workspaceUrl the <code>URL</code> of selected workspace
2745+
* @return <code>true</code> if workspace is used, <code>false</code> if not
2746+
* used.
2747+
*/
2748+
private static boolean isWorkspaceLocked(URL workspaceUrl) {
2749+
Path lockFile = getLockInfoFile(workspaceUrl);
2750+
if (lockFile != null && Files.exists(lockFile)) {
2751+
return true;
2752+
}
2753+
return false;
2754+
}
2755+
2756+
/**
2757+
* Returns the lock file.
2758+
*
2759+
* @param workspaceUrl the <code>URL</code> of selected workspace
2760+
* @return the path to the <code>.lock_info</code> file within the specified
2761+
* workspace, or <code> null</code> if the workspace URL cannot be
2762+
* converted to a valid URI
2763+
*/
2764+
private static Path getLockInfoFile(URL workspaceUrl) {
2765+
Path lockFile = Path.of(".metadata", ".lock_info"); //$NON-NLS-1$ //$NON-NLS-2$
2766+
try {
2767+
return Path.of(URIUtil.toURI(workspaceUrl)).resolve(lockFile);
2768+
} catch (URISyntaxException e) {
2769+
return null;
2770+
}
2771+
}
2772+
27232773
/**
27242774
* Returns the ids of all plug-ins that extend the
27252775
* <code>org.eclipse.ui.startup</code> extension point.

0 commit comments

Comments
 (0)