JavaBeans Activation Framework standard extension, developers who use Java technology can take advantage of standard services to
-determine the type of an arbitrary piece of data,
-encapsulate access to it,
-discover the operations available on it, and
-to instantiate the appropriate bean to perform said operation(s).
The API doc said: It is used by the JavaMail API to manage MIME data.But actually, it is more general purpose.
2. The Spec saidâŠ
With the JavaBeans Activation Framework standard extension, developers
who use Java technology can take advantage of standard services to
determine the type of an arbitrary piece of data,
encapsulate access to it,
discover the operations available on it, and
to instantiate the appropriate bean to perform said operation(s).
The API doc said: It is used by the JavaMail API to manage MIME data.
But actually, it is more general purpose.
3. JAF
JSR-925
Latest spec version is 1.1
Itâs an old spec - released on April 2006
Itâs originally an extension API
Now available as standard API in Java SE 6 and Java EE 5
Has only one package javax.activation (4 interfaces, 13 classes)
Spec document can be seen online at
http://www.oracle.com/technetwork/java/jaf-1-150219.pdf
API documentation
4. JAF architecture
Major components on the JAF architecture
Encapsulates an object that contains data, and that can return both
âą a stream providing data access, and
âą a string defining the MIME type describing the data.
Beans extend the CommandObject
interface in order to interact with
JAF services
âą Allows consumers of its interfaces to determine the
provides a consistent interface âcommandsâ available on a particular MIME type
between JAF-aware clients and âą An interface to retrieve an object that can operate
other subsystems on an object of a particular MIME type
Source: JAF Specification v1.1
5. DataSource Interface
Provides access to an arbitrary collection of data
Get name of the data, data-type name (content type), and the data itself as
InputStream or OutputStream
Two implementation classes provided
URLDataSource
simplifies the handling of data described by URLs
FileDataSource
simple DataSource object that encapsulates a file
provides data typing services -> delegated to a FileTypeMap object.
Other implementation
javax.mail.internet.MimePartDataSource
6. DataContentHandler interface
convert the object to a byte stream and write it to the output stream
convert streams in to objects
Used to get object/data which can be transferred
Uses java.awt.datatransfer.DataFlavor to indicate the data that can be
accessed
DataFlavor is a data format as would appear on a clipboard, during drag and
drop, or in a file system
7. CommandMap class
An abstract class provides an interface to a registry of command objects
available in the system
Developer develop their own implementation or use
MailcapCommandMap class that implements a CommandMap whose
configuration is based on mailcap files (RFC 1524)
Command list available from a MIME Type is stored in CommandInfo object
8. CommandObject interface
Interface to be implemented by JavaBeans components that are Activation
Framework aware
Simple interface with one method:
setCommandContext(String verb, DataHandler dh)
9. Example: Compose an e-mail with attachment
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.internet.*;
import javax.mail.*;
...
// Create a message.
MimeMessage message = new MimeMessage(session);
...
// Create the Multipart to be added the parts to
Multipart multipart= new MimeMultipart();
// Create and fill the first text message part
MimeBodyPart mbp = new MimeBodyPart(); mbp.setText(Body);
multipart.addBodyPart(mbp);
// Create a file attachment and fill as second message part
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(âC:attachment.zipâ);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
multipart.addBodyPart(mbp);
// Add the multipart to the message
message.setContent(multipart);
...
10. JAF in use
Another example of JAF in use:
REST easy with the JavaBeans Activation Framework
http://www.javaworld.com/javaworld/jw-10-2007/jw-10-resteasy.html
Example also available in Java 6âą New Features: A Tutorial book, chapter-13
ISBN 0-9752128-8-5
Other presentation deck:
http://www.hilbertinc.com/whitepapers/JAF.pdf