|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 IBM Corporation. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * IBM Corporation - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.ui.internal; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.net.URISyntaxException; |
| 19 | +import java.net.URL; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.Properties; |
| 23 | +import org.eclipse.core.runtime.URIUtil; |
| 24 | +import org.eclipse.e4.ui.workbench.swt.internal.copy.WorkbenchSWTMessages; |
| 25 | +import org.eclipse.jface.dialogs.MessageDialog; |
| 26 | +import org.eclipse.osgi.util.NLS; |
| 27 | +import org.eclipse.swt.widgets.Shell; |
| 28 | + |
| 29 | +/** |
| 30 | + * Utility class for reading and presenting workspace lock information. |
| 31 | + * |
| 32 | + * <p> |
| 33 | + * This class is used during two different phases of the Eclipse application |
| 34 | + * lifecycle: |
| 35 | + * </p> |
| 36 | + * <ul> |
| 37 | + * <li>before the Workbench is created (no workbench windows exist, such as when |
| 38 | + * the Workspace Launcher dialog is displayed)</li> |
| 39 | + * <li>after the Workbench has been created and workbench windows are |
| 40 | + * available</li> |
| 41 | + * </ul> |
| 42 | + * |
| 43 | + * <p> |
| 44 | + * To support both environments, this class does not rely on workbench-specific |
| 45 | + * APIs such as {@code PlatformUI.getWorkbench()} or {@code IWorkbenchWindow}, |
| 46 | + * nor on any API that requires an initialized workbench window. Only SWT-level |
| 47 | + * constructs (for example, {@link org.eclipse.swt.widgets.Display} and |
| 48 | + * {@link org.eclipse.swt.widgets.Shell}) and core/runtime APIs are used. |
| 49 | + * </p> |
| 50 | + * |
| 51 | + * @since 3.5 |
| 52 | + */ |
| 53 | +public class WorkspaceLock { |
| 54 | + |
| 55 | + public static final String PROCESS_ID = "process-id"; //$NON-NLS-1$ |
| 56 | + |
| 57 | + public static final String DISPLAY = "display"; //$NON-NLS-1$ |
| 58 | + |
| 59 | + public static final String HOST = "host"; //$NON-NLS-1$ |
| 60 | + |
| 61 | + public static final String USER = "user"; //$NON-NLS-1$ |
| 62 | + |
| 63 | + /** |
| 64 | + * Extract the lock details of the selected workspace if it is locked by another |
| 65 | + * Eclipse application |
| 66 | + * |
| 67 | + * @param workspaceUrl the <code>URL</code> of selected workspace |
| 68 | + * @return <code>String</code> details of lock owned workspace, |
| 69 | + * <code>null or Empty</code> if not locked |
| 70 | + */ |
| 71 | + @SuppressWarnings("restriction") |
| 72 | + public static String getWorkspaceLockDetails(URL workspaceUrl) { |
| 73 | + Path lockFile = getLockInfoFile(workspaceUrl); |
| 74 | + if (lockFile != null && Files.exists(lockFile)) { |
| 75 | + StringBuilder lockDetails = new StringBuilder(); |
| 76 | + Properties properties = new Properties(); |
| 77 | + try (InputStream is = Files.newInputStream(lockFile)) { |
| 78 | + properties.load(is); |
| 79 | + String prop = properties.getProperty(USER); |
| 80 | + if (prop != null) { |
| 81 | + lockDetails.append(NLS.bind(WorkbenchSWTMessages.IDEApplication_workspaceLockOwner, prop)); |
| 82 | + } |
| 83 | + prop = properties.getProperty(HOST); |
| 84 | + if (prop != null) { |
| 85 | + lockDetails.append(NLS.bind(WorkbenchSWTMessages.IDEApplication_workspaceLockHost, prop)); |
| 86 | + } |
| 87 | + prop = properties.getProperty(DISPLAY); |
| 88 | + if (prop != null) { |
| 89 | + lockDetails.append(NLS.bind(WorkbenchSWTMessages.IDEApplication_workspaceLockDisplay, prop)); |
| 90 | + } |
| 91 | + prop = properties.getProperty(PROCESS_ID); |
| 92 | + if (prop != null) { |
| 93 | + lockDetails.append(NLS.bind(WorkbenchSWTMessages.IDEApplication_workspaceLockPID, prop)); |
| 94 | + } |
| 95 | + |
| 96 | + } catch (IOException e) { |
| 97 | + WorkbenchPlugin.log(e); |
| 98 | + } |
| 99 | + return lockDetails.toString(); |
| 100 | + } |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns the lock file. |
| 106 | + * |
| 107 | + * @param workspaceUrl the <code>URL</code> of selected workspace |
| 108 | + * @return the path to the <code>.lock_info</code> file within the specified |
| 109 | + * workspace, or <code> null</code> if the workspace URL cannot be |
| 110 | + * converted to a valid URI |
| 111 | + */ |
| 112 | + public static Path getLockInfoFile(URL workspaceUrl) { |
| 113 | + Path lockFile = Path.of(".metadata", ".lock_info"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 114 | + try { |
| 115 | + return Path.of(URIUtil.toURI(workspaceUrl)).resolve(lockFile); |
| 116 | + } catch (URISyntaxException e) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Opens an error dialog indicating that the selected workspace is locked by |
| 123 | + * another Eclipse instance. |
| 124 | + * <p> |
| 125 | + * This method works in both early startup (before the Workbench is created) and |
| 126 | + * in normal runtime (after Workbench windows exist). |
| 127 | + * </p> |
| 128 | + * |
| 129 | + * @param shell the parent {@link Shell} for the dialog, or |
| 130 | + * {@code null} if no workbench window is available |
| 131 | + * @param workspacePath the absolute path of the workspace that could not |
| 132 | + * be locked |
| 133 | + * @param workspaceLockOwner a formatted description of the existing lock owner |
| 134 | + */ |
| 135 | + @SuppressWarnings("restriction") |
| 136 | + public static void showWorkspaceLockedDialog(Shell shell, String workspacePath, String workspaceLockOwner) { |
| 137 | + String lockMessage = NLS.bind(WorkbenchSWTMessages.IDEApplication_workspaceCannotLockMessage2, workspacePath); |
| 138 | + String wsLockedError = lockMessage + System.lineSeparator() + System.lineSeparator() |
| 139 | + + NLS.bind(WorkbenchSWTMessages.IDEApplication_workspaceLockMessage, workspaceLockOwner); |
| 140 | + |
| 141 | + MessageDialog.openError(shell, |
| 142 | + WorkbenchSWTMessages.IDEApplication_workspaceCannotLockTitle, wsLockedError); |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments