1 23 24 package org.objectweb.jorm.mapper.rdb.lib; 25 26 import java.sql.Connection ; 27 import java.sql.SQLException ; 28 29 import javax.sql.DataSource ; 30 31 import org.objectweb.jorm.api.JormConfigurator; 32 import org.objectweb.jorm.api.PException; 33 import org.objectweb.jorm.api.PExceptionProtocol; 34 import org.objectweb.jorm.lib.Mapper; 35 import org.objectweb.jorm.mapper.rdb.adapter.RdbAdapterFactory; 36 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter; 37 38 42 public class MapperJDBC extends Mapper implements PMapperRdb { 43 44 private DataSource source = null; 45 46 private RdbAdapter adapter; 47 48 public MapperJDBC() throws PException { 49 super(); 50 } 51 52 public MapperJDBC(JormConfigurator jc) { 53 super(jc); 54 } 55 56 61 public void closeConnection(Object conn) throws PException { 62 try { 63 ((Connection ) conn).close(); 64 } catch (SQLException se) { 65 throw new PException(se, "Problem while closing connection."); 66 } 67 } 68 69 72 public Object getConnection() throws PException { 73 if (source == null) { 74 throw new PException("Cannot get an SQL connection: no datasource specified."); 75 } 76 try { 77 return source.getConnection(); 78 } catch (SQLException se) { 79 throw new PException(se, "Problem while allocating connection."); 80 } 81 } 82 83 87 public Object getConnection(Object ctxt) throws PException { 88 if (!(ctxt instanceof ConnectionSpecJDBC)) 89 throw new PException("Requires a JDBC connection specification."); 90 ConnectionSpecJDBC cs = (ConnectionSpecJDBC) ctxt; 91 try { 92 if (cs.user != null && cs.passwd != null) { 93 return source.getConnection(cs.user, cs.passwd); 94 } 95 return source.getConnection(); 96 } catch (SQLException se) { 97 throw new PException(se, 98 "Problem while allocating connection with SPEC."); 99 } 100 } 101 102 public Object getConnection(Object connectionContext, Object user) throws PException { 103 return getConnection(connectionContext); 104 } 105 106 112 public void setConnectionFactory(Object cf) throws PException { 113 if (cf == null) 114 return; 115 if (cf instanceof DataSource ) { 116 source = (DataSource ) cf; 117 return; 118 } 119 throw new PException("Requires a JDBC connection specification."); 120 } 121 122 125 public Object getConnectionFactory() { 126 return source; 127 } 128 129 public void start() throws PException { 130 String mn = getMapperName(); 131 if (mn == null || "rdb.automatic".equalsIgnoreCase(mn)) { 133 try { 136 adapter = RdbAdapterFactory.getTypeConverter(source); 137 } catch (org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapterException e) { 138 throw new PException(e, "Impossible to find a RdbAdapter in automatic mode"); 139 } 140 setMapperName("rdb." + adapter.getName()); 142 } else { 143 int idx = mn.indexOf('.'); 144 if (idx == -1) { 145 mn = RdbAdapterFactory.DATABASE_NAME_JDBC; 146 } else { 147 mn = mn.substring(idx + 1); 148 } 149 try { 150 adapter = RdbAdapterFactory.getTypeConverter(mn); 151 } catch (org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapterException e) { 152 throw new PException(e, "" + 153 "Impossible to find a RdbAdapter for the specified mapper name: " 154 + getMapperName()); 155 } 156 } 157 adapter.setLogger(logger); 158 super.start(); 159 } 160 161 164 public RdbAdapter getRdbAdapter() { 165 return adapter; 166 } 167 } 168 | Popular Tags |