Is it possible to get a resource from a feature? - eclipse

I can access a resource in a plugin
Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
URL fileURL = bundle.getEntry("resources/resource.txt");
Is it possible to do something similar using a feature?

Related

How to read the .xml file path in Eclipse rcp application using java?

I have find the solution.This solution will help me to read file from appropriate folder path.
**
String resourceFolder=""; URL url=new
URL("platform:/plugin/com.example"+resourceFolder);
String resourceFolderPath=org.eclipse.core.runtime.FileLocator.toFileURL(url).getPath();
resourceFile=resourceFolderPath+"xxxx.xml";
**
Is there any other way to find it?
FileLocator is the correct API to use to access plugin resources.
Use FileLocator.find to get a URL to a resource:
URL url = FileLocator.find(bundle, new Path("relative path in plugin"), null);
bundle is the Bundle for the plugin. For the current plugin you can get this using:
Bundle bundle = FrameworkUtil.getBundle(getClass());
to get the bundle of a different plugin use:
Bundle bundle = Platform.getBundle("plugin id");
The URL returned by find is an bundleentry URL. You can use openStream on this to read to resource.
If you want a file URL you can convert the url using:
URL fileURL = FileLocator.toFileURL(url);
This may cause Eclipse to copy the resource in to a file in a temporary location.

How to set .realm file on realm

I'm developing an app using realm. then I want to initialize realm by a set .realm file on the app. It is initially on /Users/*****/Library/Developer/CoreSimulator/Devices/~****~/Documents/default.realm/
I want the default.realm file on the app folder aligned with storyboard and ViewController.swift and other files.
How can I solve this?
You can pass a fileURL in the Realm init method
let config = Realm.Configuration(
// Get the URL to the bundled file
fileURL: Bundle.main.url(forResource: "MyBundledData", withExtension: "realm"),
// Open the file in read-only mode as application bundles are not writeable
readOnly: true)
// Open the Realm with the configuration
let realm = try! Realm(configuration: config)
See the documentation for more information. There are more things you can configure here too.
Important note: See the comment in the code above:
Open the file in read-only mode as application bundles are not writeable
To have the realm file sitting in your bundle, you would not be able to write to it, it would be read only. If you want to ship a pre-filled database with your app you can do this in your bundle but if you want it to be writeable you would need to copy it to the documents directory of your app on first launch and then use the version in that directory instead.
resources/files that you add in xcode and include in the 'copy bundle resources' in the build settings are included in your bundle. The bundle is not writeable. To ship a file with your app that you need to be writeable would need to be moved to a suitable folder. usually the app documents folder.

can't access play-scala public folder resources

For my play-scala project, My routes file has:
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
however, I am not able to retrieve any resource from this folder. For example, I placed a png file calleg favicon.png in the public folder, yet, if I run
http://localhost:9000/public/favicon.png
the play server can't find the resource. Any ideas about what to look for to repair this problem?
The server also says that tried the route:
GET/assets/$file<.+>controllers.Assets.versioned(path:String = "/public", file:Asset)
But no luck.
You are reading your configuration wrong, the correct request would look like this:
http://localhost:9000/assets/favicon.png
That way favicon.png will be searched in public folder, not another way around
If you want your original URL to work, you'll need to change your configuration to following:
GET /public/*file controllers.Assets.versioned(path="/public", file: Asset)

Eclipse RCP local history is empty

I'm trying to get the local history for a file, but its always empty:
FileSystemResourceManager fsm =
((org.eclipse.core.internal.resources.Workspace)ResourcesPlugin.getWorkspace()).getFileSystemManager();
IFileState[] fileStates =
fsm.getHistoryStore().getStates(myFile.getFullPath(),null);
In the .metadata/.plugins/org.eclipse.core.resources/.history/ folder I can find the items for the file, but I cant reach them via code.
The APIs you are using are internal use only and are not part of the Eclipse API, see Eclipse API Rules of Engagement.
You get the IFileState from an IFile object using the getHistory method.
IFile file = get ifile
IFileState [] states = file.getHistory(progress monitor);

How to get the eclipse installation/plugins directory or path

How to get the Eclipse installation directory through programming in swt/java.
I actually want to get the plugins directory of the eclipse.
Update (January 2012)
James Moore mentions in the comments that the FAQ and API are quite old.
FileLocator.resolve(URL) is now preferred to the deprecated Platform.resolve().
As this example shows, you need to pass the actual resource (here a bundle), not the name of the resource, in order to resolve it:
private static URI locateFile(String bundle, String fullPath) {
try {
URL url = FileLocator.find(Platform.getBundle(bundle), new Path(fullPath), null);
if(url != null)
return FileLocator.resolve(url).toURI();
} catch (Exception e) {}
return null;
}
}
See also "How to refer a file from jar file in eclipse plugin" for more.
Original answer (January 2011)
Maybe the FAQ "How do I find out the install location of a plug-in?" can help here:
You should generally avoid making assumptions about the location of a plug-in at runtime.
To find resources, such as images, that are stored in your plug-in’s install directory, you can use URLs provided by the Platform class. These URLs use a special Eclipse Platform protocol, but if you are using them only to read files, it does not matter.
In Eclipse 3.1 and earlier, the following snippet opens an input stream on a file called sample.gif located in a subdirectory, called icons, of a plug-in’s install directory:
Bundle bundle = Platform.getBundle(yourPluginId);
Path path = new Path("icons/sample.gif");
URL fileURL = Platform.find(bundle, path);
InputStream in = fileURL.openStream();
If you need to know the file system location of a plug-in, you need to use Platform.resolve(URL). This method converts a platform URL to a standard URL protocol, such as HyperText Transfer Protocol (HTTP), or file.
Note that the Eclipse Platform does not specify that plug-ins must exist in the local file system, so you cannot rely on this method’s returning a file system URL under all circumstances in the future.
In Eclipse 3.2, the preferred method seems to be:
Bundle bundle = Platform.getBundle(yourPluginId);
Path path = new Path("icons/sample.gif");
URL fileURL = FileLocator.find(bundle, path, null);
InputStream in = fileURL.openStream();
Use the below code to get the plugin path :
this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();