Apache Jakarta and Beyond: A Java Programmeramp;#039;s Introduction [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Apache Jakarta and Beyond: A Java Programmeramp;#039;s Introduction [Electronic resources] - نسخه متنی

Larne Pekowsky

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







16.7. Motivation for Scripting


Now that BeanShell has been examined in some detail, the advantages and possibilities offered by a scripting language should begin to be evident. Here are just a few:


Experimenting with New APIs


BeanShell provides an excellent environment for trying out new tools, such as many discussed in this book. For example, the ability to easily try different patterns and see how they match different inputs can make learning regular expressions much easier.


Rapid Testing


There is no substitute for formal automated tests like those developed in Chapter 4 using JUnit. However, BeanShell makes it possible to quickly run through several test cases that may later be folded into automated tests. BeanShell makes it easy to create an instance of the class or classes to be tested, interactively feed a number of sample arguments to the methods, and make sure the results are correct.


Rapid Development


This chapter has already covered a number of ways in which BeanShell can accelerate the development process. Interfaces can be implemented in BeanShell and loaded with a call to source(). If the implementation doesn't work, the source BeanShell file can be changed and the application can reload it without even needing to restart. Taking this same idea further, entire systems can be developed using BeanShell, using its ability to interpret Java code and load .java files dynamically.

What these approaches have in common is that both the compilation and restart/reload steps of Java development can be completely avoided. On a fast computer each individual compilation may not take very long, but they can add up over a day, to say nothing of how this time accumulates over the entire lifetime of a project.

So far nothing has been said about how fast programs in BeanShell run. In most cases BeanShell programs will run slower than compiled versions but often by a smaller factor than might be expected. A wide class of applications are

I/O bound, meaning they spend most of their time waiting for data from disk or a database or a network connection. For such programs BeanShell may "feel" as fast as Java. In many cases it may turn out that BeanShell is "fast enough," and there is no need to ever move certain segments of code into Java.

In addition, for programs that run only a short time, the speed differences may be offset by the ability to retry a script repeatedly in a running BeanShell session, whereas it would be necessary to restart the Java VM and reload the classes to test the Java versions.


Configurable Behavior


Chapter 12 discussed many ways to configure an application, but all those methods rested on the same fundamental concept. All the tools in Chapter 12 treated Chapter 13 that regular expressions do not constitute a complete language; there are certain kinds of patterns or tests that they simply cannot do. Using BeanShell, the e-mail system could be extended to take the name of scripts, and messages would be passed through these scripts. This would make it possible to perform much more sophisticated tests such as determining whether the address from which the message originated is on a list of known spammers. In addition, this would make it possible to configure the system's behavior on detecting spam; spam could be discarded, written to a database for later analysis, or automatically forwarded to a spam-tracking service.

This same extensibility could be offered by writing new classes. The system would then be configured by naming these classes in the configuration file. However, this approach places a number of additional burdens on end users; they must have the full Java development kit, they must go through their own testing cycles, they must typically write more code than would be required by BeanShell, and so on.

The notions of rapid development and easy configuration through scripting will appear throughout the remainder of this book.

Along with the many advantages offered by BeanShell are some downsides that developers should be aware of. Debugging a program that mixes Java and BeanShell may be difficult because the BeanShell code cannot be examined line by line in a debugger such as the one provided in Eclipse. The ease of replacing code may make it tempting to drop new scripts into production environments without going through proper testing and QA. There may also be security issues because BeanShell makes it somewhat easier for malicious hackers to introduce arbitrary code into a system. However, with some care and thought BeanShell can be a useful and powerful tool.


/ 207