Guy Matz | 3 May 21:58 2016
Picon

Groovy defaults method argument to null?

I have a method that starts like this:
class Sidecar {
def static String getVersions(String render_server) {

def rest = new RESTClient('http://' + render_server) try { def vpath = '/r/api/versions'
log.debug "Hitting http://${render_server}${vpath}"
def resp = rest.get(path:vpath)
and when I call it as Sidecar.getVersions('server-name') it works as expected. When I "accidentally" call it without a parameter, though, Groovy does not complain until it attempts the REST call:groovy:000> r = Sidecar.getVersions()
15:49:41.209 [main] DEBUG com.jcrew.jenkins.helpers.Sidecar - Hitting http://null/r/api/versions

Is Groovy defaulting the parameter to null when it's not supplied?
Suderman Keith | 2 May 23:24 2016

Groovy kernel for Jupyter Notebook

Does anyone know of a Groovy kernel for iPython/Jupyter (http://jupyter.org/)?   My searches have not turned up much. I don't want a Groovy alternative to iPython/Jupyter notebooks (i.e. http://beakernotebook.com/ won’t cut it), I need a Groovy kernel for Jupyter I can extend to integrate with a Python application/framework we are using.

I found https://github.com/jericks/geoscript-groovy-kernel but it is a simply a Python wrapper that calls geoscript-groovy and I was hoping for a native implementation, that is, something written in Groovy that listens to and responds on the ZMQ sockets.

I have a simple proof of concept implementation working, but I don't want to put more effort into reinventing the wheel if there is already something better out there.

Cheers,
Keith
------------------------------
Research Associate
Department of Computer Science
Vassar College
Poughkeepsie, NY

Guy Matz | 27 Apr 18:36 2016
Picon

RESTClient error when POSTing JSON with embedded GString

Hi!  The code below chokes when I try to use the GString "${build.number}" within the fields var.  The error I get is:
Apr 27, 2016 12:32:25 PM net.sf.json.JSONObject _fromBean
INFO: Property 'value' of class org.codehaus.groovy.runtime.GStringImpl has no read method. SKIPPED

Anyone know what I'm doing wrong there?  Works fine when i use a regular string

import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.JSON

def build=['number':456]
def changes=['data':'123 -> 124', 'render':'234 -> 235','web':'678 -> 679']
def jiraUser='jira'
def jiraPassword='password'
def changelog='ch-ch-ch-ch-changes'

def jiraProject = 'Change Management System'
def jiraProjectCode = 'CMS'
def fields = ['project': [key:'CMS'],
   'issuetype': [name:'Continuous Delivery'],
   // 'summary': "Changes in QA Deploy ",
   'summary': "Changes in QA Deploy #${build.number}",
   'description': changelog,
   'customfield_20904': build.number.toString(),
   'customfield_20905': changes['data'],
   'customfield_20906': changes['render'],
   'customfield_20907': changes['web']
]
def reqParams = [
  fields : fields
]

def jiraServer = 'https://stage.atlassian.net'
def createTicketPath = '/rest/api/latest/issue'

def rest = new RESTClient(jiraServer)
rest.headers['Authorization'] = 'Basic '+"${jiraUser}:${jiraPassword}".bytes.encodeBase64()

try {
  println "Attempting REST call with . . ." + reqParams
  def resp = rest.post( path : createTicketPath, body: reqParams, contentType: JSON)
  println resp.status
  println resp.data
}
catch(Exception e) {
  println "ERROR!!! " + e
}

Philippe Chantry | 27 Apr 13:16 2016
Picon

512k limit ?

Hi all,

I'm trying to upload files to an ECM with a multipart POST HTTP request 
using HttpURLConnection.

It works fine if the file size is less than or equal to 524288 bytes (= 
512k), and fails if the file is larger.

Is 512k an inner limitation of groovy for a POST parameter size ? Is 
there some workarounds ?

(The same program written in pure java has not this limitation problem.)

Thank you,
Philippe

Guy Matz | 22 Apr 15:45 2016
Picon

Garb'ing libraries in jenkins script console

Anyone know how to get this - or an equivalent - to work in jenkins script console?

<at> Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.2')
import groovyx.net.http.*

Thanks!
Gerald Wiltse | 21 Apr 09:24 2016
Picon
Gravatar

Deploying extension Methods

Hello All, 

I'm trying to deploy my first groovy extension methods to remove JVM.  The methods work great in my IDE, but not on a remote JVM which has loaded the JAR. 

I use gradle to build, i can see that the metadata and class are included in the JAR, but I get the error:

No signature of method: java.util.ArrayList.sumSelectedAttributes() is applicable for argument types: (java.util.ArrayList) values: [[messageCount, incomingConnectionsCount, outgoingConnectionsCount, ...]]

What could be the difference between calling the methods locally versus calling them on a JVM which has the JAR on the classpath which is causing this?

I mostly followed MR Haki's wonderful tutorial: 

Here are more details. 

Any insight would be much appreciated. 


John Smiljanic | 20 Apr 22:08 2016
Picon

Adding a Trait to a ClassNode during semantic analysis

Hi All,

Groovy 2.3.7.

I've been experimenting with trying to add a Trait (or Mixin) to a ClassNode during semantic analysis (compiler phase).  I've tried using ClassNode::addInterface and ClassNode::addMixin passing the appropriate.  I sort of expected ClassNode to have an addTrait or something like. 

Is setting a trait at this phase of compilation supported?

JR
Gerald Wiltse | 20 Apr 07:07 2016
Picon
Gravatar

Methods for Creating Ranges

I can find no examples of different ways to create a range.  There's a plethora of examples on what you can do when you start by creating a range like so:  "1..10"

But, how does one create a range when the min and max values are stored in variables?  There's no range constructor.  I see that it's a form of a list, but I see no helper methods for dynamically creating ranges given a min and max value. 

I even tried to get really fancy, but this evaluates to a string. 

def "10..15"
assert Eval.x(v"return x").getClass().name == "​​​​​​​​​​​java.lang.String"

My use case is this.  I populate a bunch of form fields with variable definitions... but they all get passed to my code as strings. But I want to pass port ranges and lists and maps. So, the Eval() method is exactly what I needed.. it just isn't working for ranges. 

Regards,
Jerry


Erwin Müller | 19 Apr 14:50 2016

Weird behavior with .with

Hello,

I'm trying to construct a DSL, but I don't like to write a builder, I want to 
use just the Groovy syntax. But I have now a weird behavior of a script. I try 
to explain in the source code.

----
profile.with { // Profile class
    hostname with { } // ProfileProperties class // 1.
    hosts() with { } // 2.
    remote.with { service = 'openssh' } // 3.
    mail with { // 4.
        service = 'postfix'
        delivery = 'courier'
    }
}
----
----
class Profile {

    Object methodMissing(String name, Object args) {
         p = new ProfileProperties()
         list.add(p);
         return p
    }

    Object propertyMissing(String name, Object args) {
         p = new ProfileProperties()
         list.add(p);
         return p
    }
}
----
----
class ProfileProperties {

    Object propertyMissing(String name, Object args) {
         map.put(name, args);
    }
}
----

1. here, the method Profile#methodMissing() is called with name="hostname" and 
args=[null], that returns ProfileProperties

2. here, the method Profile#methodMissing() is called with name="hostname" and 
args=[], that returns ProfileProperties

3. a) here, the method Profile#propertyMissing() is called with name="hostname" 
and args=null, that returns ProfileProperties. 

3. b) Later, the method ProfileProperties#propertyMissing() is called with 
name="service" and args="openssh"

4. a) Here, the method Profile#propertyMissing() is called with name="service" 
and args="postfix". *That is strange.* Why is first the property called?

4. b) Later, the method Profile#methodMissing() is called with name="mail" and 
args=[courier], that returns ProfileProperties. *That is strange.* Why is the 
argument "courier"?

