1

I'd like to pass a function parameter (i.e. a FunctionObject) in SSJS to a bean, and then call that function as a kind of callback function. For instance:

companyBox.setGenerator(function() {
    return @DbColumn("", SystemBean.getViewName(), 2);              
});

so that I can call that function whenever I need actualized values.

I'm expecting this to fail because the SSJS-context is missing, but the idea is still tempting. Or maybe it's because the function cannot be Serialized, but what that exactly means I don't know (yet). In this case, the companyBox object is created from a managed bean with view/page scope: once the current page disappears I no longer need it.

Clues are welcome...

D.Bugger
  • 2,300
  • 15
  • 19
  • 1
    I don't know what is possible... With all respect I don't know why you would even want to do this. If you're working with Java - then move to java and be done with it. Trying to mix it in with @functions just seems off to me when you could just do all the work in Java itself. – David Leedy Mar 24 '15 at 18:13
  • Maybe this will help http://stackoverflow.com/questions/9099093/calling-ssjs-from-java – Thomas Adrian Mar 24 '15 at 19:54
  • @David: it's the management module that I cannot change (yet), and it's in SSJS. The bean is a multi-purpose class, and it requires a function that generates selectable values, e.g. for a drop-down list. All I'd like to do is call that function. I could indeed pass its name, but I just asked myself: wouldn't it be better to pass the function ans an object, so it can be called (back)? – D.Bugger Mar 24 '15 at 21:31
  • @Thomas: Thanks for the link, I saw that question and... I forgot about it. It might be doable that way, but then I'd have to pass the name (or the whole function) as a string. Not ideal, but if it's the only way, I'll probably do that. – D.Bugger Mar 24 '15 at 21:35
  • Hm, I expect I can use a formula-string in Formula language in 95% of all cases. That case seems closed, but I'd still be interested to know whether calling a function using a FunctionObject from Java is possible. – D.Bugger Mar 24 '15 at 22:58
  • does any of the function require parameters? if so, will you pass them in SSJS call, or Java code collects them? – Frantisek Kossuth Mar 26 '15 at 15:28
  • See the code above. The companyBox is a Java object, and the method setGenerator is supposed to be called with a function object, i.e. a com.ibm.jscript.std.FunctionObject. This class supports a call method, but nothing happens when I use it. So the function itself is in SSJS, and the caller is Java. – D.Bugger Mar 26 '15 at 18:58

1 Answers1

1

Yes it is. You have two options.

1) Use "value binding". Thomas Adrian commented about this question, Sven Hasselbach blogged about calling external SSJS library (cool stuff, by the way).

In short: make a call to inner JSF engine to resolve value binding - what can be SSJS (including call to your method) or any other binding (EL). And it works with "on fly" constructed expressions, passed as String.

2) Use Function object as parameter. Blueprint for that technique is available here. Your comment reveals you are aware of com.ibm.jscript package. Well, current implementation of call() method of com.ibm.jscript.std.FunctionObject class will support your empirical observation:

/*     */   public FBSObject call(FBSValueVector paramFBSValueVector) {
/* 163 */     return null;
/*     */   }

Hint: JD Eclipse and JD Eclipse-realign strongly recommended!

The other call method with more complex signature is the way to go. It is used to define custom @Functions, for example this snippet.

Community
  • 1
  • 1
Frantisek Kossuth
  • 3,524
  • 2
  • 23
  • 42
  • Not exactly what I was looking for, i.e. a call of an SSJS function from Java, where the function is passed as a function parameter. By the way: Thomas's question was the earlier one... – D.Bugger Mar 26 '15 at 09:38
  • 1
    As you can see in the code above, the *call* method won't work - it just returns null. And the other methods have the problem that they need the correct ExecutionContext. Otherwise your call cannot use the SSJS "add ons" like @Formula and other IBM extensions for the JSInterpreter (f.e. *println*). – Sven Hasselbach Mar 27 '15 at 09:50
  • So it was the wrong 'call' ;-) Thank you all, especially for the snippet on OpenNTF. – D.Bugger Mar 27 '15 at 13:15