Tải bản đầy đủ (.pdf) (358 trang)

jruby on rails, 2007

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.71 MB, 358 trang )

this print for content only—size & color not accurate spine = 0.839" 360 page count
EMPOWERING PRODUCTIVITY FOR THE JAVA

DEVELOPER
Practical JRuby on Rails Web 2.0 Projects:
Bringing Ruby on Rails to the Java

Platform
Dear Reader,
The book you’re holding in your hand right now introduces JRuby on Rails in a
practical and easy style. The Ruby language and the Rails framework have been
making waves in the developer world for a while now. JRuby is the next step in
the evolution of Ruby. This book introduces Ruby, Rails, and JRuby; describes
how you can use them to create exciting solutions that cross the chasm between
the Java™ and Ruby languages; and also shows how you can revolutionize your
web development in Java by making use of JRuby on Rails.
In my opinion, JRuby is the one technology that is building bridges between
the powerful Java platform and a more dynamic programmer experience. This
is epitomized by JRuby on Rails, which gives you all kinds of new and important
possibilities. Many current Java web frameworks are actively being inspired by
the Rails developer experience, but none of them are even close yet.
That’s why I wrote this book. I wanted to tell the world that there is a better
alternative. I wanted to show in a clear and obvious way how you can make
good use of these exciting technologies. I wanted to document JRuby but also
provide practical examples that could be immediately applied. That’s why this
book employs a project-driven approach. Most of the code introduced is part of
one of the four systems that you build during the course of the book.
After reading this book, you should be able to start a new project based on
JRuby on Rails and take it all the way from inception to production. If you don’t
know Ruby, you’ll be able to pick it up from this book. You’ll learn how to use
the Rails framework to build web applications—and you’ll also learn how to


apply these in a JRuby environment.
Ola Bini
JRuby Core Developer
US $42.99
Shelve in
Java Programming/
Development
User level:
Intermediate–Advanced
Bini
JRuby on Rails
Web 2.0 Projects
THE EXPERT’s VOICE
®
IN JAVA

TECHNOLOGY
Practical
JRuby on Rails
Web 2.0 Projects
Bringing Ruby on Rails to the Java


Platform
CYAN
MAGENTA
YELLOW
BLACK
PANTONE 123 C
Ola Bini

JRuby Project Lead/Core Developer
Forewords by Pat Eyler and Martin Fowler, Chief Scientist, ThoughtWorks
Companion
eBook
Available
THE APRESS ROADMAP
Beginning Rails
Beginning POJOs
The Definitive Guide
to Grails
Practical JRuby on Rails
Web 2.0 Projects
www.apress.com
java.apress.com
SOURCE CODE ONLINE
Companion eBook

See last page for details
on $10 eBook version
ISBN-13: 978-1-59059-881-8
ISBN-10: 1-59059-881-4
9 781590 598818
5 4 2 9 9
Learn and apply the new agile open source JRuby to
bring your Ruby on Rails Web 2.0 code and projects into
your enterprise Java

application stack and more.
Practical

Ola Bini
Practical JRuby on Rails
Web 2.0 Projects
Bringing Ruby on Rails to the
Java

Platform
881-4 FM.qxd 8/31/07 1:43 PM Page i
Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to the Java™ Platform
Copyright © 2007 by Ola Bini
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-59059-881-8
ISBN-10 (pbk) 1-59059-881-4
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Java™ and all Java-based marks are the trademarks or registered trademarks of Sun Microsystems, Inc.,
in the US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was
written without endorsement from Sun Microsystems, Inc.
Lead Editor: Steve Anglin
Technical Reviewer: Pat Eyler
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jason Gilmore,
Jonathan Hassell, Matthew Moodie, Jeffrey Pepper, Ben Renow-Clarke, Dominic Shakeshaft,
Matt Wade, Tom Welsh
Project Manager: Sofia Marchant
Copy Editor: Susannah Pfalzer
Assistant Production Director: Kari Brooks-Copony

