📄 filemanagedconnection.java
字号:
package com.j2ee14.ch23.jca; import javax.resource.*;import javax.resource.spi.*;import javax.security.auth.*;import java.util.*;import java.io.*;import java.net.*;import javax.transaction.xa.*;//FileManagedConnection代表了物理的连接public class FileManagedConnection implements ManagedConnection{ protected Socket socket;//和server连接的Socket protected PrintStream serverStream;// protected BufferedReader serverBufferedReader; protected boolean destroyed;//是否销毁 protected PrintWriter out; protected Set connections = new HashSet();//被管理的连接 protected Set connectionListeners = new HashSet();//连接监听器 FileManagedConnectionFactory factory;//连接工厂 public FileManagedConnection(FileManagedConnectionFactory factory) { this.factory = factory; } //为连接增加事件监听器 public void addConnectionEventListener(ConnectionEventListener l) { connectionListeners.add(l); } //清除连接监听器 public void removeConnectionEventListener(ConnectionEventListener l) { connectionListeners.remove(l); } //返回连接工厂 public FileManagedConnectionFactory getFactory () { return factory; } //增加一个连接,并且返回它 public Object getConnection (Subject subject, ConnectionRequestInfo cxRequestInfo) { FileConnectionImpl connection = new FileConnectionImpl(); connection.setManager(this); connection.setLogWriter (out); addConnection(connection); return connection; } //清除占用的资源 public void cleanup() throws ResourceException { destroyedError(); Iterator it = connections.iterator(); while (it.hasNext()) { FileConnectionImpl demoConnectionImpl = (FileConnectionImpl) it.next(); demoConnectionImpl.invalidate(); } connections.clear(); } //销毁所有的物理连接 public void destroy() { if (destroyed) return; Iterator it = connections.iterator(); while (it.hasNext()) { FileConnectionImpl FileConnectionImpl = (FileConnectionImpl) it.next(); FileConnectionImpl.invalidate(); } connections.clear(); if (socket != null) try {socket.close();} catch (Exception e){} destroyed = true; } //把一个Connection和这个被管理的连接关联 public void associateConnection(Object _connection) throws ResourceException { destroyedError(); if (_connection instanceof FileConnection) { FileConnectionImpl connection = (FileConnectionImpl)_connection; FileManagedConnection demoManagedConnection = connection.getManager(); if (demoManagedConnection == this) return; try { demoManagedConnection.removeConnection(connection); } catch(Exception e) { } addConnection(connection); connection.setManager (this); } else { throw new javax.resource.spi.IllegalStateException ("Invalid connection object: " + _connection); } } //不支持此方法 public XAResource getXAResource() throws ResourceException { throw new NotSupportedException("不支持分布式事务"); } //不支持此方法 public LocalTransaction getLocalTransaction() throws ResourceException { throw new NotSupportedException ("Local transaction not supported"); } //返回元数据 public ManagedConnectionMetaData getMetaData() { return new FileManagedConnectionMetaData(this); } protected void destroyedError() throws javax.resource.spi.IllegalStateException { if (destroyed) throw new javax.resource.spi.IllegalStateException ("FileManagedConnection 已经销毁"); } //增加一个和此被管理连接(ManagedConnection)关联连接(Connection) protected void addConnection(FileConnection connection) { connections.add(connection); } //删除一个和此被管理连接(ManagedConnection)关联连接(Connection) protected void removeConnection(FileConnection connection) { connections.remove(connection); } //设置LogWriter public void setLogWriter(PrintWriter _out) { out = _out; } public PrintWriter getLogWriter() { return out; } //判断是否已经销毁 public boolean isDestroyed() { return destroyed; } //关闭连接的事件,释放资源,由连接监听器来处理 void connectionClosedEvent() { Iterator it = connectionListeners.iterator(); while (it.hasNext()) { ConnectionEventListener listener = (ConnectionEventListener) it.next(); listener.connectionClosed (new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED)); } } //打开物理连接,物理连接是和EIS的正在的连接。通过Socket来进行通信 public void openPhysicalConnection (String serverName, int portNumber) throws UnknownHostException, IOException { socket = new Socket (serverName, portNumber); serverStream = new PrintStream (socket.getOutputStream()); serverBufferedReader = new BufferedReader (new InputStreamReader (socket.getInputStream())); } //业务方法,它是同步的,同时只能一个和Server进行通信 public synchronized String getTextFile(String name) throws ResourceException { serverStream.println("##BEGIN##"); serverStream.println (name); try { String s,s2=new String(); while((s=serverBufferedReader.readLine())!=null) { if(!s.equals("##BYE##")) { s2+=s+"\n"; } else continue; } return s2; } catch (Exception e) { throw new ResourceException ("调用getTextFile()发生错误: " + e.toString()); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -