Port RJS to Java. Ugh. i dread this task. I really do. Recreating RJS templates in Java….Yuck. I dread this task because I made fun of the prospect of some Java programmer having to recreate RJS in Java a few weeks ago on this blog, but, I’ve created an application in Rails that must be ported to Java. Don’t ask why, just know that it must be ported to Java. This isn’t the first time, I’ve had to port something from Rails to Java.
So…..errr…..what does one do if your Rails “prototype” makes heavy use of some of the most advanced features of RJS templates and you have to migrate to Java? My first thought: ask the fine readership of this blog if they have found themselves in a similar situation, or, better yet, is anyone out there already doing this? C’mon, I can’t be the only one in this wide world who has been using RJS and wondered, “Why can’t I do this in my Java application?” ….The first step, Google “RJS in Java” - that search returned a few hits, but mostly links to Rails RJS pages and links to blogs bemoaning the fact that Java isn’t there yet.
The real lesson here is “Never prototype with a technology more capable than the target platform.” So, take this as a lesson, if you are using Rails as a prototyping language, don’t use RJS, you’ll end up wishing that your prototyping technology was your production technology. As I’ve said before, J2EE is a trusted, capable platform, and in some ways it is lightyears ahead of Ruby, but, as far as web applications go Rails does laps around Java. So, maybe porting RJS to Java might help level the playing field? Maybe? Who knows, all I know is that I’m not the only only who thinks that Java is not Web 2.0.
Read on for the options…
The Options
Mission: “Port a Rails application to Java”, Caveat: “The Rails application makes heavy use of RJS templates”. Specifically, your application calls RJS templates that send a mixture of freeform Javascript, insertion calls, scriptaculous effects, render _partial calls. What options do I have?
1. Write Javascript Directly (Get out the Rhino book) Just write a series of JSP pages that just alter the response MIME type and send back the appropriate Javascript using Prototype and Scriptaculous.
Unfortunately, I’m gathering that much Web 2.0 development in Java consists of developers coding JavaScript directly, and I’m fairly certain that Java developers haven’t moved past XmlHttpRequest as a data transfer technology stage yet. In fact, the idea that Javascript can evaluate a Javascript response isn’t something I’m seeing in any Java frameworks at the moment, and trying to explain the concept to people not familiar with the approach is proving difficult. Here’s the idea, you are writing Javascript that makes a remote request, the response contains Javascript which is evaluated by the same Javascript that made the remote request. Now dig into the details of RJS and you’ll start to realize that having an abstraction on top of this is essential. I’m not willing to write raw Javascript because I’m not willing to debug raw Javascript; as easy as Prototype makes Javascript - Javascript still sucks
Good AJAX development should require a minimum amount of direct Javascript coding; preferably none. You need to be able to rely on a good set of abstractions and helpers that solve 80-90% of your Javascript needs. In Rails, the Javascript helpers and the Javascript generators make it so that one can create a highly AJAX-ified application without having to write reams of Javascript. You only end up writing Javascript to handle the cases not covered by the helpers, and the Javascript you end up writing is straightfoward.
option 1 - dead on arrival.
2. Recreate RJS in Java. Create an Abstraction Similar to the JavaScriptGenerator in Rails In this approach, An existing servlet/action/whatever would just be forwarding to a “jjs” instead of a “jsp”. A JJSServlet would be invoked which would render a script contained in a JJS script.
I’m leaning toward this option, but I’m also resigned to the fact that this approach is: A. Very derivative of rails, B. Will be limited to a subset of Rails features. The JJS script would be analogous to a JSP page that renders Javascript, I’d be writing some abstractions to approximate the page object, I might have to write some logic that would take care of rendering a partial. In other words, I wouldn’t be looking to recreate the entire set of Rails features, I’d be coding to the requirements of the project in question and porting over features as I need them.
….and, the whole time, I’ll no doubt be thinking, “you know, if I could just do this in Rails, it would’ve been done ages ago”. Porting derivative technologies is never very satisfying, you always end up playing catch up.
3. Use another framework, something like Google Web Toolkit.
I could just opt to use Google Web Toolkit, and this is an option I’m going to look into, but the problem here is that I’m not interested in such a dramatic change. What I want is the ability to integrate a Javascript response in the middle of an existing Java web application. I worry that the adoption of Google Web Toolkit is going to just create a whole new level of abstraction that I’m not interested in adopting. But, it has to be said, Google Web Toolkit is to Java as RJS is to Ruby, but methinks that Google Web Toolkit goes a bit too far. From what I can see GWT isn’t something that augments an existing web application, it takes it over.
Conclusion
So there you have it, I’m leaning toward Option #2 and am beginning to think that it needs to be open source for the good of Javaland. Anyone have any input / suggestions?


