pat's bloghttp://www.java.net/blogs/pat
enJSR-274 - Standardizing BeanShellhttp://www.java.net/blog/pat/archive/2005/05/jsr274_standard.html
<!-- | 0 --><p>I'm very happy to announce that the JCP has begun voting on a JSR to standardize the BeanShell scripting language through the Java<br />
Community Process.</p>
<p/>
<p>The goal of this JSR will be to formalize the langauge and provide the RI and<br />
TCK which will allow it to be included in a future release of the J2SE. This<br />
effort will build upon the introduction of the javax.script API by providing a<br />
standard, Java syntax compatible, scripting language as part of the Java<br />
platform.</p>
<p/>
<p>I think this is a very exciting proposal and will lead to very good things<br />
for both the Java platform and for existing users of BeanShell. I encourage<br />
everyone to read the JSR proposal (<a href="http://jcp.org/en/jsr/detail?id=274" title="http://jcp.org/en/jsr/detail?id=274">http://jcp.org/en/jsr/detail?id=274</a>),<br />
which provides more details and motivation. But let me briefly anticipate<br />
a few questions:</p>
<p/>
<p>1) Why spend time on this? Why not just put the time into making<br />
BeanShell better independently?</p>
<p/>
<p>Well, the primary reason is that we really believe that the Java platform needs<br />
a standardized, "native" Java scripting language and is long overdue for one.<br />
There are so many things that developers and end users can do with a dynamic<br />
language and scripting capabilities (prototyping, application extension,<br />
configuration, testing, dynamic deployment) and this capability has been there<br />
just under the surface since Java 1.1, waiting to be tapped. This<br />
is why we created BeanShell and have maintained it (sometimes lovingly,<br />
sometimes reluctantly) for the past seven years. During that time BeanShell<br />
has matured into a truly Java syntax compatible language, while staying<br />
relatively lean and simple. It only takes a couple of hundred K of code (less<br />
than the size of some of the Swing components) to bring all of this to Java.<br />
So, we think it's time that it gets done.</p>
<p/>
<p>Next, from the project's point of view, we think that this will make BeanShell<br />
better. There is nothing like a little pressure to get people moving and<br />
get things done. We are very much looking forward to the involvement of new<br />
voices and outside experts on the project and I believe that this process is<br />
going to benefit everyone, most of all BeanShell users.</p>
<p/>
<p>2) Won't this effort suffer from the pitfalls of designing a language by<br />
committee?</p>
<p/>
<p>The purpose of the JSR will be primarily to ratify the existing language, not<br />
to create a new one. We aren't going to start from scratch or go off in<br />
completely uncharted territory. Of course having the benefit of new people<br />
and ideas we may find new answers to old problems, but I believe that we can<br />
keep a firm grip on the process and get this done with an outcome that will<br />
make everyone happy. Because we have been very conservative with the core of<br />
BeanShell over the years it has stayed relatively clutter free. This leaves us<br />
a lot of flexibility and room for future enhancement. We do have some<br />
interesting and bold ideas brewing, but those proposals will come from the<br />
community based on their merit and not from an attempt to solve every problem<br />
via the expert group.</p>
<p/>
<p>3) So what is the plan? Where is the roadmap for BeanShell?</p>
<p/>
<p>The first step will be to finalize the current (2.x) release with the existing<br />
feature set. This work will be mainly bug fixes, but will also move a bit<br />
further towards Java 5 syntax compatibility. At JavaOne next month<br />
we will be hosting a BeanShell BOF (please join us) to talk about plans for<br />
BeanShell 3.0 and solicit feedback on the JSR. We expect BeanShell 3.0 to be<br />
fully Java 5 compatible in terms of base syntax, to offer some new (long<br />
desired) features in the scripting domain, and to address performance and<br />
management issues. We are currently updating the beanshell.org web site and<br />
services and we're moving the codebase into Subversion. We will soon have<br />
a Wiki which will allow the community to help us even more. We will be<br />
releasing much more regularly starting immediately. The goal will be to have<br />
the JSR in good shape by the end of the year, with an alpha release of 3.0<br />
including new features developed concurrently by the community.</p>
<p/>
http://www.java.net/blog/pat/archive/2005/05/jsr274_standard.html#commentsJSRWed, 25 May 2005 16:05:16 +0000pat233354 at http://www.java.netThe new javax.script API....http://www.java.net/blog/pat/archive/2005/02/the_new_javaxsc.html
<!-- | 0 --><p>A few days ago the JCP cranked out a public review of JSR-223, which has so far been received with a few blog notes and relatively little discussion. This is not surprising, given that if you you read the JCP description for this group it appears to have something to do with getting PHP pages into WAR files. (Useful, not very exciting). But I'm going to let you in on a little secret. There are really two APIs buried in this specification and<br />
one of them is actually quite interesting. Java is about to get a standardized API for working with scripting languages and it's not just about web applications any more.
</p>
<p>
While the original focus of JSR-223 was indeed to develop a standard API for deploying scripted pages in web applications, over the past year the expert group has actually spent most of its time on the lower level, prerequisite API for exposing scripting language engines to the Java platform. The new <strong>javax.script</strong> package provides a standardized script engine API, similar to<br />
the IBM/Apache BSF (Bean Scripting Framework), but takes the integration much further, offering: a more powerful discovery mechanism, fine grained namespace and script environment management, powerful application integration, and other advanced features.
</p>
<p>
Since much of the specification is aimed at scripting engine developers, some of these user APIs may not be immediately obvious. I'll just run down some of the important points here and leave the details for an upcoming article.
</p>
<p>The Java Scripting API includes:<br />
<br/></p>
<ul>
<li>Standardized packaging and deployment of scripting language engines for use<br />
with Java Applications and Webapps. The JAR services mechanism allows you to simply drop in a JAR file to add support for a new language.</p>
<li>An API for discovery and instantiation of scripting language engines<br />
utilizing metadata, including: common language names, file extensions, and<br />
MIME types.</p>
<li>A simple API for evaluation of scripts, optionally supporting script<br />
compilation and method invocation for both procedural and object oriented<br />
scripting languages.</p>
<li>A fine grained, pluggable namespace and script context API, allowing<br />
complete control over the evaluation environment of scripts and management of<br />
engines. Create your own namespaces of bound values with well defined scoping<br />
relationships. </p>
<li>Tight application integration via Map based namespaces and proxy<br />
Java interface binding to scripts. Expose parts of your host application to<br />
scripts via a Map interface that binds directly to the namespace of a script.<br />
Expose methods of a procedural script via a real Java proxy interface.</p>
<li>A standardized classification of script engine threading models, allowing<br />
developers to optimize engine usage if desired. Languages are categorized<br />
into one of three levels of concurrency support: Java language semantics,<br />
"thread isolated" concurrency, and purely stateless concurrent interpreters.</p>
<li>A deliberately limited, but useful API for language independent script<br />
code generation. This allows clients to generate basic "output" and method<br />
call statements in a language neutral way, enabling simple macro generation<br />
and scripted action recording in any target language.</p>
<li>A "non-normative" description of Java bindings which can be implemented by<br />
languages that to work with Java methods and objects. This includes a<br />
reference implementation intended to clarify and ease the implementation of<br />
finer points of Java language semantics such as overloaded method resolution.
</ul>
<p>
The reference implementation is available from the JSR-223 home page:<br />
<a href="http://jcp.org/aboutJava/communityprocess/pr/jsr223/"><br />
http://jcp.org/aboutJava/communityprocess/pr/jsr223/</a>
</p>
<p>
The download includes <strong>javax.script</strong> engine implementations for JavaScript, PHP, and Groovy.
</p>
<p>
Interestingly, despite numerous references to the<br />
<a href="http://www.beanshell.org/">BeanShell</a> Java scripting language in the<br />
specification, the development of a BeanShell engine by the spec lead, and the participation of<br />
the primary BeanShell developer (me) in the spec development, the Sun download<br />
does not include a BeanShell engine implementation or examples. (The politics of Groovy continue to amaze me.) But not to fear, the next release of BeanShell will include an engine implementation as part of its own distribution.
</p>
<p>
Check it out!
</p>
http://www.java.net/blog/pat/archive/2005/02/the_new_javaxsc.html#commentsJSRFri, 11 Feb 2005 08:11:56 +0000pat232918 at http://www.java.netStupid Scanner tricks...http://www.java.net/blog/pat/archive/2004/10/stupid_scanner.html
<!-- | 0 --><p>One of the things I've always wanted in Java is a "one liner" trick to read all of the text from a stream. For example, I often want to be able to grab the contents of a URL or file as a simple String, without a lot of typing. The URL class tantalizingly holds out its getContent() method, but sadly, content handlers were never really taken seriously. I don't even particularly care about performance, I'd just like something for the simple case, in standard Java, that's not too hard to remember. Well, the Java 1.5 java.util.Scanner class finally has the answer...</p>
<p/>
<p>Suppose I have a stream:</p>
<pre>
InputStream source = new URL("http://pat.net/misc/foo.txt").openStream();
</pre><p>
The canonical way to gather it to a String has always been to use a BufferedReader, e.g.</p>
<pre>
BufferedReader br = new BufferedReader( new InputStreamReader( source ) );
StringBuffer text = new StringBuffer();
for ( String line; (line = br.readLine()) ! = null )
text.append( line );
</pre><p>
This is about 4 lines of tediousness code (assuming the resulting StringBuffer is good enough), uses two classes, a loop, and too many parentheses. I must have typed code like this a million times, as I bet a lot of people have.
</p>
<p>I've often been tempted to try to shorten it a bit using the DataInputStream readFully() method:</p>
<pre>
byte [] buf = new byte[ source.available() ];
new DataInputStream( source ).readFully( buf );
String text = new String( buf );
</pre><p>
That would be a bit less typing and involve only an array and a class. The problem is that it relies on the input stream's available() method to reflect the total size of the data to be returned... which in general it doesn't. The available() method works for files and you could always substitute your own size if you can get it from other meta-data, but it's still a messy solution and doesn't exactly roll off of the finger tips.
</p>
<p>Finally now with Java 1.5's Scanner I have a true one-liner:</p>
<pre>
String text = new Scanner( source ).useDelimiter("\\A").next();
</pre><p>
One line, one class. The only tricky is to remember the regex \A, which matches the beginning of input. This effectively tells Scanner to tokenize the entire stream, from beginning to (illogical) next beginning. As a bonus, Scanner can work not only with an InputStream as the source, but also a File, Channel, or anything that implements the new java.lang.Readable interface. For example, to read a file:</p>
<pre>
String text = new Scanner( new File("poem.txt") ).useDelimiter("\\A").next();
</pre><p>
Finally, before someone chastizes me I should point out that you can accommodate a specific character set with all of the above examples. In the first you'd set the charset in the InputStreamReader, in the second you'd specify it with the String constructor, and in the Scanner example you can pass a charset to the constructor.
</p>
<p>Enjoy!</p>
<p/></p>
http://www.java.net/blog/pat/archive/2004/10/stupid_scanner.html#commentsProgrammingSun, 24 Oct 2004 08:18:53 +0000pat232596 at http://www.java.net