1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-18 21:55:45 +02:00

[368454] provide thread safety for cachedRemoteFiles hashmap

This commit is contained in:
David McKnight 2012-01-12 14:51:26 +00:00
parent 49423f4665
commit 065065194f
2 changed files with 53 additions and 33 deletions

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2010 IBM Corporation and others. * Copyright (c) 2006, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -46,6 +46,7 @@
* David McKnight (IBM) - [272882] [api] Handle exceptions in IService.initService() * David McKnight (IBM) - [272882] [api] Handle exceptions in IService.initService()
* David McKnight (IBM) - [244041] [files] Renaming a file looses Encoding property * David McKnight (IBM) - [244041] [files] Renaming a file looses Encoding property
* David McKnight (IBM) - [320713] [dstore] xml file transfer error on zOS * David McKnight (IBM) - [320713] [dstore] xml file transfer error on zOS
* David McKnight (IBM) - [368454] provide thread safety for cachedRemoteFiles hashmap
*******************************************************************************/ *******************************************************************************/
package org.eclipse.rse.subsystems.files.core.servicesubsystem; package org.eclipse.rse.subsystems.files.core.servicesubsystem;
@ -1154,7 +1155,9 @@ public class FileServiceSubSystem extends RemoteFileSubSystem implements IFileSe
IHost host = getHost(); IHost host = getHost();
IFileServiceSubSystemConfiguration config = (IFileServiceSubSystemConfiguration) newConfig; IFileServiceSubSystemConfiguration config = (IFileServiceSubSystemConfiguration) newConfig;
// file subsystem specific bits // file subsystem specific bits
_cachedRemoteFiles.clear(); synchronized (_cachedRemoteFiles){
_cachedRemoteFiles.clear();
}
_languageUtilityFactory = null; _languageUtilityFactory = null;
setFileService(config.getFileService(host)); setFileService(config.getFileService(host));
setHostFileToRemoteFileAdapter(config.getHostFileAdapter()); setHostFileToRemoteFileAdapter(config.getHostFileAdapter());

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2002, 2009 IBM Corporation and others. * Copyright (c) 2002, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -29,6 +29,7 @@
* Martin Oberhuber (Wind River) - [226574][api] Add ISubSystemConfiguration#supportsEncoding() * Martin Oberhuber (Wind River) - [226574][api] Add ISubSystemConfiguration#supportsEncoding()
* David McKnight (IBM) - [225747] [dstore] Trying to connect to an "Offline" system throws an NPE * David McKnight (IBM) - [225747] [dstore] Trying to connect to an "Offline" system throws an NPE
* David McKnight (IBM) - [272882] [api] Handle exceptions in IService.initService() * David McKnight (IBM) - [272882] [api] Handle exceptions in IService.initService()
* David McKnight (IBM) - [368454] provide thread safety for cachedRemoteFiles hashmap
*******************************************************************************/ *******************************************************************************/
package org.eclipse.rse.subsystems.files.core.subsystems; package org.eclipse.rse.subsystems.files.core.subsystems;
@ -1271,35 +1272,43 @@ public abstract class RemoteFileSubSystem extends SubSystem implements IRemoteFi
*/ */
public void cacheRemoteFile(IRemoteFile file, String path) public void cacheRemoteFile(IRemoteFile file, String path)
{ {
boolean containsKey = false;
if (_cachedRemoteFiles.containsKey(path)) synchronized (_cachedRemoteFiles){
{ containsKey = _cachedRemoteFiles.containsKey(path);
IRemoteFile oldFile = (IRemoteFile)_cachedRemoteFiles.remove(path); }
if (oldFile == file) if (containsKey){
{ IRemoteFile oldFile = null;
synchronized (_cachedRemoteFiles){
oldFile = (IRemoteFile)_cachedRemoteFiles.remove(path);
}
if (oldFile == file){
// already cached - recache // already cached - recache
_cachedRemoteFiles.put(path, file); synchronized (_cachedRemoteFiles){
_cachedRemoteFiles.put(path, file);
}
return; return;
} }
// replace file under parent // replace file under parent
if (oldFile instanceof RemoteFile) { if (oldFile instanceof RemoteFile) {
RemoteFile roldFile = (RemoteFile)oldFile; RemoteFile roldFile = (RemoteFile)oldFile;
if (roldFile._parentFile != null) // prevent parent query from bug #196664 if (roldFile._parentFile != null){ // prevent parent query from bug #196664
{
roldFile._parentFile.replaceContent(oldFile, file); roldFile._parentFile.replaceContent(oldFile, file);
} }
} }
else if (oldFile != null && oldFile.getParentRemoteFile() != null) { else if (oldFile != null && oldFile.getParentRemoteFile() != null) {
oldFile.getParentRemoteFile().replaceContent(oldFile, file); oldFile.getParentRemoteFile().replaceContent(oldFile, file);
} }
// preserve persistent information from old file to new // preserve persistent information from old file to new
if (oldFile != null) if (oldFile != null){
oldFile.copyContentsTo(file); oldFile.copyContentsTo(file);
}
}
synchronized (_cachedRemoteFiles){
_cachedRemoteFiles.put(path, file);
} }
_cachedRemoteFiles.put(path, file);
} }
/** /**
@ -1321,18 +1330,19 @@ public abstract class RemoteFileSubSystem extends SubSystem implements IRemoteFi
*/ */
public IRemoteFile getCachedRemoteFile(String path) public IRemoteFile getCachedRemoteFile(String path)
{ {
if (_cachedRemoteFiles.size() > 0) synchronized (_cachedRemoteFiles){
{ if (_cachedRemoteFiles.size() > 0)
path = path.replaceAll("//", "/"); //$NON-NLS-1$ //$NON-NLS-2$ {
if (path.endsWith("\\") || (path.endsWith("/") && path.length() > 1)) //$NON-NLS-1$ //$NON-NLS-2$ path = path.replaceAll("//", "/"); //$NON-NLS-1$ //$NON-NLS-2$
{ if (path.endsWith("\\") || (path.endsWith("/") && path.length() > 1)) //$NON-NLS-1$ //$NON-NLS-2$
path = path.substring(0, path.length() - 1); {
} path = path.substring(0, path.length() - 1);
if (_cachedRemoteFiles.containsKey(path)) }
{ if (_cachedRemoteFiles.containsKey(path))
{return (IRemoteFile)_cachedRemoteFiles.get(path);} {
} {return (IRemoteFile)_cachedRemoteFiles.get(path);}
}
}
} }
return null; return null;
} }
@ -1356,7 +1366,10 @@ public abstract class RemoteFileSubSystem extends SubSystem implements IRemoteFi
//If getContents() is implemented correctly, no matches should be removed //If getContents() is implemented correctly, no matches should be removed
String prefix = file.getAbsolutePath() + file.getSeparator(); String prefix = file.getAbsolutePath() + file.getSeparator();
//Clone the hashMap in order to avoid ConcurrentModificationException in the iterator //Clone the hashMap in order to avoid ConcurrentModificationException in the iterator
HashMap tmpMap = (HashMap)_cachedRemoteFiles.clone(); HashMap tmpMap = null;
synchronized (_cachedRemoteFiles){
tmpMap = (HashMap)_cachedRemoteFiles.clone();
}
Iterator it = tmpMap.keySet().iterator(); Iterator it = tmpMap.keySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
String remotePath = (String)it.next(); String remotePath = (String)it.next();
@ -1367,13 +1380,15 @@ public abstract class RemoteFileSubSystem extends SubSystem implements IRemoteFi
} }
} }
_cachedRemoteFiles.remove(file.getAbsolutePath()); removeCachedRemoteFile(file.getAbsolutePath());
} }
} }
protected void removeCachedRemoteFile(String path) protected void removeCachedRemoteFile(String path)
{ {
_cachedRemoteFiles.remove(path); synchronized (_cachedRemoteFiles){
_cachedRemoteFiles.remove(path);
}
} }
@ -1382,7 +1397,9 @@ public abstract class RemoteFileSubSystem extends SubSystem implements IRemoteFi
switch (e.getState()) switch (e.getState())
{ {
case CommunicationsEvent.AFTER_DISCONNECT : case CommunicationsEvent.AFTER_DISCONNECT :
_cachedRemoteFiles.clear(); synchronized (_cachedRemoteFiles){
_cachedRemoteFiles.clear();
}
// DKM - taking this out because it causes an exception when the event occurs in Modal Context // DKM - taking this out because it causes an exception when the event occurs in Modal Context
//ISystemRegistry sr = RSECorePlugin.getTheSystemRegistry(); //ISystemRegistry sr = RSECorePlugin.getTheSystemRegistry();
//sr.connectedStatusChange(this, false, true, true); //sr.connectedStatusChange(this, false, true, true);