001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson;
028: */
029:
030: package com.caucho.jca.program;
031:
032: import com.caucho.config.*;
033: import com.caucho.config.program.*;
034: import com.caucho.jca.*;
035: import com.caucho.util.*;
036: import com.caucho.webbeans.*;
037: import com.caucho.webbeans.component.*;
038: import com.caucho.webbeans.manager.*;
039:
040: import javax.webbeans.*;
041: import javax.resource.spi.*;
042:
043: /**
044: * Program to associate a resource adapter
045: */
046: public class ResourceAdapterAssociationProgram extends ConfigProgram {
047: private static final L10N L = new L10N(
048: ResourceAdapterAssociationProgram.class);
049:
050: private final ResourceAdapterController _raController;
051:
052: /**
053: * Creates a resource adapter program based on the class.
054: */
055: public ResourceAdapterAssociationProgram(Class cl) {
056: ResourceArchive ra = ResourceArchiveManager
057: .findResourceArchive(cl.getName());
058:
059: if (ra == null) {
060: throw new ConfigException(
061: L
062: .l(
063: "'{0}' does not have a defined resource-adapter. Check the rar or META-INF/resin-ra.xml files",
064: cl.getName()));
065: }
066:
067: WebBeansContainer webBeans = WebBeansContainer.create();
068:
069: ComponentFactory<ResourceAdapterController> comp = webBeans
070: .resolveByType(ResourceAdapterController.class, Names
071: .create(ra.getResourceAdapterClass().getName()));
072:
073: if (comp == null) {
074: throw new ConfigException(
075: L
076: .l(
077: "'{0}' does not have a configured resource-adapter for '{1}'.",
078: ra.getResourceAdapterClass()
079: .getName(), cl.getName()));
080: }
081:
082: _raController = (ResourceAdapterController) comp.get();
083: }
084:
085: /**
086: * Configures the bean using the current program.
087: *
088: * @param bean the bean to configure
089: * @param env the Config environment
090: */
091: public void inject(Object bean, ConfigContext env) {
092: try {
093: ResourceAdapterAssociation association = (ResourceAdapterAssociation) bean;
094:
095: association.setResourceAdapter(_raController
096: .getResourceAdapter());
097: } catch (Exception e) {
098: throw ConfigException.create(e);
099: }
100: }
101: }
|