< Zurück | Inhalt | Weiter >

261

263

263

264

264

267

268

269

11.6.1

11.6.2

Monday Morning, 10 A.M. ......................................................

Back at His Desk ....................................................................

269

272

11.7

Documenting, Prototyping, and Stakeholder Buy-In ...........................

272

11.7.1

11.7.2

11.7.3

Documenting ..........................................................................

Stakeholder Buy-In ................................................................

Prototyping .............................................................................

272

274

275

11.8

11.9

Review ................................................................................................ What You Still Don’t Know ..................................................................

276

276

11.10 Resources ..........................................................................................

11.11 Exercises ............................................................................................

276

277

Chapter 12

12.1

12.2

12.3

12.4

12.5

12.6

12.7

12.8

12.9

Analysis and Design: Seeking the Objects ................................

What You Will Learn ...........................................................................

Facing the Blank Page .......................................................................

Using CRC Cards ...............................................................................

Finding the Objects .............................................................................

Finding the Methods and Attributes ....................................................

Essential and Nonessential ................................................................

Analysis Paralysis ...............................................................................

Real Software Engineering .................................................................

Core Classes ......................................................................................

279

279

280

280

280

283

284

287

288

289

12.10 Review ................................................................................................

12.11 What You Still Don’t Know ..................................................................

12.12 Resources ..........................................................................................

12.13 Exercises ............................................................................................

289

289

289

292


Chapter 13

13.1

13.2

13.3

13.4

JUnit: Automating Unit Testing ...................................................

What You Will Learn ...........................................................................

JUnit: Why All the Fuss? ....................................................................

Design Then Test Then Code ............................................................

Installing and Running JUnit ...............................................................

295

295

296

296

297

13.4.1

13.4.2

Downloading and Unzipping .................................................. Using JUnit .............................................................................

299

301

13.5

Writing Test Cases .............................................................................

303

13.5.1

13.5.2

JUnit Assertions .....................................................................

Running a Test Case .............................................................

306

308

13.6

13.7

13.8

13.9

Running Test Suites ...........................................................................

Review ................................................................................................ What You Still Don’t Know ..................................................................

Resources ..........................................................................................

309

312

312

313

13.10 Exercises 313

Chapter 14

14.1

14.2

14.3

14.4

14.5

Storing the Data ............................................................................

What You Will Learn ...........................................................................

Follow the Objects ..............................................................................

Of Persistence ....................................................................................

Thinking of the Future, or Painting in Corners .................................... Oracle, PostgreSQL, MySQL .............................................................

315

315

316

316

316

316

14.5.1

14.5.2

14.5.3

14.5.4

MySQL ...................................................................................

PostgreSQL ...........................................................................

Oracle ....................................................................................

Selection Criteria ....................................................................

317

317

318

318

14.6

14.7

14.8

14.9

Being Self-Contained ..........................................................................

Beyond the Basics .............................................................................. Persistence Is Not the Whole Story ....................................................

Setting Up PostgreSQL for BudgetPro ...............................................

318

319

322

322

14.9.1

14.9.2

14.9.3

14.9.4

Installing PostgreSQL ............................................................

Creating a postgres User .................................................

Creating Our Database ..........................................................

Straight JDBC ........................................................................

322

323

324

325

14.10 Review 325


14.11

What You Still Don’t Know ..................................................................

326

14.12

Resources ..........................................................................................

326

14.13

Exercises ............................................................................................

326

Chapter 15

Accessing the Data: An Introduction to JDBC ...........................

327

15.1

What You Will Learn ...........................................................................

327

15.2

Introducing JDBC ...............................................................................

328

15.3

Making Connections ...........................................................................

329


15.3.1 Downloading JDBC for MySQL ..............................................

332

15.4

Querying Data ....................................................................................

332

15.5

Getting Results ...................................................................................

334

15.6

Updates, Inserts, Deletes ...................................................................

336

15.7

Review ................................................................................................

336

15.8

What You Still Don’t Know ..................................................................

336

15.9

Resources ..........................................................................................

337

15.10

Exercises ............................................................................................

338

PART III

