KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > jca > inflow > TestResourceAdapter


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.jca.inflow;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.resource.ResourceException JavaDoc;
30 import javax.resource.spi.ActivationSpec JavaDoc;
31 import javax.resource.spi.BootstrapContext JavaDoc;
32 import javax.resource.spi.ResourceAdapter JavaDoc;
33 import javax.resource.spi.ResourceAdapterInternalException JavaDoc;
34 import javax.resource.spi.endpoint.MessageEndpoint JavaDoc;
35 import javax.resource.spi.endpoint.MessageEndpointFactory JavaDoc;
36 import javax.transaction.xa.XAResource JavaDoc;
37
38 import org.jboss.mx.util.MBeanServerLocator;
39 import org.jboss.mx.util.ObjectNameFactory;
40
41 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
42
43 /**
44  * A TestResourceAdapter.
45  *
46  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
47  * @version $Revision: 42598 $
48  */

49 public class TestResourceAdapter implements ResourceAdapter JavaDoc, TestResourceAdapterMBean
50 {
51    public static final ObjectName JavaDoc mbean = ObjectNameFactory.create("jboss.test:test=TestResourceAdapter");
52    
53    BootstrapContext JavaDoc ctx;
54
55    ConcurrentReaderHashMap endpoints = new ConcurrentReaderHashMap();
56    
57    public TestResourceAdapterInflowResults testInflow() throws Exception JavaDoc
58    {
59       TestResourceAdapterInflow test = new TestResourceAdapterInflow(this);
60       return test.run();
61    }
62    
63    public TestResourceAdapterWorkManagerResults testWorkManager() throws Exception JavaDoc
64    {
65       TestResourceAdapterWorkManager test = new TestResourceAdapterWorkManager(this);
66       return test.run();
67    }
68    
69    public TestResourceAdapterTimerResults testTimer() throws Exception JavaDoc
70    {
71       TestResourceAdapterTimer test = new TestResourceAdapterTimer(this);
72       return test.run();
73    }
74    
75    public TestResourceAdapterTxInflowResults testTxInflow() throws Exception JavaDoc
76    {
77       TestResourceAdapterTxInflow test = new TestResourceAdapterTxInflow(this);
78       return test.run();
79    }
80    
81    public void endpointActivation(MessageEndpointFactory JavaDoc endpointFactory, ActivationSpec JavaDoc spec) throws ResourceException JavaDoc
82    {
83       MessageEndpoint JavaDoc endpoint = endpointFactory.createEndpoint(null);
84       endpoints.put(spec, endpoint);
85    }
86
87    public void endpointDeactivation(MessageEndpointFactory JavaDoc endpointFactory, ActivationSpec JavaDoc spec)
88    {
89       MessageEndpoint JavaDoc endpoint = (MessageEndpoint JavaDoc) endpoints.remove(spec);
90       if (endpoint != null)
91          endpoint.release();
92    }
93    
94    public XAResource JavaDoc[] getXAResources(ActivationSpec JavaDoc[] specs) throws ResourceException JavaDoc
95    {
96       // TODO getXAResources
97
return null;
98    }
99    
100    public void start(BootstrapContext JavaDoc ctx) throws ResourceAdapterInternalException JavaDoc
101    {
102       this.ctx = ctx;
103
104       try
105       {
106          MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
107          server.registerMBean(this, mbean);
108       }
109       catch (Exception JavaDoc e)
110       {
111          throw new ResourceAdapterInternalException JavaDoc(e);
112       }
113    }
114
115    public void stop()
116    {
117       try
118       {
119          MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
120          server.unregisterMBean(mbean);
121          
122          for (Iterator JavaDoc i = endpoints.entrySet().iterator(); i.hasNext();)
123          {
124             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
125             MessageEndpoint JavaDoc endpoint = (MessageEndpoint JavaDoc) entry.getValue();
126             if (endpoint != null)
127             {
128                endpoint.release();
129                i.remove();
130             }
131          }
132       }
133       catch (Exception JavaDoc ignored)
134       {
135       }
136    }
137    
138    MessageEndpoint JavaDoc getEndpoint(String JavaDoc name) throws Exception JavaDoc
139    {
140       for (Iterator JavaDoc i = endpoints.entrySet().iterator(); i.hasNext();)
141       {
142          Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
143          TestActivationSpec spec = (TestActivationSpec) entry.getKey();
144          if (name.equals(spec.getName()))
145             return (MessageEndpoint JavaDoc) entry.getValue();
146       }
147       throw new Exception JavaDoc("MessageEndpoint not found for name: " + name);
148    }
149 }
150
Popular Tags