<?xml version="1.0" encoding="utf-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="en">
<title>Pat Niemeyer&apos;s Blog</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/pat/" />
<modified>2008-01-02T17:42:16Z</modified>
<tagline></tagline>
<id>tag:weblogs.java.net,2008:/blog/pat/133</id>
<generator url="http://www.movabletype.org/" version="3.01D">Movable Type</generator>
<copyright>Copyright (c) 2005, pat</copyright>
<entry>
<title>JSR-274 - Standardizing BeanShell</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/pat/archive/2005/05/jsr274_standard_1.html" />
<modified>2008-01-02T17:42:16Z</modified>
<issued>2005-05-25T17:05:16Z</issued>
<id>tag:weblogs.java.net,2005:/blog/pat/133.2495</id>
<created>2005-05-25T17:05:16Z</created>
<summary type="text/plain">I&apos;m very happy to announce that the JCP has begun voting on a JSR to standardize the BeanShell scripting language through the Java
Community Process.</summary>
<author>
<name>pat</name>

<email>pat@pat.net</email>
</author>
<dc:subject>JSR</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/pat/">
<![CDATA[I'm very happy to announce that the JCP has begun voting on a JSR  to standardize the BeanShell scripting language through the Java
Community Process.
<p/>

The goal of this JSR will be to formalize the langauge and provide the RI and
TCK which will allow it to be included in a future release of the J2SE.  This
effort will build upon the introduction of the javax.script API by providing a
standard, Java syntax compatible, scripting language as part of the Java
platform.
<p/>

I think this is a very exciting proposal and will lead to very good things
for both the Java platform and for existing users of BeanShell.  I encourage
everyone to read the JSR proposal (http://jcp.org/en/jsr/detail?id=274), 
which provides more details and motivation.  But let me briefly anticipate 
a few questions:
<p/>

1) Why spend time on this?  Why not just put the time into making 
BeanShell better independently?
<p/>

Well, the primary reason is that we really believe that the Java platform needs
a standardized, "native" Java scripting language and is long overdue for one.
There are so many things that developers and end users can do with a dynamic
language and scripting capabilities (prototyping, application extension, 
configuration, testing, dynamic deployment) and this capability has been there 
just under the surface since Java 1.1, waiting to be tapped.  This
is why we created BeanShell and have maintained it (sometimes lovingly, 
sometimes reluctantly) for the past seven years.  During that time BeanShell 
has matured into a truly Java syntax compatible language, while staying
relatively lean and simple.  It only takes a couple of hundred K of code (less
than the size of some of the Swing components) to bring all of this to Java.
So, we think it's time that it gets done.
<p/>

Next, from the project's point of view, we think that this will make BeanShell 
better.  There is nothing like a little pressure to get people moving and 
get things done.  We are very much looking forward to the involvement of new
voices and outside experts on the project and I believe that this process is
going to benefit everyone, most of all BeanShell users.
<p/>

2) Won't this effort suffer from the pitfalls of designing a language by 
committee?
<p/>

The purpose of the JSR will be primarily to ratify the existing language, not
to create a new one.  We aren't going to start from scratch or go off in 
completely uncharted territory.  Of course having the benefit of new people 
and ideas we may find new answers to old problems, but I believe that we can
keep a firm grip on the process and get this done with an outcome that will
make everyone happy.   Because we have been very conservative with the core of
BeanShell over the years it has stayed relatively clutter free.  This leaves us
a lot of flexibility and room for future enhancement.  We do have some
interesting and bold ideas brewing, but those proposals will come from the
community based on their merit and not from an attempt to solve every problem
via the expert group.
<p/>

3) So what is the plan?  Where is the roadmap for BeanShell?
<p/>

