Custom configuration

From Resin 3.0

Revision as of 04:07, 14 February 2006 by Sam (Talk | contribs)
Jump to: navigation, search


http://www.caucho.com/resin-3.0/config/init.xtp

Contents

Overview

Resin provides a configuration API to populate application beans from an XML file. Resin's configuration machinery supports Inversion of Control and allows the use of EL expressions.

Using Resin to parse XML

Resin's configuration API is the easiest method to parse XML.

  • Create Java classes mirroring the XML file.
  • Call Resin's configuration with an instance of the classes and the XML file.

Calling the Configuration

import com.caucho.config.*;

...

Config config = new Config();
MovieListing movies = new MovieList();

InputStream is = ...;

config.configure(bean, is);

Sample XML Parsing

Sample XML File

<movie-listing>
  <movie director="Jackson" title="LOTR: Fellowship of the Ring"/>
  <movie director="Jackson" title="LOTR: The Two Towers"/>
  <movie director="Jackson" title="LOTR: Return of the King"/>

  <movie director="Gilliam" title="Monty Python's Holy Grail"/>
  <movie director="Gilliam" title="Brazil"/>
</movie-listing>

To model the XML file, you'll use a Movie class and a MovieListener class:

Movie.java

public class Movie {
  private String _director;
  private String _title;

  public void setDirector(String director)
  {
    _director = director;
  }

  public void setTitle(String title)
  {
    _title = title;
  }
}

The movie listing encapsulates a list of movies

MovieListing.java

public class MovieListing {
  private ArrayList _movies = new ArrayList();

  public void addMovie(Movie movie)
  {
    _movies.add(movie);
  }
}
Personal tools