PreWork
I am able to invoke or evaluate javascript expressions from java as follows:
jsEngine.eval("ComputerName == 'mymachine'", myScriptContext);
The attribute value ComputerName is read from myScriptContext object.
Scenario
Now the problem is that I want to have some custom functions which can be part of the evaluated expression like:
jsEngine.eval("startswith(ComputerName, 'my')", myScriptContext);
Since startswith is not an available function (I must support startswith with that signature), I tried to write a .js file, where I defined my custom functions & also appended my expression to be evaluated to that script and executed as follows:
jsEngine.eval("_js_having_myfunction_and_expression_"); // note - no myScriptContext
Problem
startswith gets recognized by JS engine ONLY IF I DO NOT present myScriptContext. The moment I present myScriptContet, I get this exception:
Exception in thread "main":
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: startswith is not a function
Related
I'm trying to implement a .Net component in a Coldfusion application (this is on my development machine running CF 11.
I set up the object like this:
<cfset dll = ExpandPath('./com/Interop.CADXLib.dll')>
<cfobject class="CADXLib.CadX" type=".NET" name="cadx" assembly="#dll#”>
That’s in my application.cfc, in the onRequestStart() method. Then in my index.cfm I call:
<cfdump var="#cadx#”>
The object dumps just fine. I can see all the internal methods, including the one I need to get things going, the OpenDesign() method, which (in theory) opens a local file and reads the contents.
So next I set the actual filename I want to access:
<cfset thisfile="1571269P01R01_ARTIOS.ARD">
<cfset thispath=getDirectoryfromPath(getCurrentTemplatePath())>
<cfset thisfile=thispath & thisfile>
This works, and outputting “thistfile” gives me the correct path location. So now I call the actual method:
<cfset result=cadx.OpenDesign(thisfile,0)>
That’s where I get stuck. I get this error:
Either there are no methods with the specified method name and argument types or the OpenDesign method is overloaded with argument types that ColdFusion cannot decipher reliably. ColdFusion found 0 methods that match the provided arguments. If this is a Java object and you verified that the method exists, use the javacast function to reduce ambiguity.
I try the javacast function:
<cfset result=cadx.OpenDesign(JavaCast("string",thisfile),JavaCast("int",0))>
Same error. Method not found. But based on the dump, the method is clearly there.
Has anyone encountered this or something similar?
i'm trying to create a grammar that join togheter a script language with the possibility to create method.
Grammar
grammar org.example.domainmodel.Domainmodel with org.eclipse.xtext.xbase.Xbase
generate domainmodel "http://www.example.org/domainmodel/Domainmodel"
import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase
Model:
imports = XImportSection
methods += XMethodDeclaration*
body = XBlockScriptLanguage;
XMethodDeclaration:
"def" type=JvmTypeReference name=ValidID
'('(params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')'
body=XBlockExpression
;
XBlockScriptLanguage returns xbase::XExpression:
{xbase::XBlockExpression}
(expressions+=XExpressionOrVarDeclaration ';'?)*
;
At the moment i create the following JvmModelInferr, for defining the main method for scripting language.
JvmModelInferr
def dispatch void infer(Model model, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(
model.toClass("myclass")
).initializeLater [
members += model.toMethod("main", model.newTypeRef(Void::TYPE)) [
parameters += model.toParameter("args", model.newTypeRef(typeof(String)).addArrayTypeDimension)
setStatic(true)
body = model.body
]
]
}
When i tryed to use my grammar, i obtain the following error after that i wrote my method:
no viable alternative at input 'def'
The method mymethod() is undefined
The problem is related only with method declaration, without it myclass.java is created.
Moreover i obtain the "Warning 200" for a not clear grammar, why?
There are two fixes that appear necessary:
The imports section is not marked as optional. If it was intended to be optional, it should be declared as imports ?= XImportSection. Or, add necessary import statements to your JvmModelInferr example.
The dispatch keyword isn't defined in your grammar. As defined, a method should consist of def, followed by a Java type (the return type), and then the method's name (then the body, etc.). You could add `(dispatch ?= 'dispatch') if you're targeting Xtend and intend to support its multiple dispatch feature (or your own version of it).
HTH
Please forgive me, as I am a Java man dabbling in Javascript business :)
I wanted to be able to define a set of integration test cases to be easy to script against a Java application. I thought Javascript would be a perfect language to script against. To that end, I am using the Rhino engine that comes with JDK 7, via Java's Scripting API. The scripts would have access to Java classes already defined in the application, and could be reused to define use case scenarios for integration testing.
In the Java application, I have binded the javascript engine itself to the script as jsengine, so that I can load javascript files (Including a JavaScript file during Rhino eval).
I have two Javascript files, as defined below:
Function.js:
function send(msg) {
send.sendMessage(msg);
}
TestCase.js
jsengine.eval(new java.io.FileReader("Function.js");
sendMsg("Test Message");
I also have the following object defined and binded to the script as "javaobj":
public class TestConnection {
...
public void send(String message) {
// Code to send the string message via JMS
}
}
However, the Rhino engine complains with the following Exception. It seems to not like calling the javaobj's send method, for some reason.
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function send in object
function sendMsg(msg) {...}. (TestCase.js#3) in TestCase.js at line number 3
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:224)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:212)
at com.foo.test.scenario.JavaScriptEngine.execute(JavaScriptEngine.java:56)
at com.foo.test.TestSuite.start(TestSuite.java:88)
at com.foo.test.TestSuite.main(TestSuite.java:41)
Caused by: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function send in object
function sendMsg(msg) {...}. (TestCase.js#3) in TestCase.js at line number 3
at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3773)
at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3751)
at sun.org.mozilla.javascript.internal.ScriptRuntime.typeError(ScriptRuntime.java:3779)
at sun.org.mozilla.javascript.internal.ScriptRuntime.typeError2(ScriptRuntime.java:3798)
at sun.org.mozilla.javascript.internal.ScriptRuntime.notFunctionError(ScriptRuntime.java:3869)
at sun.org.mozilla.javascript.internal.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2345)
at sun.org.mozilla.javascript.internal.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2312)
at sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:1524)
at sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:854)
at sun.org.mozilla.javascript.internal.InterpretedFunction.call(InterpretedFunction.java:164)
at sun.org.mozilla.javascript.internal.ContextFactory.doTopCall(ContextFactory.java:429)
at com.sun.script.javascript.RhinoScriptEngine$1.superDoTopCall(RhinoScriptEngine.java:116)
at com.sun.script.javascript.RhinoScriptEngine$1.doTopCall(RhinoScriptEngine.java:109)
at sun.org.mozilla.javascript.internal.ScriptRuntime.doTopCall(ScriptRuntime.java:3163)
at sun.org.mozilla.javascript.internal.InterpretedFunction.exec(InterpretedFunction.java:175)
at sun.org.mozilla.javascript.internal.Context.evaluateReader(Context.java:1159)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:214)
... 4 more
Has anyone ever encountered this type of issue with Rhino?
P.S. This question seems related, but no answer given as well (TypeError in Rhino: migration from Java 6 to Java 7)
Looks like I found my own answer. There was a name conflict between the Javascript function and the name of the binded Java object. Both having the same name, the engine tries to call a non-existent method on a Function object!
Silly me... :P
I used AJAX to call an action and pass parameters, the AJAX call occurs from xsl page and its as follows:
xmlHttp.open("GET","examcont?action=AJAX_SectionsBySessionId&sessionId="+sessionId,true);
I decided to put the amp; after & as xsl raises this error when I removed it:
The reference to entity "sessionId" must end with the ';' delimiter
the problem is that the action is unable to read the parameter sessionId however I tried the same action URL but without the amp; and the action reads the parameter successfully
The problem seems to be that the & represents & in the style sheet but gets expanded/escaped to & again during output (because it is HTML/XML). You may try to use the following in XSL to avoid escaping:
xmlHttp.open("GET","examcont?action=AJAX_SectionsBySessionId<xsl:text disable-output-escaping="yes">&</xsl:text>sessionId="+sessionId,true);
However, note that - if you happen to let your XSL run in the browser - this does not work (although it is correct XSL and it should) on Firefox according to https://bugzilla.mozilla.org/show_bug.cgi?id=98168.
As portable alternative, you can use the following which avoids mentioning & by inserting it at runtime with what you might call "Javascript-escaping":
xmlHttp.open("GET","examcont?action=AJAX_SectionsBySessionId"+String.fromCharCode(38)+"sessionId="+sessionId,true);
Also have a look at similar question with deeper discussion and other options using a html entity in xslt (e.g. )
I'm new to sphinx, and I've encountered a few problems:
$1 After setting max_matches = 200 in class searchd in csft.conf, I called
org.sphx.api.test.main(new String[]{"-h", "127.0.0.1","-i", "magnet","-p", "9312", "-l", "100", "keyword"});
in a java main method. The error returned is
Error: searchd error: per-query max_matches=1000 out of bounds (per-server max_matches=200)
As you can see, I've added the param: -l = 100, what else should I set to prevent this error in Java?
$2 I want to use sortMode = SphinxClient.SPH_SORT_TIME_SEGMENTS to have the search result ordering by time desc. My attribute is written like this in csft.conf:
sql_attr_timestamp=UNIX_TIMESTAMP(upload_time) as dt
Could anyone tell me how can I set the attribute in Java code? I've tried to set the sortClause String in java, but it always said that Attribute XXX has not been found.
$3 I want to know whether SphinxClient in Java is thread safe, becaust I don't like to create a SphinxClient instance every time a person do a query.
Thanks in advance!
If the class you are using is https://code.google.com/p/sphinxtools/source/browse/trunk/src/org/sphx/test/test.java?r=2
then the function never even inspects 'argv'. It hardcodes all the variables. There is nothing passed as the third param to setLimits
sql_attr_timestamp simply accepts a column name, no functions or anything. The function call HAS to be in the main sql_query
My java is very rusty, but would have to say no. It stores all sort of state in private varibles. Multiple threads using the client at once will clober them.