1

I have a Groovy class, MyRequest, that is used to process each HTTP request. In the constructor, this class sets an instance variable, _session, to be the value of request.getSession(true) (where request is the current HttpServletRequest).

In my Groovy script, the first thing I do is create an instance of MyRequest. Second, I check for a specific property of the Groovlet session object. With my first request to the script, this session object is null so I get a null object reference error. The second request to the script, the session object is defined and no error occurs.

According to the Groovlet documentation, this situation should be expected because I did not explicitly set the Groovlet's session object after checking for its existence in my script.

I really don't want to have to add yet more copy an paste code to my scripts (and any future ones in the project). This is one of the reasons I created the MyRequest object -- to define the session object for any script that instantiates it. So, how can I define the session object for my Groovy scripts within the MyRequest class? Can I use the metaClass object in some way?

James Sumners
  • 14,485
  • 10
  • 59
  • 77

1 Answers1

0

Groovlets provide session scope for you out of the box, no need for request.getSession().

If from your script you do:

new MyRequest(session)

then given a sloppy, untyped MyRequest class like so:

class MyRequest {
    def _session
    MyRequest(sess) {
        _session = sess
    }

    def nullSafe() {
        if(_sess?.notExist) // do something...
    }
}

MyRequest methods can refer to session safely (provided you use groovy null safe (?) operator). I have moved away from Groovlets, but when I was rolling my own, Groovy gurus suggested using Thread locals when passing session, request, response et al from script scope to objects...

virtualeyes
  • 11,147
  • 6
  • 56
  • 91
  • Since I'm already passing in HttpServletRequest I wanted to avoid passing in session as well. I guess that'll have to work though. Thank you. – James Sumners Nov 30 '11 at 20:32
  • Sorry, I had to unaccept the answer. I accepted it without fully thinking it through. This answer does not really solve the problem I am trying to avoid -- initializing the session object in every script I write in my project. – James Sumners Nov 30 '11 at 21:16
  • @jsumners no problem, I had the same issue, but solved it by having a gateway script that passed groovlet session, request, response, etc. to target object. If you have multiple scripts, may need to extend GroovyServlet a la Gaelyk and inject session via MOP into your "MyRequest" class. Could also make use of missingMethod, PropertyAccessInterceptor, or other MOP tool to get/set session state. Look into Grails as well, session is injected into controllers. In fact, curious why you are not using Grails, too heavyweight for your use case? – virtualeyes Nov 30 '11 at 21:30
  • I'm upset with Grails right now and just want to implement something in Groovy that works and can later be used in Grails. My opinion of Grails and its documentation is not for polite conversation. Groovy's documentation is also grating my last nerve. (If you really want to know -- https://plus.google.com/117985402767330500483/posts/CFdcHUsuD5v ) – James Sumners Nov 30 '11 at 21:50
  • Been there trust me ;-) Gave up on Grails 3X, 1.2, 1.3, and 2.0 However, I discovered a solution to the biggest gripe of all, slow restart times. Grails 2.0 restarts in 3 seconds now including bootstrapped test data. Grails documentation is like Shakespeare compared to Groovy's tangled, archaic web. I won't say don't re-invent the wheel (taking the Groovlet route), I spent months doing so and in the process learned Groovy, something I would not have done, or at least not fully, had I started with just Grails. – virtualeyes Nov 30 '11 at 22:39