I came across this piece of code when looking at an application.
groovy.util.ConfigObject config = (groovy.util.ConfigObject)org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(arrayOfCallSite[438].callGetProperty(this.grailsApplication), groovy.util.ConfigObject.class);
My question are, what does a callsite array do and how is it initialized to contain the data to be retrieved later on?(In this case the config)I tried reading the documentation for groovy but there is no useful description at all explaining what it does and only a list of methods and attributes is shown in the docs.
It's looks like decompiled groovy code or generated java code from groovy.
Let's see the simple groovy script:
cfg = new ConfigObject()
ConfigObject c = cfg
The line with assignment of script variable cfg value into local type defined c variable in java will look like:
ScriptBytecodeAdapter.setGroovyObjectProperty(localObject, Script61235.class, this, (String)"cfg");
ConfigObject c = (ConfigObject)ScriptBytecodeAdapter
.castToType(arrayOfCallSite[2].callGroovyObjectGetProperty(this), ConfigObject.class)
so, your code approximately corresponds to this groovy code:
ConfigObject config = this.grailsApplication.xxx
where xxx we don't see in your question.
better to find original groovy code and you will understand it better )
Related
I'm building a route which calls a groovy script whose path is dynamically computed and, if the script can't be found, defaults to a generic, static script:
.doTry()
.toD("language://groovy:resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy")
.doCatch(FileNotFoundException.class)
.script().groovy("resource:classpath:scripts/defaultResponseHandler.groovy")
.end()
The problem is that the exchange property consumerType is not resolved since the uri string parameter of toD is evaluated using groovy and not simple.
MultipleCompilationErrorsException -> startup failed:
Script_09b4150584d9e2c979353feee06897b5.groovy: 1: Unexpected input: 'scripts/${exchangeProperty.consumerType}' # line 1, column 20.
resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy
^
1 error
How can I obtain the desired behavior?
According to the error shown there, it seems Camel is not able to resolve the string you provided in the toD().
By default, the expression you pass to a dynamic to is evaluated as Simple language but, as described in To Dynamic Camel documentation, you can specify other languages for the dynamic evaluation.
In your case, you are trying to evaluate the endpoint with groovy language but then you're using Simple language to substitute a piece of the name of the script.
One solution I've found (yet not the best) would be to specify the language for the interpretation of the string as simple and then use language:groovy to specify the endpoint that will need to be called.
You could write something like this:
.doTry()
.toD("language:simple:language://groovy:resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy")
.doCatch(FileNotFoundException.class)
.script().groovy("resource:classpath:scripts/defaultResponseHandler.groovy")
.end()
It seems to work, but I hope someone else comes up with a better solution.
In a python project I generate autoapi documntation. Special comment appear in generated html files.
For instance it's working and displaying on final html page:
def do_action(self,params):
"""
This is function to do some cool stuffs.
Actually it should
"""
pass
Or
...
applicationConfig = None
"""This variable hold some important data"""
However I would like autoapi generate some custom comment into html page
For example I've got a comment in code like this:
"""These are public variable:"""
p_var1 = "segg"
p_var2 = "fos"
But this last comment not shown in generated documentation. Maybe because it hasn't connected to any definition structure in source code? (I mean neither variable declaration nor function or class declaration)
Anyway, how should force sphinx to generate html entry from any comments arrounded by triple apostrophe?
There are two options for having sphinx parse variable comments. The first is via attribute docstrings, which are specified in pep 224 to belong below the attribute that they describe, as in your first example. While it was rejected, it is the format sphinx requires in order to work correctly:
p_var1 = "segg"
"""Docstring for p_var1"""
Renders as:
Alternatively, sphinx will also pick up comments above the attribute that start with a colon and treat them like a docstring, which in some cases looks a bit better in the source code:
#: Description for p_var1
p_var1 = "segg"
Renders also as:
There is no option to pick up a comment without a module, exception, class, method, function, or variable being attached to it, becauseautodoc explicitly only considers information from docstrings (and call signatures, but that's the only exception).
I have a bunch of common groovy functions which I am reusing in different 'Test Plans'. I want to keep them in separate script files. I don't want to create jar. How can I import those files in JMeter JSR223 Assertion, Preprocessor and Postprocessor with minimum fuzz? These are more or less modular functions and I want to keep them separate for obvious reason.
I tried using "Script file" section of JSR223. But it seems that it is only for overriding the script.
How can I use an external groovy script file in JSR 223 assertion/preprocessor/post-processor?
If you have a bunch of functions which you`d like to re-use in different jsr223 elements you can:
In your test-plan create one initial JSR223 preprocessor (or sampler) and define your functions:
def sum(Integer a, Integer b) {
return a + b
}
def isA(Integer x, Integer y) {
}
def isB(Integer x, Integer y) {
}
Then using closure put them as objects:
vars.putObject('sum', this.&sum)
vars.putObject('isA', this.&isA)
vars.putObject('isB', this.&isB)
In any other JSR223 element in the script you can use those functions like that:
def sum= vars.getObject(‘sum’);
println sum(2, 2);
I don't really understand why Script file option doesn't work for you, you can save your Groovy code as separate files on the file system and reference them via "Script file" input.
Whatever.
You can make JSR223 Test Elements modular just like any other Test Element using:
Test Fragment
Module Controller
and Include Controller combination
You can define the Groovy functions in {JMETER_HOME}/bin/utility.groovy script file.
Optionally you can define the function in your own Groovy script and set the property groovy.utilities=bin/utility.groovy in user.properties file.
For example, add the following to the Groovy script
def getRandomRangeOption() {
Random random = new Random()
random.nextBoolean() ? "Between" : "Except"
}
You can call the function with
${__groovy(getRandomRangeOption())}
from anywhere in you JMX.
I imagine I'm screwing something up with these declarations, but I've got a groovy class with a field defined like this:
Map<String, SomeType> _someField = [:]
I do inserts like this:
_someField.put( someStringVariable, someTypeInstance )
...and then later, when I check whether a key I know has been mapped is present, the check fails:
_someField.containsKey( someStringVariable )
The only way I can get this to succeed is by calling toString(), like so:
_someField.containsKey( someStringVariable.toString() )
I'm using the generic declaration of the map so my IDE gives me auto completion on the value types, so I'd really like (I think) to keep the type information there.
I've tried changing the key type from String to GString, but to no avail. I've tried changing the map initialization from the groovy shorthand [:] to new LinkedHashMap<>, also to no avail.
Any ideas whether I can keep the type information and avoid having to use toString()?
So this was a case where the variable being fed to containsKey() in the instances where it is failing were of type org.codehaus.groovy.runtime.GStringImpl because they were generated by a function that was performing variable expansion on map values, and that function was creating groovy interpolated strings for values instead of Java Strings.
A quick check on the type of the variable confirmed the type problem, and then it was just a matter of tracking back to find the source of the interpolated string.
In Groovy, I get my hands on a Hudson job:
def job = hudson.model.Hudson.instance.getItem("Rsync library to docs-stage")
Now I would like to have a list of the parameter names for that job. Sounds simple, but Googling around isn't getting me there yet. Could someone enlighten me?
Thanks.
I'n not sure what you mean by "parameter names." As a last resort, you can get the configuration of the job as an XML String:
print job.configFile.asString()
Since this is XML, you can parse it as needed. Depending on what parameter names you are looking for, you can probably also get them directly in Java. Can you post the XML for what you are looking for? That will help in commenting on a better way.
For the parameters needed by this poster, it is easy to get using Groovy:
def params = job.getProperty('hudson.model.ParametersDefinitionProperty')
.parameterDefinitions
params.each { p -> println "$p.name"}