Our vBulletin migration is complete.
Welcome vBulletin users! All content and user information from the Micro Focus Forums (vBulletin) site has been migrated to this site. READ MORE.
Knowledge Partner
Knowledge Partner
400 views

AJC - writeLog function not thread safe?

In the AJC ecmascript library that is included as a package in IDM4
there is a comment related to the writeLog function. I'm assuming the
comment is from Shon Vella (as he's listed as the person who ported this
from Java). The comment states "Unlike the original AJC, this function
is not thread-safe - I think this can be fixed"

My questions are (especially to Shon), what makes it not thread safe?
For very low volume usage is it risky to use this? Also, any suggestions
on how can this be fixed?

I'm not much of a Java programmer, so wondering if anyone else can comment.
Alex McHugh - Knowledge Partner - Stavanger, Norway
Who are the Knowledge Partners
If you appreciate my comments, please click the Like button.
If I have resolved your issue, please click the Accept as Solution button.
Labels (1)
0 Likes
8 Replies
Knowledge Partner
Knowledge Partner

Re: AJC - writeLog function not thread safe?

On 1/19/2012 9:25 AM, Alex McHugh wrote:
> In the AJC ecmascript library that is included as a package in IDM4
> there is a comment related to the writeLog function. I'm assuming the
> comment is from Shon Vella (as he's listed as the person who ported this
> from Java). The comment states "Unlike the original AJC, this function
> is not thread-safe - I think this can be fixed"
>
> My questions are (especially to Shon), what makes it not thread safe?
> For very low volume usage is it risky to use this? Also, any suggestions
> on how can this be fixed?


Shon alas, has moved on to another company, and stopped monitoring the
forum, which really is too bad. He is the most awesome resource we have
had in the forum.

> I'm not much of a Java programmer, so wondering if anyone else can comment.


0 Likes
Knowledge Partner
Knowledge Partner

Re: AJC - writeLog function not thread safe?

On 19.01.2012 15:44, Geoffrey Carman wrote:
> Shon alas, has moved on to another company, and stopped monitoring the
> forum, which really is too bad. He is the most awesome resource we have
> had in the forum.


I suspected as much as he hasn't been posting for a while. Is he still
at iDENTiTYAUTOMATiON?

It's been a while since he left Novell. I can understand he wouldn't
want to dedicate time to a product he's no longer involved with.

Regardless, are there any other good java programmers here who might be
able to help me out?
Alex McHugh - Knowledge Partner - Stavanger, Norway
Who are the Knowledge Partners
If you appreciate my comments, please click the Like button.
If I have resolved your issue, please click the Accept as Solution button.
0 Likes
Knowledge Partner
Knowledge Partner

Re: AJC - writeLog function not thread safe?

On 1/19/2012 12:20 PM, Alex McHugh wrote:
> On 19.01.2012 15:44, Geoffrey Carman wrote:
>> Shon alas, has moved on to another company, and stopped monitoring the
>> forum, which really is too bad. He is the most awesome resource we have
>> had in the forum.

>
> I suspected as much as he hasn't been posting for a while. Is he still
> at iDENTiTYAUTOMATiON?


Yes, and apparently quite enjoying it, which is nice to hear.

> It's been a while since he left Novell. I can understand he wouldn't
> want to dedicate time to a product he's no longer involved with.


He pointed out when I made this same point that he is several years
removed from the code now, and I believe his last year or three at
Novell he had moved off IDM anyway.

> Regardless, are there any other good java programmers here who might be
> able to help me out?


I too would like to know the answer to this one.
0 Likes
Anonymous_User Absent Member.
Absent Member.

Re: AJC - writeLog function not thread safe?

Hi Alex

Can you post the code you refer to

In general code that is safe to call by multiple threads at the same time is
considered thread safe. If we assume that this is a library that could be
invoked by serveral drivers at the same time then how will it behave if two
drivers call it at exactly the same time. You will have to look up the java
docs for the objects ECMA is wrapping to see whether they are considered
thread safe as well.

Rgds

Justin

"Alex McHugh" <alexmchugh@no-mx.forums.novell.com> wrote in message
news:3JVRq.72$qL2.16@kovat.provo.novell.com...
> In the AJC ecmascript library that is included as a package in IDM4 there
> is a comment related to the writeLog function. I'm assuming the comment is
> from Shon Vella (as he's listed as the person who ported this from Java).
> The comment states "Unlike the original AJC, this function is not
> thread-safe - I think this can be fixed"
>
> My questions are (especially to Shon), what makes it not thread safe? For
> very low volume usage is it risky to use this? Also, any suggestions on
> how can this be fixed?
>
> I'm not much of a Java programmer, so wondering if anyone else can
> comment.



0 Likes
Knowledge Partner
Knowledge Partner

Re: AJC - writeLog function not thread safe?

Code is:

/**
* Write to a log file.
*
* <p>Note(sv): Unlike the original AJC, this function is not
thread-safe - I think this can be fixed.</p>
*
* @param {String} filename filename (including path)
* @param {String} msg message to write to the file
* @param {String} encoding string to define encoding type to be used
e.g. UTF-8
*
* @type String
* @return the message if successful, error string otherwise
*/
function writeLog(filename, msg, encoding)
{
if (!encoding)
{
encoding = System.getProperty("file.encoding")
}
var bw = null;
try
{
bw = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(filename, true),encoding));
bw.write(msg+System.getProperty("line.separator"));
return msg;
}
catch(e) {
return String(e);
}
finally
{
if (bw)
{
bw.flush();
bw.close();
}
}
}