Why is in case 4. first the properties "service" and "delivery" called, and not 
the method "mail"?

I would have expected the behavior that first the method "mail()" is called, 
that invokes the Profile#methodMissing() method that would create and return 
the ProfileProperties object. Then later, the "service" and "delivery" 
properties are intercepted by the ProfileProperties#propertyMissing() method.

Kind regards,

Erwin.

--

-- 
Erwin Müller - erwin.mueller@...
Software Entwickler - (+49)  01577-9505569
Pgp - https://pgp.mit.edu/pks/lookup?op=get&search=0x02E820911DD910FD
Meine Seite - http://www.mueller-public.de/
ANRI - http://www.anr-institute.com/
Gerald Wiltse | 19 Apr 04:01 2016
Picon
Gravatar

Classloader Won't Load Some Classes

I have a bunch of classes in different packages called "Datasources" which are all very similar. They extend an abstract base class, they have quite a few dependencies and implement some traits.  They also use <at> InheritConstructors annotation. 

I am unable to load any of them using newInstance() : 

import com.gsi.monitor.loader.CustomLoader
URLClassLoader loader = CustomLoader.getCustomLoader("master")
Datasource dsi = loader.loadClass("com.gsi.monitor.updater.global.Datasource").newInstance()
dsi.run()
println dsi.output

Error:    unable to resolve class Datasource 


What I find interesting, is that several other classes load as expected, and that the error is "unable to resolve".   The other classes I've tested are simpler classes, so I'm wondering what it is about the "Datasource" classes that us causing them to fail.  Could it be the annotations, or dependencies, etc? 

Note: These are running inside the script engine of Logicmonitor monitoring platform.   It has groovy-all-2.3.0 library. 

Any ideas?



Gerald R. Wiltse
David M. Karr | 18 Apr 19:50 2016
Picon
Gravatar

Required named parameters in constructor?

First of all, I'm not attempting a "Groovy vs. anything else" argument, 
so put away the flamethrowers.  I find myself defending Groovy when I 
have the chance.

I was, however, reading a Ruby book, as I've never looked at it before, 
as I found I had to learn about Puppet, and I concluded that you can't 
work effectively with robust Puppet modules without understanding Ruby.

As a result, I found myself paying attention to to how Ruby features map 
to Groovy.  I'm not talking about syntax, just functional features.

I got to the point in the Ruby book where it mentioned that you could 
set particular keyword parameters (like Groovy Named Parameters) to be 
required.  I can't think of a "direct" way to map this to Groovy.  I 
suppose you could implement an "ad hoc" strategy that throws if 
particular values aren't set.  Is there a more concise way?

Just as an observation, I also note that Ruby allows mixing both 
"positional" and "named" parameters in the same constructor call.


Gmane