View | Details | Raw Unified | Return to bug 59780 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/ui/internal/ide/messages.properties (+6 lines)
Lines 841-846 Link Here
841
IDEApplication_workspaceCannotBeSetMessage=Error in runtime; workspace cannot be set.  Exiting.
841
IDEApplication_workspaceCannotBeSetMessage=Error in runtime; workspace cannot be set.  Exiting.
842
IDEApplication_workspaceCannotLockTitle=Workspace Cannot be Locked
842
IDEApplication_workspaceCannotLockTitle=Workspace Cannot be Locked
843
IDEApplication_workspaceCannotLockMessage=Could not launch the product because the associated workspace is currently in use.
843
IDEApplication_workspaceCannotLockMessage=Could not launch the product because the associated workspace is currently in use.
844
IDEApplication_confirmOpenWorkspaceWOLockTitle=Open Workspace
845
IDEApplication_confirmOpenWorkspaceWOLockMessage=\
846
	The workspace is on a file system which does not support file locking. \
847
	Do you want to open the workspace without locking? \
848
	Please note that opening the same workspace a second time might cause data corruption and data loss.\n\
849
	Open workspace?
844
IDEApplication_versionTitle = Different Workspace Version
850
IDEApplication_versionTitle = Different Workspace Version
845
IDEApplication_versionMessage = \
851
IDEApplication_versionMessage = \
846
This workspace was written with a different version of the product and needs to be updated.\n\n\
852
This workspace was written with a different version of the product and needs to be updated.\n\n\
(-)src/org/eclipse/ui/internal/ide/IDEApplication.java (-30 / +112 lines)
Lines 17-22 Link Here
17
import java.io.OutputStream;
17
import java.io.OutputStream;
18
import java.net.MalformedURLException;
18
import java.net.MalformedURLException;
19
import java.net.URL;
19
import java.net.URL;
20
import java.nio.channels.FileLock;
20
import java.util.Properties;
21
import java.util.Properties;
21
22
22
import org.eclipse.core.runtime.CoreException;
23
import org.eclipse.core.runtime.CoreException;
Lines 41-47 Link Here
41
 * @since 3.0
42
 * @since 3.0
42
 */
43
 */
