KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jca > support > ResourceAdapterFactoryBean


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jca.support;
18
19 import javax.resource.ResourceException JavaDoc;
20 import javax.resource.spi.BootstrapContext JavaDoc;
21 import javax.resource.spi.ResourceAdapter JavaDoc;
22 import javax.resource.spi.XATerminator JavaDoc;
23 import javax.resource.spi.work.WorkManager JavaDoc;
24
25 import org.springframework.beans.BeanUtils;
26 import org.springframework.beans.factory.DisposableBean;
27 import org.springframework.beans.factory.FactoryBean;
28 import org.springframework.beans.factory.InitializingBean;
29 import org.springframework.util.Assert;
30
31 /**
32  * {@link org.springframework.beans.factory.FactoryBean} that bootstraps
33  * the specified JCA 1.5 {@link javax.resource.spi.ResourceAdapter},
34  * starting it with a local {@link javax.resource.spi.BootstrapContext}
35  * and exposing it for bean references. It will also stop the ResourceAdapter
36  * on context shutdown. This corresponds to 'non-managed' bootstrap in a
37  * local environment, according to the JCA 1.5 specification.
38  *
39  * <p>This is essentially an adapter for bean-style bootstrapping of a
40  * JCA ResourceAdapter, allowing the BootstrapContext or its elements
41  * (such as the JCA WorkManager) to be specified through bean properties.
42  *
43  * @author Juergen Hoeller
44  * @since 2.0.3
45  * @see #setResourceAdapter
46  * @see #setBootstrapContext
47  * @see #setWorkManager
48  * @see javax.resource.spi.ResourceAdapter#start(javax.resource.spi.BootstrapContext)
49  * @see javax.resource.spi.ResourceAdapter#stop()
50  */

51 public class ResourceAdapterFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
52
53     private ResourceAdapter JavaDoc resourceAdapter;
54
55     private BootstrapContext JavaDoc bootstrapContext;
56
57     private WorkManager JavaDoc workManager;
58
59     private XATerminator JavaDoc xaTerminator;
60
61
62     /**
63      * Specify the target JCA ResourceAdapter as class, to be instantiated
64      * with its default configuration.
65      * <p>Alternatively, specify a pre-configured ResourceAdapter instance
66      * through the "resourceAdapter" property.
67      * @see #setResourceAdapter
68      */

69     public void setResourceAdapterClass(Class JavaDoc resourceAdapterClass) {
70         Assert.isAssignable(ResourceAdapter JavaDoc.class, resourceAdapterClass);
71         this.resourceAdapter = (ResourceAdapter JavaDoc) BeanUtils.instantiateClass(resourceAdapterClass);
72     }
73
74     /**
75      * Specify the target JCA ResourceAdapter, passed in as configured instance
76      * which hasn't been started yet. This will typically happen as an
77      * inner bean definition, configuring the ResourceAdapter instance
78      * through its vendor-specific bean properties.
79      */

80     public void setResourceAdapter(ResourceAdapter JavaDoc resourceAdapter) {
81         this.resourceAdapter = resourceAdapter;
82     }
83
84     /**
85      * Specify the JCA BootstrapContext to use for starting the ResourceAdapter.
86      * <p>Alternatively, you can specify the individual parts (such as the
87      * JCA WorkManager) as individual references.
88      * @see #setWorkManager
89      * @see #setXaTerminator
90      */

91     public void setBootstrapContext(BootstrapContext JavaDoc bootstrapContext) {
92         this.bootstrapContext = bootstrapContext;
93     }
94
95     /**
96      * Specify the JCA WorkManager to use for bootstrapping the ResourceAdapter.
97      * @see #setBootstrapContext
98      */

99     public void setWorkManager(WorkManager JavaDoc workManager) {
100         this.workManager = workManager;
101     }
102
103     /**
104      * Specify the JCA XATerminator to use for bootstrapping the ResourceAdapter.
105      * @see #setBootstrapContext
106      */

107     public void setXaTerminator(XATerminator JavaDoc xaTerminator) {
108         this.xaTerminator = xaTerminator;
109     }
110
111
112     /**
113      * Builds the BootstrapContext and starts the ResourceAdapter with it.
114      * @see javax.resource.spi.ResourceAdapter#start(javax.resource.spi.BootstrapContext)
115      */

116     public void afterPropertiesSet() throws ResourceException JavaDoc {
117         if (this.resourceAdapter == null) {
118             throw new IllegalArgumentException JavaDoc("'resourceAdapter' or 'resourceAdapterClass' is required");
119         }
120         if (this.bootstrapContext == null) {
121             this.bootstrapContext = new SimpleBootstrapContext(this.workManager, this.xaTerminator);
122         }
123         this.resourceAdapter.start(this.bootstrapContext);
124     }
125
126
127     public Object JavaDoc getObject() {
128         return this.resourceAdapter;
129     }
130
131     public Class JavaDoc getObjectType() {
132         return (this.resourceAdapter != null ? this.resourceAdapter.getClass() : ResourceAdapter JavaDoc.class);
133     }
134
135     public boolean isSingleton() {
136         return true;
137     }
138
139
140     /**
141      * Stops the ResourceAdapter.
142      * @see javax.resource.spi.ResourceAdapter#stop()
143      */

144     public void destroy() {
145         this.resourceAdapter.stop();
146     }
147
148 }
149
Popular Tags