📄 managedconnectionimpl.java
字号:
/* * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */package samples.connectors.mailconnector.ra.outbound;import javax.mail.*;import javax.mail.internet.*;import javax.resource.spi.*;import java.lang.Object;import javax.resource.ResourceException;import javax.transaction.xa.XAResource;import javax.security.auth.Subject;import java.io.PrintWriter;import java.util.*;import java.util.logging.*;import javax.resource.cci.*;import javax.resource.spi.security.PasswordCredential;import javax.resource.spi.SecurityException;import javax.resource.spi.IllegalStateException;import javax.resource.NotSupportedException;import java.io.ObjectInputStream;import java.io.InputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.net.Socket;import java.net.InetAddress;import java.net.UnknownHostException;import samples.connectors.mailconnector.api.*;import samples.connectors.mailconnector.share.*;/** * The ManagedConnectionImpl class represents a physical connection to the * backend Mail Server. */public class ManagedConnectionImpl implements ManagedConnection{ private ManagedConnectionFactoryImpl mcf; private JavaMailConnectionEventListener eventListener; private Set connectionSet; // set of Mail Server Connections private PrintWriter logWriter; private boolean destroyed; private MailServerStore store = null; //Several connections fro a store private static int testCounter = 0; private int myId = 0; private PasswordCredential passCred = null; static Logger logger = Logger.getLogger("samples.connectors.mailconnector.ra.outbound", "samples.connectors.mailconnector.ra.outbound.LocalStrings"); ResourceBundle resource = ResourceBundle.getBundle("samples.connectors.mailconnector.ra.outbound.LocalStrings"); /** * Constructor. * * @param mcf the ManagedConnectionFactory that created this instance * @param subject security context as JAAS subject * @param cxRequestInfo ConnectionRequestInfo instance */ ManagedConnectionImpl(ManagedConnectionFactoryImpl mcf, Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException { myId = testCounter++; logger.info(" 3B.- (" + myId + ") ManagedConnection::constructor"); this.mcf = mcf; // Note: this will select the credential that matches this MC's MCF. // The credential's MCF is set by the application server. this.passCred = Util.getPasswordCredential(mcf, subject, cxRequestInfo); // Open the physical connection to a store openStore(cxRequestInfo); connectionSet = new HashSet(); eventListener = new JavaMailConnectionEventListener(this); } /** * Creates a new connection handle to the Mail Server represented by the * ManagedConnection instance. This connection handle is used by the * application code to refer to the underlying physical connection. * * @param subject security context as JAAS subject * @param cxRequestInfo ConnectionRequestInfo instance * * @return Connection instance representing the connection handle * * @exception ResourceException if the method fails to get a connection */ public Object getConnection(Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException { logger.info(" 4.- (" + myId + ") testManagedConnection::getConnection: ConnectionManager requested a Connection handle"); checkIfDestroyed(); PasswordCredential pc = Util.getPasswordCredential(mcf, subject, cxRequestInfo); if (!Util.isPasswordCredentialEqual(pc, passCred)) { //logger.finest("getConnection: User authentication failed"); throw new SecurityException(resource.getString("PRINCIPAL_DOES_NOT_MATCH")); } // We only need the Folder name as all the connections share the store String folderName; if (cxRequestInfo != null) { folderName = ((ConnectionRequestInfoImpl)cxRequestInfo).getFolderName(); } else { // Use default values folderName = mcf.getFolderName(); } javax.mail.Folder folder; try { folder = store.getFolder(folderName); } catch (Exception e) { logger.info("ManagedConnectionImpl::getConnection threw exception: " + e); throw new ResourceException(e.getMessage()); } JavaMailConnection javaMailCon = new JavaMailConnectionImpl(this, folder ); addJavaMailConnection(javaMailCon); //logger.finest("getConnection: returning a Mail Server Connection handle to CM"); return javaMailCon; } /** * Destroys the physical connection. * * @exception ResourceException if the method fails to destroy the * connection */ public void destroy() throws ResourceException { if (destroyed) return; logger.info(" 9.- (" + myId + ") ManagedConnection::destroy called"); destroyed = true; testCounter--; invalidateJavaMailConnections(); try { store.closeStore(); } catch (Exception e) { logger.info("ManagedConnectionImpl::destroy threw exception: " + e); throw new ResourceException(e.getMessage()); } } /** * Initiates a cleanup of the client-specific state maintained by a * ManagedConnection instance. The cleanup should invalidate all connection * handles created using this ManagedConnection instance. * * @exception ResourceException if the cleanup fails */ public void cleanup() throws ResourceException { checkIfDestroyed(); logger.info(" 8.- (" + myId + ") ManagedConnection::cleanup called"); invalidateJavaMailConnections(); } private void invalidateJavaMailConnections() { Iterator it = connectionSet.iterator(); while (it.hasNext()) { JavaMailConnectionImpl javaMailCon = (JavaMailConnectionImpl) it.next(); javaMailCon.invalidate(); } connectionSet.clear(); } /** * Used by the container to change the association of an application-level * connection handle with a ManagedConnection instance. The container * should find the right ManagedConnection instance and call the * associateConnection method. * * @param connection application-level connection handle * * @exception ResourceException if the attempt to change the association * fails */ public void associateConnection(Object connection) throws ResourceException { checkIfDestroyed(); if (connection instanceof JavaMailConnection) { JavaMailConnectionImpl javaMailCon = (JavaMailConnectionImpl) connection; javaMailCon.associateConnection(this); } else { throw new IllegalStateException(resource.getString("INVALID_CONNECTION")); } } /** * Adds a connection event listener to the ManagedConnection instance. * The registered ConnectionEventListener instances are notified of * connection close and error events as well as local-transaction-related * events on the Managed Connection. * * @param listener a new ConnectionEventListener to be registered */ public void addConnectionEventListener(ConnectionEventListener listener) { eventListener.addConnectorListener(listener); } /** * Removes an already registered connection event listener from the * ManagedConnection instance. * * @param listener already registered connection event listener to be * removed */ public void removeConnectionEventListener(ConnectionEventListener listener) { eventListener.removeConnectorListener(listener); } /** * Returns a javax.transaction.xa.XAresource instance. An application * server enlists this XAResource instance with the Transaction Manager * if the ManagedConnection instance is being used in a JTA transaction * that is being coordinated by the Transaction Manager. * * Because this implementation does not support transactions, the method * throws an exception. * * @return the XAResource instance * * @exception ResourceException if transactions are not supported */ public XAResource getXAResource() throws ResourceException { throw new NotSupportedException(resource.getString("NO_XATRANSACTION"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -