1 24 package org.objectweb.jalisto.se.jca; 25 26 import org.objectweb.jalisto.se.api.JalistoProperties; 27 import org.objectweb.jalisto.se.JalistoFactory; 28 import org.objectweb.jalisto.se.impl.trace.Trace; 29 import org.objectweb.jalisto.se.impl.trace.TraceNull; 30 import org.objectweb.jalisto.se.jca.session.*; 31 32 import javax.transaction.xa.Xid ; 33 import java.util.*; 34 35 public class JalistoXARPool { 36 37 public JalistoXARPool() { 38 } 39 40 public static synchronized JalistoXAResource getBoundXAResource(Xid xid, JalistoManagedConnection mc) { 41 String connectionId = mc.getCfIdentification(); 42 trace.println(Trace.JCAPOOL, TRACE_ID + " getBoundXAResource with {0}", connectionId); 43 ConnectionFactoryRecord record = getRecord(connectionId); 44 Map cfMap = record.getAssociatedXAResources(); 45 JalistoXAResource result = (JalistoXAResource) cfMap.get(xid); 46 trace.println(Trace.JCAPOOL, TRACE_ID + " return result : {0}", result); 47 return result; 48 } 49 50 public static void setJdoConnectionFactory(String key, JalistoConnectionFactory jalistoConnectionFactory) { 51 ConnectionFactoryRecord record = new ConnectionFactoryRecord(jalistoConnectionFactory); 52 identifier2connectionFactoryRecordMap.put(key, record); 53 } 54 55 public static synchronized JalistoXAResource getOrBindJalistoXAResource(Xid xid, JalistoManagedConnection mc) { 56 String mcId = mc.getCfIdentification(); 57 JalistoXAResource xaResource; 58 59 trace.println(Trace.JCAPOOL, TRACE_ID + " getOrBindJalistoXAResource"); 60 trace.println(Trace.JCAPOOL, 61 TRACE_ID + " available XA resources in pool: {0}", 62 new Integer (freeXars.size())); 63 trace.println(Trace.JCAPOOL, 64 TRACE_ID + " connection factory identification : {0}", 65 mcId); 66 trace.println(Trace.JCAPOOL, TRACE_ID + " XID : {0}", xid); 67 68 ConnectionFactoryRecord record = getRecord(mcId); 69 Map xid2resourceMap = record.associatedXAResources; 70 xaResource = (JalistoXAResource) xid2resourceMap.get(xid); 71 if (xaResource != null) { 72 trace.println(Trace.JCAPOOL, TRACE_ID + " found a XAR for XID : {0}", xid); 73 } else { 74 if (freeXars.isEmpty()) { 75 xaResource = new JalistoXAResource(getOrBindManagedJalistoSession(mcId, mc.getConnectionURL()), trace); 76 } else { 77 xaResource = (JalistoXAResource) freeXars.getFirst(); 78 xaResource.setManagedJalistoSession(getOrBindManagedJalistoSession(mcId, mc.getConnectionURL())); 79 freeXars.removeFirst(); 80 } 81 trace.println(Trace.JCAPOOL, TRACE_ID + " bound new XAR to XID : {0}", xid); 82 xid2resourceMap.put(xid, xaResource); 83 xaResource.setCurrentXid(xid); 84 trace.println(Trace.JCAPOOL, TRACE_ID + " bound xar : {0}", identifier2connectionFactoryRecordMap); 85 } 86 return xaResource; 87 } 88 89 public static void setTrace(Trace t) { 90 trace = t; 91 trace.println(Trace.JCAPOOL, TRACE_ID + " setTrace"); 92 } 93 94 public static synchronized void releaseXAResource(JalistoXAResource jdoxar) { 95 trace.println(Trace.JCAPOOL, TRACE_ID + " releaseXAResource"); 96 try { 97 Iterator keys = identifier2connectionFactoryRecordMap.values().iterator(); 98 while (keys.hasNext()) { 99 ConnectionFactoryRecord record = (ConnectionFactoryRecord) keys.next(); 100 Map cfMap = record.getAssociatedXAResources(); 101 Object xidKey = jdoxar.getCurrentXid(); 102 if (cfMap.containsKey(xidKey)) { 103 JalistoXAResource xar = (JalistoXAResource) cfMap.get(xidKey); 104 if (jdoxar.equals(xar)) { trace.println(Trace.JCAPOOL, TRACE_ID + " jalistoxar in Map"); 106 ManagedSession mpm = xar.getManagedJalistoSession(); 107 if (mpm != null) { 108 record.getAssociatedManagedSession().add(mpm); 109 } 110 cfMap.remove(xidKey); 111 } 112 } 113 } 114 } catch (Exception e) { 115 e.printStackTrace(); 116 } 117 if (!freeXars.contains(jdoxar)) { 118 freeXars.addLast(jdoxar); 119 } 120 } 121 122 private static final ConnectionFactoryRecord getRecord(String connectionId) { 123 return (ConnectionFactoryRecord) identifier2connectionFactoryRecordMap.get(connectionId); 124 } 125 126 private static synchronized ManagedSession getOrBindManagedJalistoSession(String connectionId, String connectionURL) { 127 trace.println(Trace.JCAPOOL, TRACE_ID + " getOrBindManagedJalistoSession({0})", connectionId); 128 ConnectionFactoryRecord record = getRecord(connectionId); 129 List mpmList = record.getAssociatedManagedSession(); 130 ManagedSession result; 131 if (!mpmList.isEmpty()) { 132 result = (ManagedSession) mpmList.remove(0); 133 } else { 134 JalistoConnectionFactory jalistoCF = record.getConnectionFactory(); 135 if (jalistoCF == null) { 136 throw new RuntimeException (TRACE_ID + " jalistoCF null in JalistoXARPool during binding..."); 137 } 138 trace.println(Trace.JCAPOOL, TRACE_ID + " getOrBindManagedJalistoSession : create new ManagedJalistoSession"); 139 JalistoProperties prop = JalistoFactory.getInternalFactory().getProperties(connectionURL); 140 ManagedSession mpm; 141 if (prop.isMonoImplementation()) { 142 mpm = new ManagedSessionMonoImpl(prop); 143 } else if (prop.isReadOnlyImplementation()) { 144 mpm = new ManagedSessionReadOnlyImpl(prop); 145 } else { 146 mpm = new ManagedSessionMultiImpl(prop); 147 } 148 result = mpm; 149 } 150 return result; 151 } 152 153 private static class ConnectionFactoryRecord { 154 private JalistoConnectionFactory connectionFactory; 155 private List associatedManagedSession; 156 private Map associatedXAResources; 157 158 public ConnectionFactoryRecord(JalistoConnectionFactory connectionFactory) { 159 this.connectionFactory = connectionFactory; 160 this.associatedManagedSession = new ArrayList(); 161 this.associatedXAResources = new HashMap(); 162 } 163 164 public final List getAssociatedManagedSession() { 165 return associatedManagedSession; 166 } 167 168 public final Map getAssociatedXAResources() { 169 return associatedXAResources; 170 } 171 172 public final JalistoConnectionFactory getConnectionFactory() { 173 return connectionFactory; 174 } 175 } 176 177 178 private static final Map identifier2connectionFactoryRecordMap = new HashMap(); 179 private static LinkedList freeXars = new LinkedList(); 180 private static Trace trace = new TraceNull(); 181 182 private static final String TRACE_ID = "[JalistoXARPool]"; 183 } 184 | Popular Tags |