KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Types JavaDoc;
25
26 import org.jboss.deployment.DeploymentException;
27 import org.jboss.logging.Logger;
28
29 import org.jboss.metadata.MetaData;
30 import org.w3c.dom.Element JavaDoc;
31
32 /**
33  * Imutable class which holds a mapping between a Java Class and a JDBC type
34  * and a SQL type.
35  *
36  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
37  * @author <a HREF="sebastien.alborini@m4x.org">Sebastien Alborini</a>
38  * @version $Revision: 37459 $
39  */

40 public final class JDBCMappingMetaData
41 {
42    private static Logger log = Logger.getLogger(JDBCMappingMetaData.class.getName());
43
44    /**
45     * Gets the JDBC type constant int for the name. The mapping from name to jdbc
46     * type is contained in java.sql.Types.
47     *
48     * @param name the name for the jdbc type
49     * @return the int type constant from java.sql.Types
50     * @see java.sql.Types
51     */

52    public static int getJdbcTypeFromName(String JavaDoc name) throws DeploymentException
53    {
54       if(name == null)
55       {
56          throw new DeploymentException("jdbc-type cannot be null");
57       }
58
59       try
60       {
61          Integer JavaDoc constant = (Integer JavaDoc)Types JavaDoc.class.getField(name).get(null);
62          return constant.intValue();
63
64       }
65       catch(Exception JavaDoc e)
66       {
67          log.warn("Unrecognized jdbc-type: " + name + ", using Types.OTHER", e);
68          return Types.OTHER;
69       }
70    }
71
72    /** fully qualified Java type name */
73    private final String JavaDoc javaType;
74    /** JDBC type according to java.sql.Types */
75    private final int jdbcType;
76    /** SQL type */
77    private final String JavaDoc sqlType;
78    /** parameter setter */
79    private final String JavaDoc paramSetter;
80    /** result set reader */
81    private final String JavaDoc resultReader;
82
83    /**
84     * Constructs a mapping with the data contained in the mapping xml element
85     * from a jbosscmp-jdbc xml file.
86     *
87     * @param element the xml Element which contains the metadata about
88     * this mapping
89     * @throws DeploymentException if the xml element is not semantically correct
90     */

91    public JDBCMappingMetaData(Element JavaDoc element) throws DeploymentException
92    {
93       javaType = MetaData.getUniqueChildContent(element, "java-type");
94       jdbcType = getJdbcTypeFromName(MetaData.getUniqueChildContent(element, "jdbc-type"));
95       sqlType = MetaData.getUniqueChildContent(element, "sql-type");
96       paramSetter = MetaData.getOptionalChildContent(element, "param-setter");
97       resultReader = MetaData.getOptionalChildContent(element, "result-reader");
98    }
99
100    /**
101     * Gets the java type of this mapping. The java type is used to differentiate
102     * this mapping from other mappings.
103     *
104     * @return the java type of this mapping
105     */

106    public String JavaDoc getJavaType()
107    {
108       return javaType;
109    }
110
111    /**
112     * Gets the jdbc type of this mapping. The jdbc type is used to retrieve data
113     * from a result set and to set parameters in a prepared statement.
114     *
115     * @return the jdbc type of this mapping
116     */

117    public int getJdbcType()
118    {
119       return jdbcType;
120    }
121
122    /**
123     * Gets the sql type of this mapping. The sql type is the sql column data
124     * type, and is used in CREATE TABLE statements.
125     *
126     * @return the sql type String of this mapping
127     */

128    public String JavaDoc getSqlType()
129    {
130       return sqlType;
131    }
132
133    public String JavaDoc getParamSetter()
134    {
135       return paramSetter;
136    }
137
138    public String JavaDoc getResultReader()
139    {
140       return resultReader;
141    }
142 }
143
Popular Tags