3535import java .io .InputStream ;
3636import java .io .StringReader ;
3737import java .io .StringWriter ;
38+ import java .net .MalformedURLException ;
3839import java .net .URI ;
3940import java .net .URISyntaxException ;
4041import java .net .URL ;
42+ import java .nio .file .Files ;
43+ import java .nio .file .Path ;
4144import java .util .ArrayList ;
4245import java .util .Arrays ;
4346import java .util .Collection ;
8083import org .eclipse .core .runtime .SafeRunner ;
8184import org .eclipse .core .runtime .Status ;
8285import org .eclipse .core .runtime .SubMonitor ;
86+ import org .eclipse .core .runtime .URIUtil ;
8387import org .eclipse .core .runtime .dynamichelpers .IExtensionTracker ;
8488import org .eclipse .core .runtime .jobs .Job ;
8589import org .eclipse .core .runtime .preferences .ConfigurationScope ;
114118import org .eclipse .e4 .ui .workbench .modeling .EModelService ;
115119import org .eclipse .e4 .ui .workbench .modeling .EPartService ;
116120import org .eclipse .e4 .ui .workbench .modeling .ISaveHandler ;
121+ import org .eclipse .e4 .ui .workbench .swt .internal .copy .WorkbenchSWTMessages ;
117122import org .eclipse .emf .ecore .EObject ;
118123import org .eclipse .emf .ecore .resource .Resource ;
119124import 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