1 /***
2 *
3 * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.jencks;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.jencks.factory.BootstrapContextFactoryBean;
23 import org.springframework.beans.BeansException;
24 import org.springframework.beans.factory.DisposableBean;
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.context.ApplicationContext;
27 import org.springframework.context.ApplicationContextAware;
28
29 import javax.resource.spi.BootstrapContext;
30 import javax.resource.spi.ResourceAdapter;
31
32 /***
33 * Represents a base JCA container which has no dependency on Geronimo
34 * and requires a mandatory {@link BootstrapContext} and {@link ResourceAdapter}
35 * properties to be configured.
36 * <p/>
37 * Typically Spring users will use the {@link BootstrapContextFactoryBean} to create
38 * the {@link BootstrapContext} instance, with the work manager and transaction manager.
39 *
40 * @version $Revision: 1.2 $
41 */
42 public class JCAContainer implements InitializingBean, DisposableBean, ApplicationContextAware {
43 private static final transient Log log = LogFactory.getLog(JCAContainer.class);
44 private BootstrapContext bootstrapContext;
45 private ResourceAdapter resourceAdapter;
46 private ApplicationContext applicationContext;
47 private boolean lazyLoad = false;
48
49 public JCAContainer() {
50 }
51
52 public JCAConnector addConnector() {
53 return new JCAConnector(getBootstrapContext(), getResourceAdapter());
54 }
55
56 public void afterPropertiesSet() throws Exception {
57 if (resourceAdapter == null) {
58 throw new IllegalArgumentException("resourceAdapter must be set");
59 }
60 if (bootstrapContext == null) {
61 if (bootstrapContext == null) {
62 throw new IllegalArgumentException("bootstrapContext must be set");
63 }
64 }
65 resourceAdapter.start(bootstrapContext);
66
67
68 if (!lazyLoad) {
69 if (applicationContext == null) {
70 throw new IllegalArgumentException("applicationContext should have been set by Spring");
71 }
72 applicationContext.getBeansOfType(JCAConnector.class);
73 }
74
75 String version = null;
76 Package aPackage = Package.getPackage("org.jencks");
77 if (aPackage != null) {
78 version = aPackage.getImplementationVersion();
79 }
80
81 log.info("Jencks JCA Container (http://jencks.org/) has started running version: " + version);
82 }
83
84 public void destroy() throws Exception {
85 if (resourceAdapter != null) {
86 resourceAdapter.stop();
87 }
88 }
89
90
91
92 public ApplicationContext getApplicationContext() {
93 return applicationContext;
94 }
95
96 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
97 this.applicationContext = applicationContext;
98 }
99
100 public ResourceAdapter getResourceAdapter() {
101 return resourceAdapter;
102 }
103
104 public void setResourceAdapter(ResourceAdapter resourceAdapter) {
105 this.resourceAdapter = resourceAdapter;
106 }
107
108 public BootstrapContext getBootstrapContext() {
109 return bootstrapContext;
110 }
111
112 public void setBootstrapContext(BootstrapContext bootstrapContext) {
113 this.bootstrapContext = bootstrapContext;
114 }
115
116 public boolean isLazyLoad() {
117 return lazyLoad;
118 }
119
120 public void setLazyLoad(boolean lazyLoad) {
121 this.lazyLoad = lazyLoad;
122 }
123
124 }