001 package biz.hammurapi.jms.adapter.jca; 002 003 import java.io.PrintWriter; 004 import java.util.ArrayList; 005 import java.util.Collection; 006 import java.util.Iterator; 007 008 import javax.resource.NotSupportedException; 009 import javax.resource.ResourceException; 010 import javax.resource.spi.ConnectionEvent; 011 import javax.resource.spi.ConnectionEventListener; 012 import javax.resource.spi.ConnectionRequestInfo; 013 import javax.resource.spi.LocalTransaction; 014 import javax.resource.spi.ManagedConnection; 015 import javax.resource.spi.ManagedConnectionMetaData; 016 import javax.security.auth.Subject; 017 import javax.transaction.xa.XAResource; 018 019 import biz.hammurapi.jms.adapter.jca.ManagedConnectionFactoryImpl.JmsConnectionRequestInfo; 020 021 public class ManagedConnectionImpl implements ManagedConnection { 022 023 private static final String TRANSACTIONS_NOT_SUPPORTED_MESSAGE = "Transactions not supported"; 024 025 026 private ConnectionImpl connection; 027 private Collection listeners = new ArrayList(); 028 private PrintWriter out; 029 private String productName; 030 private String version; 031 private int maxConnections; 032 private ManagedConnectionFactoryImpl factory; 033 034 035 public ManagedConnectionImpl(ManagedConnectionFactoryImpl factory, String productName, String version, int maxConnections) throws ResourceException { 036 super(); 037 this.factory = factory; 038 this.productName = productName; 039 this.version = version; 040 this.maxConnections = maxConnections; 041 } 042 043 ManagedConnectionFactoryImpl getFactory() { 044 return factory; 045 } 046 047 /** 048 * @see ManagedConnection#getConnection(Subject, ConnectionRequestInfo) 049 */ 050 public Object getConnection(Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException { 051 if (cxRequestInfo==null) { 052 throw new ResourceException("Request info is null"); 053 } 054 055 if (cxRequestInfo instanceof JmsConnectionRequestInfo) { 056 JmsConnectionRequestInfo jcri = (JmsConnectionRequestInfo) cxRequestInfo; 057 if (connection==null) { 058 connection = new ConnectionImpl(this, jcri.isStateful(), jcri.getState()); 059 } else { 060 ((ConnectionImpl) connection).init(jcri.isStateful(), jcri.getState()); 061 } 062 return connection; 063 } 064 065 throw new ResourceException("Unrecognized request info type: "+cxRequestInfo.getClass()+", expected "+JmsConnectionRequestInfo.class); 066 } 067 068 /** 069 * @see ManagedConnection#destroy() 070 */ 071 public synchronized void destroy() throws ResourceException { 072 connection = null; 073 listeners.clear(); 074 } 075 076 public synchronized void close() { 077 Iterator it = listeners.iterator(); 078 ConnectionEvent event = new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED); 079 event.setConnectionHandle(connection); 080 while (it.hasNext()) { 081 ConnectionEventListener listener = (ConnectionEventListener) it.next(); 082 (listener).connectionClosed(event); 083 } 084 085 if (sharedAdapter!=null) { 086 sharedAdapter.close(); 087 sharedAdapter = null; 088 } 089 } 090 091 /** 092 * @see ManagedConnection#associateConnection(Object) 093 */ 094 public void associateConnection(Object connection) throws ResourceException { 095 this.connection = (ConnectionImpl) connection; 096 } 097 098 /** 099 * @see ManagedConnection#addConnectionEventListener(ConnectionEventListener) 100 */ 101 public synchronized void addConnectionEventListener(ConnectionEventListener listener) { 102 listeners.add(listener); 103 } 104 105 /** 106 * @see ManagedConnection#removeConnectionEventListener(ConnectionEventListener) 107 */ 108 public synchronized void removeConnectionEventListener(ConnectionEventListener listener) { 109 listeners.remove(listener); 110 } 111 112 /** 113 * @see ManagedConnection#getXAResource() 114 */ 115 public XAResource getXAResource() throws ResourceException { 116 throw new NotSupportedException(TRANSACTIONS_NOT_SUPPORTED_MESSAGE); 117 } 118 119 /** 120 * @see ManagedConnection#getLocalTransaction() 121 */ 122 public LocalTransaction getLocalTransaction() throws ResourceException { 123 throw new NotSupportedException(TRANSACTIONS_NOT_SUPPORTED_MESSAGE); 124 } 125 126 /** 127 * @see ManagedConnection#getMetaData() 128 */ 129 public ManagedConnectionMetaData getMetaData() throws ResourceException { 130 return new ManagedConnectionMetaDataImpl(productName, version, maxConnections, "(unknown)"); 131 } 132 133 /** 134 * @see ManagedConnection#setLogWriter(PrintWriter) 135 */ 136 public void setLogWriter(PrintWriter out) throws ResourceException { 137 this.out = out; 138 } 139 140 /** 141 * @see ManagedConnection#getLogWriter() 142 */ 143 public PrintWriter getLogWriter() throws ResourceException { 144 return out; 145 } 146 147 public void cleanup() throws ResourceException { 148 // Nothing to do. 149 } 150 151 private SharedAdapter sharedAdapter; 152 153 public Object get(String name) { 154 synchronized (this) { 155 if (sharedAdapter == null) { 156 sharedAdapter = factory.getSharedAdapter(); 157 } 158 } 159 return sharedAdapter.get(name); 160 } 161 }