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 ;
@@ -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