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.cfg;
031:
032: import com.caucho.config.program.ConfigProgram;
033: import com.caucho.config.Config;
034: import com.caucho.config.ConfigException;
035: import com.caucho.config.program.ContainerProgram;
036: import com.caucho.config.types.*;
037: import com.caucho.jca.*;
038: import com.caucho.jca.cfg.JavaMailConfig;
039: import com.caucho.jmx.IntrospectionMBean;
040: import com.caucho.jmx.Jmx;
041: import com.caucho.loader.ClassLoaderListener;
042: import com.caucho.loader.CloseListener;
043: import com.caucho.loader.Environment;
044: import com.caucho.loader.EnvironmentListener;
045: import com.caucho.loader.StartListener;
046: import com.caucho.naming.Jndi;
047: import com.caucho.util.CharBuffer;
048: import com.caucho.util.L10N;
049: import com.caucho.webbeans.*;
050: import com.caucho.webbeans.component.*;
051: import com.caucho.webbeans.manager.*;
052:
053: import javax.annotation.PostConstruct;
054: import javax.management.Attribute;
055: import javax.management.MBeanAttributeInfo;
056: import javax.management.MBeanInfo;
057: import javax.management.MBeanServer;
058: import javax.management.NotificationFilter;
059: import javax.management.ObjectName;
060: import javax.naming.Context;
061: import javax.naming.InitialContext;
062: import javax.resource.spi.*;
063: import javax.webbeans.*;
064: import java.lang.reflect.Constructor;
065: import java.lang.reflect.Method;
066: import java.util.ArrayList;
067: import java.util.logging.Level;
068: import java.util.logging.Logger;
069:
070: /**
071: * Configuration for the connection-factory pattern.
072: */
073: public class ConnectionFactoryConfig extends BeanConfig {
074: private static final Logger log = Logger
075: .getLogger(ConnectionFactoryConfig.class.getName());
076:
077: private static L10N L = new L10N(ConnectionFactoryConfig.class);
078:
079: private ResourceAdapter _ra;
080:
081: public ConnectionFactoryConfig() {
082: setBeanConfigClass(ManagedConnectionFactory.class);
083: }
084:
085: @Override
086: protected String getDefaultScope() {
087: return null;
088: }
089:
090: public void setResourceAdapter(ResourceAdapter ra) {
091: _ra = ra;
092: }
093:
094: public void init() {
095: super .init();
096:
097: ComponentImpl comp = getComponent();
098:
099: ManagedConnectionFactory managedFactory = (ManagedConnectionFactory) comp
100: .get();
101:
102: if (managedFactory instanceof ResourceAdapterAssociation) {
103: Class cl = managedFactory.getClass();
104:
105: ResourceAdapter ra = findResourceAdapter(cl);
106:
107: ResourceAdapterAssociation factoryAssoc = (ResourceAdapterAssociation) managedFactory;
108:
109: try {
110: factoryAssoc.setResourceAdapter(ra);
111: } catch (Exception e) {
112: throw ConfigException.create(e);
113: }
114: }
115:
116: ResourceManagerImpl rm = ResourceManagerImpl.create();
117:
118: ConnectionPool cm = rm.createConnectionPool();
119:
120: if (getName() != null)
121: cm.setName(getName());
122:
123: ResourceArchive rar = null;
124:
125: if (rar != null) {
126: String trans = rar.getTransactionSupport();
127:
128: if (trans == null) { // guess XA
129: cm.setXATransaction(true);
130: cm.setLocalTransaction(true);
131: } else if (trans.equals("XATransaction")) {
132: cm.setXATransaction(true);
133: cm.setLocalTransaction(true);
134: } else if (trans.equals("NoTransaction")) {
135: cm.setXATransaction(false);
136: cm.setLocalTransaction(false);
137: } else if (trans.equals("LocalTransaction")) {
138: cm.setXATransaction(false);
139: cm.setLocalTransaction(true);
140: }
141: }
142: /*
143: cm.setLocalTransactionOptimization(getLocalTransactionOptimization());
144: cm.setShareable(getShareable());
145: */
146:
147: Object connectionFactory;
148:
149: try {
150: connectionFactory = cm.init(managedFactory);
151: cm.start();
152:
153: WebBeansContainer webBeans = WebBeansContainer.create();
154:
155: if (getName() != null) {
156: Jndi.bindDeepShort(getName(), connectionFactory);
157:
158: webBeans.addSingleton(connectionFactory, getName());
159: } else
160: webBeans.addSingleton(connectionFactory);
161: } catch (Exception e) {
162: throw ConfigException.create(e);
163: }
164: }
165:
166: private ResourceAdapter findResourceAdapter(Class cl) {
167: if (_ra != null)
168: return _ra;
169:
170: ResourceArchive ra = ResourceArchiveManager
171: .findResourceArchive(cl.getName());
172:
173: if (ra == null) {
174: throw new ConfigException(
175: L
176: .l(
177: "'{0}' does not have a defined resource-adapter. Either define it in a <resource-adapter> property or check the rar or META-INF/resin-ra.xml files",
178: cl.getName()));
179: }
180:
181: WebBeansContainer webBeans = WebBeansContainer.create();
182:
183: ComponentFactory<ResourceAdapterController> raComp = webBeans
184: .resolveByType(ResourceAdapterController.class, Names
185: .create(ra.getResourceAdapterClass().getName()));
186:
187: if (raComp == null) {
188: throw new ConfigException(
189: L
190: .l(
191: "'{0}' does not have a configured resource-adapter for '{1}'.",
192: ra.getResourceAdapterClass()
193: .getName(), cl.getName()));
194: }
195:
196: ResourceAdapterController raController = (ResourceAdapterController) raComp
197: .get();
198:
199: return raController.getResourceAdapter();
200: }
201:
202: @Override
203: public void deploy() {
204: }
205: }
|