KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > ActivationSpecWrapper


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
21 import javax.resource.spi.ActivationSpec JavaDoc;
22 import javax.resource.spi.ResourceAdapter JavaDoc;
23 import javax.resource.spi.endpoint.MessageEndpointFactory JavaDoc;
24 import javax.transaction.SystemException JavaDoc;
25 import javax.transaction.xa.XAResource JavaDoc;
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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
37  */

38 public class ActivationSpecWrapper implements ResourceManager {
39
40     protected final ActivationSpec JavaDoc activationSpec;
41
42     private final ResourceAdapterWrapper resourceAdapterWrapper;
43     private final String JavaDoc 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 JavaDoc activationSpecClass,
62                                  final String JavaDoc containerId,
63                                  final ResourceAdapterWrapper resourceAdapterWrapper,
64                                  final ClassLoader JavaDoc cl) throws IllegalAccessException JavaDoc, InstantiationException JavaDoc, ClassNotFoundException JavaDoc {
65         Class JavaDoc clazz = cl.loadClass(activationSpecClass);
66         this.activationSpec = (ActivationSpec JavaDoc) clazz.newInstance();
67         this.containerId = containerId;
68         this.resourceAdapterWrapper = resourceAdapterWrapper;
69     }
70
71     /**
72      */

73     public ActivationSpecWrapper(ActivationSpec JavaDoc 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 // public String getActivationSpecClass() {
85
// return activationSpecClass;
86
// }
87

88     public String JavaDoc getContainerId() {
89         return containerId;
90     }
91
92     public ResourceAdapterWrapper getResourceAdapterWrapper() {
93         return resourceAdapterWrapper;
94     }
95
96
97     //GBeanLifecycle implementation
98
public void activate(final MessageEndpointFactory JavaDoc messageEndpointFactory) throws ResourceException JavaDoc {
99         ResourceAdapter JavaDoc 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 JavaDoc messageEndpointFactory) {
107         ResourceAdapter JavaDoc resourceAdapter = activationSpec.getResourceAdapter();
108         if (resourceAdapter != null) {
109             resourceAdapterWrapper.endpointDeactivation(messageEndpointFactory, activationSpec);
110         } else {
111             //this should never happen, activation spec should have been registered with r.a.
112
throw new IllegalStateException JavaDoc("ActivationSpec was never registered with ResourceAdapter");
113         }
114     }
115
116     //Operations.
117
public NamedXAResource getRecoveryXAResources() throws SystemException JavaDoc {
118         if (resourceAdapterWrapper == null) {
119             throw new IllegalStateException JavaDoc("Attempting to use activation spec when it is not activated");
120         }
121         try {
122             XAResource JavaDoc[] xaResources = resourceAdapterWrapper.getXAResources(new ActivationSpec JavaDoc[]{activationSpec});
123             if (xaResources == null || xaResources.length == 0) {
124                 return null;
125             }
126             return new WrapperNamedXAResource(xaResources[0], containerId);
127         } catch (ResourceException JavaDoc e) {
128             throw (SystemException JavaDoc) new SystemException JavaDoc("Could not get XAResource for recovery for mdb: " + containerId).initCause(e);
129         }
130     }
131
132     public void returnResource(NamedXAResource xaResource) {
133         //do nothing, no way to return anything.
134
}
135
136 }
137
Popular Tags