|
Introduction This article provides an overview of the Generic Connection Framework (GCF), a J2ME API that provides broad support for connection types. GCF was originally defined to rely on the J2ME platform's Connected Limited Device Configuration (CLDC), version 1.0, because the familiar J2SE java.net and java.io APIs were considered too large to fit into the constrained memory available in mobile devices. Today you can find the GCF not only in CLDC-based profiles, such as the Mobile Information Device Profile (MIDP) and the Information Module Profile (IMP), but also in Connected Device Configuration (CDC)-based profiles, such as the Foundation Profile and its relatives the Personal Basis Profile and Personal Profile - and now, with JSR 197, on the J2SE platform as well. You can also find the GCF in an increasing number of optional packages, including those that provide Bluetooth support and access to files and smart cards. A Generic Approach to Connectivity The GCF is a straightforward hierarchy of interfaces and classes to create connections (such as HTTP, datagram, or streams) and perform I/O. As the name implies, the GCF provides a generic approach to connectivity. It is generic because it provides a common foundation API for all the basic connection types - for packet-based (data blocks) and stream-based (contiguous or sequence of data) input and output. This generalization is possible through the use of:
The base CLDC 1.0 GCF is illustrated in Figure 1:
At the top of the interface hierarchy is the
For packet-based I/O the GCF defines
In addition to the connection hierarchy, the GCF provides the Connector class, which is the connection factory, and the
Not defined by the GCF but related to it are The GCF is so practical and flexible that it is used across J2ME profiles and optional packages, and now on the J2SE platform as well. The relationship between the base CLDC GCF and the GCF for profiles and platforms is illustrated in Figure 2:
MIDP and the Foundation Profile extend the base CLDC 1.0 GCF, while the J2SE version of GCF comes directly from the GCF for Foundation Profile. The GCF - An Extensible Framework for Not-So-Generic Connections
The GCF, which is defined in the When we look closely at a typical connection and its related I/O protocol we see that these are not generic. For example, HTTP connections have peculiarities that reflect their request/response nature, while a Bluetooth connection exposes the dynamic (ad hoc) nature of its protocol, and a socket connection exposes low-level networking methods for timeouts and keep-alive options. Fortunately, the GCF is extensible. New connection types, which are defined and standardized via the Java Community Process (JCP), can be added by defining a new Connection subtype and supporting classes, providing a Connector factory class that supports the newly defined connection type, and defining a new URL scheme that identifies the new connection type. Figure 3 illustrates how a particular profile or optional package could extend the GCF:
A new connection type can extend the basic Connection interface or one of its subtypes. A Rich Set of Connection Types The GCF supports a large number of connection types, across profiles and platforms, all in a consistent manner. URLs play a very important role in the GCF. One of the most widely used inventions of the Internet, URLs describe the location and access method for a resource on the Internet using a hierarchical notation. In the GCF, URLs identify connection types and endpoints. The general format of a URL, as defined in RFC 1738 (and later in RFC 2396) is as follows:
Note that the delimiters are literal, and required as appropriate. For example, parameters are optional, but if supplied must be preceded by a semi-colon. Of special importance is the scheme, which in the GCF describes the connection type to create. Table 1 summarizes some of the GCF URL schemes and connection types that are currently defined by the JCP: Table 1. GCF URL Schemes and Connection Types
Note that most of the connection types are optional (device manufacturers are not required to support them) and defined by an optional package. Before using an API make sure the devices you are targeting incorporate the appropriate JSR. Table 2 summarizes some of the currently defined core GCF connection types, by profile: Table 2. Summary of Core GCF Connection Types, By Profile
* A URL scheme of "file:" returns an InputConnection. The MIDP 2.0 GCF provides the most connection types, while the other profiles and J2SE just support HttpConnection, in addition to the basic CDLC GCF types. Support: Required Versus Optional
It is important to realize that even though all core interfaces and classes must be present, as defined by the specification, the actual implementations may be optional. See the "Vendors Must Support?" column in Table 2. For example, the MIDP 1.0 specification mandates support only for Using the Generic Connection Framework Using the GCF is very simple. To create a connection you use the Connector factory class and a URL. To close it, you use the created Connection subtype object. Using a connection can be easy or more involved, depending on the connection type, because each connection type has its own peculiarities. Opening a Connection - Using The Connector Class The Connector class is the connection factory. It creates and opens an instance of the appropriate subtype of Connection based on the URL you specify, if that type is supported. To create a connection, call the Connection.open() method as in the following code snippet:
The Connector class defines three
...where:
The simple form,
The Connector class contains convenience methods for creating streams of various types:
Because these helper methods are static, you can get the input or output streams directly, as follows:
This method of opening streams is discouraged, however, because you don't get a reference to the connection itself, and thus don't have access to connection-specific methods or attributes. For example, if you pass to Next are some examples of how to create various connection types:
Closing a Connection - Using the Connection Interface
The Connection interface defines the most basic connection type. Its only method is
Using a Connection Covering all the different connection types supported by the GCF is outside the scope of this article, but you can find other GCF-related articles on this site:
Conclusion: This article introduced the Generic Connection Framework, a very flexible and extensible API for network, file, serial, and other kinds of I/O. The GCF started as the I/O API for CLDC-based platforms, but due to its flexibility it has been incorporated into CDC-based profiles and the J2SE platform as well. This article also showed the wide variety of connection types that GCF makes readily available to applications based on CLDC, CDC, and J2SE. Acknowledgments Thanks to Roger Riggs and James Allen for their contributions to this article. And many thanks to Brian Christeson, for making this article just better. Back To Top | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||