1 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 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 73 public PName export(Object c, Object en) throws PException { 74 if (en == null) { 75 throw new PExceptionNaming("[" + getClassName() + "]: cannot export null!"); 76 } 77 if (en instanceof PName) { 78 if (((PName) en).getPNameManager() == this) { 80 return (PName) en; 81 } else { 82 return new RdbSequencePName(this, en); 83 } 84 } 85 86 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 ) sequence.next()).longValue(); 102 pn = new RdbSequencePName(this, new Long (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 |