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.BeanFactory;
25 import org.springframework.beans.factory.BeanFactoryAware;
26 import org.springframework.beans.factory.DisposableBean;
27 import org.springframework.beans.factory.InitializingBean;
28 import org.springframework.beans.factory.config.BeanDefinition;
29 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
30
31 import javax.resource.spi.BootstrapContext;
32 import javax.resource.spi.ResourceAdapter;
33
34 /***
35 * Represents a base JCA container which has no dependency on Geronimo
36 * and requires a mandatory {@link BootstrapContext} and {@link ResourceAdapter}
37 * properties to be configured.
38 * <p/>
39 * Typically Spring users will use the {@link BootstrapContextFactoryBean} to create
40 * the {@link BootstrapContext} instance, with the work manager and transaction manager.
41 *
42 * @version $Revision: 1.1.1.1 $
43 */
44 public class JCAContainer implements InitializingBean, DisposableBean, BeanFactoryAware {
45 private static final transient Log log = LogFactory.getLog(JCAContainer.class);
46 private BootstrapContext bootstrapContext;
47 private ResourceAdapter resourceAdapter;
48 private BeanFactory beanFactory;
49 private boolean lazyLoad = false;
50
51 public JCAConnector addConnector() {
52 return new JCAConnector(getBootstrapContext(), getResourceAdapter());
53 }
54
55 public void afterPropertiesSet() throws Exception {
56 if (resourceAdapter == null) {
57 throw new IllegalArgumentException("resourceAdapter must be set");
58 }
59 if (bootstrapContext == null) {
60 if (bootstrapContext == null) {
61 throw new IllegalArgumentException("bootstrapContext must be set");
62 }
63 }
64 resourceAdapter.start(bootstrapContext);
65
66
67 if (beanFactory == null) {
68 throw new IllegalArgumentException("beanFactory should have been set by Spring");
69 }
70 else if (!lazyLoad && beanFactory instanceof BeanDefinitionRegistry) {
71 BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
72 String[] names = registry.getBeanDefinitionNames();
73 for (int i = 0; i < names.length; i++) {
74 String name = names[i];
75 BeanDefinition definition = registry.getBeanDefinition(name);
76 if (!definition.isAbstract()) {
77 beanFactory.getBean(name);
78 }
79 }
80 }
81
82 String version = null;
83 Package aPackage = Package.getPackage("org.jencks");
84 if (aPackage != null) {
85 version = aPackage.getImplementationVersion();
86 }
87
88 log.info("Jencks JCA Container (http://jencks.org/) has started running version: " + version);
89 }
90
91 public void destroy() throws Exception {
92 if (resourceAdapter != null) {
93 resourceAdapter.stop();
94 }
95 }
96
97
98
99 public BeanFactory getBeanFactory() {
100 return beanFactory;
101 }
102
103 public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
104 this.beanFactory = beanFactory;
105 }
106
107 public ResourceAdapter getResourceAdapter() {
108 return resourceAdapter;
109 }
110
111 public void setResourceAdapter(ResourceAdapter resourceAdapter) {
112 this.resourceAdapter = resourceAdapter;
113 }
114
115 public BootstrapContext getBootstrapContext() {
116 return bootstrapContext;
117 }
118
119 public void setBootstrapContext(BootstrapContext bootstrapContext) {
120 this.bootstrapContext = bootstrapContext;
121 }
122
123 public boolean isLazyLoad() {
124 return lazyLoad;
125 }
126
127 public void setLazyLoad(boolean lazyLoad) {
128 this.lazyLoad = lazyLoad;
129 }
130
131 }