Sylvain Wallez <sylvain <at> apache.org> writes:
>
> Joerg Heinicke wrote:
>
> >Hello,
> >
> >I have a question regarding synchronization in flow script. I need an object
> >per session, so the instantiation must be synchronized. Furthermore the
> >object
> >is an Avalon component, so I need to call something like
> >cocoon.createComponent().
> >
> >Now how is it possible in Flow script? The alternative would be a
> >synchronized
> >static factory method in the class, but how can I get the Avalon lifecycle
> >then?
> >
> >
>
> Flowscript execution is synchonized on the Rhino scope, which is either
> the global (per-session) scope for a function call and the continuation
> scope for continuation call.
>
> So what about simply declaring a global variable in your flowscript,
> created using cocoon.createComponent()?
Hello Sylvain,
I tested the following two solutions. The first one does not work - more or less
like expected as the variable is not initialized immediately via
getComponent().
=====================================================================
// not synchronized, but works like expected otherwise
var queryHandler;
function getQueryHandler(create) {
if (queryHandler == null && create) {
queryHandler = cocoon.getComponent(QueryHandler.ROLE);
cocoon.session.setAttribute(QueryHandler.ROLE, queryHandler);
} else if (queryHandler == null) {
throw new ProcessingException("Could not find a query handler.");
}
return queryHandler;
}
=====================================================================
Unfortunately the second one does neither work. Though the access is indeed
synchronized, the initialization of the variable is executed on every call to a
function in this flowscript. I wonder if this behaviour is correct. Maybe I have
a false understanding "how it works", but I understand the global stuff as
something like static stuff in Java - and static initialization is only executed
once in Java.
=====================================================================
// synchronized, but executed on each flowscript call
var queryHandler = cocoon.getComponent(QueryHandler.ROLE);
function getQueryHandler(create) {
cocoon.session.setAttribute(QueryHandler.ROLE, queryHandler);
return queryHandler;
}
=====================================================================
// called from sitemap
function initQuery() {
var queryHandler = getQueryHandler(true);
...
}
function executeQuery() {
var queryHandler = getQueryHandler();
...
}
=====================================================================
Do you have another idea?
Joerg