Developing Graphical User Interfaces ................

339

Chapter 16

Getting in the Swing of Things: Designing a GUI for BudgetPro ......................................................................................


341

16.1

What You Will Learn ...........................................................................

341

16.2

A Simple Swing Program ....................................................................

342

16.3

Stompin’ at the Savoy, or The Swing Paradigm .................................

343

16.4

Slow, Slow, Quick-Quick, Slow: The Basic Swing Objects .................

345

16.5

Layout Managers ................................................................................

347

16.6

Beyond Arthur Murray: Actions, Listeners, Events .............................

348

16.7

Getting Down to Cases: Designing a GUI for BudgetPro ...................

348


16.7.1 Overview ................................................................................

350


16.7.2 Creating Pieces ......................................................................

352

16.8

Review ................................................................................................

373

16.9

What You Still Don’t Know ..................................................................

374

16.10

Resources ..........................................................................................

375

16.11

Exercises ............................................................................................

375


Chapter 17

17.1

17.2

Other Ways: Alternatives to Swing .............................................

What You Will Learn ...........................................................................

The IBM SWT Toolkit .........................................................................

377

377

378



17.2.1

Another GUI Toolkit. Why? ....................................................

378

17.2.2

Duplicated Effort. Why Cover It? ............................................

379

17.2.3

Portability: Better and Worse ..................................................

380

17.2.4

The Rest of the Chapter .........................................................

380

17.2.5

SWT: Close to the Metal ........................................................

380

17.2.6

“Hello, world” SWT Style ........................................................

381

17.3

Porting

BudgetPro to SWT .................................................................

384


17.3.1

Step 1: Convert the Class Members ......................................

387


17.3.2

Step 2: Converting the main() Method ..............................

389


17.3.3

Step 3: Converting the GUI build() and init()

Methods .................................................................................


391


17.3.4

Completing the Conversion of the BudgetPro Class ........

394


17.3.5

Completing the Conversion of the Application .......................

395


17.3.6

Closing Thoughts ...................................................................

395

17.4

17.5

17.6

17.7

17.8

SWT and gcj ......................................................................................

Review ................................................................................................ What You Still Don’t Know ..................................................................

Resources ..........................................................................................

Exercises ............................................................................................

396

398

398

398

399



PART IV

Developing Web Interfaces ..................................

401

Chapter 18

Servlets: Java Pressed into Service ............................................

403

18.1

What You Will Learn ...........................................................................

403

18.2

Servlets: Program-Centric Server-Side Documents ...........................

404

18.3

Perspective .........................................................................................

405

18.4

How to Write a Servlet ........................................................................

407

18.5

Input, Output .......................................................................................

411

18.6

Matters of State: Cookies, Hidden Variables, and the Dreaded “Back” Button ......................................................................................


413


18.6.1 Cookies ..................................................................................

414

18.7

Designing a BudgetPro Servlet ..........................................................

416


18.7.1

18.7.2

Prototype ................................................................................

Design ....................................................................................

416

417

18.8

18.9

Review ................................................................................................ What You Still Don’t Know ..................................................................

420

420

18.10 Resources ..........................................................................................

18.11 Exercises ............................................................................................

421

421

Chapter 19

19.1

19.2

19.3

JSP: Servlets Turned Inside Out .................................................

What You Will Learn ...........................................................................

Servlets Turned Inside Out: JSP ........................................................

How to Write a JSP Application ..........................................................

423

424

424

426

19.3.1

19.3.2

19.3.3

19.3.4

19.3.5

19.3.6

19.3.7

Scriptlet ..................................................................................

Declaration .............................................................................

Expression .............................................................................

Directive .................................................................................

New Syntax ............................................................................

JavaBeans in JSP ..................................................................

Tag Libraries ..........................................................................

427

429

430

431

434

434

437

19.4

19.5

19.6

19.7

19.8

Chapter 20

20.1

20.2

20.3

Using JSP with BudgetPro ................................................................. Review ................................................................................................ What You Still Don’t Know ..................................................................

Resources ..........................................................................................

Exercises ............................................................................................

Open Source Web Application Servers ......................................

