WSO2 ESB Can't run Groovy script stored in registry - groovy

I'm trying to execute a Groovy script that I'm storing in the WSO2 ESB Local registry. When I do that I'm getting the following error:
ERROR {org.apache.synapse.mediators.bsf.ScriptMediator} - The script engine returned a NoSuchMethodException executing the external groovy script : Value {name ='null', keyValue ='file:Scripts/Groovy/test.groovy'} function mediate {org.apache.synapse.mediators.bsf.ScriptMediator}
java.lang.NoSuchMethodException: No signature of method: com.sun.script.groovy.GroovyScriptEngine.mediate() is applicable for argument types: (org.apache.synapse.mediators.bsf.ScriptMessageContext) values: [org.apache.synapse.mediators.bsf.ScriptMessageContext#716f8a10]
Possible solutions: wait()
If I put the code in-line in the script mediator, everything is running ok.
I've tried to wrap the script code like this <x><![CDATA[...code...]]></x>, as shown in the Using Ruby Scripts for Mediation example:
Sample 353: Using Ruby Scripts for Mediation. I add the groovy-all-2.4.7.jar to ESB_HOME\repository\components\lib too.
How can I run the groovy scripts stored in the registry? What am I doing wrong?
Here is the Groovy Script and the proxy, wiht which I'm testing:
Groovy Script
class Example {
static def DisplayName() {
println("This is how methods work in groovy");
println("This is an example of a simple method");
}
static void main(String[] args) {
DisplayName();
}
}
Proxy service
<proxy name="TestScriptProxy" startOnLoad="true" trace="disable"
transports="http https" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<script language="groovy"><![CDATA[println "This is an in-line script";]]></script>
<script function="DisplayName"
key="file:Scripts/Groovy/test.groovy" language="groovy"/>
</inSequence>
<outSequence/>
<faultSequence/>
</target>
</proxy>
ESB version is 5.0.0 running on Windows 10.

If you have the Groovy script in the registry, the source should be the registry path. For example : gov:scripts/Groovy/test.groovy.
If you are referring a file give path relative to ESB_HOME. For example if scripts folder is in ESB_HOME directory, file:scripts/Groovy/test.groovy
First load the script file as below :
<localEntry key="DisplayNameScript" src="file:scripts/Groovy/test.groovy"/>
Now call the function
<script language="groovy" key="DisplayNameScript" function="DisplayName"/>

Related

How to hide Tomcat version from error messages when using embedded servers in Java

I have a java application where i'm using embedded Tomcat servers,
which looks like this
Tomcat tomcat = new Tomcat()
I'm creating an embedded tomcat server here.
Problem statement
whenever there's an error it displays information on which tomcat version i'm using,
how to hide this in java?
i have a little idea that i need to override ServerInfo.properties, but how do i do this?
I'm not sure how we can do this in java, but if you are using any build scripts like ant / gradle for distribution purpose, we can write a task to override / harden the jar file, and replace the ServerInfo.properties file with the customized value whatever we need.
the code for ant build scripts would look like
<target name="override.tomcat">
<jar destfile="path/to/tomcat-embed-core-9.0.62.jar" update="true">
<fileset dir="src/"> <!-- folder where you keep the directory/file to raplace-->
<include name="org/apache/catalina/util/ServerInfo.properties"/> <!-- file to replace within directory path in side the jar-->
</fileset>
</jar>
</target>
and in gradle
task overRideTomcat(type: Jar) {
from(zipTree(file("path/to/tomcat-embed-core-9.0.62.jar"))) {
exclude '**/org/apache/catalina/util/ServerInfo.properties'
}
from('src/') {
include('/org/apache/catalina/util/ServerInfo.properties')
}
archiveName "tomcat-embed-core-9.0.62.jar"
}
make sure you have the modified ServerInfo.properties file under src directory in the same path as you have mentioned in the include statement.

What is the proper syntax for using subcontrollers with Ufront?

In my main controller I followed the instructions in the Controller documentation and I have the following meta data:
#:route(GET, "/about/*")
var aboutController:AboutController;
Then in my AboutController file I have:
package controller;
import api.TestApi;
import api.PortfolioItem;
using ufront.MVC;
using ufront.web.result.AddClientActionResult;
class AboutController extends Controller
{
#:route(GET, "/graphicDesign")
public function graphicDesign()
{
// return new PartialViewResult({… etcetera
}
}
When I visit the /about/graphicDesign path in my browser, the PHP server generates an error:
PHP Fatal error: Call to a member function execute() on null in /Users/allan/Documents/Freelance/Confidant/Website/3d confidant site/ufront/www/lib/controller/HomeController.class.php on line 70
The PHP lines 69-71 have:
public function execute_aboutController() {
return $this->context->injector->_instantiate(_hx_qtype("controller.AboutController"))->execute();
}
So, do I need different syntax so that the controller instantiates properly?
fyi i upgraded to 3.4 i don't have the same issues.
yes remoting does not work but only when targeting php7 . in fact even when not targeting php7 and running in a php7 apache environment doesn't work either. also in works with Mamp & php 5.6.
i had no probs with sub controllers though.
my answer is . did you try on another php environment ?

Cassandra source code ConfigurationException expection URI in cassandra.config found cassandra.yaml

For my master thesis, I have to modify the source code of Cassandra. So, as suggested by https://wiki.apache.org/cassandra/HowToBuild, I git clone, then run ant, and everything seems nice (I managed to build the project without any error), but when I run the unitTests (cassandra/test), I have this strange error:
org.apache.cassandra.exceptions.ConfigurationException:
Expecting URI in variable: [cassandra.config].
Found[cassandra.yaml].
Please prefix the file with [file:\\\] for local files and
[file:\\<server>\] for remote files.
If you are executing this from an external tool, it needs
to set Config.setClientMode(true) to avoid loading configuration.
at org.apache.cassandra.config.YamlConfigurationLoader.getStorageConfigURL(YamlConfigurationLoader.java:80)
at org.apache.cassandra.config.YamlConfigurationLoader.loadConfig(YamlConfigurationLoader.java:100)
at org.apache.cassandra.config.DatabaseDescriptor.loadConfig(DatabaseDescriptor.java:252)
at org.apache.cassandra.config.DatabaseDescriptor.daemonInitialization(DatabaseDescriptor.java:131)
at org.apache.cassandra.auth.jmx.AuthorizationProxyTest.setup(AuthorizationProxyTest.java:48)"
I would like to test my modifications on the source code with the unitTests (because I didn't find any tutorial of how to set up cassandra from the source code on Windows, so if you have one, I would like to have the link ^^) but I didn't manage to find any solution for this bug :(. Anyone know a solution to this problem?
I am working on Windows 10 with IntelliJ and I have updated my Jdk and ant to the latest version.
I was facing the same issue. Those variables ("cassandra.config", "cassandra.storagedir", etc...) are System variables.
You can either set them in your code by doing something like:
System.setProperty("cassandra.config", "file:///<PATH>/cassandra.yaml");
You can also set them whilst running the jar file:
java -Dcassandra.config=file:///<PATH>/cassandra.yaml -jar <JAR>
Best,
Shabir
Start a new process in jdk 1.8 and start embedded cassandra in it. and run your junit in your java version. I faced similar isue which jdk11 upgrade. Now i fixed this.
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class EmbeddedCassandraApplication {
public static void main(String[] args) throws Exception {
EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra-test.yaml");
}
}

Integrating Node.js with QUnit to Jenkins

This is my first sof post so forgive my format and organization of thought. I've made a great effort to solve my problem before posting this. Part of my issue could be lack of knowledge with packages in Ubuntu or Node.js so please guide me.
I'm trying to create a XUnit xml file for Jenkins from QUnit tests for a Node.js application. I don't have the ability to run a browser or even a headless browser, also don't understand why I'd need one since the Node.js code doesn't deal with the browser.
I've been searching all over and have only been successful using qunit-tap and 'prove' to create an XML file. Prove required downloading a formatter which was a perl file. We are trying to prevent using perl stuff.
My system is an Ubuntu VM. This is a task for work and my boss is asking for the minimal amount of packages and dependencies. Our Node.js server is accepting web socket requests and passing messages back and forth with a legacy system written in php.
QUnit's output seems to be a pretty print format, in a table, when I run my tests in the console. It would be amazing to just get that into a flatter form with a flag!
Thanks in advance!
Well, for NodeJS you can go with Grunt and grunt-contrib-qunit, though I would recommend the following approach:
Leverage JUnit Logger
(https://github.com/jquery/qunit-reporter-junit) plugin for JUnit
compatible report.
Comment out console.log output in the PhantomJS Runner https://github.com/jquery/qunit/tree/master/addons/phantomjs to mute non-XML output produced by the runner
Assign a task for Apache Ant build script:
<target name="qunit" description="runs QUnit tests using PhantomJS">
<echo message="Executing QUnit Javascript Unit Tests..."/>
<exec executable="/usr/local/bin/phantomjs" output="./build/qunit/qunit-results.xml">
<arg value="./vendors/Runner/runner-muted.js" />
<arg value="test-runner.html" />
</exec>
</target>
Jenkins shall look for the report in ./build/qunit/qunit-results.xml
ditto, above answer good
use the junit report out and connect it to the standard jenkins unit testing plugin -> http://wiki.jenkins-ci.org/display/JENKINS/xUnit+Plugin
for nice easy to configure self-installed nodejs on the machine, i have to recommend the excellent -> http://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin
for jshint/csslint reports, i found the https://wiki.jenkins-ci.org/display/JENKINS/Checkstyle+Plugin plugin very nice, jshint and csslint both output to this
jshint : {
options : {
reporter : 'checkstyle',
reporterOutput : 'reports/jshint.xml',
},
src : "..."
},
csslint : {
strict : {
options : {
formatters : [{
id : 'checkstyle-xml',
dest : 'reports/csslint.xml'
}],
csslintrc: '.csslintrc'
},
src : [...],
},
},

How do you get the path of the running script in groovy?

I'm writing a groovy script that I want to be controlled via a properties file stored in the same folder. However, I want to be able to call this script from anywhere. When I run the script it always looks for the properties file based on where it is run from, not where the script is.
How can I access the path of the script file from within the script?
You are correct that new File(".").getCanonicalPath() does not work. That returns the working directory.
To get the script directory
scriptDir = new File(getClass().protectionDomain.codeSource.location.path).parent
To get the script file path
scriptFile = getClass().protectionDomain.codeSource.location.path
As of Groovy 2.3.0 the #SourceURI annotation can be used to populate a variable with the URI of the script's location. This URI can then be used to get the path to the script:
import groovy.transform.SourceURI
import java.nio.file.Path
import java.nio.file.Paths
#SourceURI
URI sourceUri
Path scriptLocation = Paths.get(sourceUri)
Note that this will only work if the URI is a file: URI (or another URI scheme type with an installed FileSystemProvider), otherwise a FileSystemNotFoundException will be thrown by the Paths.get(URI) call. In particular, certain Groovy runtimes such as groovyshell and nextflow return a data: URI, which will not typically match an installed FileSystemProvider.
This makes sense if you are running the Groovy code as a script, otherwise the whole idea gets a little confusing, IMO. The workaround is here: https://issues.apache.org/jira/browse/GROOVY-1642
Basically this involves changing startGroovy.sh to pass in the location of the Groovy script as an environment variable.
As long as this information is not provided directly by Groovy, it's possible to modify the groovy.(sh|bat) starter script to make this property available as system property:
For unix boxes just change $GROOVY_HOME/bin/groovy (the sh script) to do
export JAVA_OPTS="$JAVA_OPTS -Dscript.name=$0"
before calling startGroovy
For Windows:
In startGroovy.bat add the following 2 lines right after the line with
the :init label (just before the parameter slurping starts):
#rem get name of script to launch with full path
set GROOVY_SCRIPT_NAME=%~f1
A bit further down in the batch file after the line that says "set
JAVA_OPTS=%JAVA_OPTS% -Dgroovy.starter.conf="%STARTER_CONF%" add the
line
set JAVA_OPTS=%JAVA_OPTS% -Dscript.name="%GROOVY_SCRIPT_NAME%"
For gradle user
I have same issue when I'm starting to work with gradle. I want to compile my thrift by remote thrift compiler (custom by my company).
Below is how I solved my issue:
task compileThrift {
doLast {
def projectLocation = projectDir.getAbsolutePath(); // HERE is what you've been looking for.
ssh.run {
session(remotes.compilerServer) {
// Delete existing thrift file.
cleanGeneratedFiles()
new File("$projectLocation/thrift/").eachFile() { f ->
def fileName=f.getName()
if(f.absolutePath.endsWith(".thrift")){
put from: f, into: "$compilerLocation/$fileName"
}
}
execute "mkdir -p $compilerLocation/gen-java"
def compileResult = execute "bash $compilerLocation/genjar $serviceName", logging: 'stdout', pty: true
assert compileResult.contains('SUCCESSFUL')
get from: "$compilerLocation/$serviceName" + '.jar', into: "$projectLocation/libs/"
}
}
}
}
One more solution. It works perfect even you run the script using GrovyConsole
File getScriptFile(){
new File(this.class.classLoader.getResourceLoader().loadGroovySource(this.class.name).toURI())
}
println getScriptFile()
workaround: for us it was running in an ANT environment and storing some location parent (knowing the subpath) in the Java environment properties (System.setProperty( "dirAncestor", "/foo" )) we could access the dir ancestor via Groovy's properties.get('dirAncestor').
maybe this will help for some scenarios mentioned here.

Resources