DemoManagedConnection
DemoManagedConnection是资源适配器的关键所在,它代表了和EIS的物理连接,前面介绍的DemoConnection是由客户端使用的虚拟连接,虚拟连接要通过物理连接才能使用EIS。一个物理连接可以被多个虚拟连接使用,可以通过associateConnection方法来把虚拟连接和物理连接进行关联。DemoManagedConnection也提供了产生虚拟连接实例的方法。DemoManagedConnection的代码如例程7所示。
例程7 DemoManagedConnection的代码
1 package com.hellking.jca;
2 import javax.resource.*;
3 import javax.resource.spi.*;
4 import javax.security.auth.*;
5 import java.util.*;
6 import java.io.*;
7 import java.net.*;
8 import javax.transaction.xa.*;
9 //DemoManagedConnection代表了到EIS的物理的连接
10 public class DemoManagedConnection implements ManagedConnection
11 {
12 protected Socket socket;//和server连接的Socket
13 protected PrintStream serverStream;//
14 protected BufferedReader serverBufferedReader;
15 protected boolean destroyed;//是否销毁
16 protected PrintWriter out;
17 protected Set connections = new HashSet();//被管理的连接
18 protected Set connectionListeners = new HashSet();//连接监听器
19 DemoManagedConnectionFactory factory;//连接工厂
20
21 public DemoManagedConnection(DemoManagedConnectionFactory factory)
22 {
23 this.factory = factory;
24 }
25 //为连接增加事件监听器
26 public void addConnectionEventListener(ConnectionEventListener l)
27 {
28 connectionListeners.add(l);
29 }
30 //清除连接监听器
31 public void removeConnectionEventListener(ConnectionEventListener l)
32 {
33 connectionListeners.remove(l);
34 }
35
36 //返回连接工厂
37 public DemoManagedConnectionFactory getFactory ()
38 {
39 return factory;
40 }
41
42 //增加一个连接,并且返回它
43 public Object getConnection (Subject subject,
44 ConnectionRequestInfo cxRequestInfo)
45 {
46 DemoConnectionImpl connection = new DemoConnectionImpl();
47 connection.setManager(this);
48 connection.setLogWriter (out);
49 addConnection(connection);
50 return connection;
51 }
52
53 //清除占用的资源
54 public void cleanup() throws ResourceException
55 {
56 destroyedError();
57 Iterator it = connections.iterator();
58 while (it.hasNext())
59 {
60 DemoConnectionImpl demoConnectionImpl = (DemoConnectionImpl) it.next();
61 demoConnectionImpl.invalidate();
62 }
63 connections.clear();
64 }
65
66 //销毁所有的物理连接
67 public void destroy()
68 {
69 if (destroyed) return;
70 Iterator it = connections.iterator();
71 while (it.hasNext())
72 {
73 DemoConnectionImpl DemoConnectionImpl = (DemoConnectionImpl) it.next();
74 DemoConnectionImpl.invalidate();
75 }
76 connections.clear();
77 if (socket != null)
78 try {socket.close();}
79 catch (Exception e){}
80 destroyed = true;
81 }
82
83 //把一个Connection和这个被管理的连接关联
84 public void associateConnection(Object _connection)
85 throws ResourceException
86 {
87 destroyedError();
88 if (_connection instanceof DemoConnection)
89 {
90 DemoConnectionImpl connection =
91 (DemoConnectionImpl)_connection;
92
93 DemoManagedConnection demoManagedConnection = connection.getManager();
94 if (demoManagedConnection == this) return;
95 try
96 {
97 demoManagedConnection.removeConnection(connection);
98 }
99 catch(Exception e)
100 {
101 }
102 addConnection(connection);
103 connection.setManager (this);
104 }
105 else
106 {
107 throw new javax.resource.spi.IllegalStateException
108 ("Invalid connection object: " + _connection);
109 }
110 }
111 //不支持此方法
112 public XAResource getXAResource() throws ResourceException
113 {
114 throw new NotSupportedException("不支持分布式事务");
115 }
116
117 //不支持此方法
118 public LocalTransaction getLocalTransaction()
119 throws ResourceException
120 {
121 throw new NotSupportedException ("Local transaction not supported");
122 }
123
124 //返回元数据
125 public ManagedConnectionMetaData getMetaData()
126 {
127 return new DemoManagedConnectionMetaData(this);
128 }
129
130 protected void destroyedError()
131 throws javax.resource.spi.IllegalStateException
132 {
133 if (destroyed)
134 throw new javax.resource.spi.IllegalStateException
135 ("DemoManagedConnection 已经销毁");
136 }
137
138 //增加一个和此被管理连接(ManagedConnection)关联连接(Connection)
139 protected void addConnection(DemoConnection connection)
140 {
141 connections.add(connection);
142 }
143 //删除一个和此被管理连接(ManagedConnection)关联连接(Connection)
144 protected void removeConnection(DemoConnection connection)
145 {
146 connections.remove(connection);
147 }
148 //设置LogWriter
149 public void setLogWriter(PrintWriter _out)
150 {
151 out = _out;
152 }
153
154 public PrintWriter getLogWriter()
155 {
156 return out;
157 }
158 //判断是否已经销毁
159 public boolean isDestroyed()
160 {
161 return destroyed;
162 }
163 //关闭连接的事件,释放资源,由连接监听器来处理
164 void connectionClosedEvent()
165 {
166 Iterator it = connectionListeners.iterator();
167 while (it.hasNext())
168 {
169 ConnectionEventListener listener =
170 (ConnectionEventListener) it.next();
171 listener.connectionClosed
172 (new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED));
173 }
174 }
175
176 //打开物理连接,物理连接是和EIS的正在的连接。通过Socket来进行通信
177 public void openPhysicalConnection (String serverName, int portNumber)
178 throws UnknownHostException, IOException
179 {
180 socket = new Socket (serverName, portNumber);
181 serverStream = new PrintStream (socket.getOutputStream());
182 serverBufferedReader = new BufferedReader
183 (new InputStreamReader (socket.getInputStream()));
184 }
185 //业务方法,它是同步的,同时只能一个和Server进行通信
186 public synchronized String sayHello(String name)
187 throws ResourceException
188 {
189 serverStream.println (name);
190 try
191 {
192 String in = serverBufferedReader.readLine();
193 return in;
194 }
195 catch (Exception e)
196 {
197 throw new ResourceException
198 ("调用sayHello()发生错误: " + e.toString());
199 }
200 }
201 }
202
2 import javax.resource.*;
3 import javax.resource.spi.*;
4 import javax.security.auth.*;
5 import java.util.*;
6 import java.io.*;
7 import java.net.*;
8 import javax.transaction.xa.*;
9 //DemoManagedConnection代表了到EIS的物理的连接
10 public class DemoManagedConnection implements ManagedConnection
11 {
12 protected Socket socket;//和server连接的Socket
13 protected PrintStream serverStream;//
14 protected BufferedReader serverBufferedReader;
15 protected boolean destroyed;//是否销毁
16 protected PrintWriter out;
17 protected Set connections = new HashSet();//被管理的连接
18 protected Set connectionListeners = new HashSet();//连接监听器
19 DemoManagedConnectionFactory factory;//连接工厂
20
21 public DemoManagedConnection(DemoManagedConnectionFactory factory)
22 {
23 this.factory = factory;
24 }
25 //为连接增加事件监听器
26 public void addConnectionEventListener(ConnectionEventListener l)
27 {
28 connectionListeners.add(l);
29 }
30 //清除连接监听器
31 public void removeConnectionEventListener(ConnectionEventListener l)
32 {
33 connectionListeners.remove(l);
34 }
35
36 //返回连接工厂
37 public DemoManagedConnectionFactory getFactory ()
38 {
39 return factory;
40 }
41
42 //增加一个连接,并且返回它
43 public Object getConnection (Subject subject,
44 ConnectionRequestInfo cxRequestInfo)
45 {
46 DemoConnectionImpl connection = new DemoConnectionImpl();
47 connection.setManager(this);
48 connection.setLogWriter (out);
49 addConnection(connection);
50 return connection;
51 }
52
53 //清除占用的资源
54 public void cleanup() throws ResourceException
55 {
56 destroyedError();
57 Iterator it = connections.iterator();
58 while (it.hasNext())
59 {
60 DemoConnectionImpl demoConnectionImpl = (DemoConnectionImpl) it.next();
61 demoConnectionImpl.invalidate();
62 }
63 connections.clear();
64 }
65
66 //销毁所有的物理连接
67 public void destroy()
68 {
69 if (destroyed) return;
70 Iterator it = connections.iterator();
71 while (it.hasNext())
72 {
73 DemoConnectionImpl DemoConnectionImpl = (DemoConnectionImpl) it.next();
74 DemoConnectionImpl.invalidate();
75 }
76 connections.clear();
77 if (socket != null)
78 try {socket.close();}
79 catch (Exception e){}
80 destroyed = true;
81 }
82
83 //把一个Connection和这个被管理的连接关联
84 public void associateConnection(Object _connection)
85 throws ResourceException
86 {
87 destroyedError();
88 if (_connection instanceof DemoConnection)
89 {
90 DemoConnectionImpl connection =
91 (DemoConnectionImpl)_connection;
92
93 DemoManagedConnection demoManagedConnection = connection.getManager();
94 if (demoManagedConnection == this) return;
95 try
96 {
97 demoManagedConnection.removeConnection(connection);
98 }
99 catch(Exception e)
100 {
101 }
102 addConnection(connection);
103 connection.setManager (this);
104 }
105 else
106 {
107 throw new javax.resource.spi.IllegalStateException
108 ("Invalid connection object: " + _connection);
109 }
110 }
111 //不支持此方法
112 public XAResource getXAResource() throws ResourceException
113 {
114 throw new NotSupportedException("不支持分布式事务");
115 }
116
117 //不支持此方法
118 public LocalTransaction getLocalTransaction()
119 throws ResourceException
120 {
121 throw new NotSupportedException ("Local transaction not supported");
122 }
123
124 //返回元数据
125 public ManagedConnectionMetaData getMetaData()
126 {
127 return new DemoManagedConnectionMetaData(this);
128 }
129
130 protected void destroyedError()
131 throws javax.resource.spi.IllegalStateException
132 {
133 if (destroyed)
134 throw new javax.resource.spi.IllegalStateException
135 ("DemoManagedConnection 已经销毁");
136 }
137
138 //增加一个和此被管理连接(ManagedConnection)关联连接(Connection)
139 protected void addConnection(DemoConnection connection)
140 {
141 connections.add(connection);
142 }
143 //删除一个和此被管理连接(ManagedConnection)关联连接(Connection)
144 protected void removeConnection(DemoConnection connection)
145 {
146 connections.remove(connection);
147 }
148 //设置LogWriter
149 public void setLogWriter(PrintWriter _out)
150 {
151 out = _out;
152 }
153
154 public PrintWriter getLogWriter()
155 {
156 return out;
157 }
158 //判断是否已经销毁
159 public boolean isDestroyed()
160 {
161 return destroyed;
162 }
163 //关闭连接的事件,释放资源,由连接监听器来处理
164 void connectionClosedEvent()
165 {
166 Iterator it = connectionListeners.iterator();
167 while (it.hasNext())
168 {
169 ConnectionEventListener listener =
170 (ConnectionEventListener) it.next();
171 listener.connectionClosed
172 (new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED));
173 }
174 }
175
176 //打开物理连接,物理连接是和EIS的正在的连接。通过Socket来进行通信
177 public void openPhysicalConnection (String serverName, int portNumber)
178 throws UnknownHostException, IOException
179 {
180 socket = new Socket (serverName, portNumber);
181 serverStream = new PrintStream (socket.getOutputStream());
182 serverBufferedReader = new BufferedReader
183 (new InputStreamReader (socket.getInputStream()));
184 }
185 //业务方法,它是同步的,同时只能一个和Server进行通信
186 public synchronized String sayHello(String name)
187 throws ResourceException
188 {
189 serverStream.println (name);
190 try
191 {
192 String in = serverBufferedReader.readLine();
193 return in;
194 }
195 catch (Exception e)
196 {
197 throw new ResourceException
198 ("调用sayHello()发生错误: " + e.toString());
199 }
200 }
201 }
202