On 1/23/2012 11:27 AM, Justin Birt wrote:
> Hi Alex
>
> Can you post the code you refer to
>
> In general code that is safe to call by multiple threads at the same time is
> considered thread safe. If we assume that this is a library that could be
> invoked by serveral drivers at the same time then how will it behave if two
> drivers call it at exactly the same time. You will have to look up the java
> docs for the objects ECMA is wrapping to see whether they are considered
> thread safe as well.
>
> Rgds
>
> Justin
>
> "Alex McHugh"<alexmchugh@no-mx.forums.novell.com> wrote in message
> news:3JVRq.72$qL2.16@kovat.provo.novell.com...
>> In the AJC ecmascript library that is included as a package in IDM4 there
>> is a comment related to the writeLog function. I'm assuming the comment is
>> from Shon Vella (as he's listed as the person who ported this from Java).
>> The comment states "Unlike the original AJC, this function is not
>> thread-safe - I think this can be fixed"
>>
>> My questions are (especially to Shon), what makes it not thread safe? For
>> very low volume usage is it risky to use this? Also, any suggestions on
>> how can this be fixed?
>>
>> I'm not much of a Java programmer, so wondering if anyone else can
>> comment.

>
>


0 Likes
Knowledge Partner
Knowledge Partner

Re: AJC - writeLog function not thread safe?


BufferedWriter is thread safe. My best guess is that the way it is
called from policy makes it potentially not thread safe.

I'm wondering if the solution is to implement a mutex type logic. Test
"can I write to the file"? If not, wait a period and try again. If it
still fails.. return an error.


--
alexmchugh
------------------------------------------------------------------------
alexmchugh's Profile: http://forums.novell.com/member.php?userid=40344
View this thread: http://forums.novell.com/showthread.php?t=451047

Alex McHugh - Knowledge Partner - Stavanger, Norway
Who are the Knowledge Partners
If you appreciate my comments, please click the Like button.
If I have resolved your issue, please click the Accept as Solution button.
0 Likes
Anonymous_User Absent Member.
Absent Member.

Re: AJC - writeLog function not thread safe?

On 1/23/12 12:46 PM, alexmchugh wrote:
>
> BufferedWriter is thread safe. My best guess is that the way it is
> called from policy makes it potentially not thread safe.
>
> I'm wondering if the solution is to implement a mutex type logic. Test
> "can I write to the file"? If not, wait a period and try again. If it
> still fails.. return an error.
>
>

My ears were burning...

It is unsafe because it is unpredictable exactly what will happen when
two threads try to log to the same file at the same time, with possible
results on Unix/Linux of log messages being mixed together (especially
if you have long messages), or on windows one thread would actually fail
to open the file for writing if another thread has it open.

The original Java version was declared as "synchronized" so essentially
only thread was allowed into the function at the same time, with other
threads blocking until it was their turn. For Javascript, there is no
equivalent to a synchronized function, but it could be rewritten in
terms using a java lock declared and initialized outside the function, e.g.

var writeLogLock = new java.util.concurrent.locks.ReentrantLock();

function writeLog(filename, msg, encoding)
{
writeLogLock.lock();
try {
// do all the writelog stuff
} finally {
writeLogLock.unlock();
}
}

But this still isn't really the equivalent of the Java version because
with Javascript each driver gets is own new private copy of the
javascript functions and the lock variable each time it starts, so this
is only going to make it thread safe within the context of a single
driver instance. You still would need to make sure that no two drivers
were using the same log file.

--
Shon
0 Likes
Knowledge Partner
Knowledge Partner

Re: AJC - writeLog function not thread safe?

On 24.01.2012 22:57, Shon Vella wrote:
> But this still isn't really the equivalent of the Java version because
> with Javascript each driver gets is own new private copy of the
> javascript functions and the lock variable each time it starts, so this
> is only going to make it thread safe within the context of a single
> driver instance. You still would need to make sure that no two drivers
> were using the same log file.


Thanks for replying and the (as usual) excellent clarification. For my
needs it won't be a problem to have one log file per driver, so I think
I can use this function with the addition of a lock variable as you
outlined.
Alex McHugh - Knowledge Partner - Stavanger, Norway
Who are the Knowledge Partners
If you appreciate my comments, please click the Like button.
If I have resolved your issue, please click the Accept as Solution button.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.