Home » Modeling » EMF » Problem Creating Resource
Problem Creating Resource [message #534494] |
Wed, 19 May 2010 11:11 |
Mathias Zenger Messages: 10 Registered: July 2009 |
Junior Member |
|
|
Hi,
I have some problems handling resources. Perhaps someone can give me some hints.
In an implementation class I am trying to serialize my model to a file called 'systems.systemmanager'. It should use standard XMI serialization but fails when trying to create the needed resource.
Probably this happens since I have not explicitly registered a resource factory. However, since I copied the code from a Ganymede project where I had no explicit registration nor a registration using extensions (except generated_package) I expected to run this successfully in my Galileo project, too.
/**
* Get a file in the plug-in installation directory.
* @generated NOT
*/
private File getFile(String name) throws IOException {
URL url = Activator.getDefault().getBundle().getEntry("/");
url = FileLocator.toFileURL(url);
return new File(url.getPath() + name);
}
/**
* Load the stored systems from file systems.systemmanager
* If the file does not exist in the plug-in's
* installation directory we create a new dummy system
* and save it in a new file.
* @generated NOT
*/
private void loadSystems() {
ResourceSet set = new ResourceSetImpl();
try {
File file = getFile("systems.systemmanager");
if (!file.createNewFile()) {
// Load contents from existing file
URI uri = URI.createFileURI(file.getAbsolutePath());
Resource res = set.getResource(uri, true);
root = (SystemManagerImpl)res.getContents().get(0);
}
else {
// Create a dummy system and save to file
SystemBase system = SystemManagerFactory.eINSTANCE.createSystemBase();
system.setName("Dummy");
root.getSystems().add(system);
saveSystems();
}
}
catch (Exception e) {
Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Loading systems failed:\n" + e.getMessage());
Activator.getDefault().getLog().log(status);
}
}
/**
* Save the systems to file systems.systemmanager
* @generated NOT
*/
public void saveSystems() {
ResourceSet set = new ResourceSetImpl();
try {
File file = getFile("systems.systemmanager");
file.createNewFile();
URI uri = URI.createFileURI(file.getAbsolutePath());
Resource res = set.createResource(uri);
res.getContents().add(root);
res.save(Collections.EMPTY_MAP);
}
catch (Exception e) {
Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Saving systems failed:\n" + e.getMessage());
Activator.getDefault().getLog().log(status);
}
}
Concrete failure in saveSystems():
Resource res = set.createResource(uri);
// returns null
Concrete failure in loadSystems():
Resource res = set.getResource(uri, true);
// exception: "a registered resource factory is needed"
After all I read about factory resolving I thought it should use a default one for XMI. Am I wrong? Or is maybe a plug-in in my target missing?
Thanks for your advice!
|
|
|
Re: Problem Creating Resource [message #534512 is a reply to message #534494] |
Wed, 19 May 2010 11:37 |
|
Hi Mathias,
You seem to be creating a file inside a bundle at runtime. I think
that's generally not good practice. Should you be using the state
location or the workspace?
If you create the EMF resource with ResourceSet.createResource()
followed by Resource.save() you don't need to create an empty file in
advance. BTW. I think that file must not be entirely empty. You'd need
some minimum content. It's better to go the recommended way, as I
outlined above.
Cheers
/Eike
----
http://thegordian.blogspot.com
http://twitter.com/eikestepper
Am 19.05.2010 13:11, schrieb Mathias Zenger:
> Hi,
>
> I have some problems handling resources. Perhaps someone can give me
> some hints.
>
> In an implementation class I am trying to serialize my model to a file
> called 'systems.systemmanager'. It should use standard XMI
> serialization but fails when trying to create the needed resource.
> Probably this happens since I have not explicitly registered a
> resource factory. However, since I copied the code from a Ganymede
> project where I had no explicit registration nor a registration using
> extensions (except generated_package) I expected to run this
> successfully in my Galileo project, too.
>
> /**
> * Get a file in the plug-in installation directory.
> * @generated NOT
> */
> private File getFile(String name) throws IOException {
> URL url = Activator.getDefault().getBundle().getEntry("/");
> url = FileLocator.toFileURL(url);
> return new File(url.getPath() + name);
> }
>
> /**
> * Load the stored systems from file systems.systemmanager
> * If the file does not exist in the plug-in's
> * installation directory we create a new dummy system
> * and save it in a new file.
> * @generated NOT
> */
> private void loadSystems() {
> ResourceSet set = new ResourceSetImpl();
> try {
> File file = getFile("systems.systemmanager");
> if (!file.createNewFile()) {
> // Load contents from existing file
> URI uri = URI.createFileURI(file.getAbsolutePath());
> Resource res = set.getResource(uri, true);
> root = (SystemManagerImpl)res.getContents().get(0);
> }
> else {
> // Create a dummy system and save to file
> SystemBase system =
> SystemManagerFactory.eINSTANCE.createSystemBase();
> system.setName("Dummy");
> root.getSystems().add(system);
> saveSystems();
> }
> }
> catch (Exception e) {
> Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID,
> "Loading systems failed:\n" + e.getMessage());
> Activator.getDefault().getLog().log(status);
> }
> }
>
> /**
> * Save the systems to file systems.systemmanager
> * @generated NOT
> */
> public void saveSystems() {
> ResourceSet set = new ResourceSetImpl();
> try {
> File file = getFile("systems.systemmanager");
> file.createNewFile();
> URI uri = URI.createFileURI(file.getAbsolutePath());
> Resource res = set.createResource(uri);
> res.getContents().add(root);
> res.save(Collections.EMPTY_MAP);
> }
> catch (Exception e) {
> Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID,
> "Saving systems failed:\n" + e.getMessage());
> Activator.getDefault().getLog().log(status);
> }
> }
> Concrete failure in saveSystems():
> Resource res = set.createResource(uri);
> // returns null
>
> Concrete failure in loadSystems():
> Resource res = set.getResource(uri, true);
> // exception: "a registered resource factory is needed"
>
> After all I read about factory resolving I thought it should use a
> default one for XMI. Am I wrong? Or is maybe a plug-in in my target
> missing?
>
> Thanks for your advice!
Cheers
/Eike
----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper
|
|
| |
Re: Problem Creating Resource [message #534577 is a reply to message #534559] |
Wed, 19 May 2010 14:49 |
Ed Merks Messages: 33141 Registered: July 2009 |
Senior Member |
|
|
This is a multi-part message in MIME format.
--------------060808050801000009050208
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Mathias,
Some things to note.
1. EMF works with URIs in general, (http:, jar:, file:, bundleentry:,
platform:) so there's no need convert to a java.io.File.
2. You should never need to cast to an AbcImpl class; use Abc, i.e.,
SystemManager not SystemManagerImpl.
3. I imagine your model's plugin.xml has a resource factory
registration for the "systemmanage" file extension.
4. Think hard about where you're storing your data. As Eike suggests
storing it in the installed bundle's location can't possibly be a
good idea.
Mathias Zenger wrote:
> Dear Eike,
>
> Thanks for the reply. I have an RCP application and used EMF to
> generate the data model. I just need a way to store/restore the
> objects my application is dealing with. The basic idea was to use XMI
> serialization and save the model in a file located in the application
> root directory. I don't know if I got you right but do you think
> leaving away file creation solves the potential factory issue?
> However, I'll give it a try tomorrow and check if the empty file
> causes a problem. For me the whole material is quite new, sorry :)
> Mathias
--------------060808050801000009050208
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Mathias,<br>
<br>
Some things to note. <br>
<ol>
<li>EMF works with URIs in general, (http:, jar:, file:,
bundleentry:, platform:) so there's no need convert to a java.io.File. <br>
</li>
<li>You should never need to cast to an AbcImpl class; use Abc, i.e.,
SystemManager not SystemManagerImpl.</li>
<li>I imagine your model's plugin.xml has a resource factory
registration for the "systemmanage" file extension.</li>
<li>Think hard about where you're storing your data. As Eike
suggests storing it in the installed bundle's location can't possibly
be a good idea.<br>
</li>
</ol>
<br>
<br>
Mathias Zenger wrote:
<blockquote cite="mid:ht0sec$38f$1@build.eclipse.org" type="cite">Dear
Eike,
<br>
<br>
Thanks for the reply. I have an RCP application and used EMF to
generate the data model. I just need a way to store/restore the objects
my application is dealing with. The basic idea was to use XMI
serialization and save the model in a file located in the application
root directory. I don't know if I got you right but do you think
leaving away file creation solves the potential factory issue? However,
I'll give it a try tomorrow and check if the empty file causes a
problem. For me the whole material is quite new, sorry :) <br>
Mathias
<br>
</blockquote>
</body>
</html>
--------------060808050801000009050208--
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Goto Forum:
Current Time: Fri Apr 26 00:33:23 GMT 2024
Powered by FUDForum. Page generated in 0.03418 seconds
|