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
36   * a single activation specification on a resource adapter
37   *
38   * @version $Revision: 1.4 $
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  
52      public JCAConnector() {
53      }
54  
55      public JCAConnector(BootstrapContext bootstrapContext, ResourceAdapter resourceAdapter) {
56          this.bootstrapContext = bootstrapContext;
57          this.resourceAdapter = resourceAdapter;
58      }
59  
60      public void afterPropertiesSet() throws Exception {
61          if (activationSpec == null) {
62              throw new IllegalArgumentException("activationSpec must be set");
63          }
64  
65          ResourceAdapter temp = activationSpec.getResourceAdapter();
66          if (temp == null && resourceAdapter != null) {
67              activationSpec.setResourceAdapter(resourceAdapter);
68          }
69          else if (resourceAdapter == null) {
70              resourceAdapter = activationSpec.getResourceAdapter();
71              if (resourceAdapter == null) {
72                  throw new IllegalArgumentException("resourceAdapter property must be set on the activationSpec object");
73              }
74          }
75          if (bootstrapContext == null) {
76              throw new IllegalArgumentException("bootstrapContext must be set");
77          }
78          if (endpointFactory == null) {
79              if (ref == null) {
80                  throw new IllegalArgumentException("either the endpointFactory or ref properties must be set");
81              }
82              if (transactionManager != null) {
83                  endpointFactory = new DefaultEndpointFactory(beanFactory, ref, transactionManager, getName());
84              } else {
85                  // TODO should we have some way of finding a ManagedConnection or other local transaction hook?
86                  endpointFactory = new DefaultEndpointFactory(beanFactory, ref);
87              }
88          }
89          log.info("Activating endpoint for activationSpec: " + activationSpec + " using endpointFactory: " + endpointFactory);
90          resourceAdapter.endpointActivation(endpointFactory, activationSpec);
91      }
92  
93      public void destroy() throws Exception {
94          if (resourceAdapter != null && activationSpec != null) {
95              resourceAdapter.endpointDeactivation(endpointFactory, activationSpec);
96          }
97      }
98  
99      // Properties
100     //-------------------------------------------------------------------------
101 
102     public String getName() {
103         return name;
104     }
105 
106     public void setBeanName(String name) {
107         this.name = name;
108     }
109 
110     public ActivationSpec getActivationSpec() {
111         return activationSpec;
112     }
113 
114     public void setActivationSpec(ActivationSpec activationSpec) {
115         this.activationSpec = activationSpec;
116     }
117 
118     /***
119      * Returns the name of the MessageListener POJO in Spring
120      */
121     public String getRef() {
122         return ref;
123     }
124 
125     /***
126      * Sets the name of the MessageListener POJO in Spring
127      */
128     public void setRef(String ref) {
129         this.ref = ref;
130     }
131 
132     public MessageEndpointFactory getEndpointFactory() {
133         return endpointFactory;
134     }
135 
136     public void setEndpointFactory(MessageEndpointFactory endpointFactory) {
137         this.endpointFactory = endpointFactory;
138     }
139 
140     public BootstrapContext getBootstrapContext() {
141         return bootstrapContext;
142     }
143 
144     public void setBootstrapContext(BootstrapContext bootstrapContext) {
145         this.bootstrapContext = bootstrapContext;
146     }
147 
148     public ResourceAdapter getResourceAdapter() {
149         return resourceAdapter;
150     }
151 
152     public void setResourceAdapter(ResourceAdapter resourceAdapter) {
153         this.resourceAdapter = resourceAdapter;
154     }
155 
156     public TransactionManager getTransactionManager() {
157         return transactionManager;
158     }
159 
160     public void setTransactionManager(TransactionManager transactionManager) {
161         this.transactionManager = transactionManager;
162     }
163 
164     public BeanFactory getBeanFactory() {
165         return beanFactory;
166     }
167 
168     public void setBeanFactory(BeanFactory beanFactory) {
169         this.beanFactory = beanFactory;
170     }
171 }