技术开发 频道

J2EE连接器开发实践之: J2EE连接器的开发

  DemoManagedConnectionFactory

  DemoManagedConnectionFactory是DemoManagedConnection的工厂,它的主要任务是创建和匹配DemoManagedConnection实例,在创建DemoManagedConnection实例时,DemoManagedConnection实例同时打开到EIS的物理连接。DemoManagedConnectionFactory的代码如例程8所示。

  例程8 DemoManagedConnectionFactory的代码

  在DemoManagedConnectionFactory类中,设置了两个属性,Port和Server,Port表示连接EIS使用的端口,Server表示连接EIS时使用的URL。这两个属性需要在资源适配器的部署描述符里指定具体的值。

 

  1 package com.hellking.jca;
  2 import javax.resource.*;
  3 import javax.resource.spi.*;
  4 import javax.security.auth.*;
  5 import java.util.Set;
  6 import java.util.Iterator;
  7 import java.io.*;
  8 public class DemoManagedConnectionFactory
  9     implements ManagedConnectionFactory, Serializable
10 {
11     protected PrintWriter out = new PrintWriter(System.out);
12     private int port;//连接EIS的端口
13     private String server;//eis服务器的url
14     
15     
16     //创建连接工厂,指定连接工厂的连接管理者为connectionManager
17     public Object createConnectionFactory
18          (ConnectionManager connectionManager)
19     {
20           DemoConnectionFactoryImpl demoConnectionFactoryImpl =
21             new DemoConnectionFactoryImpl (this, connectionManager);
22           demoConnectionFactoryImpl.setLogWriter(out);
23           return demoConnectionFactoryImpl;
24     }
25       
26     //创建连接工厂,没有指定连接管理者
27     public Object createConnectionFactory()
28     {
29           DemoConnectionFactoryImpl demoConnectionFactoryImpl =
30             new DemoConnectionFactoryImpl (this, null);
31           demoConnectionFactoryImpl.setLogWriter(out);
32           return demoConnectionFactoryImpl;
33       }
34     
35     //创建被管理的连接,被管理的连接是和EIS的真实连接,它是物理连接。
36     public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo cri)
37     throws ResourceException
38    {
39           DemoManagedConnection demoManagedConnection =
40 new DemoManagedConnection(this);
41           demoManagedConnection.setLogWriter(out);
42           try
43             {
44                 //打开物理连接
45                 System.out.println("打开物理连接.....");
46                 demoManagedConnection.openPhysicalConnection (server, port);
47                 return demoManagedConnection;
48             }
49           catch (IOException e)
50           {
51             throw new ResourceException (e.toString());
52           }
53       }
54     
55     //匹配被管理的连接,如果匹配到连接,则返回,否则返回null
56     public ManagedConnection matchManagedConnections
57         (Set connections, Subject subject, ConnectionRequestInfo cri)
58       {
59           Iterator it = connections.iterator();
60           while (it.hasNext())
61             {
62                 Object obj = it.next();
63                 if (obj instanceof DemoManagedConnection)
64                   {
65                   DemoManagedConnection demoManagedConnection =
66 (DemoManagedConnection) obj;
67                   DemoManagedConnectionFactory demoManagedConnectionf =
68 demoManagedConnection.getFactory();
69                   if (demoManagedConnectionf.equals(this))
70                   {
71                     return demoManagedConnection;
72                  }
73               }
74                 }
75           return null;
76       }    
77         
78     public int hashCode()
79     {
80           if (server == null) return 0;
81           return server.hashCode();
82     }
83     //判断两个被管理的连接工厂是否相等
84     public boolean equals(Object o)
85       {
86           if (o == null) return false;
87           if (!(o instanceof DemoManagedConnectionFactory))
88             return false;
89           DemoManagedConnectionFactory other =
90             (DemoManagedConnectionFactory)o;
91           if (server.equalsIgnoreCase(other.server) &&
92             port == other.port) return true;
93           return false;
94       }    
95       public void setLogWriter(java.io.PrintWriter out)
96      {
97           this.out = out;
98       }
99     
100     public PrintWriter getLogWriter()
101     {
102           return out;
103      }
104     
105     public void setServer (String server)
106     {
107           this.server = server;
108     }
109     
110     public String getServer ()
111       {
112           return server;
113       }
114     
115     public void setPort (Integer port)
116     {
117           this.port = port.intValue();
118     }
119     
120     public Integer getPort ()
121     {
122           return new Integer(port);
123     }
124 }
125
0
相关文章