KickJava   Java API By Example, From Geeks To Geeks.

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


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.lib.MapperJCA;
29 import org.objectweb.jorm.lib.JormConfiguratorImpl;
30 import org.objectweb.jorm.api.JormConfigurator;
31 import org.objectweb.jorm.util.api.Loggable;
32
33 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
34 import javax.resource.ResourceException JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 /**
38  * Allocates a new JCA mapper and configures it.
39  */

40 public class JcaMapperFactory implements JormTestMapperFactory {
41     public static final String JavaDoc RESOURCE_MCF_CLASSNAME = "mapper.jca.mcf.classname";
42     public static final String JavaDoc ADAPTER_PROPERTIES_SETTER = "mapper.jca.mcf.propsetter";
43     public static final String JavaDoc DEFAULT_MAPPER_NAME = "fos";
44
45     /**
46      * Creates a new mapper for executing a mapping. The specifications of this
47      * mapper are passed through the properies parameter.
48      * @param prop The properties specifying the mapper to create.
49      * @return The created mapper.
50      * @throws PException
51      */

52     public PMapper createMapper(Properties JavaDoc prop) throws PException {
53         String JavaDoc mcfcn, ascn, jormconf, submapper = null;
54         if ((mcfcn = prop.getProperty(RESOURCE_MCF_CLASSNAME)) == null) {
55             throw new PException("TEST: ManagedConnectionFactory class name mandatory for configuring a JCA adapter.");
56         }
57         if ((ascn = prop.getProperty(ADAPTER_PROPERTIES_SETTER)) == null) {
58             throw new PException("TEST: JcaAdapterPropertiesSetter class name mandatory for configuring a JCA adapter.");
59         }
60         if ((jormconf = prop.getProperty(JormTestMapperFactory.JORM_CONFIG_FILE)) == null) {
61             throw new PException("TEST: JORM config file mandatory for configuring tests.");
62         }
63         JormConfigurator jc = new JormConfiguratorImpl();
64         jc.configure(jormconf);
65         jc.configureLog(jormconf);
66         PMapper res = new MapperJCA(jc);
67         res.setMapperName((submapper == null) ? DEFAULT_MAPPER_NAME : DEFAULT_MAPPER_NAME + "." + submapper);
68         ((Loggable) res).setLoggerFactory(jc.getLoggerFactory());
69         ManagedConnectionFactory JavaDoc cs = null;
70         try {
71             cs = (ManagedConnectionFactory JavaDoc) Class.forName(mcfcn).newInstance();
72             JcaAdapterPropertiesSetter as = (JcaAdapterPropertiesSetter) Class.forName(ascn).newInstance();
73             as.assignProperties(cs, prop, jc.getLoggerFactory());
74             res.setConnectionFactory(cs.createConnectionFactory());
75         } catch (Exception JavaDoc e) {
76             e.printStackTrace();
77             if (e instanceof ResourceException JavaDoc) {
78                 ((ResourceException JavaDoc) e).getLinkedException().printStackTrace();
79             }
80         }
81         res.start();
82         return res;
83     }
84 }
85
Popular Tags