Eclipse RCP local history is empty - eclipse

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);

Related

What is the API for Show in System Explorer in Eclipse Platform

I want to know whether there is any platform independent Eclipse platform API as part of IResource to open the folder or directory in Explorer. As part of eclipse plugin development, I know that it can be developed specific to OS like Windows, Mac or Linux. But I am interested to know any such functionality of Eclipse platform. Eclipse also provides such functionality as part of IDE to Show in System Explorer. You can see the following image. Basically I am interested to know about any such IResource API. In windows we can execute the command from command prompt to open the folder in explorer using explorer.exe . Please suggest me. I have also gone through the following SO links.
Eclipse show project in explorer
In eclipse, reveal current file in filesystem
This is the code that the Resource Info page uses:
ECommandService commandService = PlatformUI.getWorkbench().getService(ECommandService.class);
EHandlerService handlerService = PlatformUI.getWorkbench().getService(EHandlerService.class);
Command command = commandService.getCommand(ShowInSystemExplorerHandler.ID);
if (command.isDefined()) {
ParameterizedCommand parameterizedCommand = commandService
.createCommand(ShowInSystemExplorerHandler.ID, Collections.singletonMap(
ShowInSystemExplorerHandler.RESOURCE_PATH_PARAMETER, locationStr));
if (handlerService.canExecute(parameterizedCommand)) {
handlerService.executeHandler(parameterizedCommand);
}
}
where locationStr is the full path of the resource.
Note the ShowInSystemExplorerHandler class is internal so accessing the static fields is really against the rules, but should be OK here or you can just copy the strings to your own constants.
You could call the Show in System Explorer command which has the ID org.eclipse.ui.ide.showInSystemExplorer for that.

Eclipse: How to Create "Linked Resource" not in .projects Directory?

I am using the Eclipse RCP for Developers to create a version of the Workbench that's specialized to the development we do at work. Most of this has already been done, but I am fine-tuning now.
To initialize a project, users have to create a "preferences" file, which contains various settings. These settings include the "paths" for the code/files they want the project to load. Until now, users have had to nest all relevant code/files in the directory that contains the preferences file, like so:
./folder/file.prefs
./folder/nestedFolder/
./folder/nestedFolder2
(And so on.)
However, I'd like for users to be able to provide relative paths for folders/files not in the home directory, like so:
../../randomFolder/
The only problem I've seen (this works fine for integrating the code/file itself) is getting it to "show up" in the package explorer tool. To do this, you have to create a linked resource (as I already wrote code to do, for nested directories).
However, when I try to link to non-nested folders, I get this error:
ResourceException: Could not created linked resource {path}. Parent resource not accessible.
My code looks like this:
for (String path: FolderPaths) {
IFolder folder = project.getFolder(path)
if (!folder.exists()) {
folder.createLink(pathBase.append(path), IResource.FOLDER, nullProgressMonitor)
}
}
"pathBase" is an "IPath" pointing to the home directory's full-path.
Note that even if the FolderPath "path" is relative to the original prefs file:
../test/folder
Creating an IFolder with the "project.getFolder(path)" method changes it to:
/test/folder
Presumably, this is because the "workspace" is now attempting to create a project folder in the local directory, then link that folder to the path I pass in; this has always worked in the past, but now I get the error above.

Servlet cannot find the file I'm trying to open

I read that the servlets map the current location based on the url. Clicking a button from my Home.jsp page directs me to my servlet, ExcelUploader. The URL when said button is clicked is
http://localhost:8080/ServletExample/ExcelUploader
I'm trying to open an excel file located in the same folder as my JSP. so that means I have to move one folder up relative to the url above. I have this in my servlet:
InputStream inp = new FileInputStream("../OpenMe.xls");
However I'm still getting a
java.io.FileNotFoundException: ..\OpenMe.xls (The system cannot find the file specified)
This is how my project is setup:
The FileInputStream operates on the local disk file system relative to the working directory and knows absolutely nothing about the fact that it's invoked from a Java EE web application. Any relative path you pass to it is relative to the folder which was been opened at the moment the command to start the server is executed. This is often the server's own installation folder, but in case of an IDE this can also be project's own root folder. This variable is not controllable from inside your Java code. You should not be relying on that.
You've stored the file as a resource of the public webcontent. So it's available as a webcontent resource by ServletContext#getResourceAsStream() which returns an InputStream. If you have absolutely a legitimate reason to invoke the servlet by its URL instead of just using the file's own URL http://localhost:8080/ServletExample/OpenMe.xls, then you should be getting at as follows:
InputStream input = getServletContext().getResourceAsStream("/OpenMe.xls");
// ...
If your intent is indeed to restrict the file's access to by the servlet only, you might want to consider to move the file into the /WEB-INF folder, so that the enduser can never open it directly by entering the file's own URL. You only need to change the resource path accordingly.
InputStream input = getServletContext().getResourceAsStream("/WEB-INF/OpenMe.xls");
// ...
You should not be using getRealPath() as suggested by the other answer. This won't work when the servletcontainer is configured to expand the WAR file into memory instead of into local disk file system, which is often the case in 3rd party hosts. It would return null then.
See also:
getResourceAsStream() vs FileInputStream
Paths for files that live in the webtree have to be "translated" using getRealPath before they are usable, like this:
File excelFile = new File(getServletContext().getRealPath("/OpenMe.xls"));
While you're at it, using the default package isn't a good idea, create a package for your files.

How to load an IFile and modify the same in eclipse plug-in development

I have an IFile shows the absolute path as
/dynamic/WebContent/testing.html
I want to make some changes in the above testing.html.
It means I need to load the file, then need to modify it and then save it .
What is the procedure to execute the above steps in eclipse plug-in development.
See IFile.getContents(), setContents() and create() methods.
Use getContents to read file into memory, modify it, then use setContents to write it out. That will keep your workspace in sync. If you try to convert to File and use standard Java file I/O, your workspace will be out of sync (requiring refresh) and you will be completely incompatible with other file storage systems used by Eclipse
If your file is local try :
import java.io.RandomAccessFile;
RandomAccessFile file = new RandomAccessFile("IFile".getLocation().toFile(), "rwd");

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();