Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a log file which is locked by another application

Tags:

java

I want to access a log file that is locked by a third party Java application. The file is locked for the whole day and will be released the next day. However, my objective is to read it now using RandomAccessFile (must use this class because I need to start/store the last position while reading) without waiting until tomorrow.

Currently, I can read the log only if I unlock it with a file Unlocker software. Can anyone suggest any jar/utilities that I can use in my Java program to meet my objective?

like image 678
Joe Ijam Avatar asked Nov 10 '11 09:11

Joe Ijam


2 Answers

Assuming you're using a Microsoft operating system:
The software Shadow Copy is using using Microsoft's volume-shadow-service (VSS) to copy locked files. You could use the software from within the Java Runtime Environment or perhaps make use of the VSS-API yourself via Java Native Interface.

My approach would be to shadow-copy the file and then access the content through it's copy. The downside is that you're possibly reading outdated information if the file has been updated since your copy operation.

However, this is just a guess as I'm not familiar with this topic.

like image 132
xmoex Avatar answered Oct 04 '22 14:10

xmoex


You can lock/unlock files and folders in Java but only by application that locked them (you programmed). However there is no Java method/class which can unlock file used by other process.

You should bundle your application with another (native) software. For example you could create shell script for Linux systems and execute it. In Java application detect in which OS it is running so you can execute proper script/software.

When application requires RW lock, system must ensure that no one else have rights to modify it, thats why you need to kill process that is using it.

If you have access to source code of that 3rd party Java application (that is actually locking file you need), then you could implement server side which will listen requests for unlocking file and approval for locking it back again.

By my opinion better approach would be to transfer file by that application to yours, then do what you want and 3rd party app can run without interruption (shouldn't be noticeable). If you need to modify it, then 3rd should wait, your modifies and send back an updated version, 3rd continue to work.

like image 33
DRAX Avatar answered Oct 04 '22 15:10

DRAX