1 /**
2 *
3 * Copyright 2004-2005 The Apache Software Foundation
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.apache.geronimo.connector;
19
20 import javax.resource.ResourceException;
21 import javax.resource.spi.ActivationSpec;
22 import javax.resource.spi.ResourceAdapter;
23 import javax.resource.spi.endpoint.MessageEndpointFactory;
24 import javax.transaction.SystemException;
25 import javax.transaction.xa.XAResource;
26
27 import org.apache.geronimo.transaction.manager.NamedXAResource;
28 import org.apache.geronimo.transaction.manager.ResourceManager;
29 import org.apache.geronimo.transaction.manager.WrapperNamedXAResource;
30
31 /**
32 * Wrapper for ActivationSpec instances.
33 * The framework assumes all RequiredConfigProperties are of type String, although it
34 * is unclear if this is required by the spec.
35 *
36 * @version $Rev: 356022 $ $Date: 2005-12-11 12:58:34 -0800 (Sun, 11 Dec 2005) $
37 */
38 public class ActivationSpecWrapper implements ResourceManager {
39
40 protected final ActivationSpec activationSpec;
41
42 private final ResourceAdapterWrapper resourceAdapterWrapper;
43 private final String containerId;
44
45 /**
46 * Default constructor required when a class is used as a GBean Endpoint.
47 */
48 public ActivationSpecWrapper() {
49 activationSpec = null;
50 containerId = null;
51 resourceAdapterWrapper = null;
52 }
53
54 /**
55 * Normal managed constructor.
56 *
57 * @param activationSpecClass Class of admin object to be wrapped.
58 * @throws IllegalAccessException
59 * @throws InstantiationException
60 */
61 public ActivationSpecWrapper(final String activationSpecClass,
62 final String containerId,
63 final ResourceAdapterWrapper resourceAdapterWrapper,
64 final ClassLoader cl) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
65 Class clazz = cl.loadClass(activationSpecClass);
66 this.activationSpec = (ActivationSpec) clazz.newInstance();
67 this.containerId = containerId;
68 this.resourceAdapterWrapper = resourceAdapterWrapper;
69 }
70
71 /**
72 */
73 public ActivationSpecWrapper(ActivationSpec activationSpec, ResourceAdapterWrapper resourceAdapterWrapper) {
74 this.activationSpec = activationSpec;
75 this.resourceAdapterWrapper = resourceAdapterWrapper;
76 this.containerId = null;
77 }
78
79 /**
80 * Returns class of wrapped ActivationSpec.
81 *
82 * @return class of wrapped ActivationSpec
83 */
84
85
86
87
88 public String getContainerId() {
89 return containerId;
90 }
91
92 public ResourceAdapterWrapper getResourceAdapterWrapper() {
93 return resourceAdapterWrapper;
94 }
95
96
97
98 public void activate(final MessageEndpointFactory messageEndpointFactory) throws ResourceException {
99 ResourceAdapter resourceAdapter = activationSpec.getResourceAdapter();
100 if (resourceAdapter == null) {
101 resourceAdapterWrapper.registerResourceAdapterAssociation(activationSpec);
102 }
103 resourceAdapterWrapper.endpointActivation(messageEndpointFactory, activationSpec);
104 }
105
106 public void deactivate(final MessageEndpointFactory messageEndpointFactory) {
107 ResourceAdapter resourceAdapter = activationSpec.getResourceAdapter();
108 if (resourceAdapter != null) {
109 resourceAdapterWrapper.endpointDeactivation(messageEndpointFactory, activationSpec);
110 } else {
111
112 throw new IllegalStateException("ActivationSpec was never registered with ResourceAdapter");
113 }
114 }
115
116
117 public NamedXAResource getRecoveryXAResources() throws SystemException {
118 if (resourceAdapterWrapper == null) {
119 throw new IllegalStateException("Attempting to use activation spec when it is not activated");
120 }
121 try {
122 XAResource[] xaResources = resourceAdapterWrapper.getXAResources(new ActivationSpec[]{activationSpec});
123 if (xaResources == null || xaResources.length == 0) {
124 return null;
125 }
126 return new WrapperNamedXAResource(xaResources[0], containerId);
127 } catch (ResourceException e) {
128 throw (SystemException) new SystemException("Could not get XAResource for recovery for mdb: " + containerId).initCause(e);
129 }
130 }
131
132 public void returnResource(NamedXAResource xaResource) {
133
134 }
135
136 }