KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > JdbcMapperFactory


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

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 JavaDoc;
37
38 /**
39  * Allocates a new JDBC mapper and configures it.
40  */

41 public class JdbcMapperFactory implements org.objectweb.jorm.JormTestMapperFactory {
42     public final static String JavaDoc URL = "jdbc.url";
43     public final static String JavaDoc USER = "jdbc.user";
44     public final static String JavaDoc PASSWD = "jdbc.passwd";
45     public final static String JavaDoc DRIVER = "jdbc.driver";
46     public final static String JavaDoc RDB_PRODUCT = "jdbc.product";
47     public final static String JavaDoc DEFAULT_MAPPER_NAME = "rdb";
48
49     public final static String JavaDoc SPY_LOGGER_NAME = "org.objectweb.jorm.rdb.sql";
50
51     /**
52      * Creates a new mapper for executing a mapping. The specifications of this
53      * mapper are passed through the properies parameter.
54      * @param prop The properties specifying the mapper to create.
55      * @return The created mapper.
56      * @throws org.objectweb.jorm.api.PException
57      */

58     public PMapper createMapper(Properties JavaDoc prop) throws PException {
59         String JavaDoc 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