You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Tom Schneider <sc...@gmail.com> on 2007/12/02 21:13:47 UTC

JRuby in Struts 2

These last 2 weeks, Richard Burton and I have been working on adding 
JRuby support to S2.  We've been successful in getting a very basic 
action up and running, but we're running into something of an impedance 
mismatch between S2 and Ruby.  Some of the issues we've run into:

1. Ruby's object properties are typeless, this means that when we go to 
set values on ruby actions and ruby domain objects, the type conversion 
is useless because we can't look at the property type to figure out what 
to convert it to.

2. No annotations in Ruby, a lot of functionality in S2 is driven 
through annotations.

3. For the reasons stated in #1 and #2, it's hard to recreate the domain 
model on a save action so the properties can be set back onto the domain 
model.

For these reasons, it quickly becoming clear to me that we can't just 
create s2 actions in ruby.  The language has too many differences with 
Java to really be a drop in replacement.  (Groovy is much better in this 
regard)  So the only alternative I can think of is to create a 
fundamentally new web framework that runs in s2, but uses Ruby as it's 
language.  So I'm curious to see where others on the dev list see Ruby 
fitting into the s2 ecosystem.  Should we have a rails-like framework 
that is pretty close to Ruby on Rails and makes an easy transition from 
rails?  Or do we want something that diverges from the rails and 
provides an alternate to rails development on the JRuby platform?  This 
is a Brave New World for struts so I think it's useful to get some ideas 
on where we might take this.
Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: JRuby in Struts 2

Posted by Brian Pontarelli <br...@pontarelli.com>.
Tom Schneider wrote:
> I agree, I think it would be interesting to create a plugin that gives 
> us a seamless full stack: Struts2/Spring/JPA or Struts2/Guice/JPA.
As an aside, I've been working on a framework originally called Vertigo 
and now called JCatapult (jcatapult.org) that is a full stack framework 
for web apps. It uses Struts2/SmartURLs/Guice/JPA-Hibernate and a bunch 
of other stuff. It takes cues from Rails and other locations and should 
be up and running with full documentation and examples in the next few 
weeks. Feel free to check out the code and send me questions and comments.

-bp


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: JRuby in Struts 2

Posted by rvalyi <fo...@opensymphony.com>.
Tom,

I don't really know what to think about making a yet new Struts2 oriented toward JRuby on Rails but I'm sure working on a better integration between JRuby on Rails and legacy J2EE frameworks such as Struts2 would be indeed useful.

Some think Rails succeeded mainly because of ActiveRecord. Well the view part which is 'suboptimal' in Struts as you said is also certainly a reason.

As for reusing the clever declarative validation language from Rails, please, bare in mind this reply I already gave to the ActiveHibernate (allow using Hibernate from JRoR) author on the JRuby mailing list:

"I saw a chapter of the 'Rails Recipes' book that could help to do the trick, it's
called how to use ActiveRecords validations without ActiveRecord. So if ever that helps here is the recommended way to do. May be you can include or document this for ActiveHibernate:

the basic trick is to do:

[i]
    Problem
You have your own non-database-backed model objects that require
validations. You would like to re-use the ActiveRecord validation frame-
work to declare how these objects should be validated. You also want to
take advantage of the error_messages_on( ) and error_messages_for( ) helpers
in your views.
How do you make your custom model objects work with ActiveRecord
validations?

[...]

make such a module that takes the ActiveRecord validation module:

module Validateable
 [:save, :save!, :update_attribute].each{|attr| define_method(attr){}}
 def method_missing(symbol, *params)
   if(symbol.to_s =~ /(.*)_before_type_cast$/)
     send($1)
   end
 end
 def self.append_features(base)
   super
   base.send(:include, ActiveRecord::Validations)
 end
end

Place this module in lib/validateable.rb, and you can mix it into your own
non-ActiveRecord models as needed. Now, in terms of validation and
error reporting, your model is virtually indistinguishable from a real
ActiveRecord model.
Here's an example of a non-ActiveRecord model that supports ActiveRe-
cord validations.

 ValidatingNonARObjects/app/models/person.rb
class Person
  include Validateable
  attr_accessor :age
  validates_numericality_of :age
end


To trigger the validations, just call the model's valid?( ) method in your
controllers. This will both return true or false depending on whether
the object passes validations and will populate the model's errors( ) so
that they can be accessed from the ActiveRecord view helpers.
Here's an example of how you can interact with the validations from
the Rails console:
chad> ruby script/console
>> person = Person.new
=> #<Person:0x236e180>
>> person.age = "NOT A NUMBER"
=> "NOT A NUMBER"
>> person.valid?
=> false
>> person.errors
=> #<ActiveRecord::Errors:0x236b430 ...@age="NOT A NUMBER",
@errors=#<ActiveRecord::Errors:0x236b430 ...>>,
@errors={"age"=>["is not a number"]}>
>> person.age = 30
=> 30
>> person.valid?
=> true
>> person.errors
=> #<ActiveRecord::Errors:0x236b430 @base=#<Person:0x236e180 @age=30..>
"[/i]


Hope this will help. Good luck in your implementation. Regards,

Raphael Valyi.
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=172941&messageID=247866#247866


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: JRuby in Struts 2

Posted by Tom Schneider <sc...@gmail.com>.
I agree, I think it would be interesting to create a plugin that gives 
us a seamless full stack: Struts2/Spring/JPA or Struts2/Guice/JPA.

However, one of the advantages of Groovy/Ruby is the fact that the 
classes can be updated just by reloading the page and new language 
features, such as closures and dynamic properties.  This is where rails 
really shines, it takes advantage of those language features in the 
framework itself.  Since we're currently restricted to Java, that limits 
what we can do.  We can get close, but can we get close enough.

One of the areas where I plan on exploring this question is with 
validation.  The current validation in s2 is ok for the basic stuff, but 
falls short when you start getting complicated validation logic.  There 
just isn't good support out-of-the-box for this type of thing, so I was 
hoping to integrate some Grails validation support into s2/webwork.  
Closures are a very elegant way to define validation for a domain 
object, the simplicity of a declarative XML type syntax with the power 
of a full language.
Tom


Ted Husted wrote:
> I would say that Rails is an excellent source of inspiration, but I
> don't think Ruby or Rails is magic. Rails incorporates a number of
> good ideas, and a "take no prisoners" perspective toward productivity,
> but that doesn't mean every idea is perfectly implemented.
>
> It's my belief that we can achieve the same level of productivity in
> Java by using tools like Eclipse, Maven, and JPA to their full extent.
> I'm especially impressed by the plugins now available with MyEclipse.
> With Tomcat and Derby bundled in, we can download Java and MyEclipse
> and start writing enterprise-grade applications out of the box.
>
> A key to the success of Rails has been ActiveRecord. Likewise, a key
> to creating a highly productive Java stack is adopting and exploiting
> JPA. For the type of applications that most people write, especially
> with something like Rails, JPA is an ideal technology.
>
> I'm finishing up a JPA/Struts training course now. I'm presenting it
> next week, and after I hope to do a series of companion tutorials. I'm
> not sure that everyone realizes how close the Java ecosystem may be to
> a productivy tipping point, where we might see 2x and 3x gains, just
> by aggressively integrating and exploiting the tools we already have
> at our fingertips.
>
> -Ted.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
> For additional commands, e-mail: dev-help@struts.apache.org
>
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: JRuby in Struts 2

Posted by Ted Husted <hu...@apache.org>.
On Dec 2, 2007 3:13 PM, Tom Schneider <sc...@gmail.com> wrote:
> So I'm curious to see where others on the dev list see Ruby
> fitting into the s2 ecosystem.  Should we have a rails-like framework
> that is pretty close to Ruby on Rails and makes an easy transition from
> rails?  Or do we want something that diverges from the rails and
> provides an alternate to rails development on the JRuby platform?  This
> is a Brave New World for struts so I think it's useful to get some ideas
> on where we might take this.

I would say that Rails is an excellent source of inspiration, but I
don't think Ruby or Rails is magic. Rails incorporates a number of
good ideas, and a "take no prisoners" perspective toward productivity,
but that doesn't mean every idea is perfectly implemented.

It's my belief that we can achieve the same level of productivity in
Java by using tools like Eclipse, Maven, and JPA to their full extent.
I'm especially impressed by the plugins now available with MyEclipse.
With Tomcat and Derby bundled in, we can download Java and MyEclipse
and start writing enterprise-grade applications out of the box.

A key to the success of Rails has been ActiveRecord. Likewise, a key
to creating a highly productive Java stack is adopting and exploiting
JPA. For the type of applications that most people write, especially
with something like Rails, JPA is an ideal technology.

