My file structure has a symbolic link to a directory /home/me/myDir -> /some/other/dir. This link gets updated by another process and the notifies my process. Upon notification I attempt to get the new canonical path:
public static String getPath()
{
File file = new File("/home/me/myDir");
if(file.exists())
{
try
{
String canonical = file.getCanonicalPath();
return canonical;
}
catch ...
}
}
The problem is that after the link is changed (an i have verified it changes) it is taking 3-5 times of calling the above getPath() method for to actually get the new path before that the previous path is returned. The only thing I can think of is that java might be optimizing this method and returning the old path. Any ideas or insight is greatly appreciated.
The canonical path is always an absolute and unique path. If String pathname is used to create a file object, it simply returns the pathname. This method first converts this pathname to absolute form if needed. To do that it will invoke the getAbsolutePath() Method and then maps it to its unique form.
The most issues with canonical paths occur when you are passing the name of a dir and not file. For file, if we are providing absolute path that is also the canonical path. But for dir it means omitting the last "/". For example, "/var/tmp/foo" is a canonical path while "/var/tmp/foo/" is not.
A canonical path is always an absolute path. Converting from a path to a canonical path makes it absolute (usually tack on the current working directory so e.g. ./file. txt becomes c:/temp/file. txt ).
A canonical document is a standardized representation that a document might assume while it is passing through your webMethods system. A canonical document acts as the intermediary data format between resources.
Try disabling Java's canonicalization cache. This can be done by setting the system properties sun.io.useCanonCaches
and sun.io.useCanonPrefixCache
to false.
By default, canonical file names are cached for 30 seconds (read from source here).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With