Production Editor: Laura Cheu
Compositor: Gina Rexrode
Proofreader: Lisa Hamilton
Indexer: Julie Grady
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or
visit .
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit
.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any
person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by
the information contained in this work.
The source code for this book is available to readers at in the Source Code/
Download section. You will need to answer questions pertaining to this book in order to successfully
download the code.
881-4 FM.qxd 8/31/07 1:43 PM Page ii
This book is dedicated to Hans Nordlöf for believing in me and always being my mentor.
881-4 FM.qxd 8/31/07 1:43 PM Page iii
Contents at a Glance
Foreword by Pat Eyler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Foreword by Martin Fowler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
■CHAPTER 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■CHAPTER 2 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

PROJECT 1
■ ■ ■
The Store (Shoplet)
■CHAPTER 3 Introduction to Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
■CHAPTER 4 Store Administration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
■CHAPTER 5 A Database-Driven Shop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
PROJECT 2
■ ■ ■
A Content Management
System (CoMpoSe)
■CHAPTER 6 Java Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
■CHAPTER 7 A Rails CMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
■CHAPTER 8 Content Rendering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
PROJECT 3
■ ■ ■
An Administration System
(Big Brother)
■CHAPTER 9 A JRuby Enterprise Bean. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
■CHAPTER 10 An EJB-Backed Rails Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
■CHAPTER 11 Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
iv
881-4 FM.qxd 8/31/07 1:43 PM Page iv
PROJECT 4
■ ■ ■
A Library System (LibLib)
■CHAPTER 12 Web Services with JRuby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
■CHAPTER 13 JRuby and Message-Oriented Systems. . . . . . . . . . . . . . . . . . . . . . . . 229
■CHAPTER 14 The LibLib Rails Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
■CHAPTER 15 Coda: Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
■APPENDIX A Ruby for Java Programmers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289

■APPENDIX B JRuby Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
■APPENDIX C Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
v
881-4 FM.qxd 8/31/07 1:43 PM Page v
881-4 FM.qxd 8/31/07 1:43 PM Page vi
Contents
Foreword by Pat Eyler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Foreword by Martin Fowler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
■CHAPTER 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
A Brief History of Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
A Brief History of Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
A Brief History of JRuby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Why JRuby on Rails? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Overview of the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Chapter 1: Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Chapter 2: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Project 1: The Store (Shoplet) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Chapter 3: Introduction to Rails. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Chapter 4: Store Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Chapter 5: A Database-Driven Shop. . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Project 2: A Content Management System (CoMpoSe) . . . . . . . . . . . . 7
Chapter 6: Java Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Chapter 7: A Rails CMS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Chapter 8: Content Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Project 3: An Administration System (BigBrother) . . . . . . . . . . . . . . . . 7

Chapter 9: A JRuby Enterprise Bean. . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Chapter 10: An EJB-Backed Rails Application . . . . . . . . . . . . . . . . . . . 7
Chapter 11: Deployment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Project 4: A Library System (LibLib) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Chapter 12: Web Services with JRuby . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Chapter 13: JRuby and Message-Oriented Systems. . . . . . . . . . . . . . 8
Chapter 14: The LibLib Rails Application . . . . . . . . . . . . . . . . . . . . . . . . 8
vii
881-4 FM.qxd 8/31/07 1:43 PM Page vii
Chapter 15: Coda: Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Appendix A: Ruby for Java Programmers . . . . . . . . . . . . . . . . . . . . . . . 8
Appendix B: JRuby Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Appendix C: Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
■CHAPTER 2 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Installing JRuby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Binary JRuby. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
JRuby from Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Testing the Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
RubyGems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Rake. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
ActiveRecord-JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
BlueCloth and RedCloth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Facets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Mongrel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Mongrel JCluster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Setting Up a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

