summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/EJB 3.0 Code/Rima Examples/src/examples/integration/out_loan_ra/ManagedConnectionImpl.java
blob: 20871726caeabcadeaf1485021ede0171034a8a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package examples.integration.out_loan_ra;

import java.io.*;
import java.util.*;

import javax.resource.*;
import javax.resource.spi.*;
import javax.resource.spi.security.PasswordCredential;
import javax.resource.spi.IllegalStateException;
import javax.resource.spi.SecurityException;
import javax.resource.NotSupportedException;
import javax.security.auth.Subject;
import javax.transaction.xa.XAResource;

public class ManagedConnectionImpl implements ManagedConnection {

     private ConnectionEventListener connEventListener;
     private ManagedConnectionFactory manConnFactory;
     private boolean isDestroyed;
     private PrintWriter manConnLogWriter;

     ManagedConnectionImpl (ManagedConnectionFactory manConnFactory) {

          System.out.println("ManagedConnectionImpl(ManagedConnectionFactory) called");

          this.manConnFactory = manConnFactory;
     }

     public Object getConnection(Subject subject, ConnectionRequestInfo connectionRequestInfo) 
          throws ResourceException {

          System.out.println("ManagedConnectionImpl.getConnection(Subject, ConnectionRequestInfo) called");

          ConnectionImpl conn = new ConnectionImpl(this);

          return conn;
     }

     public void destroy() throws ResourceException {

          System.out.println("ManagedConnectionImpl.destroy() called");

          isDestroyed=true;

          cleanup();
     }

     public void cleanup() throws ResourceException {

          System.out.println("ManagedConnectionImpl.cleanup() called");
     }

    public void associateConnection(Object connection) throws ResourceException {

        throw new NotSupportedException("ManagedConnectionImpl.associateConnection() not supported.");
    }

    public void addConnectionEventListener(ConnectionEventListener connEventListener) {

	  System.out.println("ManagedConnectionImpl.addConnectionEventListener(ConnectionEventListener) called");

        this.connEventListener = connEventListener;
    }

    public void removeConnectionEventListener (ConnectionEventListener connEventListener) {

    }

    public XAResource getXAResource() throws ResourceException {

        throw new NotSupportedException("Global transactions are not supported");
    }

    public LocalTransaction getLocalTransaction() throws ResourceException {

        throw new NotSupportedException("Local transactions are not supported");        
    }

     public ManagedConnectionMetaData getMetaData() throws ResourceException {
     
          if (isDestroyed)
               throw new ResourceException ("Managed connection has already been closed.");

          return new ManagedConnectionMetaDataImpl (this);
     }

     public void setLogWriter(PrintWriter manConnLogWriter) {

          this.manConnLogWriter = manConnLogWriter;
     }

     public PrintWriter getLogWriter() {

          return manConnLogWriter;
     }

     void sendEvent(int eventType, Exception e, Object connHandle) {

          System.out.println("ManagedConnectionImpl.sendEvent(int, e, connHandle) called");
	
          ConnectionEvent connEvent = null;

          if (e == null) 
               connEvent = new ConnectionEvent(this, eventType);
          else 
               connEvent = new ConnectionEvent(this, eventType, e);
          
          connEvent.setConnectionHandle(connHandle);

          switch (connEvent.getId()) {
               case ConnectionEvent.CONNECTION_CLOSED:
                    this.connEventListener.connectionClosed(connEvent);
                    break;
               case ConnectionEvent.LOCAL_TRANSACTION_STARTED:
                    this.connEventListener.localTransactionStarted(connEvent);
                    break;
               case ConnectionEvent.LOCAL_TRANSACTION_COMMITTED:
                    this.connEventListener.localTransactionCommitted(connEvent);
                    break;
               case ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK:
                    this.connEventListener.localTransactionRolledback(connEvent);
                    break;
               case ConnectionEvent.CONNECTION_ERROR_OCCURRED:
                    this.connEventListener.connectionErrorOccurred(connEvent);
                    break;
               default:
                    throw new IllegalArgumentException("Unsupported event: " + connEvent.getId());
          }
     }

     ManagedConnectionFactory getManagedConnectionFactory() {

          return manConnFactory;
     }
}