Recent top five:
Let's talk about exceptions ...
How do you handle exceptions? Do you think upfront about the type of exceptions that you want to catch or do you just let
the outside world handle it?
-- Jeroen van Bergen in JW Blogs
| Enterprise AJAX - Transcend the Hype |
| Memory Analysis in Eclipse |
| Oracle Compatibility Developer's Guide |
| Memory Analysis in Eclipse |
By using application metadata, wise developers can accomplish a similar feat. Metadata, or data about data, describes software in an external and fluid manner that replaces internal hardcoded constants. By stashing those details in a separate editable text file, you allow yourself and other users/developers to drastically alter application behavior without writing new code. The metadata deployment descriptors of Java Enterprise applications testify to that flexibility. Take a look at the following increasingly sophisticated uses of metadata and consider how you might add the technique to your own coding repertoire.
Consider a basic application that connects to a database and issues a SQL query via JDBC. To connect to the database, you'll
need a number of parameters: driver, URL, user name, and password. An example of how those values are typically initialized
is listed in the MetadataBasicSample.java source file in the Resources section.
Listing 1 contains a skeletal version of the code:
Listing 1. Hardcoded JDBC application
public class MetadataBasicSample {
private String dbDriver;
private String dbURL;
private String dbUser;
private String dbPassword;
public static void main(String args[]) {
// ... CODE SNIPPED ... //
}
/** Constructor simply calls init(). */
public MetadataBasicSample() {
init();
}
/** Load property values into data members. */
private void init() {
try {
// set the properties
this.dbDriver = "oracle.jdbc.driver.OracleDriver";
this.dbURL = "jdbc:oracle:thin:@127.0.0.1:1521:DEV2";
this.dbUser = "monkey";
this.dbPassword = "password";
// load the driver
Class.forName(dbDriver);
} catch (Exception e) {
throw new RuntimeException("UNABLE TO INIT, EXITING...");
}
}
/** Retrieves a Connection using the hardcoded parameters. */
private Connection getConnection()
throws SQLException {
return DriverManager.getConnection(dbURL, dbUser, dbPassword);
}
}
The init() method initializes the data members with hardcoded values and, if any parameter needs changing, the code is edited and recompiled.
The application and its details are tightly coupled, making for an unnecessarily complex system.
A better design calls for decoupling the data from the application -- moving the data into a text-based configuration file that you can update separately from the code.
The database parameters, even in their hardcoded format, suggest a data structure in which properties are of name=value format.
The Properties class possesses a nifty load() method for loading such data into true Properties objects. Using the ClassLoader, any application can discover and transform any properties text file into a Properties object. Listing 2 shows a text file named MetadataPropsSample.props designed for that purpose.