What You Will Learn ...........................................................................

Downloading JBoss ............................................................................

Be an Enabler, or “Let’s Be Codependent!” ........................................

438

439

440

440

441

443

443

444

444

20.3.1

20.3.2

Nonroot-Installed Software ....................................................

Finer Grained Control ............................................................

445

450

20.4

20.5

Installing JBoss ...................................................................................

Things That Make It Go ......................................................................

450

451

20.5.1

20.5.2

20.5.3

20.5.4

System V Init System .............................................................

RedHat/Fedora chkconfig ....................................................

Other Distributions .................................................................

IDE Integration .......................................................................

451

458

463

464

xviii Contents

image


20.6

20.7

20.8

20.9

Disposition of Forces ..........................................................................

Apache Geronimo ...............................................................................

Installing Geronimo .............................................................................

Running the Geronimo Server ............................................................

464

464

467

468

20.10 Review ................................................................................................

20.11 What You Still Don’t Know ..................................................................

20.12 Resources ..........................................................................................

468

469

469



PART V

Developing Enterprise Scale Software ...............

471

Chapter 21

Introduction to Enterprise JavaBeans ........................................

473

21.1

What You Will Learn ...........................................................................

473

21.2

Expanding to EJBs .............................................................................

473


21.2.1 EJB Concepts ........................................................................

474


21.2.2 Bean Types ............................................................................

477


21.2.3 Under the Hood ......................................................................

482

21.3

What’s in a Name? An Introduction to JNDI .......................................

482


21.3.1 Naming and Directory System Concepts ...............................

482


21.3.2 Common Directory Services ..................................................

484


21.3.3 Putting a Face to a Name: JNDI ............................................

487


21.3.4 Using JNDI with JBoss ...........................................................

491

21.4

Review ................................................................................................

491

21.5

What You Still Don’t Know ..................................................................

492

21.6

Resources ..........................................................................................

492

Chapter 22

Building an EJB .............................................................................

493

22.1

What You Will Learn ...........................................................................

493

22.2

EJBs: You Don’t Know Beans? ..........................................................

493


22.2.1 SessionBean ....................................................................

495


22.2.2 EJBObject .........................................................................

497


22.2.3 EJBHome ..............................................................................

498


22.2.4 Summarizing the Pieces ........................................................

499


22.2.5 EJBLocalHome and EJBLocalObject ......................

499


22.2.6 Compiling Your Beans ...........................................................

501

22.3

Review ................................................................................................

502


22.4

What You Still Don’t Know ..................................................................

502

22.5

Resources ..........................................................................................

503

Chapter 23

Deploying EJBs .............................................................................

505

23.1

What You Will Learn ...........................................................................

505

23.2

Lend Me Your EAR: Enterprise Packaging and Deployment .............

506


23.2.1 What’s in an EJB-JAR File .....................................................

508


23.2.2 Using Our Bean .....................................................................

511


23.2.3 Packaging the Servlet ............................................................

512

23.3

Deploying the EAR .............................................................................

514


23.3.1 JBoss .....................................................................................

515


23.3.2 Geronimo ...............................................................................

515

23.4

Maintaining a Distributed Application .................................................

516


23.4.1 Ant and CVS ..........................................................................

516


23.4.2 XDoclet ..................................................................................

517

23.5

Abstracting Legacy Applications .........................................................

518

23.6

Review ................................................................................................

518

23.7

What You Still Don’t Know ..................................................................

519

23.8

Resources ..........................................................................................

519

Chapter 24

Parting Shots .................................................................................

521

24.1

The Future’s So Bright, I Squint and Look Confused .........................

521

24.2

Our Book Is Yours ..............................................................................

522

24.3

Came the Revolution ..........................................................................

522

24.4

What You Still Don’t Know ..................................................................

523

24.5

Resources ..........................................................................................

523

Appendix A

ASCII Chart ....................................................................................

525

Appendix B

A Java Swing GUI for BudgetPro ................................................

527

Appendix C

GNU General Public License ........................................................

539


Index 549

Preface


JAVA AND LINUX