43
public class IDEApplication implements IPlatformRunnable, IExecutableExtension {
44
public class IDEApplication implements IPlatformRunnable, IExecutableExtension {
44
45
	private static final String PROP_OSGI_LOCKING = "osgi.locking"; //$NON-NLS-1$
45
	/**
46
	/**
46
	 * The name of the folder containing metadata information for the workspace.
47
	 * The name of the folder containing metadata information for the workspace.
47
	 */
48
	 */
Lines 128-133 Link Here
128
    public void setInitializationData(IConfigurationElement config,
129
    public void setInitializationData(IConfigurationElement config,
129
            String propertyName, Object data) throws CoreException {
130
            String propertyName, Object data) throws CoreException {
130
        // There is nothing to do for IDEApplication
131
        // There is nothing to do for IDEApplication
132
    	// This code will never be executed but the eclipse warning will go away ;)
133
    	if (false)
134
    		throw new CoreException(null);
131
    }
135
    }
132
136
133
    /**
137
    /**
Lines 296-336 Link Here
296
     *         otherwise.
300
     *         otherwise.
297
     */
301
     */
298
    private boolean checkValidWorkspace(Shell shell, URL url) {
302
    private boolean checkValidWorkspace(Shell shell, URL url) {
299
        // a null url is not a valid workspace
303
    	boolean toBeReturned = false;
300
        if (url == null) {
304
    	// a null url is not a valid workspace
301
			return false;
305
        if (url != null) {
306
	        String version = readWorkspaceVersion(url);
307
	
308
	        // if the version could not be read, then there is not any existing
309
	        // workspace data to trample, e.g., perhaps its a new directory that
310
	        // is just starting to be used as a workspace
311
	        if (version == null) {
312
	        	toBeReturned = true;
313
			} else {
314
				final int ide_version = Integer.parseInt(WORKSPACE_VERSION_VALUE);
315
				int workspace_version = Integer.parseInt(version);
316
				// equality test is required since any version difference (newer
317
				//or older) may result in data being trampled
318
				if (workspace_version == ide_version) {
319
					toBeReturned = true;
320
				} else {
321
					// At this point workspace has been detected to be from a version
322
					// other than the current ide version -- find out if the user wants
323
					// to use it anyhow.
324
					String title = IDEWorkbenchMessages.IDEApplication_versionTitle;
325
					String message = NLS.bind(IDEWorkbenchMessages.IDEApplication_versionMessage, url.getFile());
326
					
327
					MessageBox mbox = new MessageBox(shell, SWT.OK | SWT.CANCEL
328
							| SWT.ICON_WARNING | SWT.APPLICATION_MODAL);
329
					mbox.setText(title);
330
					mbox.setMessage(message);
331
					toBeReturned = mbox.open() == SWT.OK;
332
				}
333
			}
334
        }
335
        toBeReturned &= checkLockingAllowed(shell,url);
336
        return toBeReturned;
337
    }
338
	/**
339
	 * Check if the given workspace url points to a filesystem which supports locking.
340
	 * If locking is not possible, a message is displayed asking whether
341
	 * locking should be disabled. 
342
	 * If locking is disabled, true is returned otherwise false.
343
	 * @param shell 
344
	 * @param url the workspace location
345
	 * @return true if workspace locking is possible (or disabled), false otherwise
346
	 */
347
	private boolean checkLockingAllowed(Shell shell, URL url) {
348
		// try locking
349
		File versionFile = new File(getVersionFile(url, true).getParentFile(),".lock"); //$NON-NLS-1$
350
		if (versionFile == null) {
351
			// don't know what's wrong here
352
			return true;
302
		}
353
		}
303
354
		boolean lockingAllowed = isLockingAllowed(versionFile);
304
        String version = readWorkspaceVersion(url);
355
		if (lockingAllowed) {
305
306
        // if the version could not be read, then there is not any existing
307
        // workspace data to trample, e.g., perhaps its a new directory that
308
        // is just starting to be used as a workspace
309
        if (version == null) {
310
			return true;
356
			return true;
311
		}
357
		}
312
358
		boolean disableLocking = MessageDialog.openQuestion(shell, 
313
        final int ide_version = Integer.parseInt(WORKSPACE_VERSION_VALUE);
359
			IDEWorkbenchMessages.IDEApplication_confirmOpenWorkspaceWOLockTitle, 
314
        int workspace_version = Integer.parseInt(version);
360
			IDEWorkbenchMessages.IDEApplication_confirmOpenWorkspaceWOLockMessage
315
361
		);
316
        // equality test is required since any version difference (newer
362
		if (disableLocking) {
317
        // or older) may result in data being trampled
363
			System.setProperty(PROP_OSGI_LOCKING, "none"); //$NON-NLS-1$
318
        if (workspace_version == ide_version) {
319
			return true;
364
			return true;
320
		}
365
		}
366
		// locking not allowed, let user choose another workspace
367
		return false;
368
	}
321
369
322
        // At this point workspace has been detected to be from a version
370
	/**
323
        // other than the current ide version -- find out if the user wants
371
	 * This is basically a copy of the Platforms checks
324
        // to use it anyhow.
372
	 * @see BasicLocation
325
        String title = IDEWorkbenchMessages.IDEApplication_versionTitle;
373
	 * Can't use that one here since it's not yet loaded when
326
        String message = NLS.bind(IDEWorkbenchMessages.IDEApplication_versionMessage, url.getFile());
374
	 * this method is invoked (it's also internal).
327
375
	 * 
328
        MessageBox mbox = new MessageBox(shell, SWT.OK | SWT.CANCEL
376
	 * @param lockFile The file that is locked (or tried to)
329
                | SWT.ICON_WARNING | SWT.APPLICATION_MODAL);
377
	 * @return boolean indicator
330
        mbox.setText(title);
378
	 */
331
        mbox.setMessage(message);
379
	private static boolean isLockingAllowed(File lockFile) {
332
        return mbox.open() == SWT.OK;
380
		String lockMode = System.getProperties().getProperty(PROP_OSGI_LOCKING);
333
    }
381
		if (lockMode == null)
382
			lockMode = "java.nio"; //$NON-NLS-1$
383
		if ("none".equals(lockMode)) //$NON-NLS-1$
384
			return true;
385
		else if ("java.io".equals(lockMode)) {//$NON-NLS-1${
386
			// io locking means removing the file, can't check that here!
387
			return true;
388
		} else if ("java.nio".equals(lockMode)) {//$NON-NLS-1$
389
			FileOutputStream stream = null;
390
			FileLock lock = null;
391
			try {
392
				stream = new FileOutputStream(lockFile, true);
393
				lock = stream.getChannel().tryLock();
394
			} catch (IOException e) {
395
				// ignored
396
			} finally {
397
				if (lock != null) {
398
					try {
399
						lock.release();
400
					} catch (IOException e) {
401
						// ignore
402
					}
403
				}
404
				if (stream != null) {
405
					try {
406
						stream.close();
407
					} catch (IOException e) {
408
						// ignore
409
					}
410
				}
411
			}
412
			return lock != null;
413
		}
414
		return false;
415
	}
334
416
335
    /**
417
    /**
336
     * Look at the argument URL for the workspace's version information. Return
418
     * Look at the argument URL for the workspace's version information. Return
(-)src/org/eclipse/ui/internal/ide/IDEWorkbenchMessages.java (+2 lines)
Lines 825-830 Link Here
825
	public static String IDEApplication_workspaceCannotBeSetMessage;
825
	public static String IDEApplication_workspaceCannotBeSetMessage;
826
	public static String IDEApplication_workspaceCannotLockTitle;
826
	public static String IDEApplication_workspaceCannotLockTitle;
827
	public static String IDEApplication_workspaceCannotLockMessage;
827
	public static String IDEApplication_workspaceCannotLockMessage;
828
	public static String IDEApplication_confirmOpenWorkspaceWOLockTitle;
829
	public static String IDEApplication_confirmOpenWorkspaceWOLockMessage;
828
	public static String IDEApplication_versionTitle;
830
	public static String IDEApplication_versionTitle;
829
	public static String IDEApplication_versionMessage;
831
	public static String IDEApplication_versionMessage;
830
	public static String GlobalBuildAction_BuildRunningTitle;
832
	public static String GlobalBuildAction_BuildRunningTitle;

Return to bug 59780