Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Crafting Metadata

Decouple applications and their details using properties, XML, and cryptography

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
When a story is reconstructed, its central plot doesn't change. But depending on the author and audience, the plot is obfuscated by changes in context: settings, scene sequences, characters, tone, and dialogue. But the story, the core of what happens isn't reinvented. New incarnations of ancient myths populate silver screens, books, even television sitcoms, and legal-thriller writers reiterate the same stories with new casts.

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.

Traditional configuration files

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.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources