KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > mapper > jca > JCAMapper


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.mapper.jca;
19
20 import org.objectweb.jorm.api.PException;
21 import org.objectweb.jorm.api.JormConfigurator;
22 import org.objectweb.jorm.lib.MapperJCA;
23 import org.objectweb.util.monolog.api.LoggerFactory;
24 import org.objectweb.fractal.api.control.LifeCycleController;
25 import org.objectweb.speedo.api.ExceptionHelper;
26 import org.objectweb.speedo.api.SpeedoProperties;
27 import org.objectweb.speedo.mapper.api.MapperAttributes;
28 import org.objectweb.medor.eval.prefetch.lib.PrefetchCacheImpl;
29 import org.objectweb.perseus.persistence.api.ConnectionHolderFactory;
30 import org.objectweb.perseus.persistence.api.ConnectionHolder;
31 import org.objectweb.perseus.persistence.api.PersistenceException;
32 import org.objectweb.perseus.pool.api.Pool;
33
34 import javax.jdo.JDOUserException;
35
36 public class JCAMapper extends MapperJCA
37         implements MapperAttributes, LifeCycleController, ConnectionHolderFactory {
38
39     public final static String JavaDoc MONOLOG_FACTORY_BINDING = "monolog-factory";
40     public final static String JavaDoc POOL_BINDING = "pool";
41     public final static String JavaDoc LOGGER_NAME
42             = SpeedoProperties.LOGGER_NAME + ".rt.mapper.jca";
43     public final static String JavaDoc RESOUCE_ADAPTER_BINDINNG = "resouce-adapter";
44
45     private boolean started;
46
47     private boolean initialized = false;
48     
49     private boolean checkConnectivityAtStartup = true;
50
51     private Pool pool;
52
53     public JCAMapper() throws PException {
54     }
55
56     public boolean getCheckConnectivityAtStartup() {
57         return checkConnectivityAtStartup;
58     }
59     public void setCheckConnectivityAtStartup(boolean b) {
60         checkConnectivityAtStartup = b;
61     }
62     // IMPLEMENTATION OF THE UserBindingController INTERFACE //
63
//-------------------------------------------------------//
64

65     public Object JavaDoc getFcBindings(String JavaDoc s) {
66         if (MONOLOG_FACTORY_BINDING.equals(s))
67             return getLoggerFactory();
68         else if (RESOUCE_ADAPTER_BINDINNG.equals(s))
69             return getConnectionFactory();
70         else if (POOL_BINDING.equals(s))
71             return pool;
72         return null;
73     }
74
75     public void addFcBinding(String JavaDoc s, Object JavaDoc o) {
76         if (MONOLOG_FACTORY_BINDING.equals(s))
77             setLoggerFactory((LoggerFactory) o);
78         else if (POOL_BINDING.equals(s))
79             pool = (Pool) o;
80         else if (RESOUCE_ADAPTER_BINDINNG.equals(s))
81             try {
82                 setConnectionFactory(o);
83             } catch (PException e) {
84                 throw new JDOUserException(
85                         "Impossible to assign the connection factory to the mapper", e);
86             }
87     }
88
89     public void removeFcBinding(String JavaDoc s, Object JavaDoc o) {
90         if (MONOLOG_FACTORY_BINDING.equals(s))
91             setLoggerFactory(null);
92         else if (POOL_BINDING.equals(s))
93             pool = null;
94         else if (RESOUCE_ADAPTER_BINDINNG.equals(s))
95             try {
96                 setConnectionFactory(null);
97             } catch (PException e) {
98                 throw new JDOUserException(
99                         "Impossible to remove the connection factory to the mapper", e);
100             }
101     }
102
103
104     // IMPLEMENTATION OF THE Pool INTERFACE //
105
//--------------------------------------//
106
public ConnectionHolder createConnectionHolder() throws PersistenceException {
107         return null;
108     }
109
110     // IMPLEMENTATION OF THE LifeCycleController INTERFACE //
111
//-----------------------------------------------------//
112

113     public String JavaDoc getFcState() {
114         return started ? STARTED : STOPPED;
115     }
116
117     public void startFc() {
118         started = true;
119         if (!initialized) {
120             try {
121
122                 JormConfigurator jc = getJormConfigurator();
123                 jc.setLoggerFactory(getLoggerFactory());
124                 setJormConfigurator(jc);
125                 PrefetchCacheImpl pc = new PrefetchCacheImpl(
126                         getLoggerFactory().getLogger(
127                                 "org.objectweb.speedo.rt.query.prefetch"));
128                 setPrefetchCache(pc);
129                 start();
130                 initialized = true;
131             } catch (PException e) {
132                 throw new RuntimeException JavaDoc(
133                         "Impossible to configure the mapper: "
134                         + ExceptionHelper.getNested(e).getMessage());
135             }
136         }
137     }
138
139     public void stopFc() {
140         started = false;
141     }
142
143 }
144
Popular Tags