I'm finishing up a JPA/Struts training course now. I'm presenting it
next week, and after I hope to do a series of companion tutorials. I'm
not sure that everyone realizes how close the Java ecosystem may be to
a productivy tipping point, where we might see 2x and 3x gains, just
by aggressively integrating and exploiting the tools we already have
at our fingertips.

-Ted.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: JRuby in Struts 2

Posted by Dave Newton <ne...@yahoo.com>.
--- Martin Gilday <ma...@imap.cc> wrote:
> I think having erb (is that right?) views

We do; they're called "scriptlets". Erb is a step
backwards from anything I want to be involved with.

To answer somebody else's question: the reason to use
a different framework besides JRoR with JRuby is
because Rails forces you into the Rails framework.

It's difficult to extend, difficult to debug, and I've
spent more time working around its limitations and
code than is practical. For certain application spaces
it's a beautiful thing; step outside of its domain and
it will beat you about the head and shoulders
repeatedly and with great vigor.

Now that the JRuby compiler is working I'd be much
more interested in pursuing it (or Groovy, or Clojure,
or Scala) as "glue", particularly for validation,
rules, possibly configuration, etc. 

(I don't have much interest in ActiveRecord, either,
but that's primarily because I'm currently working
with a lot of legacy data for which ActiveRecord is
particularly unsuited.)

d.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: JRuby in Struts 2

Posted by Martin Gilday <ma...@imap.cc>.
If there is a need for a JRuby web framework in the spirit of S2/WebWork
then I would say there is an argument for keeping it seperate.  Take the
ideology from S2 but then take the best of breed from the Ruby world
where you can.  Things like activerecord-jdbc etc.

Looking at your reply to Ted, if this is a wishlist for adding dynamic
things to S2..  I think that your idea for Grails/Groovy validation
would be really good.
The only part of S2 that I feel is less than perfect is the views.  This
is partly down to Java as a whole, I just don't like JSP/Freemarker that
much.  I think having erb (is that right?) views or GSPs would be pretty
cool.  With Groovy actions you'd be be able to change things on the fly
too.


----- Original message -----
From: "Tom Schneider" <sc...@gmail.com>
To: "Struts Developers List" <de...@struts.apache.org>
Date: Sun, 02 Dec 2007 16:33:09 -0600
Subject: Re: JRuby in Struts 2

No Martin, that's a valid question.  Is there a need for JRuby in S2?  
I'm not really the person driving this initiative, it's Richard Burton 
with assistance from Charles Nutter (one of the JRuby core developers).  
I just provided some S2 technical expertise for them.  However, since 
Sun employs 2 of the core JRuby developers, I don't think it's going 
away any time soon.  I think originally we thought we could write S2 
actions in JRuby.  Since that didn't work out so well, the question 
becomes: 'Now what?'
Tom

Martin Gilday wrote:
> Could you explain your reasoning as to why you need a new framework?  If
> someone wants a Rails like framework for JRuby, why would they not just
> use Rails on JRuby?  If you were to make a diverging framework, as you
> mention, why would it need to have anything to do with Struts 2?  What
> parts of the S2 eco-system would you keep?  Would trying to make it part
> of Struts 2 not preclude you from making full use of JRuby?  If you want
> to do something Rails-esque on the Java platform then there is already
> the fantastic Grails.
>
> Sorry if this sounds negative.  I'm just not sure if there really is a
> need to try and merge in a new Ruby based framework and S2.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: JRuby in Struts 2

Posted by Ted Husted <hu...@apache.org>.
We did/do have BSF/JavaScript Actions for Struts 1. I had been
thinking of bringing that forward for Struts 2, but there's always
something else going on :) You might want to look there for
inspiration:

 * http://struts.apache.org/1.3.8/struts-scripting/index.html

Going back to your original post. As to #1, the type issue, I'd try a
Struts 1 approach. Use only String properties on the Action. Try
replacing the default interceptor with one that doesn't try to do
conversion, but sets everything as if it were a String. Hopefully,
JRuby can take it from there.

Alternatively, there's also the notion of first gathering the input
into a Map in a base Action, and then converting from Map to an object
with properties. This could even be a useful strategy for some Java
implementations and make use of  a marker interface, like MapAware.

As to #2, we haven't had annotations that long, and I don't think it's
been anyone's intention to make Struts 2 an annotation-dependent
framework. What is that we can suddenly do with annotations that can't
be done with XML descriptors?

Moreover, while there might not be annotations in Ruby (yet), could
there be annotations in JRuby?

