KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > sequence > lib > SpeedoSequenceManager


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
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  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  */

25
26 package org.objectweb.speedo.sequence.lib;
27
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.jdo.datastore.Sequence;
32
33 import org.objectweb.fractal.api.control.BindingController;
34 import org.objectweb.jorm.api.PClassMapping;
35 import org.objectweb.jorm.api.PException;
36 import org.objectweb.jorm.api.PMapper;
37 import org.objectweb.jorm.facility.naming.generator.LongGenMgr;
38 import org.objectweb.jorm.facility.naming.generator.LongGenMgrRegistry;
39 import org.objectweb.jorm.facility.naming.rdbsequence.RdbSequenceLongGen;
40 import org.objectweb.jorm.mapper.rdb.lib.PMapperRdb;
41 import org.objectweb.speedo.sequence.lib.SpeedoSequence;
42 import org.objectweb.speedo.api.ExceptionHelper;
43 import org.objectweb.speedo.pm.api.ProxyManagerFactory;
44 import org.objectweb.speedo.sequence.api.SequenceManager;
45 import org.objectweb.util.monolog.api.BasicLevel;
46 import org.objectweb.util.monolog.api.Logger;
47
48 /**
49  * The SpeedoSequenceManager
50  * @author Y.Bersihand
51  */

