⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connect the enterprise with the jca, part 2.htm

📁 本人收集的关于Java Connector Architecture的资料
💻 HTM
📖 第 1 页 / 共 4 页
字号:
adapter is a complex task, I can only scratch the surface in this article. 
Nevertheless, by the end, you will understand a basic JCA adapter's 
construction, and grasp the effort required to build your own. 
<P>To accomplish those goals, I first describe the sample adapter's 
capabilities, as well as how to deploy and run it. I then introduce the 
implementation classes, followed by what occurs when the adapter executes in the 
container. Finally, I discuss the lessons learned from creating the sample 
adapter. 
<P><STRONG>Note:</STRONG> To download the <CODE>myjca.rar</CODE> source code 
that accompanies this article, see <A 
href="http://www.javaworld.com/javaworld/jw-02-2002/jw-0201-jca2_p.html#resources">Resources</A>. 

<P><FONT size=+1><STRONG>What the sample JCA adapter implementation does (and 
doesn't) contain </STRONG></FONT><BR>First, it's important to frame the 
sample-adapter discussion by describing its functionality. This article's sample 
adapter doesn't actually hook up to an enterprise system; it merely implements 
the interfaces required to deploy the adapter and look up a connection. 
<P>Moreover, the sample adapter implements only those classes required for the 
JCA specification's connection management section. Further, most of the 
adapter's method implementations contain print statements that let you see the 
method calls' order, without hooking up a debugger. The sample adapter does not, 
however, address transaction and security contacts. 
<P>This article's sample adapter does not use the CCI (Common Client Interface) 
interfaces. It strictly demonstrates the classes necessary to connect to an 
enterprise system. 
<P><FONT size=+1><STRONG>What you need to deploy the sample adapter 
</STRONG></FONT><BR>To use the adapter (or any JCA adapter for that matter), you 
need a J2EE application server with JCA 1.0 specification support. I used BEA's 
WebLogic 6.1 server to test the sample adapter; however, other application 
servers should work. 
<P>To deploy the sample adapter with BEA's WebLogic 6.1, you must: 
<OL>
  <LI>Navigate to the connector tree in the Administration Console 
  <LI>Click on the "Install new connector" link 
  <LI>Navigate to the <A 
  href="http://www.javaworld.com/javaworld/jw-02-2002/jw-0201-jca2_p.html#resources"><CODE>myjca.rar</CODE></A> 
  file, then upload it </LI></OL>
<P>In a later section, you'll find instructions on how to compile and build 
<CODE>myjca.rar</CODE>. 
<P><FONT size=+1><STRONG>The sample class files </STRONG></FONT><BR>Next, let's 
delve into the Java classes required to implement the sample JCA adapter. The 
adapter includes two class categories: 
<P>
<UL>
  <LI><STRONG>Managed classes:</STRONG> The application server calls managed 
  classes to perform the connection management. They're needed only if the 
  application server is managing the connection (via a connection pool), a 
  likely situation. 
  <LI><STRONG>Physical connection classes:</STRONG> These required classes, 
  which the aforementioned managed classes may call, establish the connection to 
  the EIS (enterprise information systems). </LI></UL>
<P><STRONG>MyManagedConnectionFactory </STRONG><BR>With the 
<CODE>MyManagedConnectionFactory</CODE> class, which implements the 
<CODE>ManagedConnectionFacytory</CODE> interface, you create the 
<CODE>MyConnectionFactory</CODE> and <CODE>MyManagedConnection</CODE> classes. 
The <CODE>MyManagedConnectionFactory</CODE> class acts as the main entry point 
for the application server to call into the adapter: 
<P><CODE>
<P><BR>package myjca;<BR><BR>import java.io.PrintWriter;<BR>import 
java.io.Serializable;<BR>import java.sql.DriverManager;<BR>import 
java.util.Iterator;<BR>import java.util.Set;<BR>import 
javax.resource.ResourceException;<BR>import javax.resource.spi.*;<BR>import 
javax.security.auth.Subject;<BR><BR>public class 
MyManagedConnectionFactory<BR>&nbsp;&nbsp;&nbsp;&nbsp;implements 
ManagedConnectionFactory, 
Serializable<BR>{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public 
MyManagedConnectionFactory() 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnectionFactory.constructor");<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public 
Object createConnectionFactory(ConnectionManager cxManager) throws 
ResourceException 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnectionFactory.createConnectionFactory,1");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
new MyDataSource(this, 
cxManager);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public 
Object createConnectionFactory() throws ResourceException 
{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnectionFactory.createManagedFactory,2");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
new MyDataSource(this, 
null);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public 
ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo 
info) 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnectionFactory.createManagedConnection");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
new MyManagedConnection(this, 
"test");<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public 
ManagedConnection matchManagedConnections(Set connectionSet, Subject subject, 
ConnectionRequestInfo 
info)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws 
ResourceException<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnectionFactory.matchManagedConnections");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public void 
setLogWriter(PrintWriter out) throws ResourceException 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnectionFactory.setLogWriter");<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public 
PrintWriter getLogWriter() throws ResourceException 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnectionFactory.getLogWriter");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
DriverManager.getLogWriter();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public 
boolean equals(Object obj) 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(obj == 
null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
false;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(obj instanceof 
MyManagedConnectionFactory)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int 
hash1 = 
((MyManagedConnectionFactory)obj).hashCode();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int 
hash2 = 
hashCode();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
hash1 == 
hash2;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
false;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;public 
int 
hashCode()<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>}<BR></CODE>
<P><STRONG>MyManagedConnection </STRONG><BR>The <CODE>MyManagedConnection</CODE> 
class implements the <CODE>ManagedConnection</CODE> interface. 
<CODE>MyManagedConnection</CODE> encapsulates the adapter's physical connection, 
in this case the <CODE>MyConnection</CODE> class: 
<P><CODE>
<P><BR>package myjca;<BR><BR>import java.io.PrintWriter;<BR>import 
java.sql.Connection;<BR>import java.sql.SQLException;<BR>import 
java.util.*;<BR>import javax.resource.NotSupportedException;<BR>import 
javax.resource.ResourceException;<BR>import javax.resource.spi.*;<BR>import 
javax.security.auth.Subject;<BR>import 
javax.transaction.xa.XAResource;<BR><BR><BR>public class 
MyManagedConnection<BR>&nbsp;&nbsp;implements 
ManagedConnection<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp; private 
MyConnectionEventListener myListener;<BR>&nbsp;&nbsp;&nbsp;&nbsp; private String 
user;<BR>&nbsp;&nbsp;&nbsp;&nbsp; private ManagedConnectionFactory 
mcf;<BR>&nbsp;&nbsp;&nbsp;&nbsp; private PrintWriter 
logWriter;<BR>&nbsp;&nbsp;&nbsp;&nbsp; private boolean 
destroyed;<BR>&nbsp;&nbsp;&nbsp;&nbsp; private Set 
connectionSet;<BR><BR>&nbsp;&nbsp;MyManagedConnection(ManagedConnectionFactory 
mcf, String 
user)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection");<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.mcf = 
mcf;<BR>&nbsp;&nbsp;&nbsp;&nbsp;this.user = 
user;<BR>&nbsp;&nbsp;&nbsp;&nbsp;connectionSet = new 
HashSet();<BR>&nbsp;&nbsp;&nbsp;&nbsp;myListener = new 
MyConnectionEventListener(this);<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;private 
void throwResourceException(SQLException ex)<BR>&nbsp;&nbsp;&nbsp;&nbsp;throws 
ResourceException<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;ResourceException 
re = new ResourceException("SQLException: " 
+<BR>ex.getMessage());<BR>&nbsp;&nbsp;&nbsp;&nbsp;re.setLinkedException(ex);<BR>&nbsp;&nbsp;&nbsp;&nbsp;throw 
re;<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;public Object getConnection(Subject 
subject, 
ConnectionRequestInfo<BR>connectionRequestInfo)<BR>&nbsp;&nbsp;&nbsp;&nbsp;throws 
ResourceException<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.getConnection");<BR>&nbsp;&nbsp;&nbsp;&nbsp;MyConnection 
myCon = new 
MyConnection(this);<BR>&nbsp;&nbsp;&nbsp;&nbsp;addMyConnection(myCon);<BR>&nbsp;&nbsp;&nbsp;&nbsp;return 
myCon;<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;public void 
destroy()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.destroy");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;destroyed 
= true;<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;public void 
cleanup()<BR>&nbsp;&nbsp; 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.cleanup");<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;public void 
associateConnection(Object connection)<BR>&nbsp;&nbsp; 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.associateConnection");<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;public 
void addConnectionEventListener(ConnectionEventListener 
listener)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.addConnectionEventListener");<BR>&nbsp;&nbsp;&nbsp;&nbsp;myListener.addConnectorListener(listener);<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;public 
void removeConnectionEventListener(ConnectionEventListener 
listener)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.removeConnectionEventListener");<BR>&nbsp;&nbsp;&nbsp;&nbsp;myListener.removeConnectorListener(listener);<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;public 
XAResource getXAResource()<BR>&nbsp;&nbsp;&nbsp;&nbsp;throws 
ResourceException<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.getXAResource");<BR>&nbsp;&nbsp;&nbsp;&nbsp;return 
null;<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;public LocalTransaction 
getLocalTransaction()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
System.out.println("In 
MyManagedConnection.getLocalTransaction");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
null;<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;public ManagedConnectionMetaData 
getMetaData()<BR>&nbsp;&nbsp;&nbsp;&nbsp;throws 
ResourceException<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.getMetaData");<BR>&nbsp;&nbsp;&nbsp;&nbsp;return new 
MyConnectionMetaData(this);<BR>&nbsp;&nbsp;}<BR><BR><BR>&nbsp;&nbsp;public void 
setLogWriter(PrintWriter out)<BR>&nbsp;&nbsp;&nbsp;&nbsp;throws 
ResourceException<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.setLogWriter");<BR>&nbsp;&nbsp;&nbsp;&nbsp;logWriter = 
out;<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;public PrintWriter 
getLogWriter()<BR>&nbsp;&nbsp;&nbsp;&nbsp;throws 
ResourceException<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.getLogWriter");<BR>&nbsp;&nbsp;&nbsp;&nbsp;return 
logWriter;<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;Connection 
getMyConnection()<BR>&nbsp;&nbsp;&nbsp;&nbsp;throws 
ResourceException<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.getMyConnection");<BR>&nbsp;&nbsp;&nbsp;&nbsp;return 
null;<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;boolean 
isDestroyed()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.isDestroyed");<BR>&nbsp;&nbsp;&nbsp;&nbsp;return 
destroyed;<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;String 
getUserName()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.getUserName");<BR>&nbsp;&nbsp;&nbsp;&nbsp;return 
user;<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;void sendEvent(int eventType, 
Exception 
ex)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("In 
MyManagedConnection.sendEvent,1");<BR>&nbsp;&nbsp;&nbsp;&nbsp;myListener.sendEvent(eventType, 
ex, null);<BR>&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;void sendEvent(int eventType, 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -