📄 filemanagedconnectionfactory.java
字号:
package com.j2ee14.ch23.jca;import javax.resource.*;import javax.resource.spi.*;import javax.security.auth.*;import java.util.Set;import java.util.Iterator;import java.io.*;public class FileManagedConnectionFactory implements ManagedConnectionFactory, Serializable{ protected PrintWriter out = new PrintWriter(System.out); private int port;//连接EIS的端口 private String server;//eis服务器的url //创建连接工厂,指定连接工厂的连接管理者为connectionManager public Object createConnectionFactory (ConnectionManager connectionManager) { FileConnectionFactoryImpl demoConnectionFactoryImpl = new FileConnectionFactoryImpl (this, connectionManager); demoConnectionFactoryImpl.setLogWriter(out); return demoConnectionFactoryImpl; } //创建连接工厂,没有指定连接管理者 public Object createConnectionFactory() { FileConnectionFactoryImpl demoConnectionFactoryImpl = new FileConnectionFactoryImpl (this, null); demoConnectionFactoryImpl.setLogWriter(out); return demoConnectionFactoryImpl; } //创建被管理的连接,被管理的连接是和EIS的真实连接,它是物理连接。 public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo cri) throws ResourceException { FileManagedConnection demoManagedConnection = new FileManagedConnection(this); demoManagedConnection.setLogWriter(out); try { //打开物理连接 System.out.println("打开物理连接....."); demoManagedConnection.openPhysicalConnection (server, port); return demoManagedConnection; } catch (IOException e) { throw new ResourceException (e.toString()); } } //匹配被管理的连接,如果匹配到连接,则返回,否则返回null public ManagedConnection matchManagedConnections (Set connections, Subject subject, ConnectionRequestInfo cri) { Iterator it = connections.iterator(); while (it.hasNext()) { Object obj = it.next(); if (obj instanceof FileManagedConnection) { FileManagedConnection demoManagedConnection = (FileManagedConnection) obj; FileManagedConnectionFactory demoManagedConnectionf = demoManagedConnection.getFactory(); if (demoManagedConnectionf.equals(this)) { return demoManagedConnection; } } } return null; } public int hashCode() { if (server == null) return 0; return server.hashCode(); } //判断两个被管理的连接工厂是否相等 public boolean equals(Object o) { if (o == null) return false; if (!(o instanceof FileManagedConnectionFactory)) return false; FileManagedConnectionFactory other = (FileManagedConnectionFactory)o; if (server.equalsIgnoreCase(other.server) && port == other.port) return true; return false; } public void setLogWriter(java.io.PrintWriter out) { this.out = out; } public PrintWriter getLogWriter() { return out; } public void setServer (String server) { this.server = server; } public String getServer () { return server; } public void setPort (Integer port) { this.port = port.intValue(); } public Integer getPort () { return new Integer(port); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -