Java Coffee Break Newsletter Volume 3, Issue 2 http://www.javacoffeebreak.com/ ISSN 1442-3790 ================================================================= In this issue * Java in the news * Book Review - Client/Server Programming with Java and CORBA * Book Review - JavaSpaces Principles, Patterns and Practice * Inside Java Column : Inside the Java Virtual Machine * Q&A : Why aren't cookies stored by my servlets accessible to my CGI scripts or ASP pages? * Q&A : How do I pass a primitive data type by reference? For example, how do I make an int passed to a function modifiable. ================================================================= /* * Curious about class loaders? Confused by composition? * Threatened by thread safety or befuddled by finalizers? * Check out Design Techniques articles and discussion * topics for an in-depth treatment of Java design issues. * --> http://www.artima.com/designtechniques */ ================================================================= In the News Here are a selection of recent news items that may be of interest to Java developers. /*/ Sun released JavaMail API 1.1.3, Java 3D API 1.2beta1 This month, Sun Microsystems made available updated versions of the JavaMail and Java3D APIs, available through the Java Developer Collection (JDC). For more information, see http://java.sun.com/jdc/ /*/ Rumors of Sun Microsystems/IBM rift over Java exagerated Rumors of the death of the Sun/IBM Java alliance appear to have been greatly exagerated. Recent gossip held that IBM was preparing to split, over Sun's reluctance to release Java for standardization. A recent article in InfoWorld suggests otherwise, which is good news for developers in either camp. A fragmented Java would weaken cross-platform portability, and threaten the future of Java. See an article at Infoworld on the topic, at http://www.infoworld.com/articles/ic/xml/00/01/18/000118icjava.xml ================================================================= Book Review - Client/Server Programming with Java and CORBA, Second Edition Author : Robert Orfali, Dan Harkey Publisher : John Wiley & Sons ISBN : 047124578X Experience: Intermediate - Expert Review Courtesy of Amazon.com The standard by which all other CORBA books are judged, Client/Server Programming with Java and CORBA is the book to read if you're thinking about doing anything with this language-bridging technology. Working toward the Object Web, a computing phenomenon in which the Internet is full of code modules that users can assemble in many different ways to suit their needs, Orfali and Harkey explain the Common Object Request Brokerage Architecture (CORBA), which goes a long way toward realizing that goal. This book is the single best CORBA resource available anywhere. Appropriately enough, the book opens with a comparison of the client/server architectures of Java and CORBA. It then goes on to cover dynamic invocations of CORBA objects. There's a discussion of the trade-offs involved in choosing among sockets, HTTP/CGI, remote method invocation (RMI), and CORBA/IIOP, complete with a table that compares the features of all the competitors. The authors then explore the relative advantages and disadvantages of two- and three-tier database query systems under JDBC. The book concludes with a fully implemented client/server transaction-handling system. The authors' prose and code is lucid and complete, and all of the numerous code samples appear on the companion CD-ROM. --David Wall For more information about this title, or to order it, visit http://www.davidreilly.com/goto.cgi?isbn=047124578X ================================================================= Book Review - JavaSpaces(TM) Principles, Patterns and Practice (The Jini(TM) Technology Series) Author : Eric Freeman, Susanne Hupfer, Ken Arnold   Publisher : Addison-Wesley ISBN : 0201309556 Experience: Intermediate-Expert Review courtesy of Amazon.com JavaSpaces Principles, Patterns, and Practice delivers an exciting introduction to the world of distributed, high-performance computing on Java's Jini platform using the new JavaSpaces API. Written for academic and business developers, this guide will help you begin using the Jini platform by outlining its powerful, elegant solutions for distributed computing. After a foreword by distributed computing pioneer David Gelernter, the book provides a short technology overview describing the makeup of JavaSpaces. The authors atomize their description of JavaSpaces as an overseer application that lets programs running on separate computers store and share persistent data. While the JavaSpaces API is by itself remarkably simple, this book demonstrates with deliberate fanfare the resolution of common distributed computing problems using complex design patterns. Early sections look at the basics of reading, writing, and searching for data stored in JavaSpaces as well as presenting task and result bags as solutions to managing work done in parallel. The book also elaborates on the readers/writers problem, well-known within the field of computer science, and even offers a means of addressing it. The authors use code samples from a chat message server and a model of a paging system using message channels during their discussion of message passing and communication with JavaSpaces. One section on distributed patterns presents some common solutions to doing work in parallel, including the Marketplace pattern, illustrated with an e-commerce bidding application. Further sections cover distributed events and transactions as they apply to JavaSpaces. The book closes with two excellent examples, one for a distributed messaging service and another for a brute force attack on encrypted passwords. With the debut of JavaSpaces, business developers gain access to distributed processing previously available only to academic researchers. The JavaSpaces solution, along with JavaSpaces Principles, Patterns, and Practice, will let any Java developer audition distributed computing for the first time. --Richard Dragan For more information about this title, or to order it, visit http://www.davidreilly.com/goto.cgi?isbn=0201309556 ================================================================= Inside Java : The Java Programming Language Inside Java offers a glimpse behind the Java platform, and related technologies. This month, we examine the Java Virtual Machine, which allows Java code to be executed on a wide variety of hardware and software environments. -- David Reilly At the heart of the Java platform lies the Java Virtual Machine, or JVM. Most programming languages compile source code directly into machine code, suitable for execution on a particular microprocessor architecture. The difference with Java is that it uses bytecode - a special type of machine code.  Java bytecode executes on a special type of microprocessor. Strangely enough, there wasn't a hardware implementation of this microprocessor available when Java was first released. Instead, the processor architecture is emulated by what is known as a "virtual machine". This virtual machine is an emulation of a real Java processor - a machine within a machine (Figure One). The only difference is that the virtual machine isn't running on a CPU - it is being emulated on the CPU of the host machine. * Figure One - JVM emulation run on a physical CPU The Java Virtual Machine is responsible for interpreting Java bytecode, and translating this into actions or operating system calls. For example, a request to establish a socket connection to a remote machine will involve an operating system call. Different operating systems handle sockets in different ways - but the programmer doesn't need to worry about such details. It is the responsibility of the JVM to handle these translations, so that the operating system and CPU architecture on which Java software is running is completely irrelevant to the developer. * Figure Two - JVM handles translations The Java Virtual Machine forms part of a large system, the Java Runtime Environment (JRE). Each operating system and CPU architecture requires a different JRE. The JRE comprises a set of base classes, which are an implementation of the base Java API, as well as a JVM. The portability of Java comes from implementations on a variety of CPUs and architectures. Without an available JRE for a given environment, it is impossible to run Java software. Differences between JVM implementations Though implementations of Java Virtual Machines are designed to be compatible, no two JVMs are exactly alike. For example, garbage collection algorithms vary between one JVM and another, so it becomes impossible to know exactly when memory will be reclaimed. The thread scheduling algorithms are different between one JVM and another (based in part on the underlying operating system), so that it is impossible to accurately predict when one thread will be executed over another. Initially, this is a cause for concern from programmers new to the Java language. However, it actually has very little practical bearing on Java development. Such predictions are often dangerous to make, as thread scheduling and memory usage will vary between different hardware environments anyway. The power of Java comes from not being specific about the operating system and CPU architecture - to do so reduces the portability of software. Summary The Java Virtual Machine provides a platform-independent way of executing code, by abstracting the differences between operating systems and CPU architectures. Java Runtime Environments are available for a wide variety of hardware and software combinations, making Java a very portable language. Programmers can concentrate on writing software, without having to be concerned with how or where it will run. The idea of virtual machines is nothing new, but Java is the most widely used virtual machine used today. Thanks to the JVM, the dream of Write Once-Run Anywhere (WORA) software has become a reality. For diagrams, and back issues of this column, visit http://www.javacoffeebreak.com/articles/inside_java/ ================================================================= Q&A: Why aren't cookies stored by my servlets accessible to my CGI scripts or ASP pages? By default, cookies are accessible to every HTTP request for the current directory, and any subdirectories. Now on most web servers and servlet engines, servlets are located in a special directory. For example, when using servletrunner (which ships with the Java Servlet Development Kit), servlets must be invoked under the /servlet/ directory.  http://webserver/servlet/servletname If you want your cookies to be accessible elsewhere, you must specify the root path of your webserver, using the javax.servlet.Cookie.setPath( String ) method. // Create a cookie for everyone to share Cookie myCookie = new Cookie ("counter", "1"); // Set path for cookie myCookie.setPath( "/" ); Once you've set the path, any script (for example, stored in /cgi-bin/) can access the cookies stored by your servlets. ================================================================= Q&A: How do I pass a primitive data type by reference? For example, how do I make an int passed to a function modifiable. Java passes all primitive data types by value. This means that a copy is made, so that it cannot be modified. When passing Java objects, you're passing an object reference, which makes it possible to modify the object's member variables. If you want to pass a primitive data type by reference, you need to wrap it in an object. The easiest of all is to pass it as an array (or even a Vector). Your array only needs to contain a single element, but wrapping it in an array means it can be changed by a function. Here's a simple example of it in action. public static void increment(int[] array, int amount) { array[0] = array[0] + amount; } public static void main(String args[]) { int[] myInt = { 1 }; increment (myInt, 5); System.out.println ("Array contents : " + myInt[0]); } Of course, if you're modifying the contents of parameters passed to a method, you really should try to avoid this behavior. It increases the complexity of code, and really should be avoided. The preferred way is to return a value from a method, rather than modifying parameter values directly. ================================================================= The Java Coffee Break Newsletter is only sent out to email subscribers who have requested it, and to readers of the comp.lang.java.programmer and comp.lang.java.help newsgroups. If you'd like to receive our newsletter, and get the latest Java news, tips and articles from our site, then get your FREE subscription & back issues from http://www.javacoffeebreak.com/newsletter/ If you are an email subscriber and no longer wish to receive the JCB Newsletter, please unsubscribe by emailing javacoffeebreak-unsubscribe@listbot.com