001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jca.inflow;
023:
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import javax.management.MBeanServer;
028: import javax.management.ObjectName;
029: import javax.resource.ResourceException;
030: import javax.resource.spi.ActivationSpec;
031: import javax.resource.spi.BootstrapContext;
032: import javax.resource.spi.ResourceAdapter;
033: import javax.resource.spi.ResourceAdapterInternalException;
034: import javax.resource.spi.endpoint.MessageEndpoint;
035: import javax.resource.spi.endpoint.MessageEndpointFactory;
036: import javax.transaction.xa.XAResource;
037:
038: import org.jboss.mx.util.MBeanServerLocator;
039: import org.jboss.mx.util.ObjectNameFactory;
040:
041: import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
042:
043: /**
044: * A TestResourceAdapter.
045: *
046: * @author <a href="adrian@jboss.com">Adrian Brock</a>
047: * @version $Revision: 57211 $
048: */
049: public class TestResourceAdapter implements ResourceAdapter,
050: TestResourceAdapterMBean {
051: public static final ObjectName mbean = ObjectNameFactory
052: .create("jboss.test:test=TestResourceAdapter");
053:
054: BootstrapContext ctx;
055:
056: ConcurrentReaderHashMap endpoints = new ConcurrentReaderHashMap();
057:
058: public TestResourceAdapterInflowResults testInflow()
059: throws Exception {
060: TestResourceAdapterInflow test = new TestResourceAdapterInflow(
061: this );
062: return test.run();
063: }
064:
065: public TestResourceAdapterWorkManagerResults testWorkManager()
066: throws Exception {
067: TestResourceAdapterWorkManager test = new TestResourceAdapterWorkManager(
068: this );
069: return test.run();
070: }
071:
072: public TestResourceAdapterTimerResults testTimer() throws Exception {
073: TestResourceAdapterTimer test = new TestResourceAdapterTimer(
074: this );
075: return test.run();
076: }
077:
078: public TestResourceAdapterTxInflowResults testTxInflow()
079: throws Exception {
080: TestResourceAdapterTxInflow test = new TestResourceAdapterTxInflow(
081: this );
082: return test.run();
083: }
084:
085: public void endpointActivation(
086: MessageEndpointFactory endpointFactory, ActivationSpec spec)
087: throws ResourceException {
088: MessageEndpoint endpoint = endpointFactory.createEndpoint(null);
089: endpoints.put(spec, endpoint);
090: }
091:
092: public void endpointDeactivation(
093: MessageEndpointFactory endpointFactory, ActivationSpec spec) {
094: MessageEndpoint endpoint = (MessageEndpoint) endpoints
095: .remove(spec);
096: if (endpoint != null)
097: endpoint.release();
098: }
099:
100: public XAResource[] getXAResources(ActivationSpec[] specs)
101: throws ResourceException {
102: // TODO getXAResources
103: return null;
104: }
105:
106: public void start(BootstrapContext ctx)
107: throws ResourceAdapterInternalException {
108: this .ctx = ctx;
109:
110: try {
111: MBeanServer server = MBeanServerLocator.locateJBoss();
112: server.registerMBean(this , mbean);
113: } catch (Exception e) {
114: throw new ResourceAdapterInternalException(e);
115: }
116: }
117:
118: public void stop() {
119: try {
120: MBeanServer server = MBeanServerLocator.locateJBoss();
121: server.unregisterMBean(mbean);
122:
123: for (Iterator i = endpoints.entrySet().iterator(); i
124: .hasNext();) {
125: Map.Entry entry = (Map.Entry) i.next();
126: MessageEndpoint endpoint = (MessageEndpoint) entry
127: .getValue();
128: if (endpoint != null) {
129: endpoint.release();
130: i.remove();
131: }
132: }
133: } catch (Exception ignored) {
134: }
135: }
136:
137: MessageEndpoint getEndpoint(String name) throws Exception {
138: for (Iterator i = endpoints.entrySet().iterator(); i.hasNext();) {
139: Map.Entry entry = (Map.Entry) i.next();
140: TestActivationSpec spec = (TestActivationSpec) entry
141: .getKey();
142: if (name.equals(spec.getName()))
143: return (MessageEndpoint) entry.getValue();
144: }
145: throw new Exception("MessageEndpoint not found for name: "
146: + name);
147: }
148: }
|