JRuby Cookbook 1st Edition Edelson 2024 Scribd Download
JRuby Cookbook 1st Edition Edelson 2024 Scribd Download
com
https://ebookname.com/product/jruby-cookbook-1st-edition-
edelson/
OR CLICK BUTTON
DOWNLOAD EBOOK
https://ebookname.com/product/elixir-cookbook-1st-edition-pereira/
ebookname.com
https://ebookname.com/product/php-cookbook-1st-edition-david-sklar/
ebookname.com
https://ebookname.com/product/asterisk-cookbook-1st-edition-leif-
madsen/
ebookname.com
https://ebookname.com/product/john-woo-the-films-2nd-edition-kenneth-
e-hall/
ebookname.com
Dance Research Methodologies 1 Edition Rosemary
Candelario
https://ebookname.com/product/dance-research-methodologies-1-edition-
rosemary-candelario/
ebookname.com
https://ebookname.com/product/the-sage-handbook-of-environment-and-
society-1st-edition-jules-pretty-et-al-eds/
ebookname.com
https://ebookname.com/product/new-york-city-for-dummies-5th-edition-
myka-carroll/
ebookname.com
https://ebookname.com/product/advances-in-heat-transfer-44-1st-
edition-ephraim-m-sparrow/
ebookname.com
The Oceanic Languages John Lynch
https://ebookname.com/product/the-oceanic-languages-john-lynch/
ebookname.com
www.allitebooks.com
www.allitebooks.com
JRuby Cookbook ™
www.allitebooks.com
JRuby Cookbook™
by Justin Edelson and Henry Liu
Copyright © 2009 Justin Edelson and Henry Liu. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://safari.oreilly.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Printing History:
November 2008: First Edition.
O’Reilly and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. JRuby Cookbook, the
image of an African civet, and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
ISBN: 978-0-596-51980-3
[M]
1225300397
www.allitebooks.com
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
1. Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Installing JRuby 4
1.2 Managing Packages with RubyGems 8
1.3 Using Both Ruby and JRuby 9
1.4 Sharing RubyGems 11
1.5 Referencing Java Classes from Ruby 12
1.6 Converting a Ruby Array into a Java Array 15
1.7 Adding JAR Files to the Classpath 16
1.8 Extending a Java Class in Ruby 17
1.9 Implementing a Java Interface in Ruby 18
1.10 Opening Java Classes with JRuby 22
1.11 Setting Up Eclipse for JRuby Development 24
1.12 Setting Up NetBeans for JRuby Development 28
1.13 Platform Detection in a JRuby Application 31
2. JRuby on Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.1 Installing and Setting Up Rails 34
2.2 Packaging Rails As a Java EE Web Application 37
2.3 Using an External Gem Repository with a Web Application 39
2.4 Configuring the JRuby-Rack Servlet 39
2.5 Packaging Rails with a JNDI DataSource 41
2.6 Deploying Rails on Tomcat 42
2.7 Deploying Rails on JBoss 44
2.8 Deploying Rails on Jetty 46
2.9 Deploying Rails with jetty_rails 47
2.10 Deploying Rails with Mongrel 49
2.11 Deploying Rails on the GlassFish v2 Server 51
2.12 Using the GlassFish v3 Gem 52
2.13 Using ActiveRecord Outside of Rails 53
2.14 Accessing Common Java Servlet Information 55
iii
www.allitebooks.com
2.15 Configuring Session Storage 56
2.16 Controlling the Classes, Directories, and Other Files Packaged into
a Rails WAR File 57
2.17 Changing the Name of the WAR File and the Staging Area 58
2.18 Deploying a Rails Application to the Root Context 58
2.19 Creating a Rails Application with Aptana Studio 60
2.20 Accessing Static Files in Your Rails Java EE Application 63
3. Java Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.1 Executing Ruby from Java 66
3.2 Invoking JRuby Through the Bean Scripting Framework 70
3.3 Invoking JRuby Through Java Scripting Support 72
3.4 Logging from Ruby with Jakarta Commons Logging 75
3.5 Using the Java Concurrency Utilities 77
3.6 Creating JavaBean Style Accessor Methods 79
3.7 Writing Consistent Code 80
3.8 Transforming XML with TrAX 80
3.9 Creating a Pool of JRuby Runtimes 82
3.10 Performing Remote Management with JMX 84
3.11 Accessing Native Libraries with JRuby 86
4. Enterprise Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
4.1 Creating a JNDI Context 90
4.2 Sending JMS Messages 92
4.3 Receiving JMS Messages 95
4.4 Implementing an Enterprise JavaBean with JRuby 96
4.5 Defining Spring Beans in JRuby 100
4.6 Creating Refreshable JRuby Spring Beans 103
4.7 Defining JRuby Spring Beans Inline 106
4.8 Applying Spring-Aware Interfaces to JRuby Objects 107
4.9 Creating Spring MVC Controllers with JRuby 110
4.10 Using Hibernate with JRuby 113
4.11 Using the Java Persistence API with JRuby 115
4.12 Making SOAP Calls 116
4.13 Simplifying LDAP Access 118
iv | Table of Contents
www.allitebooks.com
5.7 Manipulating Images 133
5.8 Creating SWT Applications 136
5.9 Accessing the Native Desktop 138
5.10 Accessing the System Tray 139
5.11 Swing Development with JRuby Domain-Specific Languages 141
5.12 Using the Monkeybars Framework for Swing Development 144
5.13 Creating Qt Applications with JRuby 148
7. Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
7.1 Unit Testing Java Code with Test/Unit 176
7.2 Unit Testing Java Code with dust 178
7.3 Unit Testing Java Code with Expectations 179
7.4 Testing Java Code with RSpec 181
7.5 Creating Mock Objects with Mocha 185
7.6 Modifying the JtestR Classpath 187
7.7 Grouping Tests for JtestR 188
7.8 Using the JtestR Command-Line Options 188
7.9 Running JtestR with Ant 190
7.10 Running JtestR with Maven 190
7.11 Improving JtestR Performance 192
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Table of Contents | v
www.allitebooks.com
www.allitebooks.com
Preface
And with that quote, Charles Nutter summarizes the two forces that have recently
brought attention to the JRuby project: the recognition that Java provides a powerful
platform that can be used by languages other than Java, and the increase in interest in
the Ruby programming language. In the recipes ahead, we will explore a wide variety
of usage scenarios for JRuby. In Charles’s terms, some recipes are about taking the suck
out of Java, some are about putting some extra awesome into Ruby, and some are about
both.
Audience
To fully leverage JRuby, you must be able to move freely between the Java and Ruby
domains. In writing the JRuby Cookbook, we had in mind a reader with some under-
standing of both languages, possibly with a better understanding of one or the other.
As a result, you won’t find a lot of basic introductory material, save for the first chapter
where we illustrate the areas where Ruby and Java are similar as well as where they
differ.
Our overall approach is that the purpose of the recipes in this book is not to educate
you on some preexisting Java or Ruby capability, but instead to explain how to use
JRuby within the context of, or as an enhancement to, these existing capabilities. For
example, the recipes in the JRuby on Rails chapter are written for someone who has
already created a (working) Rails application.
vii
www.allitebooks.com
Organization
Chapter 1, Getting Started
This chapter starts off with a brief introduction to JRuby before stepping through
a number of basic usages of JRuby, including how to use the RubyGems package
management system and how to interact with Java code from Ruby code. The
package concludes with a number of recipes about setting up various integrated
development environments (IDEs) for working with JRuby.
Chapter 2, JRuby on Rails
This chapter is focused on a variety of scenarios for deploying Ruby on Rails
applications using JRuby.
Chapter 3, Java Integration
This chapter starts with several recipes about invoking Ruby code from Java code
and then continues into recipes describing the usage of popular Java libraries such
as Java Native Access (JNA) and Jakarta Commons Logging from Ruby.
Chapter 4, Enterprise Java
The recipes in this chapter are all about using JRuby with enterprise Java frame-
works such as JMS, JNDI, EJB, Spring, and Hibernate.
Chapter 5, User Interface and Graphics
This chapter describes a number of JRuby-based frameworks that facilitate the
creation of user interfaces. It also includes recipes about image manipulation,
applets, and desktop integration.
Chapter 6, Build Tools
The recipes in this chapter are focused on using JRuby to enhance the build process
of your Java project. Ant and Maven, the two most popular Java-based build tools,
both have several different ways that JRuby can be used. There are also recipes
about the JRuby-specific build tools Raven and Buildr.
Chapter 7, Testing
The focus of this chapter is on JtestR, a package that includes JRuby and a variety
of popular Ruby testing tools. Through the recipes in this chapter, you will learn
how to write Ruby-based tests of Java code.
Chapter 8, The JRuby Community
This final chapter includes a few recipes about effectively participating in the JRuby
community.
viii | Preface
www.allitebooks.com
Constant width
Used for program listings. Also used within paragraphs to refer to program
elements such as namespaces, classes, and method names.
Constant width italic
Indicates text that should be replaced with user-supplied values.
Preface | ix
Comments and Questions
We at O’Reilly have tested and verified the information in this book to the best of our
ability, but mistakes and oversights do occur. Please let us know about errors you may
find, as well as your suggestions for future editions, by writing to:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the U.S. or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)
To ask technical questions or comment on the book, send email to:
bookquestions@oreilly.com
We have a website for this book where examples, errata, and any plans for future edi-
tions are listed. You can access this site at:
http://www.oreilly.com/catalog/9780596519803
For more information about this book and others, see the O’Reilly website:
http://www.oreilly.com
Acknowledgments
Thanks to the O’Reilly staff, especially our editor Mike Loukides and copyeditor Col-
leen Gorman. Thanks to Steven Shingler for his contribution to Chapter 4. And thanks
to all who reviewed this book including Juan Pablo Tarquino, John Purcell, and David
Koontz.
This book simply would not and could not exist without the tireless efforts of the whole
JRuby project team, including Charles Nutter, Thomas Enebo, Nick Sieger, and Ola
Bini. Thanks also to Sun and ThoughtWorks for their ongoing support of JRuby. The
JRuby project is hosted by The Codehaus; thanks as well to Bob McWhirter for his
work there.
We both would like to thank Nick Rockwell for his ongoing encouragement and
enthusiasm.
Justin Edelson
This book wouldn’t have happened without the love and support of my wonderful wife,
Elizabeth. Special thanks to my sons: Owen, who typed his name all by himself, and
Benjamin, who can’t yet.
x | Preface
Thanks to my team at MTV Networks: Michael Benoit, Keith Griffin, Ramesh Nutha-
lapati, Ilya Reznikov, Chris Sindel, Jeff Yemin, and Jun Zhou, for all their hard work.
Thanks also to Warren Habib for his support.
Henry Liu
Thanks to my friend Jon Baer for inviting me to my first Ruby meeting and being a great
collaborator throughout the years. I’m grateful to Francis Hwang, Matt Pelletier,
Sebastian Delmont, Trotter Cashion, and all the members of the NYC Ruby group.
They taught me Ruby and Rails by answering all my newbie questions, and it was their
passion and enthusiasm for the technology that motivated me to dig deeper. Thanks
to all my colleagues at MTV Networks and specifically Mark Ache, Luke Murphy, and
Steve Azueta for their continued support. Most of all, thanks to my family and my
partner, Naomi; without her, none of this would be possible.
Preface | xi
CHAPTER 1
Getting Started
1.0 Introduction
JRuby is an open source implementation of the Ruby programming language for the
Java Virtual Machine (JVM). It allows Ruby applications to be run within a Java Virtual
Machine and interface with libraries written in either Java or Ruby. Although the JRuby
project was initiated in 2001, interest in JRuby has grown significantly over the last few
years, reflecting an overall growth in interest in Ruby sparked by the success of the
Ruby on Rails framework. Sun has contributed to JRuby’s success by employing mem-
bers of the core development team and providing support for JRuby in the NetBeans
development environment, among other efforts. The website for the JRuby project is
currently http://www.jruby.org.
Ruby
Ruby is a dynamic object-oriented programming language created by Yukihiro Mat-
sumoto, known by the nickname Matz, in the mid-1990s. Ruby follows a style of ver-
sioning similar to the Linux kernel, where an even minor version number indicates a
stable release and an odd minor version number indicates a development release. As a
result, there are two current versions of Ruby: 1.8.6, released in March 2007, is the
current stable release, and 1.9.0, released in December 2007, is the current development
release. The standard Ruby interpreter* is written in C. There are several alternate
implementations of the interpreter, including JRuby, IronRuby (for Microsoft’s .NET
framework), and Rubinius. Ruby does not have a formal language specification; how-
ever, one is being developed through the wiki at http://spec.ruby-doc.org.
As an object-orientated language, many of the underlying concepts of Ruby will be
familiar to Java developers, even if the syntax is not. The biggest exception to this is
Ruby’s support for blocks. In Ruby, a block is a grouping of code that gets passed to a
method call. The receiving method can invoke the block any number of times and can
pass parameters to the block. Support for a similar type of element, a closure, is being
1
Exploring the Variety of Random
Documents with Different Content
law in creating the Project Gutenberg™ collection. Despite these
efforts, Project Gutenberg™ electronic works, and the medium
on which they may be stored, may contain “Defects,” such as,
but not limited to, incomplete, inaccurate or corrupt data,
transcription errors, a copyright or other intellectual property
infringement, a defective or damaged disk or other medium, a
computer virus, or computer codes that damage or cannot be
read by your equipment.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.