View Javadoc

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 javax.resource.spi.ActivationSpec;
21  import javax.resource.spi.BootstrapContext;
22  import javax.resource.spi.ResourceAdapter;
23  import javax.resource.spi.endpoint.MessageEndpointFactory;
24  import javax.transaction.TransactionManager;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.springframework.beans.factory.BeanFactory;
29  import org.springframework.beans.factory.BeanFactoryAware;
30  import org.springframework.beans.factory.BeanNameAware;
31  import org.springframework.beans.factory.DisposableBean;
32  import org.springframework.beans.factory.InitializingBean;
33  
34  /***
35   * Represents a connector in the JCA container - which represents a single
36   * activation specification on a resource adapter
37   * 
38   * @version $Revision: 1.6 $
39   */
40  public class JCAConnector implements InitializingBean, DisposableBean, BeanFactoryAware, BeanNameAware {
41      private static final transient Log log = LogFactory.getLog(JCAConnector.class);
42  
43      private ActivationSpec activationSpec;
44      private BootstrapContext bootstrapContext;
45      private MessageEndpointFactory endpointFactory;
46      private ResourceAdapter resourceAdapter;
47      private String ref;
48      private TransactionManager transactionManager;
49      private BeanFactory beanFactory;
50      private String name;
51      private JCAContainer jcaContainer;
52  
53      public JCAConnector() {
54      }
55  
56      public JCAConnector(BootstrapContext bootstrapContext, ResourceAdapter resourceAdapter) {
57          this.bootstrapContext = bootstrapContext;
58          this.resourceAdapter = resourceAdapter;
59      }
60  
61      public void afterPropertiesSet() throws Exception {
62          if (activationSpec == null) {
63              throw new IllegalArgumentException("activationSpec must be set");
64          }
65  
66          if (resourceAdapter == null) {
67              resourceAdapter = activationSpec.getResourceAdapter();
68          }
69          if (resourceAdapter == null && jcaContainer != null) {
70              resourceAdapter = jcaContainer.getResourceAdapter();
71          }
72          if (resourceAdapter == null) {
73              throw new IllegalArgumentException("resourceAdapter property must be set on the activationSpec object");
74          }
75          if (activationSpec.getResourceAdapter() == null) {
76              activationSpec.setResourceAdapter(resourceAdapter);
77          }
78  
79          if (bootstrapContext == null && jcaContainer != null) {
80              bootstrapContext = jcaContainer.getBootstrapContext();
81          }
82          if (bootstrapContext == null) {
83              throw new IllegalArgumentException("bootstrapContext must be set");
84          }
85          if (endpointFactory == null) {
86              if (ref == null) {
87                  throw new IllegalArgumentException("either the endpointFactory or ref properties must be set");
88              }
89              if (transactionManager != null) {
90                  endpointFactory = new DefaultEndpointFactory(beanFactory, ref, transactionManager, getName());
91              }
92              else {
93                  // TODO should we have some way of finding a ManagedConnection
94                  // or other local transaction hook?
95                  endpointFactory = new DefaultEndpointFactory(beanFactory, ref);
96              }
97          }
98          log.info("Activating endpoint for activationSpec: " + activationSpec + " using endpointFactory: " + endpointFactory);
99          resourceAdapter.endpointActivation(endpointFactory, activationSpec);
100     }
101 
102     public void destroy() throws Exception {
103         if (resourceAdapter != null && activationSpec != null) {
104             resourceAdapter.endpointDeactivation(endpointFactory, activationSpec);
105         }
106     }
107 
108     // Properties
109     // -------------------------------------------------------------------------
110 
111     public String getName() {
112         return name;
113     }
114 
115     public void setBeanName(String name) {
116         this.name = name;
117     }
118 
119     public ActivationSpec getActivationSpec() {
120         return activationSpec;
121     }
122 
123     public void setActivationSpec(ActivationSpec activationSpec) {
124         this.activationSpec = activationSpec;
125     }
126 
127     /***
128      * Returns the name of the MessageListener POJO in Spring
129      */
130     public String getRef() {
131         return ref;
132     }
133 
134     /***
135      * Sets the name of the MessageListener POJO in Spring
136      */
137     public void setRef(String ref) {
138         this.ref = ref;
139     }
140 
141     public MessageEndpointFactory getEndpointFactory() {
142         return endpointFactory;
143     }
144 
145     public void setEndpointFactory(MessageEndpointFactory endpointFactory) {
146         this.endpointFactory = endpointFactory;
147     }
148 
149     public BootstrapContext getBootstrapContext() {
150         return bootstrapContext;
151     }
152 
153     public void setBootstrapContext(BootstrapContext bootstrapContext) {
154         this.bootstrapContext = bootstrapContext;
155     }
156 
157     public ResourceAdapter getResourceAdapter() {
158         return resourceAdapter;
159     }
160 
161     public void setResourceAdapter(ResourceAdapter resourceAdapter) {
162         this.resourceAdapter = resourceAdapter;
163     }
164 
165     public TransactionManager getTransactionManager() {
166         return transactionManager;
167     }
168 
169     public void setTransactionManager(TransactionManager transactionManager) {
170         this.transactionManager = transactionManager;
171     }
172 
173     public BeanFactory getBeanFactory() {
174         return beanFactory;
175     }
176 
177     public void setBeanFactory(BeanFactory beanFactory) {
178         this.beanFactory = beanFactory;
179     }
180 
181     public JCAContainer getJcaContainer() {
182         return jcaContainer;
183     }
184 
185     public void setJcaContainer(JCAContainer jcaConnector) {
186         this.jcaContainer = jcaConnector;
187     }
188 }