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