Why another book on Java? Why a book on Java and Linux? Isn’t Java a plat- form-independent system? Aren’t there enough books on Java? Can’t I learn everything I need to know from the Web?

No doubt, there are a host of Java books on the market. We didn’t wake up one morning and say, “You know what the world really needs? Another book about Java!” No. What we realized was that there are a couple of “holes” in the Java book market.

First, Linux as a development platform and deployment platform for Java applications has been largely ignored. This is despite the fact that the *nix platform (meaning all UNIX and UNIX-like systems, Linux included) has long been recognized as one of the most programmer-friendly platforms in existence. Those few resources for Java on Linux that exist emphasize tools to the exclu- sion of the Java language and APIs.

Second, books on the Java language and APIs have focused on pedagogical examples that serve to illustrate the details of the language and its libraries, but very few of these examples are in themselves practically useful, and they tend



xxi


to deal only with the issues of writing programs, and not at all with deploying and maintaining them. Anyone who has worked on a major software project, especially a software project that is developed and deployed in a business for a business, knows that designing and coding are only about half of the work in- volved. Yes, writing Java code is only slightly affected by the development and the deployment platform, but the process of releasing and maintaining such applications is significantly different between platforms.

To address these missing pieces, we decided to cover development and deployment of a Java application that has command-line, GUI, servlet, and enterprise components on a Linux platform. We’re writing the guide book we wish we had had when we started writing and deploying Java applications on Linux. We’re going to show you a simplistic enterprise application, “from cradle to grave,” but along the way cover issues of design process, production environ- ment, setup, administration, and maintenance that few books bother to cover.1 If you are considering buying this book and you are wondering if there is

any information in here that you can’t get for free on the Web, then, no. There is not. In fact, there is little information in any Java or Linux book that is not available for free on the Internet. In fact, in each of our chapters we will tell you where on the Web to find virtually all of the information we present, and then some. And yet books continue to sell, and we have the chutzpah to ask you to buy the book. The reason is that Web information is scattered, unorga- nized, and of highly variable quality. We will be trying to bring all the relevant information together in this book, in a clearly organized manner (and, we would like to believe, at an acceptably high level of quality). We think that has value.

Also, this book is part of the Bruce Perens’ Open Source Series. This book is part of the Web literature. And you may freely read it and use it on the Web. We hope this book will be one of those you use on the Web and buy on paper. We don’t know about you, but we like to use Web books for reference, but for reading, we like books. We own at least three books that are available for free on the Web: Thinking in C++, Thinking in Java, and O’Reilly’s Docbook: The Definitive Guide. We hope that open publishing will be the new model.



image

1. This is not to say this book is without purely pedagogical examples. Especially in Part I we make use of your typical “throwaway” examples and single classes. To try to illustrate the basics with a complete application would obscure and confuse the points being illustrated.


FREE SOFTWARE AND JAVA


GNU/Linux2 is Free Software. It is Open Source. I don’t even want to start the debate on what each term means and which one is “right.” One of the two authors of this book is a Free Software advocate, and the other is of a purely laissez-faire attitude towards the question (we won’t tell you which, although we invite you to guess). But even with a deliberate decision to cease-fire, the question remains: Is Java Open Source or Free Software?

The answer is mixed. Neither Sun’s nor IBM’s Java implementations are Open Source or Free Software. You may download and use them for free, but you do not have the source code to them, nor do you have the right to make modifications to them.3 This book will cover the GNU Compiler for Java, which compiles Java source code to native machine code. The GNU Compiler for Java (gcj) is both Open Source and Free Software. It is, however, supporting differing levels of the Java APIs (some packages are current, some are back at

1.1.x levels) and does not fully support the AWT or Swing GUIs.

However, none of this means that you cannot write your own Java pro- grams and release them under a Free Software or Open Source license. So you can certainly develop Free Software in Java. Staunch Free Software partisans (such as Richard Stallman and the Free Software Foundation) would question the wisdom of doing so. Their argument would be that a Free Software product that depends on non-Free tools isn’t really Free Software, since to compile, use, or modify it, you need to make use of a proprietary tool.

