1 23 24 package org.objectweb.jorm; 25 26 import org.objectweb.jorm.api.PException; 27 import org.objectweb.jorm.api.PMapper; 28 import org.objectweb.jorm.mapper.rdb.lib.ConnectionSpecJDBC; 29 import org.objectweb.jorm.mapper.rdb.lib.MapperJDBC; 30 import org.objectweb.jorm.api.JormConfigurator; 31 import org.objectweb.jorm.util.api.Loggable; 32 import org.objectweb.jorm.lib.JormConfiguratorImpl; 33 import org.objectweb.util.monolog.wrapper.p6spy.P6SpyLogger; 34 import org.objectweb.util.monolog.api.BasicLevel; 35 36 import java.util.Properties ; 37 38 41 public class JdbcMapperFactory implements org.objectweb.jorm.JormTestMapperFactory { 42 public final static String URL = "jdbc.url"; 43 public final static String USER = "jdbc.user"; 44 public final static String PASSWD = "jdbc.passwd"; 45 public final static String DRIVER = "jdbc.driver"; 46 public final static String RDB_PRODUCT = "jdbc.product"; 47 public final static String DEFAULT_MAPPER_NAME = "rdb"; 48 49 public final static String SPY_LOGGER_NAME = "org.objectweb.jorm.rdb.sql"; 50 51 58 public PMapper createMapper(Properties prop) throws PException { 59 String url, user, passwd, driver, product, jormconf; 60 if ((url = prop.getProperty(URL)) == null) { 61 throw new PException("TEST: url mandatory for configuring database."); 62 } 63 user = prop.getProperty(USER, System.getProperty(USER, null)); 64 passwd = prop.getProperty(PASSWD, System.getProperty(PASSWD, null)); 65 if ((driver = prop.getProperty(DRIVER)) == null) { 66 throw new PException("TEST: driver mandatory for configuring database."); 67 } 68 product = prop.getProperty(RDB_PRODUCT, null); 69 if ((jormconf = prop.getProperty(JormTestMapperFactory.JORM_CONFIG_FILE)) == null) { 70 throw new PException("TEST: JORM config file mandatory for configuring tests."); 71 } 72 ConnectionSpecJDBC cs = new ConnectionSpecJDBC(url, driver, user, passwd); 73 JormConfigurator jcc = new JormConfiguratorImpl(); 74 jcc.configure(jormconf); 75 jcc.configureLog(jormconf); 76 PMapper res = new MapperJDBC(jcc); 77 P6SpyLogger.logger = jcc.getLoggerFactory().getLogger(SPY_LOGGER_NAME); 78 P6SpyLogger.level = BasicLevel.DEBUG; 79 res.setMapperName((product == null) ? DEFAULT_MAPPER_NAME : DEFAULT_MAPPER_NAME + "." + product); 80 ((Loggable) res).setLoggerFactory(jcc.getLoggerFactory()); 81 res.setConnectionFactory(cs); 82 res.start(); 83 return res; 84 } 85 } 86 | Popular Tags |