So, assuming that you're doing this either for a client or your employer, is recreating RJS in Java more cost effective than simply using Rails? Have you been able to demonstrate, on a cost basis(either hourly or a dollar cost), how much they'll be in for on either alternative?
I'm not asking this from the standpoint of a Rails fanboy - if Java's the best platform for building the solution, then who is this random poster to say different? On the other hand, if they don't know that they're asking for a high-risk, high cost port of a proven solution, then they really ought to know that, because you'll otherwise put the project in jeopardy.
And if it isn't high risk or high cost, why are you writing this again? :)
Robert, good question. The motivation in this instance is an organization's goal to standardize on a single production platform. And, from this perspective it does make some sense, organizations plan for a specific production platform, they hire developers for this platform, the operations team has experience deploying to this platform. But, you are certainly right, the risks and costs need to be calculated and communicated.
Personally, I'd really like to have RJS-like features available to me in a Java application using a similar approach. It is a gap that needs to be filled; hopefully, I can interest others in the effort.
Might be faster to port their other Java web applications to Rails ;)
http://article.gmane.org/gmane.comp.lang.ruby.rails/24863
@jherber
http://article.gmane.org/gmane.comp.lang.ruby.rails/24863
>>>
Here are some figures from today's most recent SVN pulls:
Java version:
10361 lines of Java code
1143 lines of JSP
8082 lines of XML
1267 lines of build configuration
-----------------------------------------------------------
20853 TOTAL lines of stuff
Rails version:
494 lines of code (386 "LOC" per rake stats)
254 lines of RHTML
75 lines of configuration (includes comments in routes.rb)
0 lines of build configuration
>>>
Comparisons like that are bollocks if you count the getters, setters and instance variables for the persistent Java-classes. Those getters, setters and instance variables are automatically created by J2EE-Tools available in Eclipse, Netbeans and IDEA.
And by the way: DRY is Bullshit!
I want to see the fields of my persistent classes including the Javadoc in my sourcecode. Why should I consult the SQL-script for that? I really like to know how RoR-developers are documenting their programs if no information about the structure of the persistenct class (ActiveRecord) is available.
@Timothy
> From what I can see GWT isn't something that augments an existing web application, it takes it over.
Just the opposite is true.
You can write the server-part in PHP, Perl, C++, Asp.Net, Ruby, Python, Java or whatever. All you need is a RCP- or JSON-connection to the client. I wouldn't call that a "taking-over". GWT gives you an incredible flexibility. You can even combine an existing web-application say the HTML-part with GWT. You can "ajaxify" any existing static or dynamic page with GWT.
What's about connecting phpBB or MediWiki with a GWT-frontend and reusing the existing PHP-infrasture say the tables, images, skins?
Just an addition:
phpBB has 60 tables. The 2 biggest tables have around 60 fields. Now have fun with Ruby on Rails. RoR is like going back to the eighties. Some years ago I wrote my programs with MS Access openened because I had to copy and paste the names of the table-columns in my programs. You have to do the same with RoR!
Additionally I had to deal with much bigger projects i.e. tables having up to 250 columns (!!) I guess RoR-fanboys are never writing bigger projects. How will you deal with such schemes?
Welcome back to stoneage with Ruby on Rails! There is no excuse and no justification for DRY. Maybe it is good enough for writing some Mickey-Mouse-applications.
@UlrichW, you are right, Rails can't handle applications with more than 2 tables. And, last time I checked every *great* application had at least one table with more than 60 columns. And, even better, *bigger* projects have tables with up to 250 columns. (Huh?) And, DRY, I mean, what's up with that, I like repeating myself as much as possible, I like repeating myself as much as possible. If I were you I'd stick with PHP, PHP is really the ultimate web application platform, by far.
Ulrich... first off if you want to know the fields available to you in an ActiveRecord class look at the db/schema.rb file that is autogenerated for you when you use migrations. Or you can look at Dave Thomas's Annotate Model's plugin.
Also I would suggest you learn about a concept called "normalization"
> Also I would suggest you learn about a concept called "normalization"
I have no control over the table-scheme. It is an existing table-scheme used by a standard-application for transport/logistics.
Take a look at taconite framework. It gives you the abstraction and it's not framework dependent. http://taconite.sourceforge.net/ Take a look at the examples and you'll see how easy it is to use
@sroach, definitely a step in the right direction, but not close enough to what I'm looking for. If insertion, deletion, and replacing was all I was looking for than this would be a good option, but, unfortunately, I'm looking for much more than the operations that framework exposes. Also, I'm looking for something that is going to work with Prototype transparently - from the examples, Taconite requires some JS setup. Also, I need to ability to push down arbitrary JS if needed. Taconite is a good start, but compared to RJS it's not there yet. But thanks. Don't get me wrong, it's an interesting idea - and Taconite could be cannabalized to creating something more capable.
The spring modules team have done some of the things you are talking about with taconite. Personally I don't feel that Javascript needs to be on the server side. https://springmodules.dev.java.net/docs/reference/0.5/html/xt.html#ajax
Also, I'm not too familiar with RJS, can you tell me what RJS has, I might have some other ideas.
2. Recreate RJS in Java. [...] I'm also resigned to the fact that this approach is: A. Very derivative of rails [...]
What's wrong with being derivative of Rails? If it's a valid and effective solution, I don't care where it comes from: what's right is right.
Dave
Why don't you try running your rails app under JRuby?
I'm curious. In what ways is Java lightyears ahead of Ruby?
> In what ways is Java lightyears ahead of Ruby
Oh, man, I'm just curious, how long have you been developing, let's start from the top. First, performance, read the latest interview with Nutter-Bini-Enebo, read various threads in the Ruby community, performance is an afterthought in Rubyland. While Java's VM might not be the fanciest of poodles it has been around for a decade and runs solidly for large institutions in a massively clustered environments. (I'll take clustered JBoss on a 32-way machine over running a bunch of mongrel clusters any day, anyone telling you otherwise is trying to sell you conference seats)
Then there is the level of support and the number of choices in the Java community. I just had to reevaluate web frameworks this weeks, and, in Java, I had at least ten major players to choose from. Each of those with a range of 2-6 years of code time behind them. Then there is the idea that if I need to do something like transform objects to XML - there are about 10 different approaches, all of which have advantages and disadvantages from a performance perspective.
Then there is real world installation, configuration, optimization experience. From monitoring to clustering, Java is lightyears ahead of Ruby in this space.
Ruby's just begun, it's a bright kid, I use it all the time, I try to seel it to people whenever I can. But contrary to the bevy of speaking consultants who roam the country, I'm going to tell you that DHH is wrong when it comes to counting Java out as a dead end.
If Rails is not ready for high-volume apps, is there some combination of Rails and Java that would make sense? Like what about using Java to create Web services that broker the database(s) on the back end, then having Rails models that abstract those services? Maybe ActiveRDF is doing something analogous to part of that. Or does that not deal with the part of Rails that isnt scaling?