There is more than one effort to produce a Free Software Java runtime implementation. None of them is “ready for prime time.” It would, in our opinion, be a very good thing for Sun to release their SDK and Java Virtual Machine as Free Software. But so far, they have steadily resisted calls to do so.


image

2. This is the only time we will refer to it as “GNU/Linux.” See Section 7.3 for the story of why GNU/Linux is the preferred name of some. We understand Stallman and the FSF’s posi- tion, but “Linux” is much easier on the eyes and ears than “GNU/Linux.” And that, not prin- ciple, is how names and words go into the language. For better or for worse, “Linux” is the name of the operating system.

3. As we write this, a very public discussion is taking place between Sun, IBM, and Eric Ray- mond, founder of the Open Source Initiative, about opening Java under some sort of open source license. At this time, no one knows how this will turn out, but it is possible that Java will be Free Software in the future.


The fact, however, that two distinct vendors (Sun and IBM) produce ef- fectively interchangeable development and runtime environments reduces some of the risk that you face when you select a platform available only from a single vendor who does not provide source code.

So, to put the case firmly: Java is free for use, but it is certainly not Free Software as defined in The GNU Manifesto 4 or the GNU General Public Li- cense.5 This is a political and philosophical issue of interest only to those aforementioned Free Software partisans. For the rest of us, this has no bearing on Java’s technical or business merits. As for us, obviously we like the language or we wouldn’t be writing about it.


YOU CAN HELP!


This book is part of the Bruce Perens’ Open Source Series. Shortly after this book is published in dead-tree form, it will be on the Web,6 free for use, redis- tribution, and modification in compliance with the terms of the Open Publica- tion License,7 with no options taken. You can immediately create your own version as permitted in that license.

Naturally enough, we plan to maintain our “official” version of the online book, so we encourage you to send suggestions, corrections, extensions, com- ments, and ideas to us. Please send any such to javalinux@multitool.net and we will try to keep our little tome up-to-date so it continues to serve the needs of the Java and Linux development communities.


ACKNOWLEDGMENTS


First off, we naturally wish to thank Mark L. Taub, our acquisitions editor at Prentice Hall PTR, for believing in the book and in open publishing as the way to put it out there. We also want to thank Bruce Perens for lending his name and powers of persuasion to open-content publishing through the Prentice Hall PTR Bruce Peren’s Open Source Series. Thanks, too, to Patrick Cash-Peterson


image

4. http://www.gnu.org/gnu/manifesto.php

5. http://www.gnu.org/copyleft/gpl.php

6. http://www.javalinuxbook.com/

7. http://www.opencontent.org/openpub/


and Tyrrell Albaugh, who worked as our in-house production contacts, for all the behind-the-scenes work they did, including overseeing the cover.

In more direct terms of content, we owe major thanks to Kirk Vogen of IBM Consulting in Minneapolis for his article on using SWT with gcj, and for his kind help in allowing us to use the ideas he first presented in his IBM developerWorks articles. In more direct terms of content, we owe major thanks to: Kirk Vogen of IBM Consulting in Minneapolis for his article on using SWT with gcj, and for his kind help in allowing us to use ideas he first presented in his IBM developerWorks articles; and to Deepak Kumar8 for graciously allow- ing us to base our build.xml file for EJBs off of a version that he wrote.

Thanks, too, to Andrew Albing for his help in drawing some of our dia- grams, and to George Logajan and to Andy Miller for sharing their insights on the more intricate details of Swing.

We also wish to express our great indebtedness to our technical reviewers, especially Andrew Hayes, Steve Huseth, and Dan Moore. A very large thank- you is also due to Alina Kirsanova whose eye for detail, endless patience, and tenacity, and overall talent with proofing, layout, and more added so much refinement and improvement to the book. We are greatful for all their contri- butions. Any errors or omissions in this text are our fault and certainly not theirs. The book is much stronger for all their efforts.

There are likely many more people we ought to thank, especially those at Prentice Hall PTR, whose names and contributions we may never know, but we do know that this was an effort of many more people than just the authors, and we are grateful to them all.



image

8. http://www.roseindia.net/