package org.jboss.ejb.plugins.cmp.jdbc.metadata;
import java.util.Iterator;
import java.util.HashMap;
import org.jboss.deployment.DeploymentException;
import org.jboss.metadata.MetaData;
import org.w3c.dom.Element;
public final class JDBCEntityCommandMetaData
{
private final String commandName;
private final Class commandClass;
private final HashMap attributes = new HashMap();
public JDBCEntityCommandMetaData( Element element )
throws DeploymentException
{
commandName = element.getAttribute( "name" );
if( commandName.trim().length() < 1 )
{
throw new DeploymentException( "entity-command element must have "
+ " not empty name attribute" );
}
String commandClassStr = element.getAttribute( "class" );
if(commandClassStr != null)
{
try
{
commandClass = GetTCLAction.
getContextClassLoader().loadClass( commandClassStr );
} catch (ClassNotFoundException e) {
throw new DeploymentException( "Could not load class: "
+ commandClassStr);
}
}
else
{
commandClass = null;
}
for( Iterator iter = MetaData.getChildrenByTagName( element, "attribute" );
iter.hasNext(); )
{
Element attrEl = (Element) iter.next();
String attrName = attrEl.getAttribute( "name" );
if( attrName == null )
{
throw new DeploymentException( "entity-command " + commandName
+ " has an attribute with no name" );
}
String attrValue = MetaData.getElementContent( attrEl );
attributes.put( attrName, attrValue );
}
}
public JDBCEntityCommandMetaData( Element element,
JDBCEntityCommandMetaData defaultValues )
throws DeploymentException
{
commandName = defaultValues.getCommandName();
String commandClassStr = element.getAttribute( "class" );
if( (commandClassStr != null)
&& (commandClassStr.trim().length() > 0) )
{
try
{
commandClass = GetTCLAction.
getContextClassLoader().loadClass( commandClassStr );
} catch (ClassNotFoundException e) {
throw new DeploymentException( "Could not load class: "
+ commandClassStr);
}
}
else
{
commandClass = defaultValues.getCommandClass();
}
attributes.putAll( defaultValues.attributes );
for( Iterator iter = MetaData.getChildrenByTagName( element, "attribute" );
iter.hasNext(); )
{
Element attrEl = (Element) iter.next();
String attrName = attrEl.getAttribute( "name" );
if( attrName == null )
{
throw new DeploymentException( "entity-command " + commandName
+ " has an attribute with no name" );
}
String attrValue = MetaData.getElementContent( attrEl );
attributes.put( attrName, attrValue );
}
}
public String getCommandName() {
return commandName;
}
public Class getCommandClass() {
return commandClass;
}
public String getAttribute( String name )
{
return (String) attributes.get( name );
}
public String toString()
{
return new StringBuffer( "[commandName=" ).append( commandName ).
append( ",commandClass=" ).append( commandClass ).
append( ",attributes=" ).append( attributes.toString() ).
append( "]" ).toString();
}
}