Plugin Development, where to save user data? - java

I am developing a plugin for Eclipse. When the user runs the application for the first time, calibration takes place. This is where user data is taken and then printed out to text files, so that when used again, the system will remember their settings.
I was wondering where is the best place to store these text files on a user's system?

There are different options with different scopes:
per installation: see the post, create a unique folder under the mentioned path and store the files there
per user: create a unique folder under the users home directory (System.getProperty("user.dir")) and store the files there
per workspace: Plugin::getStateLocation returns a path that is unique to the given plug-in for each workspace. Store your files there. Plugin is an abstract class that is implemented by the Activator or your plug-in. If you don't have such class yet, create it by specifying the Activator in the manifest editor. Alternatively you can use Platform::getStateLocation() if you have a reference to the Bundle that represents your plug-in.

A common place to store such data is the
AppData\Local\YourPluginName
directory.
You may also use the
eclipse/plugins/YourPlugin
directory.

Your plugin can store data in the workspace metadata in the plugin 'state location'
Use something like:
Bundle bundle = FrameworkUtil.getBundle(getClass());
IPath stateLoc = Platform.getStateLocation(bundle);
stateLoc will be a directory where your plugin can store whatever it likes. The location will normally be '.metadata/.plugins/your plugin id' in the workspace.

Related

Save file info for processing not seen by user in Eclipse

I am working on a project in Eclipse wherein I need to save extra information attached with file and some derived from files such that user can't see them directly. For e.g., after loading file, some process is done on that file and I need to save result of that process such that I can use it later on.
The information that I need to store can be long strings and in form of bytes too.
Is there any feature in Eclipse using which I can do this task?
I am using Eclipse 4.5.2
You can save strings using the setPersistentProperty and getPersistentProperty methods of IFile.
You can also save any sort of data you want in the 'state location' for your plugin.
Get the state location with:
Bundle bundle = FrameworkUtil.getBundle(getClass());
IPath location = Platform.getStateLocation(bundle);
This is a directory in the workspace .metadata.

Where does the EMF Client Platform store local files?

According to this acrticle , the EMF Client Platform demo application stores the objects you create either in an EMFstore or in local files. As I do not have an EMFstore, I thought the application would store the information in .xmi files or something like that. Whenever I close the application and relaunch it, my created projects and objects are loaded correctly. But I can't find any files in my workspace! And I am sure that I'm looking inside the correct workspace because I use the -data flag to launcht the application and the .metadata folder was created successfully. Any ideas?
I'm not sure if this is what you are looking for, but my created objects are stored in a folder named runtime-EclipseApplication side by side with the workspace folder. I assume your "created objects" is referring to the modelInstances like "TournamentPro" or "League" in the given example article. If you do not have these folders, you should check again if you really do not have an EMFstore.

Where is application-scope location of resources in Eclipse RCP?

I am wrapping some 3rd party application into Eclipse plugin.
This application uses some data files in "current" directory, i.e. directory where application ran from.
Initially I put all of theses files at first level in Eclipse plugin project and can access these files via
bundle.getEntry("./conf/conf.xml")
unfortunately, this code creates strange URLs like
bundleentry://88.fwk1692538903/./conf/conf.xml
Why is it so? Is it possible to put all application-scoped files into some global location and refer it from a plugin?
Use org.eclipse.core.runtime.FileLocator to convert the bundleentry URL to a normal file URL:
URL bundleURL = bundle.getEntry("...");
URL fileURL = FileLocator.toFileURL(bundleURL);
Note: If the plugin is in a jar rather than expanded during install the URL returned may reference a temporary location in the metadata where the plugin has been expanded.

Storing configuration file of an application in OS independent path

I am writing an application (basically eclipse plugin), so there are few combo-box, drop-downs etc, which I am getting values for them dynamically via XML file. My doubt is which is the best way to store these files in a particular directory so that it can be read in both Windows as well as Linux.
Initially I tried to create a config path under {eclipse.home.location} like:
String finalPath = System.getProperty("eclipse.home.location") +"/myAppConfig";
and store all of my plugin's configuration there (not only configuration but few helper jars which I programatically read in my plugin). But there is a probabilty that Eclipse installation maybe in shared location and user may not have write access to it.
I also tried to store it in a program files directory using:
System.getenv("ProgramFiles");
But this fails under non-windows environments. So my question is can anyone shed a light on this so that I can store in some common directory where it is valid for both windows and linux?
Kindly let me know if my wordings are confusing. Or is it possible to store my config files under plugins directory and get the path like this /plugins/myConfigDir ?
Try using the getStateLocation() method in Plugin.
That will give you an IPath that points to a user specific workspace location.

Java on MacOSX: how to access files in the bundle

I have a Java app which I am packaging to a Mac Application Bundle (That folder structure that contains all of the app but looks like a single executable file to the user).
My Problem:
I am reading and writing some config files in the local folder ("."). However, on Mac this seems to be the folder in which the application bundle is located (so usually the "Applications" folder and I obviously don't want that.
My question:
How can I store a file inside that bundle? How can I programmatically retrieve the bundle name to compute the fully qualified folder?
I know I could try to go the ClassLoader way, but I'd like to avoid that (for security reasons).
Or is there simply a better way how to store application cache and config data locally?
The Mac OS X Finder treats any directory whose name ends in .app as an application; right-click to Show Package Contents. It remains an otherwise normal directory for I/O purposes. This project is an example. See this answer regarding paths relative to the application bundle.
Addendum: Is there a better way how to store application cache and config data locally?
The example cited uses java.util.prefs.Preferences, but javax.jnlp.PersistenceService is an alternative.
Ok, the basic answer / solution is: don't do it.
The reason I originally wanted to do it was to cache larger amounts of data on the local HD. Java preferences are a good choice for config data (i.e. small data amounts) but fail to handle data in the megabyte size range.
My solution:
On MacOSX (System.getProperty("os.name").contains("Mac OS X")) I simply create a folder in the user's home folder (System.getProperty("user.home")). I prefix that folder with a . to ensure it is hidden from the user. This also ensures that I have write access to the folder (which could be a problem in the .app folder depending on where the user copies it)
On Windows (System.getProperty("os.name").contains("Windows")) I create that folder in the System.getenv("APPDATA") directory (note that this env variable only exists on Windows systems.
Now I have full access to the filesystem (even without admin rights) and can store as much data as I like.

Categories