The first step will be to finalize the current (2.x) release with the existing
feature set.  This work will be mainly bug fixes, but will also move a bit 
further towards Java 5 syntax compatibility.  At JavaOne next month
we will be hosting a BeanShell BOF (please join us) to talk about plans for
BeanShell 3.0 and solicit feedback on the JSR.  We expect BeanShell 3.0 to be
fully Java 5 compatible in terms of base syntax, to offer some new (long
desired) features in the scripting domain, and to address performance and
management issues.  We are currently updating the beanshell.org web site and 
services and we're moving the codebase into Subversion.  We will soon have
a Wiki which will allow the community to help us even more.  We will be 
releasing much more regularly starting immediately.  The goal will be to have 
the JSR in good shape by the end of the year, with an alpha release of 3.0 
including new features developed concurrently by the community.
<p/>]]>

</content>
</entry>
<entry>
<title>The new javax.script API....</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/pat/archive/2005/02/the_new_javaxsc.html" />
<modified>2008-01-02T17:42:16Z</modified>
<issued>2005-02-11T08:11:56Z</issued>
<id>tag:weblogs.java.net,2005:/blog/pat/133.2028</id>
<created>2005-02-11T08:11:56Z</created>
<summary type="text/plain">Java is about to get a standardized API for working with scripting languages and it&apos;s not just about web applications any more.</summary>
<author>
<name>pat</name>

<email>pat@pat.net</email>
</author>
<dc:subject>JSR</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/pat/">
<![CDATA[<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
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
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>

The Java Scripting API includes:
<br/>

<ul>
<li>Standardized packaging and deployment of scripting language engines for use
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.

<li>An API for discovery and instantiation of scripting language engines
utilizing metadata, including: common language names, file extensions, and 
MIME types.

<li>A simple API for evaluation of scripts, optionally supporting script 
compilation and method invocation for both procedural and object oriented 
scripting languages.

<li>A fine grained, pluggable namespace and script context API, allowing
complete control over the evaluation environment of scripts and management of
engines.  Create your own namespaces of bound values with well defined scoping
relationships. 

<li>Tight application integration via Map based namespaces and proxy 
Java interface binding to scripts.  Expose parts of your host application to
scripts via a Map interface that binds directly to the namespace of a script.  
Expose methods of a procedural script via a real Java proxy interface.

<li>A standardized classification of script engine threading models, allowing
developers to optimize engine usage if desired.  Languages are categorized 
into one of three levels of concurrency support: Java language semantics, 
"thread isolated" concurrency, and purely stateless concurrent interpreters.

<li>A deliberately limited, but useful API for language independent script 
code generation.  This allows clients to generate basic "output" and method
call statements in a language neutral way, enabling simple macro generation
and scripted action recording in any target language.

<li>A "non-normative" description of Java bindings which can be implemented by
languages that to work with Java methods and objects.  This includes a 
reference implementation intended to clarify and ease the implementation of 
finer points of Java language semantics such as overloaded method resolution.
</ul>

<p>
The reference implementation is available from the JSR-223 home page:
<a href="http://jcp.org/aboutJava/communityprocess/pr/jsr223/">
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 
<a href="http://www.beanshell.org/">BeanShell</a> Java scripting language in the
specification, the development of a BeanShell engine by the spec lead, and the participation of
the primary BeanShell developer (me) in the spec development, the Sun download
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>]]>

</content>
</entry>
<entry>
<title>Stupid Scanner tricks...</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html" />
<modified>2008-01-02T17:42:16Z</modified>
<issued>2004-10-24T09:18:53Z</issued>
<id>tag:weblogs.java.net,2004:/blog/pat/133.1681</id>
<created>2004-10-24T09:18:53Z</created>
<summary type="text/plain">Turn an input stream into a String in one line of code with the new java.util.Scanner.</summary>
<author>
<name>pat</name>

<email>pat@pat.net</email>
</author>
<dc:subject>Programming</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/pat/">
<![CDATA[<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...<br />
<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.<br />
</p></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.<br />
</p></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.<br />
</p></p>

<p>Enjoy!<br />
<p/></p></p>]]>

</content>
</entry>

</feed>