KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > metadata > JDBCEntityCommandMetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc.metadata;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import org.jboss.deployment.DeploymentException;
27 import org.jboss.metadata.MetaData;
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  * This immutable class contains information about entity command
32  *
33  * @author <a HREF="mailto:loubyansky@ua.fm">Alex Loubyansky</a>
34  * @version $Revision: 37459 $
35  */

36 public final class JDBCEntityCommandMetaData
37 {
38
39    // Attributes -----------------------------------------------------
40

41    /** The name (alias) of the command. */
42    private final String JavaDoc commandName;
43
44    /** The class of the command */
45    private final Class JavaDoc commandClass;
46
47    /** Command attributes */
48    private final HashMap JavaDoc attributes = new HashMap JavaDoc();
49   
50
51    // Constructor ----------------------------------------------------
52

53    /**
54     * Constructs a JDBCEntityCommandMetaData reading the entity-command element
55     * @param element - entity-command element
56     */

57    public JDBCEntityCommandMetaData( Element JavaDoc element )
58       throws DeploymentException
59    {
60       // command name
61
commandName = element.getAttribute( "name" );
62       if( commandName.trim().length() < 1 )
63       {
64          throw new DeploymentException( "entity-command element must have "
65             + " not empty name attribute" );
66       }
67
68       String JavaDoc commandClassStr = element.getAttribute( "class" );
69       if(commandClassStr != null)
70       {
71          try
72          {
73             commandClass = GetTCLAction.
74                getContextClassLoader().loadClass( commandClassStr );
75          } catch (ClassNotFoundException JavaDoc e) {
76             throw new DeploymentException( "Could not load class: "
77                + commandClassStr);
78          }
79       }
80       else
81       {
82          commandClass = null;
83       }
84
85       // attributes
86
for( Iterator JavaDoc iter = MetaData.getChildrenByTagName( element, "attribute" );
87          iter.hasNext(); )
88       {
89          Element JavaDoc attrEl = (Element JavaDoc) iter.next();
90
91          // attribute name
92
String JavaDoc attrName = attrEl.getAttribute( "name" );
93          if( attrName == null )
94          {
95             throw new DeploymentException( "entity-command " + commandName
96                + " has an attribute with no name" );
97          }
98
99          // attribute value
100
String JavaDoc attrValue = MetaData.getElementContent( attrEl );
101
102          attributes.put( attrName, attrValue );
103       }
104    }
105
106    /**
107     * Constructs a JDBCEntityCommandMetaData from entity-command xml element
108     * and default values
109     * @param element entity-command element
110     */

111    public JDBCEntityCommandMetaData( Element JavaDoc element,
112                                      JDBCEntityCommandMetaData defaultValues )
113       throws DeploymentException
114    {
115       // command name
116
commandName = defaultValues.getCommandName();
117
118       String JavaDoc commandClassStr = element.getAttribute( "class" );
119       if( (commandClassStr != null)
120          && (commandClassStr.trim().length() > 0) )
121       {
122          try
123          {
124             commandClass = GetTCLAction.
125                getContextClassLoader().loadClass( commandClassStr );
126          } catch (ClassNotFoundException JavaDoc e) {
127             throw new DeploymentException( "Could not load class: "
128                + commandClassStr);
129          }
130       }
131       else
132       {
133          commandClass = defaultValues.getCommandClass();
134       }
135
136       // attributes
137
attributes.putAll( defaultValues.attributes );
138       for( Iterator JavaDoc iter = MetaData.getChildrenByTagName( element, "attribute" );
139          iter.hasNext(); )
140       {
141          Element JavaDoc attrEl = (Element JavaDoc) iter.next();
142
143          // attribute name
144
String JavaDoc attrName = attrEl.getAttribute( "name" );
145          if( attrName == null )
146          {
147             throw new DeploymentException( "entity-command " + commandName
148                + " has an attribute with no name" );
149          }
150
151          // attribute value
152
String JavaDoc attrValue = MetaData.getElementContent( attrEl );
153
154          attributes.put( attrName, attrValue );
155       }
156    }
157
158    // Public ----------------------------------------------------------
159

160    /**
161     * @return the name of the command
162     */

163    public String JavaDoc getCommandName() {
164       return commandName;
165    }
166
167    /**
168     * @return the class of the command
169     */

170    public Class JavaDoc getCommandClass() {
171       return commandClass;
172    }
173
174    /**
175     * @return value for the passed in parameter name
176     */

177    public String JavaDoc getAttribute( String JavaDoc name )
178    {
179       return (String JavaDoc) attributes.get( name );
180    }
181
182    // Object overrides --------------------------------------------------
183

184    public String JavaDoc toString()
185    {
186       return new StringBuffer JavaDoc( "[commandName=" ).append( commandName ).
187          append( ",commandClass=" ).append( commandClass ).
188          append( ",attributes=" ).append( attributes.toString() ).
189          append( "]" ).toString();
190    }
191 }
192
Popular Tags