001 /** 002 * 003 * Copyright 2004 Hiram Chirino 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 **/ 018 package org.activemq.ra; 019 020 import java.io.PrintWriter; 021 import java.util.Iterator; 022 import java.util.Set; 023 024 import javax.jms.JMSException; 025 import javax.resource.ResourceException; 026 import javax.resource.spi.ConnectionManager; 027 import javax.resource.spi.ConnectionRequestInfo; 028 import javax.resource.spi.ManagedConnection; 029 import javax.resource.spi.ManagedConnectionFactory; 030 import javax.resource.spi.ResourceAdapter; 031 import javax.resource.spi.ResourceAdapterAssociation; 032 import javax.security.auth.Subject; 033 034 /** 035 * @version $Revision: 1.1.1.1 $ 036 * 037 * @todo Must override equals and hashCode (JCA spec 16.4) 038 */ 039 public class ActiveMQManagedConnectionFactory implements 040 ManagedConnectionFactory, ResourceAdapterAssociation { 041 042 private ActiveMQResourceAdapter adapter; 043 private PrintWriter logWriter; 044 private ActiveMQConnectionRequestInfo info = new ActiveMQConnectionRequestInfo(); 045 046 public void setResourceAdapter(ResourceAdapter adapter) throws ResourceException { 047 this.adapter = (ActiveMQResourceAdapter) adapter; 048 ActiveMQConnectionRequestInfo baseInfo = this.adapter.getInfo().copy(); 049 if( info.getClientid()==null ) 050 info.setClientid(baseInfo.getClientid()); 051 if( info.getPassword()==null ) 052 info.setPassword(baseInfo.getPassword()); 053 if( info.getServerUrl()==null ) 054 info.setServerUrl(baseInfo.getServerUrl()); 055 if( info.getUseInboundSession()==null ) 056 info.setUseInboundSession(baseInfo.getUseInboundSession()); 057 if( info.getUserName()==null ) 058 info.setUserName(baseInfo.getUserName()); 059 } 060 061 public ResourceAdapter getResourceAdapter() { 062 return adapter; 063 } 064 065 /** 066 * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory(javax.resource.spi.ConnectionManager) 067 */ 068 public Object createConnectionFactory(ConnectionManager manager) throws ResourceException { 069 return new ActiveMQConnectionFactory(this, manager, info); 070 } 071 072 /** 073 * This is used when not running in an app server. For now we are creating a 074 * ConnectionFactory that has our SimpleConnectionManager implementation but 075 * it may be a better idea to not support this. The JMS api will have many quirks 076 * the user may not expect when running through the resource adapter. 077 * 078 * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory() 079 */ 080 public Object createConnectionFactory() throws ResourceException { 081 return new ActiveMQConnectionFactory(this, new SimpleConnectionManager(), info); 082 } 083 084 /** 085 * @see javax.resource.spi.ManagedConnectionFactory#createManagedConnection(javax.security.auth.Subject, 086 * javax.resource.spi.ConnectionRequestInfo) 087 */ 088 public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo info) throws ResourceException { 089 try { 090 ActiveMQConnectionRequestInfo amqInfo = (ActiveMQConnectionRequestInfo)info; 091 return new ActiveMQManagedConnection(subject, adapter.makeConnection(amqInfo), amqInfo); 092 } catch (JMSException e) { 093 throw new ResourceException("Could not create connection.", e); 094 } 095 } 096 097 /** 098 * @see javax.resource.spi.ManagedConnectionFactory#matchManagedConnections(java.util.Set, 099 * javax.security.auth.Subject, 100 * javax.resource.spi.ConnectionRequestInfo) 101 */ 102 public ManagedConnection matchManagedConnections(Set connections, Subject subject, ConnectionRequestInfo info) throws ResourceException { 103 Iterator iterator = connections.iterator(); 104 while (iterator.hasNext()) { 105 ActiveMQManagedConnection c = (ActiveMQManagedConnection) iterator.next(); 106 if (c.matches(subject, info)) { 107 try { 108 c.associate(subject, (ActiveMQConnectionRequestInfo) info); 109 return c; 110 } catch (JMSException e) { 111 throw new ResourceException(e); 112 } 113 } 114 } 115 return null; 116 } 117 118 /** 119 * @see javax.resource.spi.ManagedConnectionFactory#setLogWriter(java.io.PrintWriter) 120 */ 121 public void setLogWriter(PrintWriter logWriter) throws ResourceException { 122 this.logWriter = logWriter; 123 } 124 125 /** 126 * @see javax.resource.spi.ManagedConnectionFactory#getLogWriter() 127 */ 128 public PrintWriter getLogWriter() throws ResourceException { 129 return logWriter; 130 } 131 132 /////////////////////////////////////////////////////////////////////////// 133 // 134 // Bean setters and getters. 135 // 136 /////////////////////////////////////////////////////////////////////////// 137 138 public String getClientid() { 139 return info.getClientid(); 140 } 141 142 public String getPassword() { 143 return info.getPassword(); 144 } 145 146 public String getServerUrl() { 147 return info.getServerUrl(); 148 } 149 150 public String getUserName() { 151 return info.getUserName(); 152 } 153 154 public void setClientid(String clientid) { 155 info.setClientid(clientid); 156 } 157 158 public void setPassword(String password) { 159 info.setPassword(password); 160 } 161 162 public void setServerUrl(String url) { 163 info.setServerUrl(url); 164 } 165 166 public void setUserName(String userid) { 167 info.setUserName(userid); 168 } 169 170 public Boolean getUseInboundSession() { 171 return info.getUseInboundSession(); 172 } 173 174 public void setUseInboundSession(Boolean useInboundSession) { 175 info.setUseInboundSession(useInboundSession); 176 } 177 }