Overall, I wouldn't overlook the idea of JRuby for Struts 1, Java's
most popular web framework, based on what we already have in the
Struts 1 BSF extension (thanks Don!).

I'd also emphasize the need for JRuby to play nice with JPA. I haven't
done any work with Groovy, but the same suggestion would hold. Though
I see at least someone has tried it:

 * http://www.curious-creature.org/2007/03/25/persistence-made-easy-with-groovy-and-jpa/

-Ted.

On Dec 2, 2007 5:33 PM, Tom Schneider <sc...@gmail.com> wrote:
> No Martin, that's a valid question.  Is there a need for JRuby in S2?
> I'm not really the person driving this initiative, it's Richard Burton
> with assistance from Charles Nutter (one of the JRuby core developers).
> I just provided some S2 technical expertise for them.  However, since
> Sun employs 2 of the core JRuby developers, I don't think it's going
> away any time soon.  I think originally we thought we could write S2
> actions in JRuby.  Since that didn't work out so well, the question
> becomes: 'Now what?'
> Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: JRuby in Struts 2

Posted by Tom Schneider <sc...@gmail.com>.
No Martin, that's a valid question.  Is there a need for JRuby in S2?  
I'm not really the person driving this initiative, it's Richard Burton 
with assistance from Charles Nutter (one of the JRuby core developers).  
I just provided some S2 technical expertise for them.  However, since 
Sun employs 2 of the core JRuby developers, I don't think it's going 
away any time soon.  I think originally we thought we could write S2 
actions in JRuby.  Since that didn't work out so well, the question 
becomes: 'Now what?'
Tom

Martin Gilday wrote:
> Could you explain your reasoning as to why you need a new framework?  If
> someone wants a Rails like framework for JRuby, why would they not just
> use Rails on JRuby?  If you were to make a diverging framework, as you
> mention, why would it need to have anything to do with Struts 2?  What
> parts of the S2 eco-system would you keep?  Would trying to make it part
> of Struts 2 not preclude you from making full use of JRuby?  If you want
> to do something Rails-esque on the Java platform then there is already
> the fantastic Grails.
>
> Sorry if this sounds negative.  I'm just not sure if there really is a
> need to try and merge in a new Ruby based framework and S2.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: JRuby in Struts 2

Posted by Martin Gilday <ma...@imap.cc>.
Could you explain your reasoning as to why you need a new framework?  If
someone wants a Rails like framework for JRuby, why would they not just
use Rails on JRuby?  If you were to make a diverging framework, as you
mention, why would it need to have anything to do with Struts 2?  What
parts of the S2 eco-system would you keep?  Would trying to make it part
of Struts 2 not preclude you from making full use of JRuby?  If you want
to do something Rails-esque on the Java platform then there is already
the fantastic Grails.

Sorry if this sounds negative.  I'm just not sure if there really is a
need to try and merge in a new Ruby based framework and S2. 


----- Original message -----
From: "Tom Schneider" <sc...@gmail.com>
To: "Struts Developers List" <de...@struts.apache.org>
Date: Sun, 02 Dec 2007 14:13:47 -0600
Subject: JRuby in Struts 2

These last 2 weeks, Richard Burton and I have been working on adding 
JRuby support to S2.  We've been successful in getting a very basic 
action up and running, but we're running into something of an impedance 
mismatch between S2 and Ruby.  Some of the issues we've run into:

1. Ruby's object properties are typeless, this means that when we go to 
set values on ruby actions and ruby domain objects, the type conversion 
is useless because we can't look at the property type to figure out what 
to convert it to.

2. No annotations in Ruby, a lot of functionality in S2 is driven 
through annotations.

3. For the reasons stated in #1 and #2, it's hard to recreate the domain 
model on a save action so the properties can be set back onto the domain 
model.

For these reasons, it quickly becoming clear to me that we can't just 
create s2 actions in ruby.  The language has too many differences with 
Java to really be a drop in replacement.  (Groovy is much better in this 
regard)  So the only alternative I can think of is to create a 
fundamentally new web framework that runs in s2, but uses Ruby as it's 
language.  So I'm curious to see where others on the dev list see Ruby 
fitting into the s2 ecosystem.  Should we have a rails-like framework 
that is pretty close to Ruby on Rails and makes an easy transition from 
rails?  Or do we want something that diverges from the rails and 
provides an alternate to rails development on the JRuby platform?  This 
is a Brave New World for struts so I think it's useful to get some ideas 
on where we might take this.
Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org