1 23 package org.objectweb.jorm.naming.lib; 24 25 import org.objectweb.jorm.naming.api.PBinder; 26 import org.objectweb.jorm.naming.api.PName; 27 import org.objectweb.jorm.naming.api.PExceptionNaming; 28 import org.objectweb.jorm.api.PBinding; 29 import org.objectweb.jorm.api.PException; 30 import org.objectweb.jorm.api.PExceptionProtocol; 31 import org.objectweb.jorm.api.PBindingCtrl; 32 import org.objectweb.jorm.api.PStateGraph; 33 import org.objectweb.jorm.api.PClassMapping; 34 import org.objectweb.perseus.cache.api.CacheEntry; 35 import org.objectweb.perseus.cache.api.CacheException; 36 import org.objectweb.perseus.cache.api.FixableCacheEntry; 37 import org.objectweb.perseus.cache.api.CacheManager; 38 39 44 public abstract class BasicPBinder 45 extends BasicPNamingContext 46 implements PBinder { 47 48 protected CacheManager cache; 49 private PClassMapping pcm; 50 protected PName nullPName; 51 52 public BasicPBinder() { 53 } 54 55 public BasicPBinder(PName nullPName) { 56 this.nullPName = nullPName; 57 } 58 59 public String getClassName() { 60 return ptype.getJormName(); 61 } 62 63 64 67 public PBinding lookup(PName pn) throws PException { 68 if (pn == null) { 69 throw new PExceptionNaming("[" + getClassName() + "]: this pname is null"); 70 } 71 if (pn.isNull()) { 72 return null; 73 } 74 if (cache == null) { 75 throw new PExceptionProtocol("No internal cache: should be managed externally!"); 76 } 77 CacheEntry ce = cache.lookup(pn); 78 if (ce != null) { 79 return (PBinding) ce.getCeObject(); 80 } 81 return null; 82 } 83 84 public void bind(PName pn, PBindingCtrl pb) throws PException { 85 if (pn == null) 86 throw new PExceptionNaming("[" + getClassName() + "]: this pname is null"); 87 if (pb == null) 88 throw new PExceptionNaming("[" + getClassName() + "]: this pbinding is null"); 89 if (!pn.getClass().equals(getNull().getClass())) 90 throw new PExceptionNaming("Can only bind " + getNull().getClass() + ", found: " + pn); 91 if (!pn.getPNameManager().equals(this)) 92 throw new PExceptionNaming("[" + getClassName() + "]: this pname is not valid in this binder (" 93 + ", \nthis:" + this 94 + ", \nthis.type:" + this.getPType().getJormName() 95 + ", \nthis.pcm:" + this.getBinderClassMapping() 96 + ", \npn.pnc: " + pn.getPNameManager() 97 + ", \npn.pnc.type: " + pn.getPNameManager().getPType().getJormName() 98 + ", \npn.pnc.pcm: " + ((PBinder) pn.getPNameManager()).getBinderClassMapping() 99 + ", \npn:" + pn + ")"); 100 if (!pb.getPClassMapping().equals(pcm)) 101 throw new PExceptionNaming("[" + getClassName() + "]: this pbinding is not valid in this binder"); 102 if (pn.isNull()) 103 throw new PExceptionNaming("[" + getClassName() + "]: this PName represent the null value"); 104 byte nextstate = PStateGraph.nextStatePBinding(pb.getStatus(), 105 PBinding.ACTION_BIND); 106 if (cache != null) { 108 synchronized (cache) { 109 try { 110 cache.fix(cache.bind(pn, pb)); 111 } catch (CacheException e) { 112 throw new PException(e, "[" + getClassName() + "]: problem with cache management"); 113 } 114 } 115 } 116 pb.setPName(pn); 117 pb.setStatus(nextstate); 118 } 119 120 public PClassMapping getBinderClassMapping() { 121 return pcm; 122 } 123 124 public void setPClassMapping(PClassMapping pcm) { 125 this.pcm = pcm; 126 } 127 128 public void unbind(PBindingCtrl pb) throws PException { 129 byte nextstate = PStateGraph.nextStatePBinding(pb.getStatus(), 130 PBinding.ACTION_UNBIND); 131 if (nextstate == pb.getStatus()) 133 return; 134 if (pb == null) 136 throw new PExceptionNaming("[" + getClassName() + "]: this pbinding is null"); 137 PName pn = pb.getPName(); 138 if (pn == null) 139 throw new PExceptionNaming("[" + getClassName() + "]: this PBinding is not bounded with a pname"); 140 if (!pb.getPClassMapping().equals(pcm)) 141 throw new PExceptionNaming("[" + getClassName() + "]: this pbinding is not valid in this binder"); 142 if (cache != null) { 143 synchronized (cache) { 144 try { 145 if (((FixableCacheEntry) cache).getCeFixCount() == 0) { 146 pb.setPName(null); 148 pb.setStatus(nextstate); 149 } else { 150 CacheEntry ce = cache.lookup(pn); 152 if (ce != null) { 153 cache.unfix(ce); 154 } 155 } 156 } catch (CacheException e) { 157 throw new PException(e, "[" + getClassName() + "]: problem with cache management"); 158 } 159 } 160 } else { 161 pb.setPName(null); 162 pb.setStatus(nextstate); 163 } 164 } 165 166 public CacheManager getCacheManager() { 167 return cache; 168 } 169 170 public void setCacheManager(CacheManager cm) throws PException { 171 cache = cm; 172 } 173 174 public PName getNull() { 175 return nullPName; 176 } 177 178 public void setNullPName(Object o) throws PException { 179 } 180 } 181 | Popular Tags |