Skip to content

Commit 243aaf8

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 243aaf8

File tree

1 file changed

+58
-8
lines changed
  • bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal

1 file changed

+58
-8
lines changed

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

Lines changed: 58 additions & 8 deletions
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;
@@ -630,8 +635,7 @@ public void update() {
630635
}
631636
};
632637
registration[0] = FrameworkUtil.getBundle(WorkbenchPlugin.class).getBundleContext()
633-
.registerService(StartupMonitor.class.getName(), startupMonitor,
634-
properties);
638+
.registerService(StartupMonitor.class.getName(), startupMonitor, properties);
635639

636640
// listener for updating the splash screen
637641
AtomicReference<ServiceListener> serviceListener = new AtomicReference<>();
@@ -1400,7 +1404,8 @@ private boolean saveAllParts(boolean confirm, boolean closing) {
14001404
if (saveHandler != null) {
14011405
if (saveHandler instanceof WWinPartServiceSaveHandler) {
14021406
try {
1403-
return ((WWinPartServiceSaveHandler) saveHandler).saveParts(parts, confirm, true, true);
1407+
return ((WWinPartServiceSaveHandler) saveHandler).saveParts(parts, confirm, true,
1408+
true);
14041409
} catch (UnsupportedOperationException e) {
14051410
// do nothing
14061411
}
@@ -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.
@@ -3501,8 +3551,8 @@ public void registerService(final Class api, final Object service) {
35013551
* is used for legacy action-based handlers which need to become active only for
35023552
* the duration of a menu being visible.
35033553
*
3504-
* @param menuIds The identifiers of the menu that is now showing; must
3505-
* not be <code>null</code>.
3554+
* @param menuIds The identifiers of the menu that is now showing; must not be
3555+
* <code>null</code>.
35063556
*/
35073557
public void addShowingMenus(final Set menuIds, final ISelection localSelection, final ISelection localEditorInput) {
35083558
menuSourceProvider.addShowingMenus(menuIds, localSelection, localEditorInput);
@@ -3517,8 +3567,8 @@ public void addShowingMenus(final Set menuIds, final ISelection localSelection,
35173567
* This is used for legacy action-based handlers which need to become active
35183568
* only for the duration of a menu being visible.
35193569
*
3520-
* @param menuIds The identifiers of the menu that is now hidden; must
3521-
* not be <code>null</code>.
3570+
* @param menuIds The identifiers of the menu that is now hidden; must not be
3571+
* <code>null</code>.
35223572
*/
35233573
public void removeShowingMenus(final Set menuIds, final ISelection localSelection,
35243574
final ISelection localEditorInput) {

0 commit comments

Comments
 (0)