How automatically update EMF ResourceSet on file change?
I need to have always up to date content of some file in my program. I've created an EMF resource set.
Because resourceSet.getResource(resourceURI, true)
takes a lot of time to complete I store the resource set in a static field, so files can be cached.
I.e. once resourceSet.getResource(resourceURI, true)
is called for some 开发者_开发百科URI the file is cached in resourceSet
.
The problem is that resourceSet
doesn't update it's cache automatically:
I.e.:
resourceSet.getResource(resourceURI, true);
// delete resourceURI from file system
// Here I expect null, but old version of the file is returned
resourceSet.getResource(resourceURI, true);
How to force resourceSet
to update cache if needed?
I'm using org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
, but probably I need another version of ResourceSet
that takes modification stamps into account?
Two things: first if you want to reload the resource, you'll have to call
aResource.unload([..]);
aResource.load([..])
As EMF is not requiring Eclipse in any way the Resource
and ResourceSet
classes are not the same as the Eclipse workspace IResource
subclasses, which mean changing a file on the file system will not cause the EMF Resources to be reloaded.
It's pretty easy to do though, have a look on the XxxxEditor
EMF generated for you, the class instantiate a IResourceChangeListener
which will receive deltas from the Eclipse workspace when a file is being modified. The generated listener process these deltas by reloading the resources.
protected IResourceChangeListener resourceChangeListener = new IResourceChangeListener() {
public void resourceChanged(IResourceChangeEvent event) {
IResourceDelta delta = event.getDelta();
//find out which EMF Resource matches with the IResource and reload it
}
}
精彩评论