The Bean Markup Language is an instance of an XML-based component
configuration or wiring language customized for the JavaBean component model.
Though the BML is built for the JavaBean component model, it can be used for
almost any type of Java components. The Java components do not have to satisfy
the criteria for a component to be a JavaBean, ie. have a null constructor, and
be serializable.
The BML is designed to be executable. It can be used to define the creation
of Java beans, the access of existing beans, the configuration of beans, and the
binding of beans to other beans through either setting/getting of bean
properties or through events. The IBM alphaworks website has provided two
implementations of BML:
- A BML player that reads a BML document using a XML parser, converts the
XML to a DOM tree, and then traverses the tree building the Java beans and
interconnects, and
- A BML compiler that uses an XML parser to read the XML file, converting it
to a DOM tree, and then generates Java source code. The source code can then
be compiled to execute as a standalone program.
The BML player uses reflection to identify methods at runtime, which becomes
a performance issue. The BML compiler generates reflection-free Java code.
However the BML allows you to load a new version of the application while one is
already running, thus enabling incremental application development.
The cool thing about the BML is that it enables you to build Java
applications in a declarative fashion. All the code generation is done either by
the BML compiler or player.
The IBM alphaworks website provides many examples on how to use the BML. It
also provides a tutorial on how to use the BML taking a couple of the sample
programs it has provided as examples.
The BML Language
The BML language provides a mechanism for capturing the structure of a
component application. The following is an explanation of some of the tags used
in the BML language:
The <bean> tag:
The <bean> element is used to create new beans or to look up beans by
name. The <bean> tag is of the format:
<bean class = "classname or serialized file" [id =
"name"]>
</bean>
The BML player or compiler builds an object registry. The object registry is
a map that associates an "id" in the tag with the JavaBean. The object
registry provides a string name to object reference mapping. The registry is
used for:
- Communication of object references between different parts of BML
scripts
- Communication of object references between Java code and BML scripts
- Communication object references between BML and JavaScript or NetRexx
scripts
A bean can be instantiated using its no-args constructor or providing
arguments to the constructor.
<bean class = "classname or serialized file" [id =
"name"]>
<args></args>
………..
</bean>
The <bean> element allows one to look up beans previously stored in the
object registry.
<bean [class = "class of registered bean"] source ="name of
registered bean">
…..
</bean>
The name is looked up in the object registry and if found, the bean is used.
The class attribute must match the class of the bean; otherwise an exception is
thrown.
The bean tag can be used to lookup the class bean too.
<bean source ="class: name of the class to find">
…..
</bean>
The class of the name provided is loaded and the instance of the Class object
representing it is returned. Special recognition is given to primitives to
enable access to the class objects that represent the primitive types.
An example:
<bean source ="class:java.lang.System">
<call-method name = "out.println">
</bean>
The <string> tag
Java Strings are immutable objects so the only way to create a non-empty
string is to pass an argument to the string tag. There are two ways the string
tag can be used:
- To create a non-empty string:
<string [value = "value of string"]> [value of string]
</string>
An instance of a string is returned with the value specified by that given
in the tag. The value is specified either in the open string tag or between
the open and closed string tags.
- To create an empty string:
<string/>
The <property> tag
The property tag enables one to either set or get a property of a JavaBean.
The value may be either an immediate value or the return value of some other
element. If the target is not specified in the tag itself the default target is
used.
- The property can be set using an immediate value:
<property [target = "bean"] name = "property name"
[index = "num"] [id = "name of the bean"] value =
"property value" />
If the index number is provided then the property must be indexed and the
numth indexed property must be set.
An example:
<bean class = "java.awt.TextField">
<property name = "text" value = "Welcome"/>
</bean>
- The property value can be set using an indirect value too:
<property [target = "bean"] name = "property name"
[index = "num"] [id = "name of the bean"] >
…
</property>
The value is obtained by evaluating the element within the open and closed
property tags and returning it. If the type of the value and the type of the
property do not match, type conversion becomes necessary. BML handles the type
conversion automatically through the use of type converters. The classes used
for type conversion are maintained in the type converter registry. Thus a type
converter could help convert a String to Font type.
- The property value can be obtained:
<property [target = "bean"] name = "property name"
[index = "num"] [id = "name of the bean"] />
The <field> tag
The field element enables the value of the field to be obtained or set to
some value. The value may be either an immediate value or the return value of
some other element. If the target is not specified in the tag itself, the
default target is used. The semantics for the property tags and field tags are
very similar. The primary difference is that field tags can be static.
- The field can be set using an immediate value:
<field [target = "bean"] name = "field name" [id =
"name of the bean"] value = "field value" />
An example:
<bean class = "java.awt.TextField">
<field name = "text" value = "Welcome"/>
</bean>
- The field value can be set using an indirect value too:
<field [target = "bean"] name = "field name" [id =
"name of the bean"] >
…
</field>
The value is obtained by evaluating the element within the open and closed
field tags and returning it. If the type of the value and the type of the
field do not match, type conversion becomes necessary. Type conversion for
fields is done through the same way as for property tags.
- The field value can be obtained:
<field [target = "bean"] name = "property name" [id =
"name of the bean"] />
The <event-binding> tag
The BML supports wiring of events between JavaBeans. Beans that implement an
event listener can listen for corresponding events generated by a source bean.
Events can be binded between beans either through the listener model or through
the use of scripts:
- Binding events using the listener model
<event-binding [target ="bean"] name = "event set
name" [filter = "filter"]>
<bean …/>
</event-binding>
The child bean becomes a listener of "event set name" type events
generated by the target bean. The filter tag is unsupported in the current
version of the BML. If the target is not specified in the tag itself, the
default target is used. If the child bean does not implement the required
interface, an exception will be generated.
Example:
<bean class = "java.awt.TextField" >
<event-binding name = "action">
<bean source = "ActionHandler">
</event-binding>
</bean>
- The event generated causes a script to be executed
<event-binding [target="bean"] name = "event set name"
[filter = "filter"]>
<script …/>
</event-binding>
The child script is executed when the target bean generates the event of
"event set name" type.
An Example from the sample code:
<event-binding name = "adjustment">
<script>
<property target ="Juggler" name =
"animationRate">
<property target="event:arg1" name = "value"/>
</property>
</script>
</event-binding>
The above binding was used to tie the Scrollbar’s adjustment event to the
animation rate of the Juggler. The animation rate gets its value from an
argument that is provided to the event. Event:arg0 is a String that represents
the name of the method through which the event is delivered and event:arg1 is
the value itself.
The <call-method> tag
The call-method tag element can be used to call methods on beans. The target
specifies the bean on which the method is called. The name gives the name of the
method that will be called. If the id attribute is present, then the return
object is registered in the object registry with that name. The number of child
elements specifies the number of arguments to the call and the order is
specified by the order of these child elements. A static method can be invoked
by setting the target to a class object and specifying the method name.
<call-method [target = "bean"] name = "method name"
[id = "name"]>
…arguments
</call-method>
Example:
<bean class = "java.awt.Label">
<call-method name = "setText">
<string> Hello!</string>
</call-method>
</bean>
I will give an explanation of the other tags in the BML in the future. What
is apparent from the BML is that it can be used for dynamic application
development. The BML allows us to take separately available components and bind
them together using the Java event model without writing any additional code.
I recommend downloading the BML from the IBM alphaworks web site and
experimenting with it.
References:
The Bean Markup Language: Part 1
By Mark Johnson
http://www.javaworld.com/javaworld/jw-08-1999/jw-08-beans.html
The Bean Markup Language: Part 2
By Mark Johnson
http://www.javaworld.com/javaworld/jw-10-1999/jw-10-beans-3.html
BML User’s Guide
By Sanjiva Weerawarana and Matthew J. Duftler
http://www.alphaworks.ibm.com
BML Tutorial
Joseph Kesselman and Matthew J. Duftler
http://www.alphaworks.ibm.com
| Sponsored Links - please visit our sponsors |
| Secure FTP Applet | | Connect to FTP securely from within your browser. | | http://www.jscape.com/sftpapplet/ |
| Java SMTP Component | | Easily add SMTP to your Java apps | | http://www.jscape.com/inetfactory/smtp.html |
| FTP Applet | | Add file transfer capabilities to your web pages. | | http://www.jscape.com/ftpapplet/index.html |
| Learn Java Server Faces! | | Use WebGalileo Faces JSF components to quickly create Java based web apps. | | http://www.jscape.com/webgalileofaces/ |
| Java FTP Component | | Easily add FTP to your Java apps. | | http://www.jscape.com/inetfactory/ftp.html |
Sponsor this site
| |