1 24 package org.objectweb.jalisto.se.jca; 25 26 import org.objectweb.jalisto.se.exception.JcaException; 27 import org.objectweb.jalisto.se.impl.trace.Trace; 28 import org.objectweb.jalisto.se.impl.trace.TraceNull; 29 import org.objectweb.jalisto.se.api.Session; 30 import org.objectweb.jalisto.se.jca.data.JalistoManagedConnectionMetaData; 31 import org.objectweb.jalisto.se.jca.session.ManagedSession; 32 33 import javax.resource.ResourceException ; 34 import javax.resource.spi.*; 35 import javax.security.auth.Subject ; 36 import javax.transaction.xa.XAException ; 37 import javax.transaction.xa.XAResource ; 38 import java.io.PrintWriter ; 39 import java.util.ArrayList ; 40 import java.util.List ; 41 42 public class JalistoManagedConnection implements ManagedConnection { 43 44 public JalistoManagedConnection(String connectionURL, ConnectionRequestInfo info, Trace trace) { 45 this.connectionURL = connectionURL; 46 this.trace = trace; 47 this.info = info; 48 metadata = new JalistoManagedConnectionMetaData(); 49 try { 50 getXAResource(); 51 } catch (ResourceException e) { 52 throw new JcaException("Can't initialize the JalistoManagedConnection", e); 53 } 54 } 55 56 public void setManagedJalistoSession(ManagedSession managedJalistoSession) { 57 this.managedJalistoSession = managedJalistoSession; 58 } 59 60 public boolean isAvalaible() { 61 return (isAvalaible && (managedJalistoSession == null) && jdoxarWrapper.isAvailable()); 62 } 63 64 public void setNotAvalaible() { 65 isAvalaible = false; 66 } 67 68 public void setCfIdentification(String id) { 69 cfIdentification = id; 70 } 71 72 public String getCfIdentification() { 73 return cfIdentification; 74 } 75 76 public String getConnectionURL() { 77 return connectionURL; 78 } 79 80 81 public void close(JalistoConnection c) throws XAException { 82 trace("close(c)"); 83 if (!c.equals(activeJalistoConnection)) { 84 throw new XAException ("Cannot close a not active connection..."); 85 } 86 for (int i = 0; i < eventListeners.size(); i++) { 87 ConnectionEventListener cel = (ConnectionEventListener) eventListeners.get(i); 88 ConnectionEvent ce = new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED); 89 ce.setConnectionHandle(c); 90 cel.connectionClosed(ce); 91 } 92 } 93 94 public Session getSession() { 95 return managedJalistoSession; 96 } 97 98 public boolean matchConnectionRequestInfo(ConnectionRequestInfo requestInfo) { 99 if (info == null) { 100 return (requestInfo == null); 101 } 102 return info.equals(requestInfo); 104 } 105 106 private void trace(Object o) { 107 String message = TRACE_ID + " " + String.valueOf(o); 108 trace.println(Trace.JCA, message); 109 } 110 111 112 public boolean equals(Object o) { 113 boolean result = false; 114 if (o instanceof JalistoManagedConnection) { 115 JalistoManagedConnection candidat = (JalistoManagedConnection) o; 116 result = (this.hashCode() == candidat.hashCode()); 117 } 118 return result; 119 } 120 121 public int hashCode() { 122 if (hashCode == 0) { 123 hashCode = super.hashCode(); 124 } 125 return hashCode; 126 } 127 128 129 130 131 public Object getConnection(Subject subject, ConnectionRequestInfo info) throws ResourceException { 133 trace("getConnection"); 134 if (activeJalistoConnection == null) { 135 activeJalistoConnection = new JalistoConnection(this, trace); 136 } else { 137 if (activeJalistoConnection.isClosed()) { 138 activeJalistoConnection.open(this); 139 } 140 } 142 return activeJalistoConnection; 143 } 144 145 public void associateConnection(Object c) throws ResourceException { 146 trace("associateConnection " + c.getClass().toString()); 147 148 if (c == null) { 149 throw new ResourceException ("Cannot associate a null connection"); 150 } 151 152 if (activeJalistoConnection == null) { 153 activeJalistoConnection = (JalistoConnection) c; 154 return; 155 } 156 157 if (activeJalistoConnection.equals(c)) { 158 return; 159 } 160 161 if (c instanceof JalistoConnection) { 162 JalistoConnection jdoJalistoConnection = (JalistoConnection) c; 163 jdoJalistoConnection.setMc(this); 164 activeJalistoConnection.invalidate(); 165 activeJalistoConnection = jdoJalistoConnection; 166 } else { 167 throw new ResourceException ("The managedConnection cannot manage this connection : not a JDOConnection"); 168 } 169 } 170 171 public XAResource getXAResource() throws ResourceException { 172 trace("getXAResource"); 173 if (jdoxarWrapper == null) { 174 trace("getXAResource : new ressource"); 175 jdoxarWrapper = new JalistoXAResourceWrapper(this, trace); 176 } 177 return jdoxarWrapper; 178 } 179 180 public void addConnectionEventListener(ConnectionEventListener listener) { 181 trace("addConnectionEventListener"); 182 eventListeners.add(listener); 183 } 184 185 public void removeConnectionEventListener(ConnectionEventListener listener) { 186 trace("removeConnectionEventListener"); 187 eventListeners.remove(listener); 188 } 189 190 public void cleanup() throws ResourceException { 191 trace("cleanup"); 192 if (activeJalistoConnection != null) { 193 activeJalistoConnection.invalidate(); 194 activeJalistoConnection = null; 195 } 196 jdoxarWrapper.cleanUp(); 197 managedJalistoSession = null; 198 isAvalaible = true; 199 } 200 201 public void destroy() throws ResourceException { 202 trace("destroy"); 203 jdoxarWrapper = null; 204 isAvalaible = false; 205 eventListeners.clear(); 206 logger = null; 207 } 208 209 public LocalTransaction getLocalTransaction() throws ResourceException { 211 throw new RuntimeException ("not implemented yet..."); 212 } 213 214 public ManagedConnectionMetaData getMetaData() throws ResourceException { 215 trace("getMetaData : " + metadata.toString()); 216 return metadata; 217 } 218 219 public void setLogWriter(PrintWriter writer) throws ResourceException { 220 trace("setLogWriter"); 221 logger = writer; 222 } 223 224 public PrintWriter getLogWriter() throws ResourceException { 225 trace("getLogWriter"); 226 return logger; 227 } 228 229 230 private ManagedSession managedJalistoSession; 231 private JalistoConnection activeJalistoConnection = null; 232 private JalistoXAResourceWrapper jdoxarWrapper; 233 234 private String connectionURL; 235 private String cfIdentification; 236 private ConnectionRequestInfo info; 237 private JalistoManagedConnectionMetaData metadata; 238 239 private List eventListeners = new ArrayList (); 240 241 private PrintWriter logger; 242 private Trace trace = new TraceNull(); 243 244 245 private boolean isAvalaible = true; 246 private int hashCode = 0; 247 248 private static final String TRACE_ID = "[JalistoManagedConnection]"; 249 } 250 | Popular Tags |