KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jdo.datastore.Sequence;
29
30 import org.objectweb.jorm.api.PBinding;
31 import org.objectweb.jorm.api.PBindingCtrl;
32 import org.objectweb.jorm.api.PException;
33 import org.objectweb.jorm.api.PExceptionProtocol;
34 import org.objectweb.jorm.api.PStateGraph;
35 import org.objectweb.jorm.facility.naming.rdbsequence.RdbSequenceBinder;
36 import org.objectweb.jorm.facility.naming.rdbsequence.RdbSequenceHelper;
37 import org.objectweb.jorm.facility.naming.rdbsequence.RdbSequencePName;
38 import org.objectweb.jorm.naming.api.PExceptionExistingName;
39 import org.objectweb.jorm.naming.api.PExceptionNaming;
40 import org.objectweb.jorm.naming.api.PName;
41 import org.objectweb.perseus.cache.api.CacheException;
42 import org.objectweb.perseus.persistence.api.ConnectionHolder;
43 import org.objectweb.perseus.persistence.api.PersistenceException;
44
45 /**
46  * Redefine the export method of the RdbSequenceBinder
47  * to use the speedo sequence instead of the jorm RdbSequenceHelper.
48  * @author Y.Bersihand
49  */

50 public class SpeedoSequenceBinder extends RdbSequenceBinder {
51
52     private Sequence sequence;
53
54     public SpeedoSequenceBinder() {
55         super();
56         sequence = null;
57     }
58
59     public Sequence getSpeedoSequence() {
60         return sequence;
61     }
62     
63     public void setSequence(Sequence sequence) {
64         this.sequence = sequence;
65         if (((SpeedoSequence) sequence).getLongGen() instanceof RdbSequenceHelper) {
66             setSequenceHelper((RdbSequenceHelper) ((SpeedoSequence) sequence).getLongGen());
67             initSequenceHelper();
68         }
69     }
70     
71     // redefine export method to use the speedo sequence
72

73     public PName export(Object JavaDoc c, Object JavaDoc en) throws PException {
74         if (en == null) {
75             throw new PExceptionNaming("[" + getClassName() + "]: cannot export null!");
76         }
77         if (en instanceof PName) {
78             //Naming context role
79
if (((PName) en).getPNameManager() == this) {
80                 return (PName) en;
81             } else {
82                 return new RdbSequencePName(this, en);
83             }
84         }
85
86         //Binder role
87
PBindingCtrl pb = (PBindingCtrl) en;
88         byte nextstate = PStateGraph.nextStatePBinding(pb.getStatus(),
89                                                        PBinding.ACTION_EXPORT);
90         if (nextstate == PBinding.LIFECYCLE_ERROR)
91             throw new PExceptionProtocol("Unauthorized operation");
92
93         PName pn = null;
94         boolean connectionAllocatedLocaly = false;
95         try {
96             if (c == null) {
97                 c = getBinderClassMapping().getPMapper().getConnection();
98                 connectionAllocatedLocaly = true;
99             }
100             initSequenceHelper();
101             long lid = ((Long JavaDoc) sequence.next()).longValue();
102             //TODO : if factory, lid = factory.next
103
pn = new RdbSequencePName(this, new Long JavaDoc(lid));
104         } finally {
105             if (connectionAllocatedLocaly) {
106                 getBinderClassMapping().getPMapper().closeConnection(c);
107             } else if (c instanceof ConnectionHolder) {
108                 try {
109                     ((ConnectionHolder) c).releaseCHConnection();
110                 } catch (PersistenceException e) {
111                     throw new PException(e, "Problem with Perseus connection releasing.");
112                 }
113             }
114         }
115
116         if (cache != null) {
117             synchronized (cache) {
118                 if (cache.lookup(pn) != null) {
119                     throw new PExceptionExistingName("[" + getClassName()
120                                                      + "]: an object has been already export with the same identifier");
121                 }
122                 try {
123                     cache.fix(cache.bind(pn, pb));
124                 } catch (CacheException e) {
125                     throw new PException(e, "[" + getClassName()
126                                             + "]: problem with cache management");
127                 }
128             }
129         }
130         pb.setPName(pn);
131         pb.setStatus(nextstate);
132         return pn;
133     }
134 }
135
Popular Tags