001 package biz.hammurapi.jms.adapter.jca; 002 003 import java.io.PrintWriter; 004 import java.util.Iterator; 005 import java.util.Set; 006 import java.util.logging.Logger; 007 008 import javax.resource.ResourceException; 009 import javax.resource.spi.ConnectionManager; 010 import javax.resource.spi.ConnectionRequestInfo; 011 import javax.resource.spi.ManagedConnection; 012 import javax.resource.spi.ManagedConnectionFactory; 013 import javax.resource.spi.ResourceAdapter; 014 import javax.resource.spi.ResourceAdapterAssociation; 015 import javax.security.auth.Subject; 016 017 public class ManagedConnectionFactoryImpl implements ManagedConnectionFactory, ResourceAdapterAssociation { 018 019 private static final Logger logger = Logger.getLogger(ManagedConnectionFactoryImpl.class.getName()); 020 021 private ResourceAdapterImpl resourceAdapter ; 022 023 private PrintWriter writer; 024 025 public class JmsConnectionRequestInfo implements ConnectionRequestInfo { 026 027 private boolean stateful; 028 private Object state; 029 030 public JmsConnectionRequestInfo(boolean stateful) { 031 this.stateful=stateful; 032 } 033 034 public JmsConnectionRequestInfo(Object state) { 035 this(true); 036 this.state=state; 037 } 038 039 public boolean isStateful() { 040 return stateful; 041 } 042 043 public Object getState() { 044 return state; 045 } 046 } 047 048 public Object createConnectionFactory(ConnectionManager connectionManager) throws ResourceException { 049 if (resourceAdapter == null) { 050 throw new ResourceException("No reference to the resource adapter"); 051 } 052 return new ConnectionFactoryImpl(this, connectionManager); 053 } 054 055 public Object createConnectionFactory() throws ResourceException { 056 if (resourceAdapter == null) { 057 throw new ResourceException("No reference to the resource adapter"); 058 } 059 return new ConnectionFactoryImpl(this, null); 060 } 061 062 public synchronized ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException { 063 return new ManagedConnectionImpl(this, "JMS Adapter", "1.0", 0); 064 } 065 066 public ManagedConnection matchManagedConnections(Set connectionSet, Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException { 067 068 if (cxRequestInfo instanceof JmsConnectionRequestInfo) { 069 Iterator iterator = connectionSet.iterator(); 070 while (iterator.hasNext()) { 071 // Anything matches - we just re-initialize connection. 072 ManagedConnectionImpl match = (ManagedConnectionImpl) iterator.next(); 073 Object connection = match.getConnection(subject, cxRequestInfo); 074 if (connection instanceof ConnectionImpl) { 075 JmsConnectionRequestInfo jcri = (JmsConnectionRequestInfo) cxRequestInfo; 076 ((ConnectionImpl) connection).init(jcri.isStateful(), jcri.getState()); 077 return match; 078 } 079 } 080 return null; 081 } 082 083 if (cxRequestInfo==null) { 084 throw new ResourceException("Request info is null"); 085 } 086 087 throw new ResourceException("Unrecognized request info type: "+cxRequestInfo.getClass()); 088 } 089 090 091 /** 092 * @see ManagedConnectionFactory#setLogWriter(PrintWriter) 093 */ 094 public void setLogWriter(PrintWriter writer) throws ResourceException { 095 this.writer = writer; 096 } 097 098 /** 099 * @see ManagedConnectionFactory#getLogWriter() 100 */ 101 public PrintWriter getLogWriter() throws ResourceException { 102 return writer; 103 } 104 105 /** 106 * Two factories are equal if they belong to the same adapter 107 */ 108 public boolean equals(Object other) { 109 if (other==this) { 110 return true; 111 } 112 113 if (other instanceof ManagedConnectionFactoryImpl) { 114 return resourceAdapter == ((ManagedConnectionFactoryImpl) other).resourceAdapter; 115 } 116 return false; 117 } 118 119 public int hashCode() { 120 return resourceAdapter==null ? super.hashCode() : resourceAdapter.hashCode(); 121 } 122 123 /** 124 * @return Returns the resourceAdapter. 125 */ 126 public ResourceAdapter getResourceAdapter() { 127 return resourceAdapter; 128 } 129 /** 130 * @param resourceAdapter The resourceAdapter to set. 131 */ 132 public void setResourceAdapter(ResourceAdapter resourceAdapter) { 133 if (resourceAdapter instanceof ResourceAdapterImpl) { 134 this.resourceAdapter = (ResourceAdapterImpl) resourceAdapter; 135 } else { 136 logger.severe("Resource adapter object handed from the App server is not of expected type "+ResourceAdapterImpl.class); 137 this.resourceAdapter = null ; 138 } 139 140 } 141 142 SharedAdapter getSharedAdapter() { 143 return resourceAdapter.getSharedAdapter(); 144 } 145 }