PROJECT 1
■ ■ ■
The Store (Shoplet)
■CHAPTER 3 Introduction to Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
The Structure of a Rails Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Controllers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
The Other Parts of Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
ActiveSupport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
ActionMailer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
ActionWebService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
ActiveResource. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
■CONTENTSviii
881-4 FM.qxd 8/31/07 1:43 PM Page viii
Rails Helper Scripts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
about . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
breakpointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
destroy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
generate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
plugin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
runner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Plug-Ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Acts As Taggable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
CAS Filter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Globalize Plug-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Rails Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
■CHAPTER 4 Store Administration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Creating a New Rails Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Running with Mongrel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
A First Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Product Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Product Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Running the Migrations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Validations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Unit Testing Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Creating a Scaffold for Products. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Ajax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Adding Some Good Looks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
More Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
User Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Order Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Adding Some Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Functional Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
■CONTENTS ix
881-4 FM.qxd 8/31/07 1:43 PM Page ix
■CHAPTER 5 A Database-Driven Shop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Browsing Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Adding a Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Viewing the Cart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Checking Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Validation and Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
ActiveRecord and JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Supported Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

How to Support a New Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
PROJECT 2
■ ■ ■
A Content Management
System (CoMpoSe)
■CHAPTER 6 Java Integration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Using Java Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Primitives. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Extending Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Java Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Gotchas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Using Ruby from Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
The JRuby Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Bean Scripting Framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
JSR223—Java Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
■CHAPTER 7 A Rails CMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
The Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
The Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Some Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
■CONTENTSx
881-4 FM.qxd 8/31/07 1:43 PM Page x
Administration Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Articles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Some Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
■CHAPTER 8 Content Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Content Rendering with XML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Ruby XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Java DOM Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Java SAX Parsing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Java DOM and XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Other Java APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Other Ways to Render Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
RedCloth (Textile). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
BlueCloth (Markdown). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
ERb. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
YAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Other Solutions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Finishing CoMpoSe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Rendering Engine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Previews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
PROJECT 3
■ ■ ■
An Administration System
(Big Brother)
■CHAPTER 9 A JRuby Enterprise Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
The Sequence Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
A JRuby Sequence Engine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

A JRuby Bean Wrapper. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
■CONTENTS xi
881-4 FM.qxd 8/31/07 1:43 PM Page xi
■CHAPTER 10 An EJB-Backed Rails Application. . . . . . . . . . . . . . . . . . . . . . . . . 181
The EJB Client Revisited. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Creating the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Creating a Small Sequence Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Sequence Controller and Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
JMX for the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Simple JMX Access to Rails. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
■CHAPTER 11 Deployment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Deploying Ruby on Rails. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
WEBrick. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
CGI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
FastCGI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Mongrel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Pack of Mongrels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Deploying JRuby on Rails. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
WEBrick. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
CGI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Mongrel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
GoldSpike (Rails Integration) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Grizzly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Rails-asyncweb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Retty. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Best Practice JRuby on Rails Deployment . . . . . . . . . . . . . . . . . . . . . . . . . 204
A Pack of JVM Mongrels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Make WAR with Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
PROJECT 4
■ ■ ■
A Library System (LibLib)
■CHAPTER 12 Web Services with JRuby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
The LibLib System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Amazon Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
SOAP4R. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Dynamic Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Using Stubs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
■CONTENTSxii
881-4 FM.qxd 8/31/07 1:43 PM Page xii
SOAP with Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Dynamic Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Using Stubs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Creating a Small Book Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
■CHAPTER 13 JRuby and Message-Oriented Systems . . . . . . . . . . . . . . . . . . 229
What Is MOM? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
The Legacy System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Add Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Remove Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Get Library Name. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Add Book Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Remove Book Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Get Book Description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Add Book Instance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Remove Book Instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Lend Book Instance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Return Book Instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234

Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
ActiveMessaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
JRuby and Message-Driven Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
A Library for Legacy Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Inter-Rails Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
■CHAPTER 14 The LibLib Rails Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
The Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Deploying More Than One Rails Instance . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Creating the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Views and Controllers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Searching and Looking at Books. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Borrowers and Librarians. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Importing from Amazon.com . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
■CONTENTS xiii
881-4 FM.qxd 8/31/07 1:43 PM Page xiii
73ed30358d714f26dd2d9c0159f8cfe0
■CHAPTER 15 Coda: Next Steps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
JRuby-extras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Contributing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Current Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Contributing to JRuby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Home Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Database Indexing with Lucene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Replacing ActiveRecord with Hibernate. . . . . . . . . . . . . . . . . . . . . . . 284
Creating a New ActiveRecord-JDBC Adapter . . . . . . . . . . . . . . . . . . 284
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287