52 public class SpeedoSequenceManager implements SequenceManager, BindingController {
53
54     public final static String JavaDoc PMF_BINDING = "proxy-manager-factory";
55     public final static String JavaDoc MAPPER_BINDING = "mapper";
56     public final static String JavaDoc MAPPER_RDB_BINDING = "mapper-rdb";
57     
58     
59     /**
60      * JORM object encapsulating the datastore
61      */

62     private PMapper mapper = null;
63     
64     /**
65      * JORM object encapsulating the datastore but in rdb version (optional)
66      */

67     private PMapperRdb mapperRdb = null;
68     
69     /**
70      * The PMF
71      */

72     private ProxyManagerFactory pmf = null;
73     
74     /**
75      * Manages the log's traces
76      */

77     private Logger logger = null;
78     
79     
80     public Logger getLogger() {
81         return logger;
82     }
83     public void setLogger(Logger logger) {
84         this.logger = logger;
85     }
86     
87     public PMapper getMapper() {
88         return mapper;
89     }
90     public void setMapper(PMapper mapper) {
91         this.mapper = mapper;
92     }
93     
94     public PMapperRdb getMapperRdb() {
95         return mapperRdb;
96     }
97     public void setMapperRdb(PMapperRdb mapperRdb) {
98         this.mapperRdb = mapperRdb;
99     }
100      // IMPLEMENTATION OF THE UserBindingController INTERFACE //
101
//-------------------------------------------------------//
102

103     public String JavaDoc[] listFc() {
104         return new String JavaDoc[] {
105             PMF_BINDING,
106             MAPPER_BINDING,
107             MAPPER_RDB_BINDING
108         };
109     }
110
111     public Object JavaDoc lookupFc(String JavaDoc s) {
112         if (PMF_BINDING.equals(s)) {
113             return pmf;
114         } else if(MAPPER_BINDING.equals(s)) {
115             return mapper;
116         } else if (MAPPER_RDB_BINDING.equals(s)) {
117             return mapperRdb;
118         } else
119             return null;
120     }
121
122     public void bindFc(String JavaDoc s, Object JavaDoc o) {
123         if ("logger".equals(s)) {
124             logger = (Logger) o;
125         } else if (PMF_BINDING.equals(s)) {
126             pmf = (ProxyManagerFactory) o;
127         } else if (MAPPER_BINDING.equals(s)) {
128             mapper = (PMapper) o;
129         } else if (MAPPER_RDB_BINDING.equals(s)) {
130             mapperRdb = (PMapperRdb) o;
131         }
132     }
133
134     public void unbindFc(String JavaDoc s) {
135         if (PMF_BINDING.equals(s)) {
136             pmf = null;
137         } else if (MAPPER_BINDING.equals(s)) {
138             mapper = null;
139         } else if (MAPPER_RDB_BINDING.equals(s)) {
140             mapperRdb = null;
141         }
142     }
143     
144     // IMPLEMENTATION OF THE SequenceManager INTERFACE //
145
//--------------------------------------------------//
146

147     /**
148      * Map of sequences defined for the pmf.
149      * The Map key is the fully qualified sequence name,
150      * i.e "packageName.sequenceName".
151      */

152     private Map JavaDoc sequenceName2sequences = new HashMap JavaDoc();
153     
154     /**
155      * The long generator manager
156      */

157     private Object JavaDoc longGenMgr = null;
158     
159     /**
160      * Get the long generator manager.
161      */

162     public Object JavaDoc getLongGenMgr() {
163         return longGenMgr;
164     }
165     
166     /**
167      * Set the long generator manager.
168      */

169     public void setLongGenMgr(Object JavaDoc longGenMgr) {
170         if (longGenMgr instanceof LongGenMgr) {
171             this.longGenMgr = longGenMgr;
172         }
173     }
174     
175     /**
176      * Add the sequence if not already registered in the list.
177      * The key is the fully qualified name of the sequence,
178      * i.e "packageName.sequenceName".
179      * @param sequence
180      */

181     public void addSequence(Object JavaDoc sequence) {
182         if (sequence instanceof SpeedoSequence) {
183             SpeedoSequence s = (SpeedoSequence) sequence;
184             String JavaDoc fqName = (s.packageName == null )?(""):(s.packageName + ".") + s.name;
185             //if the sequence is not already registered
186
if (!sequenceName2sequences.containsKey(fqName)) {
187                 sequenceName2sequences.put(fqName, s);
188                 s.setSequenceManager(this);
189             }
190         }
191     }
192     
193     /**
194      * Try to remove the sequence identified by the sequenceName.
195      * If the sequence does not exist, nothing is done.
196      * @param sequenceName the fully qualified name of the sequence,
197      * i.e "packageName.sequenceName".
198      */

199     public void removeSequence(String JavaDoc sequenceName) {
200         sequenceName2sequences.remove(sequenceName);
201     }
202     
203     /**
204      * Return the sequence associated to the name parameter.
205      * If no sequence is found, return null.
206      * @param name the fully qualified name of the sequence,
207      * i.e "packageName.sequenceName".
208      */

209     public Sequence getSequence(String JavaDoc name) {
210         SpeedoSequence s = (SpeedoSequence) sequenceName2sequences.get(name);
211         if (s != null) {
212             try {
213                 initSequence(s);
214             } catch (Exception JavaDoc e) {
215                 logger.log(BasicLevel.ERROR,
216                         "Impossible to get the sequence "
217                         + name, ExceptionHelper.getNested(e));
218                 return null;
219             }
220         }
221         return s;
222     }
223     
224     public ProxyManagerFactory getPMF() {
225         return pmf;
226     }
227     
228     public void setPMF(ProxyManagerFactory pmf) {
229         this.pmf = pmf;
230     }
231     
232     /**
233      * Assuming this sequence is registered,
234      * init the sequence:
235      * - on the datastore if the sequence has a datastore name
236      * - in memory if the sequence has no datastore name
237      * If the sequence does not exist, do nothing.
238      * @throws PException
239      */

240     private void initSequence(Object JavaDoc sequence) throws PException {
241         if (sequence instanceof SpeedoSequence) {
242             try {
243                 SpeedoSequence s = (SpeedoSequence) sequence;
244                 //if the sequence is not a rdb sequence, use jf_longGen table
245
if (s.datastoreName == null || s.datastoreName.equals("")) {
246                     if (longGenMgr == null) {
247                         //try to find the long gen manager in the registry
248
synchronized (LongGenMgrRegistry.class) {
249                             longGenMgr = LongGenMgrRegistry.getLongGenMgr(mapper);
250                             if (longGenMgr == null) {
251                                 //if not already registered, create it
252
try {
253                                     longGenMgr = (LongGenMgr) Class.forName(getLongGenMgrName()).newInstance();
254                                 } catch (Exception JavaDoc e) {
255                                     throw new PException(e, "Cannot create LongGenMgr (probably a ClassLoader problem): " + getLongGenMgrName());
256                                 }
257                                 ((LongGenMgr) longGenMgr).init(mapper, PClassMapping.CREATE_STRUCTURE_IF_NEEDED);
258                                 // and register it
259
LongGenMgrRegistry.registerLonGenMgr((LongGenMgr)longGenMgr);
260                             }
261                         }
262                     }
263                     //create the long gen and associate it to the sequence
264
s.setLongGen(((LongGenMgr) longGenMgr).getLongGen(s.getName()));
265                 } else {
266                     //else, use rdb_sequence_long_gen
267
if (mapperRdb != null) {
268                         if (s.getLongGen() == null) {
269                             RdbSequenceLongGen rdbLongGen = new RdbSequenceLongGen(mapperRdb, s.datastoreName, false);
270                             if(s.start != null) {
271                                 rdbLongGen.setSequenceStart(s.start);
272                             }
273                             if(s.increment != null) {
274                                 rdbLongGen.setSequenceIncrement(s.increment);
275                             }
276                             if(s.cache != null) {
277                                 rdbLongGen.setSequenceStart(s.cache);
278                             }
279                             //create it
280
Object JavaDoc conn = mapperRdb.getConnection();
281                             rdbLongGen.createSequence(conn);
282                             //and associate it to the sequence
283
s.setLongGen(rdbLongGen);
284                             //close the connection
285
mapperRdb.closeConnection(conn);
286                         }
287                     } else {
288                         throw new PException("Problem while init the sequence "
289                                 + ((SpeedoSequence)sequence).getName() + ": "
290                                 + "a rdb sequence has been defined in a jdo file and no rdb is linked to the application.");
291                     }
292                 }
293             } catch (Exception JavaDoc e) {
294                 throw new PException(e, "Problem while init the sequence " + ((SpeedoSequence)sequence).getName());
295             }
296         }
297     }
298     
299     /**
300      * Calculates the class name of the LongGenMgr from the mapper name. Indeed
301      * the class is the generated PClassMapping of the persistent class
302      * org.objectweb.jorm.facility.naming.generator.LongGenIncr.
303      */

304     private String JavaDoc getLongGenMgrName() {
305         String JavaDoc mn = mapper.getMapperName();
306         int idx = mn.indexOf('.');
307         if (idx != -1) {
308             mn = mn.substring(0, idx);
309         }
310         return "org.objectweb.jorm.facility.naming.generator."
311             + mn + ".LongGenIncrMapping";
312     }
313 }
314
Popular Tags