📄 jdbcprovider.java
字号:
/*
* Created on Jul 11, 2003
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package ar.com.koalas.providers;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.Closure;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.dbutils.handlers.MapListHandler;
/**
* @author DZ156H
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class JDBCProvider extends ProviderImpl {
private static final String PARAM_QUERY = "query";
private String jndi;
private String initialContext;
private String keyName;
private String description;
private Collection cachedCollection;
private boolean isCached = false;
private String beanFactoryName = Map2DynaBeanFactory.class.getName();
private Map localIndex = null;
public Collection getCollection() throws ProviderException {
try {
if( this.cachedCollection == null || !this.isCached ){
this.isCached = true;
this.cachedCollection = this.executeQuery();
}
return this.cachedCollection;
} catch (Exception e) {
e.printStackTrace();
throw new ProviderException( e );
}
}
public Object getDescription(Object id) throws ProviderException {
this.getCollection();
return this.localIndex.get(id.toString());
}
public String getJndi() {
return jndi;
}
public void setJndi(String string) {
jndi = string;
}
public String getDescription() {
return description;
}
public String getKeyName() {
return keyName;
}
public void setDescription(String string) {
description = string;
}
public void setKeyName(String string) {
keyName = string;
}
private Collection executeQuery() throws NamingException, SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, InstantiationException, ClassNotFoundException{
Context initialContext = new InitialContext();
Context envCtx = (Context) initialContext.lookup( this.initialContext );
DataSource ds = (DataSource)envCtx.lookup( this.jndi );
Connection con = ds.getConnection();
PreparedStatement ps = con.prepareStatement( this.getParam(PARAM_QUERY) );
ResultSet rs = ps.executeQuery();
try{
return this.rsToCollection( rs );
}catch( Throwable t ){
t.printStackTrace();
return null;
}finally{
rs.close();
ps.close();
con.close();
}
}
private Collection rsToCollection(ResultSet rs) throws SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, InstantiationException, ClassNotFoundException {
Collection retValue = (Collection) new MapListHandler().handle(rs);
retValue = CollectionUtils.collect( retValue, new Transformer(){
public Object transform(Object arg) {
return JDBCProvider.this.getBeanFactory().create(arg);
}} );
this.localIndex = new Hashtable();
CollectionUtils.forAllDo( retValue, new Closure(){
public void execute(Object arg) {
try {
JDBCProvider.this.localIndex.put(BeanUtils.getProperty(arg, JDBCProvider.this.getKeyName()), BeanUtils.getProperty(arg, JDBCProvider.this.getDescription()));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}} );
return retValue;
}
private BeanFactory getBeanFactory() {
if( this.beanFactoryName != null ){
try {
return (BeanFactory) Class.forName( this.getBeanFactoryName() ).newInstance();
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
} catch (ClassNotFoundException e) {
}
}
return null;
}
public String getBeanFactoryName() {
return beanFactoryName;
}
public void setBeanFactoryName(String string) {
beanFactoryName = string;
}
public String getInitialContext() {
return initialContext;
}
public void setInitialContext(String initialContext) {
this.initialContext = initialContext;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -