001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.connector.outbound;
017:
018: import java.lang.reflect.Constructor;
019: import java.util.HashMap;
020: import java.util.LinkedHashSet;
021: import java.util.Map;
022:
023: import javax.resource.ResourceException;
024: import javax.resource.spi.ManagedConnectionFactory;
025: import javax.resource.spi.ResourceAdapterAssociation;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.geronimo.connector.ResourceAdapterWrapper;
030: import org.apache.geronimo.gbean.AbstractName;
031: import org.apache.geronimo.gbean.DynamicGBean;
032: import org.apache.geronimo.gbean.DynamicGBeanDelegate;
033: import org.apache.geronimo.gbean.GBeanLifecycle;
034: import org.apache.geronimo.naming.ResourceSource;
035: import org.apache.geronimo.kernel.Kernel;
036: import org.apache.geronimo.management.geronimo.JCAManagedConnectionFactory;
037:
038: /**
039: * @version $Rev: 607943 $ $Date: 2008-01-01 15:07:17 -0800 (Tue, 01 Jan 2008) $
040: */
041: public class ManagedConnectionFactoryWrapper implements GBeanLifecycle,
042: DynamicGBean, JCAManagedConnectionFactory,
043: ResourceSource<ResourceException> {
044:
045: private static final Log log = LogFactory
046: .getLog(ManagedConnectionFactoryWrapper.class);
047:
048: private final String managedConnectionFactoryClass;
049: private final String connectionFactoryInterface;
050: private final String[] implementedInterfaces;
051: private final String connectionFactoryImplClass;
052: private final String connectionInterface;
053: private final String connectionImplClass;
054:
055: private final LinkedHashSet<Class> allImplementedInterfaces = new LinkedHashSet<Class>();
056:
057: private final ResourceAdapterWrapper resourceAdapterWrapper;
058: private final ConnectionManagerContainer connectionManagerContainer;
059:
060: private ManagedConnectionFactory managedConnectionFactory;
061:
062: private DynamicGBeanDelegate delegate;
063:
064: private boolean registered = false;
065: private final Kernel kernel;
066: private final AbstractName abstractName;
067: private final String objectName;
068: private final ClassLoader classLoader;
069:
070: //default constructor for enhancement proxy endpoint
071: public ManagedConnectionFactoryWrapper() {
072: managedConnectionFactoryClass = null;
073: connectionFactoryInterface = null;
074: implementedInterfaces = null;
075: connectionFactoryImplClass = null;
076: connectionInterface = null;
077: connectionImplClass = null;
078: kernel = null;
079: abstractName = null;
080: objectName = null;
081: classLoader = null;
082: resourceAdapterWrapper = null;
083: connectionManagerContainer = null;
084: }
085:
086: public ManagedConnectionFactoryWrapper(
087: String managedConnectionFactoryClass,
088: String connectionFactoryInterface,
089: String[] implementedInterfaces,
090: String connectionFactoryImplClass,
091: String connectionInterface, String connectionImplClass,
092: ResourceAdapterWrapper resourceAdapterWrapper,
093: ConnectionManagerContainer connectionManagerContainer,
094: Kernel kernel, AbstractName abstractName,
095: String objectName, ClassLoader cl)
096: throws InstantiationException, IllegalAccessException,
097: ClassNotFoundException {
098: this .managedConnectionFactoryClass = managedConnectionFactoryClass;
099: this .connectionFactoryInterface = connectionFactoryInterface;
100: this .implementedInterfaces = implementedInterfaces;
101: this .connectionFactoryImplClass = connectionFactoryImplClass;
102: this .connectionInterface = connectionInterface;
103: this .connectionImplClass = connectionImplClass;
104:
105: allImplementedInterfaces.add(cl
106: .loadClass(connectionFactoryInterface));
107: for (String interfaceName : implementedInterfaces) {
108: allImplementedInterfaces.add(cl.loadClass(interfaceName));
109: }
110:
111: this .resourceAdapterWrapper = resourceAdapterWrapper;
112: this .connectionManagerContainer = connectionManagerContainer;
113:
114: //set up that must be done before start
115: classLoader = cl;
116: Class clazz = cl.loadClass(managedConnectionFactoryClass);
117: managedConnectionFactory = (ManagedConnectionFactory) clazz
118: .newInstance();
119: delegate = new DynamicGBeanDelegate();
120: delegate.addAll(managedConnectionFactory);
121: this .kernel = kernel;
122: this .abstractName = abstractName;
123: this .objectName = objectName;
124: }
125:
126: public String getManagedConnectionFactoryClass() {
127: return managedConnectionFactoryClass;
128: }
129:
130: public String getConnectionFactoryInterface() {
131: return connectionFactoryInterface;
132: }
133:
134: public String[] getImplementedInterfaces() {
135: return implementedInterfaces;
136: }
137:
138: public String getConnectionFactoryImplClass() {
139: return connectionFactoryImplClass;
140: }
141:
142: public String getConnectionInterface() {
143: return connectionInterface;
144: }
145:
146: public String getConnectionImplClass() {
147: return connectionImplClass;
148: }
149:
150: public ResourceAdapterWrapper getResourceAdapterWrapper() {
151: return resourceAdapterWrapper;
152: }
153:
154: public Object getConnectionManagerContainer() {
155: return connectionManagerContainer;
156: }
157:
158: public void doStart() throws Exception {
159: //register with resource adapter if not yet done
160: if (!registered
161: && (managedConnectionFactory instanceof ResourceAdapterAssociation)) {
162: if (resourceAdapterWrapper == null) {
163: throw new IllegalStateException(
164: "Managed connection factory expects to be registered with a ResourceAdapter, but there is no ResourceAdapter");
165: }
166: resourceAdapterWrapper
167: .registerResourceAdapterAssociation((ResourceAdapterAssociation) managedConnectionFactory);
168: registered = true;
169: log
170: .debug("Registered managedConnectionFactory with ResourceAdapter "
171: + resourceAdapterWrapper.toString());
172: }
173: connectionManagerContainer.doRecovery(managedConnectionFactory);
174: }
175:
176: public void doStop() {
177: }
178:
179: public void doFail() {
180: doStop();
181: }
182:
183: //DynamicGBean implementation
184: public Object getAttribute(String name) throws Exception {
185: Thread thread = Thread.currentThread();
186: ClassLoader oldTCL = thread.getContextClassLoader();
187: thread.setContextClassLoader(classLoader);
188: try {
189: return delegate.getAttribute(name);
190: } finally {
191: thread.setContextClassLoader(oldTCL);
192: }
193: }
194:
195: public void setAttribute(String name, Object value)
196: throws Exception {
197: Thread thread = Thread.currentThread();
198: ClassLoader oldTCL = thread.getContextClassLoader();
199: thread.setContextClassLoader(classLoader);
200: try {
201: delegate.setAttribute(name, value);
202: } finally {
203: thread.setContextClassLoader(oldTCL);
204: }
205: }
206:
207: public Object invoke(String name, Object[] arguments, String[] types)
208: throws Exception {
209: //we have no dynamic operations.
210: return null;
211: }
212:
213: public Object getConnectionFactory() throws ResourceException {
214: return $getConnectionFactory();
215: }
216:
217: public Object $getResource() throws ResourceException {
218: return $getConnectionFactory();
219: }
220:
221: public Object $getConnectionFactory() throws ResourceException {
222: Object connectionFactory = connectionManagerContainer
223: .createConnectionFactory(managedConnectionFactory);
224: for (Class intf : allImplementedInterfaces) {
225: if (!intf.isAssignableFrom(connectionFactory.getClass())) {
226: throw new ResourceException(
227: "ConnectionFactory does not implement expected interface: "
228: + intf.getName());
229: }
230: }
231: return connectionFactory;
232: }
233:
234: public ManagedConnectionFactory $getManagedConnectionFactory() {
235: return managedConnectionFactory;
236: }
237:
238: /**
239: * Gets the config properties in the form of a map where the key is the
240: * property name and the value is property type (as a Class).
241: */
242: public Map<String, Class> getConfigProperties() {
243: String[] props = delegate.getProperties();
244: Map<String, Class> map = new HashMap<String, Class>();
245: for (String prop : props) {
246: if (prop.equals("logWriter")) {
247: continue;
248: }
249: map.put(prop, delegate.getPropertyType(prop));
250: }
251: return map;
252: }
253:
254: public void setConfigProperty(String property, Object value)
255: throws Exception {
256: Class cls = delegate.getPropertyType(property);
257: if (value != null && value instanceof String
258: && !cls.getName().equals("java.lang.String")) {
259: if (cls.isPrimitive()) {
260: if (cls.equals(int.class)) {
261: cls = Integer.class;
262: } else if (cls.equals(boolean.class)) {
263: cls = Boolean.class;
264: } else if (cls.equals(float.class)) {
265: cls = Float.class;
266: } else if (cls.equals(double.class)) {
267: cls = Double.class;
268: } else if (cls.equals(long.class)) {
269: cls = Long.class;
270: } else if (cls.equals(short.class)) {
271: cls = Short.class;
272: } else if (cls.equals(byte.class)) {
273: cls = Byte.class;
274: } else if (cls.equals(char.class)) {
275: cls = Character.class;
276: }
277: }
278: Constructor con = cls
279: .getConstructor(new Class[] { String.class });
280: value = con.newInstance(new Object[] { value });
281: }
282: kernel.setAttribute(abstractName, property, value);
283: }
284:
285: public Object getConfigProperty(String property) throws Exception {
286: return delegate.getAttribute(property);
287: }
288:
289: public String getObjectName() {
290: return objectName;
291: }
292:
293: public boolean isStateManageable() {
294: return false;
295: }
296:
297: public boolean isStatisticsProvider() {
298: return false;
299: }
300:
301: public boolean isEventProvider() {
302: return false;
303: }
304: }
|