■APPENDIX A Ruby for Java Programmers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Core Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Core Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Classes and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Defining Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Including and Extending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
The Singleton Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Metaprogramming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Introspection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
send . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
method_missing, const_missing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
define_method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Class.new and Module.new. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
eval and Friends. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
The Symbol to_proc Trick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
■APPENDIX B JRuby Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Referencing a Java Class or Java Interface . . . . . . . . . . . . . . . . . . . 307
Using Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Extension and Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
■CONTENTSxiv
881-4 FM.qxd 8/31/07 1:43 PM Page xiv
Primitive Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Extensions to Java Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
java.lang.Runnable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
java.util.Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
java.lang.Comparable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
java.util.Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

java.util.List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
The JRuby Module. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
parse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
compile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
require. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
■APPENDIX C Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Ruby and Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
The Ruby Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Ruby-talk Mailing List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Ruby-core Mailing List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Ruby on Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Rails-talk Mailing List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Rails-core Mailing List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Matz Blog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
O’Reilly Ruby. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
RubyInside. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
On Ruby. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Loud Thinking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Riding Rails. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Eigenclass. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Polishing Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Programming Ruby, Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . 317
The Ruby Way, Second Edition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Agile Web Development with Rails, Second Edition. . . . . . . . . . . . . 317
■CONTENTS xv
881-4 FM.qxd 8/31/07 1:43 PM Page xv
JRuby. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
The JRuby Home Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318

The JRuby Dev Mailing List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
The JRuby User Mailing List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
The #jruby IRC Channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
The JRuby-extras Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
JRuby JIRA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
JRubyInside . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Headius . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Tom’s Ruminations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Ola Bini . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Nick Sieger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Other . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
ActiveMessaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Hitta . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Ferret . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
GlassFish. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
■CONTENTSxvi
881-4 FM.qxd 8/31/07 1:43 PM Page xvi
Foreword by Pat Eyler
“Hey, you got your Ruby in my Java!”
“You got your Java on my Ruby!”
I’m not going to claim that JRuby is as delicious as a Reese’s Peanut Butter Cup, but it sure has
been a real treat watching Ola and his codevelopers work on JRuby. They’ve taken an incom-
plete, niche Ruby environment (and subcommunity) and turned it into something that is
pushing the whole Ruby community in a number of different ways.
Some time ago, Tim Bray was lamenting the lack of solid programmer tools (things like a
powerful IDE, a refactoring browser, etc.) in the Ruby world. At the time, I took the stance that
they hadn’t really been developed because the Ruby community was able to do without them.
With the advent of JRuby, though, NetBeans and Eclipse have really begun to gain momentum

