001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: MDBResourceAdapterHelper.java 2080 2007-12-04 11:22:57Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.container.mdb.helper;
025:
026: import javax.resource.ResourceException;
027: import javax.resource.spi.ResourceAdapter;
028:
029: import org.ow2.easybeans.component.api.EZBComponent;
030: import org.ow2.easybeans.component.itf.JMSComponent;
031: import org.ow2.easybeans.server.Embedded;
032:
033: /**
034: * This class allow to get the ResourceAdapter object for a given destination
035: * (activation-spec object).
036: * @author Florent Benoit
037: */
038: public final class MDBResourceAdapterHelper {
039:
040: /**
041: * Implementation of a finder.
042: */
043: private static IResourceAdapterFinder resourceAdapterFinder = null;
044:
045: /**
046: * Utility class, no public constructor.
047: */
048: private MDBResourceAdapterHelper() {
049:
050: }
051:
052: /**
053: * Gets the resource adapter object for the given jndi name (activation
054: * spec) and the given embedded object.
055: * @param jndiName the nameof the activation spec bound in the registry
056: * @param embedded the embedded server
057: * @return an instance of the resource adapter that provides the MDB
058: * activation spec.
059: * @throws ResourceException if an error occurs while trying to get the
060: * resource adapter.
061: */
062: public static ResourceAdapter getResourceAdapter(
063: final String jndiName, final Embedded embedded)
064: throws ResourceException {
065:
066: // Use the delegate if set
067: if (resourceAdapterFinder != null) {
068: return resourceAdapterFinder.getResourceAdapter(jndiName);
069: }
070:
071: // try to see if JMS mini-resource adapter service was started.
072: EZBComponent component = embedded
073: .getComponent("org.ow2.easybeans.component.joram.JoramComponent");
074: if (component != null) {
075: // get ResourceAdapter from the service
076: if (component instanceof JMSComponent) {
077: return ((JMSComponent) component).getResourceAdapter();
078: }
079: throw new IllegalArgumentException(
080: "The 'jms' component doesn't implement JMSComponent interface.");
081: }
082:
083: throw new ResourceException(
084: "MDB is used but no resource service was started.");
085:
086: }
087:
088: /**
089: * Sets the finder object that can be used to get the resource adapter object.
090: * @param finder the given instance.
091: */
092: public static void setResourceAdapterFinder(
093: final IResourceAdapterFinder finder) {
094: if (resourceAdapterFinder != null) {
095: throw new IllegalStateException(
096: "Unable to set the finder. It is already set");
097: }
098: resourceAdapterFinder = finder;
099: }
100:
101: }
|