1 27 package org.objectweb.speedo.pm.lib; 28 29 import org.objectweb.speedo.pm.api.ProxyManagerSwitch; 30 import org.objectweb.speedo.pm.api.ProxyManager; 31 import org.objectweb.speedo.pm.api.ProxyManagerFactory; 32 33 import java.util.ArrayList ; 34 import java.util.Collection ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Collections ; 38 39 47 public class SpeedoProxyManagerSwitch implements ProxyManagerSwitch { 48 49 protected ThreadLocal pms = new ThreadLocal (); 50 51 public final static String BIND_ERROR_MSG = "Impossible to associated to ProxyManager linked to the same PersistenceManagerFactory in the same context (thread)"; 52 53 56 63 public ProxyManager lookup(ProxyManagerFactory pmf) { 64 Object o = pms.get(); 65 if (o == null) { 66 return null; 67 } else if (o instanceof ProxyManager) { 68 if (((ProxyManager) o).getPersistenceManagerFactory() == pmf) 69 return (ProxyManager) o; 70 } else { 71 for (Iterator it = ((List ) o).iterator(); it.hasNext();) { 72 ProxyManager pm = (ProxyManager) it.next(); 73 if (pm.getPersistenceManagerFactory() == pmf) 74 return pm; 75 } 76 } 77 return null; 78 } 79 80 84 public void bind(ProxyManager pm) { 85 Object o = pms.get(); 86 if (o == null) { 87 pms.set(pm); 88 } else if (o instanceof ProxyManager) { 89 ProxyManager pm1 = (ProxyManager) o; 92 if (pm1.getPersistenceManagerFactory() 93 == pm.getPersistenceManagerFactory()) { 94 pms.set(pm); 95 } else { 96 ArrayList al = new ArrayList (3); 98 al.add(pm1); 99 al.add(pm); 100 pms.set(al); 101 } 102 } else { 103 List l = (List ) o; 106 for (Iterator it = l.iterator(); it.hasNext();) { 107 if (((ProxyManager) it.next()).getPersistenceManagerFactory() 108 == pm.getPersistenceManagerFactory()) { 109 it.remove(); 110 } 111 } 112 ((List ) o).add(pm); 114 } 115 } 116 117 120 public void clear() { 121 pms.set(null); 122 } 123 124 127 public boolean unbind(ProxyManager pm) { 128 Object o = pms.get(); 129 if (o == null) { 130 return false; 131 } else if (o instanceof ProxyManager) { 132 if (o == pm) { 133 pms.set(null); 134 return true; 135 } 136 } else if (o instanceof Collection ) { 137 return ((Collection ) o).remove(pm); 138 } 139 return false; 140 } 141 142 public boolean unbind(ProxyManagerFactory pmf) { 143 Object o = pms.get(); 144 if (o == null) { 145 return false; 146 } else if (o instanceof ProxyManager) { 147 if (((ProxyManager) o).getPersistenceManagerFactory() == pmf) { 148 pms.set(null); 149 return true; 150 } 151 } else { for (Iterator it = ((List ) o).iterator(); it.hasNext();) { 153 if (((ProxyManager) it.next()).getPersistenceManagerFactory() 154 == pmf) { 155 it.remove(); 156 return true; 157 } 158 } 159 } 160 return false; 161 } 162 163 166 public Collection entries() { 167 Object o = pms.get(); 168 if (o instanceof ProxyManager) 169 return Collections.singletonList(o); 170 else 171 return (Collection ) o; 172 } 173 } 174 | Popular Tags |