and are starting to produce the kinds of tools Tim was pining for into the Ruby space. I think a
lot of this came to pass because JRuby drew Java developers with Tim’s same desire for tools
into the Ruby world.
The JRuby team’s commitment to testing their implementation has been quietly filtering
back into the other development teams. These days, it’s common to see JRuby developers
hanging out on the rubinius IRC channel (#rubinius on freenode), the YARV developers are
making appearances on both the rubinius and JRuby IRC channels, and so on. The discus-
sions between these different development groups have been great to sit in on. I think you
could even build a case that JRuby has helped spur Microsoft’s IronRuby.
With JRuby running on the JVM, Ruby is better able to get into some enterprise environ-
ments. Even at my day job, where I’ve been pushing Ruby from day one, JRuby is making Ruby
solutions possible in places it wouldn’t have gone on its own. Pretty soon, JRuby will be our
common gateway between the infrastructure world of quick Ruby scripting and the applica-
tion world of large-scale Java apps. It’s a future I’m looking forward to.
JRuby has been driving changes in the Java/JVM world too. JRuby’s success and accept-
ance is helping make the JVM a better place for languages like Groovy, Jython, and their
cousins. Common requirements are being pushed by a new, more dynamic voice inside of
Sun, and over the next couple of years it will make quite a difference.
Whether you’re a Java hacker who’s new to Ruby or a Rubyist taking your first steps into
Java, this is a great guidebook to help you navigate the gray area between the new languages.
I hope you’ll enjoy Ola’s efforts to help you see not only how great JRuby is on its own, but also
how great Java and Ruby taste together.
This book is really about bringing you into an expanding new community. With each new
JRuby user, the potential for exciting change grows. I hope you’ll take up the torch as you read
this book, and that you’ll soon be out there finding (and sharing) new ways to use JRuby to
make your life better.
Happy JRuby hacking! I’ll look forward to seeing you on the JRuby IRC channel and
mailing lists soon.
xvii
881-4 FM.qxd 8/31/07 1:43 PM Page xvii

881-4 FM.qxd 8/31/07 1:43 PM Page xviii
Foreword by Martin Fowler
The world of web application development has been given quite the shake in the past couple
of years by the rise of Ruby on Rails. Many famous names (or incessant loudmouths) who are
well known in the Java world have become strong advocates of Ruby and Rails—even to the
point of leaving the Java world for good.
I’ve been using Ruby for many years, and I’m a big advocate of the language. It focuses on
a clear but simple syntax that I find captures my intentions much more clearly than the main-
stream curly brace languages. It’s fully object oriented and has powerful language features
such as closures. In particular, it offers a wide range of tools for metaprogramming and creat-
ing domain-specific languages. These features underpin Rails—making it much easier to
create such an influential web framework.
Since Rails has appeared, I’ve talked to many colleagues who’ve given it a spin. These are
people with track records of delivery with various Java and .NET web platforms. Overwhelm-
ingly, what I hear is that they feel their work is significantly more effective with Rails. I don’t
take statements like “50% more productive” seriously, not least because software productivity
is not something we can clearly measure. Lacking that, a clear majority of qualitative approval
is the strongest sign of a good technology that we are likely to find.
Thus far, most books and articles have focused on using Ruby on Rails in its original C
implementation. This volume is different because it works with the same Rails on a different
platform—Java. I view the JRuby effort to create a fully effective Ruby implementation on the
Java JVM to be an important project both for Ruby and Java. For Ruby developers, it offers a
deployment platform that is well understood, particularly in corporations. We’ve already
found that doors that were once closed to Rails now open when we start talking about a Java
deployment.
For the Java community, JRuby is important because it offers a chance to experience a
powerful language and framework while still taking advantage of Java’s excellent libraries and
the ability to work in both Ruby and Java. I see a polyglot future for the JVM, one where there
is a choice of languages you can use on it—languages that can interoperate cleanly so you can
choose the right language for a particular project. JRuby is an important step in this direction

because it brings not only a language to the JVM, but also an important framework. This book
is an important tool to understanding Rails in its new caffeinated home.
xix
881-4 FM.qxd 8/31/07 1:43 PM Page xix
881-4 FM.qxd 8/31/07 1:43 PM Page xx
About the Author
■OLA BINI, a longtime developer from Sweden, started programming at the age of 9 with Basic
on an Apple IIc; from there he learned C, C++, Assembler, Lisp, Java, Ruby, and various other
languages. He has no formal education except for a few Sun Java certifications. He worked as
system developer and architect at Karolinska Institutet between 2001 and 2007. Ola is now a
developer for ThoughtWorks Studios, the product development division of ThoughtWorks,
Ltd. He has contributed to various open-source projects, and is one of the core developers for
the JRuby project.
xxi
881-4 FM.qxd 8/31/07 1:43 PM Page xxi
881-4 FM.qxd 8/31/07 1:43 PM Page xxii
About the Technical Reviewer
■PAT EYLER has been involved in the Ruby community since 2000 and has organized the first
two semiannual Ruby Implementors Summits. He’s lucky enough to work for the Church of
Jesus Christ of Latter-day Saints on a project that he loves and to be able to use Ruby there
(and maybe JRuby soon). When he’s not working with Ruby or writing about it, he can be
found outdoors serving as a scoutmaster for a small Boy Scouts troop in Utah or hanging out
at home with his family, dog, cat, fish, and books.
xxiii
881-4 FM.qxd 8/31/07 1:43 PM Page xxiii

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×