001    package com.mockrunner.mock.connector.spi;
002    
003    import java.io.PrintWriter;
004    import java.io.Serializable;
005    import java.util.Iterator;
006    import java.util.Set;
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.security.auth.Subject;
014    
015    /**
016     * Mock implementation of <code>ManagedConnectionFactory</code>.
017     */
018    public class MockManagedConnectionFactory implements ManagedConnectionFactory, Serializable 
019    {
020            private static final long serialVersionUID = 3257291348330558264L;
021    
022        private PrintWriter logWriter;
023        private ConnectionManager connectionManager;
024    
025        public Object createConnectionFactory() throws ResourceException
026        {
027            return createConnectionFactory(null);
028        }
029    
030        public Object createConnectionFactory(ConnectionManager connectionManager) throws ResourceException
031        {
032            this.connectionManager = connectionManager;
033            return this;
034        }
035    
036        public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo requestInfo) throws ResourceException
037        {
038            Object managedConnection = null;
039            if(connectionManager == null)
040            {
041                // TODO is this correct?
042                managedConnection = new MockManagedConnection().getConnection(subject, requestInfo);
043            } 
044            else
045            {
046                managedConnection = connectionManager.allocateConnection(this, requestInfo);
047            }
048            return (MockManagedConnection)managedConnection;
049        }
050    
051        public ManagedConnection matchManagedConnections(Set set, Subject subject, ConnectionRequestInfo connectionRequestInfo) throws ResourceException
052        {
053            ManagedConnection managedConnection = null;
054            Iterator iterator = set.iterator();
055            if(iterator.hasNext())
056            {
057                // TODO also process subject and connectionrequestinfo
058                managedConnection = (ManagedConnection)iterator.next();
059            }
060            return managedConnection;
061        }
062    
063        public PrintWriter getLogWriter() throws ResourceException
064        {
065            return logWriter;
066        }
067    
068        public void setLogWriter(PrintWriter logWriter) throws ResourceException
069        {
070            this.logWriter = logWriter;
071        }
072    }