1 /*** 2 * 3 * Copyright 2004 Hiram Chirino 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 **/ 18 package org.codehaus.activemq.ra; 19 20 import javax.resource.ResourceException; 21 import javax.resource.spi.ConnectionManager; 22 import javax.resource.spi.ConnectionRequestInfo; 23 import javax.resource.spi.ManagedConnection; 24 import javax.resource.spi.ManagedConnectionFactory; 25 import javax.resource.spi.ResourceAdapter; 26 import javax.resource.spi.ResourceAdapterAssociation; 27 import javax.security.auth.Subject; 28 import java.io.PrintWriter; 29 import java.util.Iterator; 30 import java.util.Set; 31 32 /*** 33 * @version $Revision: 1.7 $ 34 */ 35 public class ActiveMQManagedConnectionFactory implements 36 ManagedConnectionFactory, ResourceAdapterAssociation { 37 38 private ActiveMQResourceAdapter adapter; 39 private PrintWriter logWriter; 40 41 public ActiveMQManagedConnectionFactory() { 42 } 43 44 /*** 45 * @see javax.resource.spi.ManagedConnectionFactory#setResourceAdapter(javax.resource.spi.ResourceAdapter) 46 */ 47 public void setResourceAdapter(ResourceAdapter adapter) throws ResourceException { 48 this.adapter = (ActiveMQResourceAdapter) adapter; 49 } 50 51 /*** 52 * @see javax.resource.spi.ManagedConnectionFactory#getResourceAdapter() 53 */ 54 public ResourceAdapter getResourceAdapter() { 55 return adapter; 56 } 57 58 /*** 59 * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory(javax.resource.spi.ConnectionManager) 60 */ 61 public Object createConnectionFactory(ConnectionManager manager) throws ResourceException { 62 return new ActiveMQConnectionFactory(this, manager, adapter.getInfo()); 63 } 64 65 /*** 66 * This is used when not running in an app server. For now we are creating a 67 * ConnectionFactory that has our SimpleConnectionManager implementation but 68 * it may be a better idea to not support this. The JMS api will have many quirks 69 * the user may not expect when running through the resource adapter. 70 * 71 * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory() 72 */ 73 public Object createConnectionFactory() throws ResourceException { 74 return new ActiveMQConnectionFactory(this, new SimpleConnectionManager(), adapter.getInfo()); 75 } 76 77 /*** 78 * @see javax.resource.spi.ManagedConnectionFactory#createManagedConnection(javax.security.auth.Subject, 79 * javax.resource.spi.ConnectionRequestInfo) 80 */ 81 public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo info) throws ResourceException { 82 return new ActiveMQManagedConnection(subject, adapter, (ActiveMQConnectionRequestInfo)info); 83 } 84 85 /*** 86 * @see javax.resource.spi.ManagedConnectionFactory#matchManagedConnections(java.util.Set, 87 * javax.security.auth.Subject, 88 * javax.resource.spi.ConnectionRequestInfo) 89 */ 90 public ManagedConnection matchManagedConnections(Set connections, Subject subject, ConnectionRequestInfo info) throws ResourceException { 91 Iterator iterator = connections.iterator(); 92 while (iterator.hasNext()) { 93 ActiveMQManagedConnection c = (ActiveMQManagedConnection) iterator.next(); 94 if (c.matches(subject, info)) { 95 return c; 96 } 97 } 98 return null; 99 } 100 101 /*** 102 * @see javax.resource.spi.ManagedConnectionFactory#setLogWriter(java.io.PrintWriter) 103 */ 104 public void setLogWriter(PrintWriter logWriter) throws ResourceException { 105 this.logWriter = logWriter; 106 } 107 108 /*** 109 * @see javax.resource.spi.ManagedConnectionFactory#getLogWriter() 110 */ 111 public PrintWriter getLogWriter() throws ResourceException { 112 return logWriter; 113 } 114 115 }