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.cassandra.meta;
22  
23  import com.datastax.driver.core.BoundStatement;
24  import com.datastax.driver.core.Row;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.synchronoss.cpo.CpoException;
28  import org.synchronoss.cpo.meta.MethodMapper;
29  
30  import java.io.Serializable;
31  import java.lang.reflect.Method;
32  import java.math.BigDecimal;
33  import java.math.BigInteger;
34  import java.net.InetAddress;
35  import java.nio.ByteBuffer;
36  import java.util.UUID;
37  
38  /**
39   * MethodMapper is a class defines the getters and setters for all the JDBC specific data classes
40   *
41   * @author david berry
42   */
43  public class CassandraMethodMapper implements Serializable, Cloneable {
44    private static final Logger logger = LoggerFactory.getLogger(CassandraMethodMapper.class);
45  
46    /**
47     * Version Id for this class.
48     */
49    private static final long serialVersionUID = 1L;
50    private static final Class<BoundStatement> bsc = BoundStatement.class;
51    private static final Class<Row> rsc = Row.class;
52    private static MethodMapper<CassandraMethodMapEntry<?,?>> methodMapper = initMethodMapper();
53  
54  
55    private CassandraMethodMapper() {
56    }
57  
58    static public CassandraMethodMapEntry<?,?> getDatasourceMethod(Class<?> c) throws CpoException {
59      return (CassandraMethodMapEntry)methodMapper.getDataMethodMapEntry(c);
60    }
61  
62    static private MethodMapper<CassandraMethodMapEntry<?,?>> initMethodMapper() throws IllegalArgumentException {
63      MethodMapper<CassandraMethodMapEntry<?,?>> mapper = new MethodMapper<>();
64      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, boolean.class, boolean.class, "getBool", "setBool"));
65      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, Boolean.class, boolean.class, "getBool", "setBool"));
66      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, ByteBuffer.class, ByteBuffer.class, "getBytes", "setBytes"));
67      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, java.util.Date.class, java.util.Date.class, "getDate", "setDate"));
68      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, BigDecimal.class, BigDecimal.class, "getDecimal", "setDecimal"));
69      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, double.class, double.class, "getDouble", "setDouble"));
70      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, Double.class, double.class, "getDouble", "setDouble"));
71      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, float.class, float.class, "getFloat", "setFloat"));
72      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, Float.class, float.class, "getFloat", "setFloat"));
73      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, InetAddress.class, InetAddress.class, "getInet", "setInet"));
74      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, int.class, int.class, "getInt", "setInt"));
75      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, Integer.class, int.class, "getInt", "setInt"));
76  //    mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_ONE, List.class, List.class, "getList", "setList"));
77      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, long.class, long.class, "getLong", "setLong"));
78      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, Long.class, long.class, "getLong", "setLong"));
79  //    mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_TWO, Map.class, Map.class, "getMap", "setMap"));
80  //    mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_TWO, Set.class, Set.class, "getSet", "setSet"));
81      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, String.class, String.class, "getString", "setString"));
82      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, UUID.class, UUID.class, "getUUID", "setUUID"));
83      mapper.addMethodMapEntry(makeCassandraMethodMapEntry(CassandraMethodMapEntry.METHOD_TYPE_BASIC, BigInteger.class, BigInteger.class, "getVarint", "setVarint"));
84  
85      return mapper;
86    }
87  
88    public static MethodMapper getMethodMapper() {
89      return methodMapper;
90    }
91  
92    private static <T> CassandraMethodMapEntry makeCassandraMethodMapEntry(int methodType, Class<T> javaClass, Class<T> datasourceMethodClass, String getterName, String setterName) throws IllegalArgumentException {
93      Method rsGetter=loadGetter(methodType, rsc, getterName);
94      Method bsSetter=loadSetter(methodType, bsc, datasourceMethodClass, setterName);
95  
96      return new CassandraMethodMapEntry(methodType, javaClass, datasourceMethodClass, rsGetter, bsSetter);
97    }
98  
99    private static <M,D> Method loadSetter(int methodType, Class<M> methodClass, Class<D> datasourceClass, String setterName) throws IllegalArgumentException {
100     Method setter;
101     try {
102       setter = methodClass.getMethod(setterName, new Class[]{int.class, datasourceClass});
103     } catch (NoSuchMethodException nsme) {
104       logger.error("Error loading Setter" + setterName, nsme);
105       throw new IllegalArgumentException(nsme);
106     }
107     return setter;
108   }
109 
110   public static <M> Method loadGetter(int methodType, Class<M> methodClass, String getterName) throws IllegalArgumentException {
111     Method getter;
112     try {
113       getter = methodClass.getMethod(getterName, new Class[]{int.class});
114     } catch (NoSuchMethodException nsme) {
115       logger.error("Error loading Getter" + getterName, nsme);
116       throw new IllegalArgumentException(nsme);
117     }
118     return getter;
119   }
120 
121 }