2017-05-27 23:54:22 -04:00
|
|
|
package com.moparisthebest.jdbc.codegen;
|
|
|
|
|
2017-07-01 02:07:04 -04:00
|
|
|
import com.moparisthebest.jdbc.Factory;
|
|
|
|
|
2017-07-03 00:45:36 -04:00
|
|
|
import javax.naming.Context;
|
|
|
|
import javax.naming.InitialContext;
|
|
|
|
import javax.naming.NamingException;
|
|
|
|
import javax.sql.DataSource;
|
2017-07-01 02:07:04 -04:00
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2017-07-03 03:05:53 -04:00
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.lang.reflect.Modifier;
|
2017-05-27 23:54:22 -04:00
|
|
|
import java.sql.Connection;
|
2017-07-01 02:07:04 -04:00
|
|
|
import java.sql.SQLException;
|
2017-05-27 23:54:22 -04:00
|
|
|
|
2017-07-03 00:45:36 -04:00
|
|
|
import static com.moparisthebest.jdbc.TryClose.tryClose;
|
|
|
|
|
2017-05-27 23:54:22 -04:00
|
|
|
/**
|
|
|
|
* Created by mopar on 5/24/17.
|
|
|
|
*/
|
2017-07-01 02:07:04 -04:00
|
|
|
public class JdbcMapperFactory<T> implements Factory<T> {
|
2017-05-27 23:54:22 -04:00
|
|
|
|
2017-07-03 03:05:53 -04:00
|
|
|
static {
|
|
|
|
try{
|
|
|
|
final Class<?> ensureContext = Class.forName(System.getProperty("QueryMapper.ensureContext.class", System.getProperty("JdbcMapper.ensureContext.class", "com.gcl.containerless.EnsureContext")));
|
|
|
|
final Method method = ensureContext.getMethod(System.getProperty("QueryMapper.ensureContext.method", System.getProperty("JdbcMapper.ensureContext.method", "setup")));
|
|
|
|
method.invoke(null);
|
|
|
|
}catch(Throwable e){
|
|
|
|
// ignore
|
|
|
|
//e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 23:54:22 -04:00
|
|
|
@SuppressWarnings("unchecked")
|
2017-07-01 02:07:04 -04:00
|
|
|
public static <T> Class<? extends T> getImplementationClass(final Class<T> jdbcMapper) throws ClassNotFoundException {
|
2017-07-03 03:05:53 -04:00
|
|
|
if(jdbcMapper.isInterface() || Modifier.isAbstract(jdbcMapper.getModifiers()))
|
2019-02-09 00:38:08 -05:00
|
|
|
return (Class<? extends T>) Class.forName(jdbcMapper.getName() + JdbcMapper.beanSuffix);
|
2017-07-03 03:05:53 -04:00
|
|
|
else
|
|
|
|
return jdbcMapper;
|
2017-07-01 02:07:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static <T> Constructor<? extends T> getConnectionConstructor(final Class<T> jdbcMapper) throws ClassNotFoundException, NoSuchMethodException {
|
|
|
|
return getImplementationClass(jdbcMapper).getConstructor(Connection.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T> Constructor<? extends T> getDefaultConstructor(final Class<T> jdbcMapper) throws ClassNotFoundException, NoSuchMethodException {
|
|
|
|
return getImplementationClass(jdbcMapper).getConstructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T> Constructor<? extends T> getFactoryConstructor(final Class<T> jdbcMapper) throws ClassNotFoundException, NoSuchMethodException {
|
|
|
|
return getImplementationClass(jdbcMapper).getConstructor(Factory.class);
|
|
|
|
}
|
|
|
|
|
2017-05-28 23:38:10 -04:00
|
|
|
public static <T> T create(final Class<T> jdbcMapper, final Connection connection) {
|
2017-05-27 23:54:22 -04:00
|
|
|
try {
|
2017-07-01 02:07:04 -04:00
|
|
|
return getConnectionConstructor(jdbcMapper).newInstance(connection);
|
2017-05-27 23:54:22 -04:00
|
|
|
} catch (Throwable e) {
|
|
|
|
throw new RuntimeException("could not create JdbcMapper, did the processor run at compile time?", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 03:05:53 -04:00
|
|
|
public static <T> T create(final Class<T> jdbcMapper, final Factory<Connection> connectionFactory) {
|
|
|
|
try {
|
|
|
|
return getFactoryConstructor(jdbcMapper).newInstance(connectionFactory);
|
|
|
|
} catch (Throwable e) {
|
|
|
|
throw new RuntimeException("could not create JdbcMapper, did the processor run at compile time?", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 23:38:10 -04:00
|
|
|
public static <T> T create(final Class<T> jdbcMapper) {
|
2017-07-01 02:07:04 -04:00
|
|
|
try {
|
|
|
|
return getDefaultConstructor(jdbcMapper).newInstance();
|
|
|
|
} catch (Throwable e) {
|
|
|
|
throw new RuntimeException("could not create JdbcMapper, did the processor run at compile time?", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 00:45:36 -04:00
|
|
|
public static Factory<Connection> connectionFactory(final DataSource dataSource) {
|
|
|
|
if (dataSource == null)
|
|
|
|
throw new NullPointerException("dataSource must be non-null");
|
|
|
|
return
|
|
|
|
//IFJAVA8_START
|
|
|
|
dataSource::getConnection
|
|
|
|
//IFJAVA8_END
|
|
|
|
/*IFJAVA6_START
|
|
|
|
new Factory<Connection>() {
|
|
|
|
@Override
|
|
|
|
public Connection create() throws SQLException {
|
|
|
|
return dataSource.getConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IFJAVA6_END*/
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Factory<Connection> connectionFactorySingleLookup(final String jndiName) {
|
|
|
|
if (jndiName == null)
|
|
|
|
throw new NullPointerException("jndiName must be non-null");
|
|
|
|
Context context = null;
|
|
|
|
try {
|
|
|
|
context = new InitialContext();
|
|
|
|
return connectionFactory((DataSource) context.lookup(jndiName));
|
|
|
|
} catch (NamingException e) {
|
|
|
|
throw new RuntimeException("JNDI lookup failed to create connection", e);
|
|
|
|
} finally {
|
|
|
|
tryClose(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Factory<Connection> connectionFactory(final String jndiName) {
|
|
|
|
if (jndiName == null)
|
|
|
|
throw new NullPointerException("jndiName must be non-null");
|
|
|
|
return
|
|
|
|
/*IFJAVA6_START
|
|
|
|
new Factory<Connection>() {
|
|
|
|
@Override
|
|
|
|
public Connection create() throws SQLException
|
|
|
|
IFJAVA6_END*/
|
|
|
|
//IFJAVA8_START
|
|
|
|
() ->
|
|
|
|
//IFJAVA8_END
|
|
|
|
{
|
|
|
|
Context context = null;
|
|
|
|
try {
|
|
|
|
context = new InitialContext();
|
|
|
|
final DataSource ds = (DataSource) context.lookup(jndiName);
|
|
|
|
return ds.getConnection();
|
|
|
|
} catch (NamingException e) {
|
|
|
|
throw new RuntimeException("JNDI lookup failed to create connection", e);
|
|
|
|
} finally {
|
|
|
|
tryClose(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*IFJAVA6_START
|
|
|
|
}
|
|
|
|
IFJAVA6_END*/
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2017-07-03 03:05:53 -04:00
|
|
|
public static <T> Factory<T> of(final Class<T> jdbcMapper) {
|
|
|
|
return new JdbcMapperFactory<T>(jdbcMapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T> Factory<T> of(final Class<T> jdbcMapper, final Factory<Connection> connectionFactory) {
|
|
|
|
return new JdbcMapperFactory<T>(jdbcMapper, connectionFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T> Factory<T> of(final Class<T> jdbcMapper, final String jndiName) {
|
|
|
|
return of(jdbcMapper, connectionFactory(jndiName));
|
|
|
|
}
|
|
|
|
|
2017-07-01 02:07:04 -04:00
|
|
|
private final Constructor<? extends T> constructor;
|
|
|
|
private final Object[] args;
|
|
|
|
|
2017-07-03 03:05:53 -04:00
|
|
|
private JdbcMapperFactory(final Class<T> jdbcMapper) {
|
2017-07-01 02:07:04 -04:00
|
|
|
try {
|
|
|
|
this.constructor = getDefaultConstructor(jdbcMapper);
|
|
|
|
this.args = null;
|
|
|
|
} catch (Throwable e) {
|
|
|
|
throw new RuntimeException("could not create JdbcMapper, did the processor run at compile time?", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 03:05:53 -04:00
|
|
|
private JdbcMapperFactory(final Class<T> jdbcMapper, final Factory<Connection> connectionFactory) {
|
|
|
|
if (jdbcMapper == null)
|
|
|
|
throw new NullPointerException("jdbcMapper must be non-null");
|
2017-07-03 00:45:36 -04:00
|
|
|
if (connectionFactory == null)
|
2017-07-01 02:07:04 -04:00
|
|
|
throw new NullPointerException("connectionFactory must be non-null");
|
|
|
|
try {
|
2017-07-03 03:05:53 -04:00
|
|
|
this.constructor = getFactoryConstructor(jdbcMapper);
|
|
|
|
} catch (Throwable e) {
|
|
|
|
throw new RuntimeException("jdbcMapper must have a constructor that takes Factory<Connection>", e);
|
2017-07-01 02:07:04 -04:00
|
|
|
}
|
|
|
|
this.args = new Object[]{connectionFactory};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T create() throws SQLException {
|
|
|
|
try {
|
|
|
|
return this.constructor.newInstance(args);
|
|
|
|
} catch (InstantiationException e) {
|
|
|
|
throw new RuntimeException("could not create JdbcMapper, did the processor run at compile time?", e);
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
throw new RuntimeException("could not create JdbcMapper, did the processor run at compile time?", e);
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
throw new RuntimeException("could not create JdbcMapper, did the processor run at compile time?", e);
|
|
|
|
}
|
2017-05-27 23:54:22 -04:00
|
|
|
}
|
|
|
|
}
|