Parsing the configuration information
Now once we have the configuration of Trader object pretty much
done, we can write the parser that will parse these
configuration files and create the appropriate object and return
it to the calling client. Here we shall see a pretty much native
and trivial implementation of the XML parsing. After all, the
purpose of this article is not to show the best way of parsing
XML but to show how it is used to get a Trader pattern up and
running.
We now write a class that is the Trader object. (Download the
source code to see a complete source for this).
public class TraderConfigParser
{
The parser is mainly responsible for the following
activities:
- Parse the component-definition.xml file and find the name of
the component, its instance and interface definition and the
service type.
/**
* Loads the trader configuration data.
* @throws JDOMException
*/
private void loadTraderConfigData() throws JDOMException
- Parse the service-config.xml file.
- Load the service configuration defined in this file with the
component name as the key from the component-definition.xml
file. For example, the component definition.xml file defines
only two components Account and Person. So the attempt should only
be to load the service information for these two components
albeit there may be other component service configurations
defined which are not in use.
- Check for the specified service type in the component-
definition.xml file and load the service configuration
information for that service type only. For example, the Account
component has definition for both "ejb" and "Java" service
types. However, in the component-definition.xml file we have
specified the service-type as "Java" . So the Trader should load
the configuration information for the service type "Java" only.
- Create an appropriate instance of the defined component
instance and put it in a hashtable with the name of the
component as the key.
- All of this is done within a method called
/**
* load the service configuration data and creates
the instance of the requested
* object and puts it into the hashtable with the
name of the component as the key.
* @param componentName
* @param interfaceName
* @param instanceName
* @param serviceType
* @throws JDOMException
* @throws ClassNotFoundException
* @throws NamingException
*/
private void loadServiceConfigData(String componentName, String
interfaceName, String instanceName, String serviceType)
throws JDOMException, ClassNotFoundException, NamingException
- There is a public method to allow clients to get hold of the
required instance of a service component by specifying the name
of the component.
/**
* Returns the requested component instance. Note that the client
needs to
* cast it back to the appropriate interface or implementation
class.
* @param componentName
* @return
*/
public Object getComponent(String componentName)
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.