1 24 package org.objectweb.jalisto.se.jca; 25 26 import org.objectweb.jalisto.se.api.*; 27 import org.objectweb.jalisto.se.api.query.QueryManager; 28 import org.objectweb.jalisto.se.api.internal.SessionInternal; 29 import org.objectweb.jalisto.se.exception.JcaException; 30 import org.objectweb.jalisto.se.impl.trace.Trace; 31 import org.objectweb.jalisto.se.impl.util.JalistoUtils; 32 33 import javax.resource.ResourceException ; 34 import javax.resource.cci.*; 35 import javax.resource.spi.ManagedConnection ; 36 import javax.transaction.xa.XAException ; 37 import java.util.Collection ; 38 39 public class JalistoConnection implements javax.resource.cci.Connection , Session { 40 41 public JalistoConnection(JalistoManagedConnection jalistoMc, Trace trace) { 42 this.trace = trace; 43 this.jalistoMc = jalistoMc; 44 } 45 46 public void setMc(ManagedConnection mc) { 47 if (mc instanceof JalistoManagedConnection) { 48 this.jalistoMc = (JalistoManagedConnection) mc; 49 } else { 50 throw new JcaException("Cannot manage connection of this class : " + mc.getClass().toString()); 51 } 52 } 53 54 public void open(JalistoManagedConnection jalistoMc) throws ResourceException { 55 this.jalistoMc = jalistoMc; 56 isValide = true; 57 isClosed = false; 58 } 59 60 public void invalidate() throws ResourceException { 61 jalistoMc = null; 62 isValide = false; 63 } 64 65 public boolean isClosed() { 66 return isClosed; 67 } 68 69 private final Session getSession() { 70 try { 71 checkClosedOrNotValid(); 72 } catch (ResourceException e) { 73 new JcaException(CLOSED_EXCEPTION_MSG); 74 } 75 Session result = jalistoMc.getSession(); 76 if (result == null) { 77 throw new JcaException("PM null in connection... You may be outside of a transaction"); 78 } 79 return result; 80 } 81 82 private void checkClosedOrNotValid() throws ResourceException { 83 if ((isClosed || (!isValide))) { 84 throw new ResourceException (CLOSED_EXCEPTION_MSG); 85 } 86 } 87 88 private static String addressOf(Object o) { 89 String s = String.valueOf(o); 90 int index = s.indexOf('@'); 91 if (index == -1) 92 return s; 93 return JalistoUtils.substring(s, index); 94 } 95 96 97 public String toString() { 98 return "JalistoConnection" + addressOf(super.toString()) + 99 " ManagedConnection" + addressOf(jalistoMc) + " ManagedPM" + 100 addressOf(jalistoMc == null ? null : jalistoMc.getSession()); 101 } 102 103 104 105 public LocalTransaction getLocalTransaction() throws ResourceException { 106 checkClosedOrNotValid(); 107 return (javax.resource.cci.LocalTransaction ) jalistoMc.getLocalTransaction(); 108 } 109 110 public synchronized void close() { 111 trace.println(Trace.JCA, TRACE_ID + " closing connection"); 112 try { 113 checkClosedOrNotValid(); 114 } catch (ResourceException e) { 115 new JcaException(CLOSED_EXCEPTION_MSG); 116 } 117 try { 118 jalistoMc.close(this); 119 } catch (XAException e) { 120 e.printStackTrace(); 121 } 122 isClosed = true; 123 trace.println(Trace.JCA, TRACE_ID + " connection closed..."); 124 } 125 126 public Interaction createInteraction() throws ResourceException { 127 checkClosedOrNotValid(); 128 return null; 129 } 130 131 public ConnectionMetaData getMetaData() throws ResourceException { 132 checkClosedOrNotValid(); 133 return null; 134 } 135 136 public ResultSetInfo getResultSetInfo() throws ResourceException { 137 checkClosedOrNotValid(); 138 return null; 139 } 140 141 public void setAutoCommit(boolean b) throws ResourceException { 142 checkClosedOrNotValid(); 143 } 144 145 public boolean getAutoCommit() throws ResourceException { 146 checkClosedOrNotValid(); 147 return false; 148 } 149 150 151 152 public void closeSession() { 153 getSession().closeSession(); 154 } 155 156 public void openSession() { 157 getSession().openSession(); 158 } 159 160 public boolean isOpen() { 161 return getSession().isOpen(); 162 } 163 164 public boolean contains(Object oid) { 165 return getSession().contains(oid); 166 } 167 168 public Object createObject(Object [] objectToCreate, Class objectClass) { 169 return getSession().createObject(objectToCreate, objectClass); 170 } 171 172 public Object createObject(Object [] objectToCreate, String objectClassName) { 173 return getSession().createObject(objectToCreate, objectClassName); 174 } 175 176 public Object createObject(Object oid, Object [] objectToCreate) { 177 return getSession().createObject(oid, objectToCreate); 178 } 179 180 public Transaction currentTransaction() { 181 return getSession().currentTransaction(); 182 } 183 184 public void defineClass(ClassDescription classDescription) { 185 getSession().defineClass(classDescription); 186 } 187 188 public void deleteObjectByOid(Object oid) { 189 getSession().deleteObjectByOid(oid); 190 } 191 192 public String getClassNameFor(Object oid) { 193 return null; 194 } 195 196 public Extent getExtent(String fullCLassName) { 197 return getSession().getExtent(fullCLassName); 198 } 199 200 public Extent getExtent(Class theClass) { 201 return getSession().getExtent(theClass); 202 } 203 204 public SessionInternal getInternalSession() { 205 return getSession().getInternalSession(); 206 } 207 208 public QueryManager getQueryManager() { 209 return getSession().getQueryManager(); 210 } 211 212 public MetaRepository getMetaRepository() { 213 return getSession().getMetaRepository(); 214 } 215 216 public boolean isClassDefined(String fullQualifiedClassName) { 217 return getSession().isClassDefined(fullQualifiedClassName); 218 } 219 220 public boolean isNewBase() { 221 return getSession().isNewBase(); 222 } 223 224 public Object makeNewFileOid(Class objectClass) { 225 return getSession().makeNewFileOid(objectClass); 226 } 227 228 public Object makeNewFileOid(String objectClassName) { 229 return getSession().makeNewFileOid(objectClassName); 230 } 231 232 public Object [] readObjectByOid(Object oid) { 233 return getSession().readObjectByOid(oid); 234 } 235 236 public void removeClass(String fullClassName) { 237 getSession().removeClass(fullClassName); 238 } 239 240 public void reorganize() { 241 getSession().reorganize(); 242 } 243 244 public Object updateObjectByOid(Object oid, Object [] objectToUpdate) { 245 return getSession().updateObjectByOid(oid, objectToUpdate); 246 } 247 248 public Collection readObjectsByOids(Collection oids) { 249 return getSession().readObjectsByOids(oids); 250 } 251 252 public Object [] refreshObjectByOid(Object oid) { 253 return getSession().refreshObjectByOid(oid); 254 } 255 256 public void eraseStorage() { 257 getSession().eraseStorage(); 258 } 259 260 261 private static final String TRACE_ID = "[JalistoConnection]"; 262 private static final String CLOSED_EXCEPTION_MSG = "Connection already closed or invalidated..."; 263 private JalistoManagedConnection jalistoMc; 264 private Trace trace; 265 private boolean isClosed = false; 266 private boolean isValide = true; 267 } 268 | Popular Tags |