View Javadoc
1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
20   */
21  package org.synchronoss.cpo.jdbc.meta;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.synchronoss.cpo.CpoException;
26  import org.synchronoss.cpo.meta.MethodMapper;
27  
28  import java.io.*;
29  import java.lang.reflect.Method;
30  import java.math.BigDecimal;
31  import java.net.URL;
32  import java.sql.*;
33  
34  /**
35   * MethodMapper is a class defines the getters and setters for all the JDBC specific data classes
36   *
37   * @author david berry
38   */
39  public class JdbcMethodMapper implements java.io.Serializable, java.lang.Cloneable {
40    private static final Logger logger = LoggerFactory.getLogger(JdbcMethodMapper.class);
41  
42    /**
43     * Version Id for this class.
44     */
45    private static final long serialVersionUID = 1L;
46    private static final Class<PreparedStatement> psc = PreparedStatement.class;
47    private static final Class<ResultSet> rsc = ResultSet.class;
48    private static final Class<CallableStatement> csc = CallableStatement.class;
49    private static MethodMapper<JdbcMethodMapEntry<?,?>> methodMapper = initMethodMapper();
50  
51  
52    private JdbcMethodMapper() {
53    }
54  
55    static public JdbcMethodMapEntry<?,?> getJavaSqlMethod(Class<?> c) throws CpoException {
56      return (JdbcMethodMapEntry)methodMapper.getDataMethodMapEntry(c);
57    }
58  
59    static private MethodMapper<JdbcMethodMapEntry<?,?>> initMethodMapper() throws IllegalArgumentException {
60      MethodMapper<JdbcMethodMapEntry<?,?>> mapper = new MethodMapper<>();
61      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, String.class, String.class, "getString", "setString"));
62      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, BigDecimal.class, BigDecimal.class, "getBigDecimal", "setBigDecimal"));
63      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, byte.class, byte.class, "getByte", "setByte"));
64      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, Byte.class, byte.class, "getByte", "setByte"));
65      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, short.class, short.class, "getShort", "setShort"));
66      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, Short.class, short.class, "getShort", "setShort"));
67      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, int.class, int.class, "getInt", "setInt"));
68      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, Integer.class, int.class, "getInt", "setInt"));
69      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, long.class, long.class, "getLong", "setLong"));
70      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, Long.class, long.class, "getLong", "setLong"));
71      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, float.class, float.class, "getFloat", "setFloat"));
72      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, Float.class, float.class, "getFloat", "setFloat"));
73      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, double.class, double.class, "getDouble", "setDouble"));
74      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, Double.class, double.class, "getDouble", "setDouble"));
75      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, byte[].class, byte[].class, "getBytes", "setBytes"));
76      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, java.sql.Date.class, java.sql.Date.class, "getDate", "setDate"));
77      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, java.sql.Time.class, java.sql.Time.class, "getTime", "setTime"));
78      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, java.sql.Timestamp.class, java.sql.Timestamp.class, "getTimestamp", "setTimestamp"));
79      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, java.sql.Clob.class, java.sql.Clob.class, "getClob", "setClob"));
80      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, java.sql.Blob.class, java.sql.Blob.class, "getBlob", "setBlob"));
81      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, java.sql.Array.class, java.sql.Array.class, "getArray", "setArray"));
82      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, java.sql.Ref.class, java.sql.Ref.class, "getRef", "setRef"));
83      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, Object.class, Object.class, "getObject", "setObject"));
84      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, URL.class, URL.class, "getURL", "setURL"));
85      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, boolean.class, boolean.class, "getBoolean", "setBoolean"));
86      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_BASIC, Boolean.class, boolean.class, "getBoolean", "setBoolean"));
87      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_STREAM, InputStream.class, InputStream.class, "getBlob", "setBinaryStream"));
88      mapper.addMethodMapEntry(makeJdbcMethodMapEntry(JdbcMethodMapEntry.METHOD_TYPE_READER, Reader.class, Reader.class, "getClob", "setCharacterStream"));
89  
90      return mapper;
91    }
92  
93    public static MethodMapper getMethodMapper() {
94      return methodMapper;
95    }
96  
97    private static <T> JdbcMethodMapEntry<?,?> makeJdbcMethodMapEntry(int methodType, Class<T> javaClass, Class<T> datasourceMethodClass, String getterName, String setterName) throws IllegalArgumentException {
98      Method rsGetter=loadGetter(methodType, rsc, getterName);
99      Method bsSetter=loadSetter(methodType, psc, datasourceMethodClass, setterName);
100     Method csGetter=loadGetter(methodType, csc, getterName);
101     Method csSetter=loadSetter(methodType, csc, datasourceMethodClass, setterName);
102 
103     return new JdbcMethodMapEntry<>(methodType, javaClass, datasourceMethodClass, rsGetter, bsSetter, csGetter, csSetter);
104   }
105 
106   private static <M,D> Method loadSetter(int methodType, Class<M> methodClass, Class<D> datasourceClass, String setterName) throws IllegalArgumentException {
107     Method setter;
108     try {
109       if (methodType == JdbcMethodMapEntry.METHOD_TYPE_BASIC) {
110         setter = methodClass.getMethod(setterName, new Class[]{int.class, datasourceClass});
111       } else {
112         setter = methodClass.getMethod(setterName, new Class[]{int.class, datasourceClass, int.class});
113       }
114 
115     } catch (NoSuchMethodException nsme) {
116       logger.error("Error loading Setter" + setterName, nsme);
117       throw new IllegalArgumentException(nsme);
118     }
119     return setter;
120   }
121 
122   private static <M> Method loadGetter(int methodType, Class<M> methodClass, String getterName) throws IllegalArgumentException {
123     Method getter;
124     try {
125       getter = methodClass.getMethod(getterName, new Class[]{int.class});
126     } catch (NoSuchMethodException nsme) {
127       logger.error("Error loading Getter" + getterName, nsme);
128       throw new IllegalArgumentException(nsme);
129     }
130     return getter;
131   }
132 
133 }