KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > embedded > resource > RARDeployment


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb3.embedded.resource;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Timer JavaDoc;
27
28 import javax.management.AttributeNotFoundException JavaDoc;
29 import javax.management.MBeanAttributeInfo JavaDoc;
30 import javax.management.MBeanException JavaDoc;
31 import javax.management.MBeanOperationInfo JavaDoc;
32 import javax.management.MBeanParameterInfo JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.management.ReflectionException JavaDoc;
35 import javax.resource.spi.ActivationSpec JavaDoc;
36 import javax.resource.spi.BootstrapContext JavaDoc;
37 import javax.resource.spi.ResourceAdapter JavaDoc;
38 import javax.resource.spi.UnavailableException JavaDoc;
39 import javax.resource.spi.XATerminator JavaDoc;
40 import javax.resource.spi.endpoint.MessageEndpointFactory JavaDoc;
41 import javax.resource.spi.work.WorkManager JavaDoc;
42
43 import org.jboss.deployment.DeploymentException;
44 import org.jboss.deployment.DeploymentInfo;
45 import org.jboss.logging.Logger;
46 import org.jboss.resource.deployment.ActivationSpecFactory;
47 import org.jboss.resource.deployment.ResourceAdapterFactory;
48 import org.jboss.resource.metadata.ConfigPropertyMetaData;
49 import org.jboss.resource.metadata.ConnectorMetaData;
50 import org.jboss.resource.metadata.DescriptionGroupMetaData;
51 import org.jboss.resource.metadata.MessageListenerMetaData;
52 import org.jboss.system.ServiceDynamicMBeanSupport;
53 import org.jboss.system.server.ServerConfigUtil;
54
55 /**
56  * @version <tt>$Revision: 45712 $</tt>
57  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
58  */

59 public class RARDeployment implements BootstrapContext JavaDoc
60 {
61    private static final Logger log = Logger.getLogger(RARDeployment.class);
62    
63    /** The deployment info */
64    protected DeploymentInfo di;
65    
66    /** The work manager */
67    protected WorkManager JavaDoc workManager;
68    
69    /** The xa terminator */
70    protected XATerminator JavaDoc xaTerminator;
71    
72    /** The meta data */
73    protected ConnectorMetaData cmd;
74    
75    /** The resource adapter */
76    protected ResourceAdapter JavaDoc resourceAdapter;
77    
78    protected ObjectName JavaDoc serviceName;
79
80    /**
81     * Create a new RAR deployment
82     *
83     * @param di the deployment info
84     */

85    public RARDeployment(DeploymentInfo di) throws Exception JavaDoc
86    {
87       this.di = di;
88       this.cmd = (ConnectorMetaData) di.metaData;
89       
90       resourceAdapter = ResourceAdapterFactory.createResourceAdapter(cmd);
91       resourceAdapter.start(this);
92    }
93    
94    // Public --------------------------------------------------------
95

96    public ObjectName JavaDoc getServiceName()
97    {
98       return serviceName;
99    }
100    
101    public void setServiceName(ObjectName JavaDoc serviceName)
102    {
103       this.serviceName = serviceName;
104    }
105
106    public Timer JavaDoc createTimer() throws UnavailableException JavaDoc
107    {
108       return new Timer JavaDoc(true);
109    }
110
111    public WorkManager JavaDoc getWorkManager()
112    {
113       return workManager;
114    }
115    
116    public void setWorkManager(WorkManager JavaDoc workManager)
117    {
118       this.workManager = workManager;
119    }
120    
121    public XATerminator JavaDoc getXATerminator()
122    {
123       return xaTerminator;
124    }
125    
126    public void setXATerminator(XATerminator JavaDoc xaTerminator)
127    {
128       this.xaTerminator = xaTerminator;
129    }
130    
131    protected String JavaDoc getInternalDescription()
132    {
133       String JavaDoc description = null;
134       DescriptionGroupMetaData dgmd = cmd.getDescription();
135       if (dgmd != null)
136          description = dgmd.getDescription();
137       if (description == null)
138          description = "RAR Deployment " + di.url;
139       return description;
140    }
141
142    protected MBeanAttributeInfo JavaDoc[] getInternalAttributeInfo()
143    {
144       Collection JavaDoc properties = cmd.getProperties();
145       MBeanAttributeInfo JavaDoc[] attrs = new MBeanAttributeInfo JavaDoc[11+properties.size()];
146       attrs[0] = new MBeanAttributeInfo JavaDoc("MetaData", ConnectorMetaData.class.getName(), "The meta data", true, false, false);
147       attrs[1] = new MBeanAttributeInfo JavaDoc("AuthenticationMechanism", String JavaDoc.class.getName(), "The authentication mechanism", true, false, false);
148       attrs[2] = new MBeanAttributeInfo JavaDoc("EISType", String JavaDoc.class.getName(), "The EIS type", true, false, false);
149       attrs[3] = new MBeanAttributeInfo JavaDoc("License", String JavaDoc.class.getName(), "The license", true, false, false);
150       attrs[4] = new MBeanAttributeInfo JavaDoc("RAClass", String JavaDoc.class.getName(), "The resource adapter class", true, false, false);
151       attrs[5] = new MBeanAttributeInfo JavaDoc("RAVersion", String JavaDoc.class.getName(), "The resource adapter version", true, false, false);
152       attrs[6] = new MBeanAttributeInfo JavaDoc("TransactionSupport", String JavaDoc.class.getName(), "The transaction support", true, false, false);
153       attrs[7] = new MBeanAttributeInfo JavaDoc("VendorName", String JavaDoc.class.getName(), "The vendor name", true, false, false);
154       attrs[8] = new MBeanAttributeInfo JavaDoc("Version", String JavaDoc.class.getName(), "The spec version", true, false, false);
155       attrs[9] = new MBeanAttributeInfo JavaDoc("ReauthenticationSupport", Boolean.TYPE.getName(), "Whether reauthentication support is supported", true, false, false);
156       attrs[10] = new MBeanAttributeInfo JavaDoc("ResourceAdapter", ResourceAdapter JavaDoc.class.getName(), "The resource adapter instance", true, false, false);
157       int n = 11;
158       for (Iterator JavaDoc i = properties.iterator(); i.hasNext();)
159       {
160          ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData) i.next();
161          attrs[n++] = new MBeanAttributeInfo JavaDoc(cpmd.getName(), cpmd.getType(), cpmd.getDescription().getDescription(), true, false, false);
162       }
163       return attrs;
164    }
165    
166    protected Object JavaDoc getInternalAttribute(String JavaDoc attribute)
167       throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
168    {
169       if ("MetaData".equals(attribute))
170          return cmd;
171       else if ("AuthenticationMechanism".equals(attribute))
172          return cmd.getAuthenticationMechanism().getAuthenticationMechansimType();
173       else if ("EISType".equals(attribute))
174          return cmd.getEISType();
175       else if ("License".equals(attribute))
176          return cmd.getLicense().getDescription().getDescription();
177       else if ("RAClass".equals(attribute))
178          return cmd.getRAClass();
179       else if ("RAVersion".equals(attribute))
180          return cmd.getRAVersion();
181       else if ("TransactionSupport".equals(attribute))
182          return cmd.getTransactionSupport();
183       else if ("VendorName".equals(attribute))
184          return cmd.getVendorName();
185       else if ("Version".equals(attribute))
186          return cmd.getVersion();
187       else if ("ReauthenticationSupport".equals(attribute))
188          return new Boolean JavaDoc(cmd.getReauthenticationSupport());
189       else if ("ResourceAdapter".equals(attribute))
190          return resourceAdapter;
191       Object JavaDoc property = cmd.getProperty(attribute);
192       if (property != null)
193          return property;
194       
195       return null;
196    }
197
198    protected MBeanOperationInfo JavaDoc[] getInternalOperationInfo()
199    {
200       MBeanOperationInfo JavaDoc[] ops = new MBeanOperationInfo JavaDoc[3];
201
202       MBeanParameterInfo JavaDoc[] createActivationSpecParams = new MBeanParameterInfo JavaDoc[]
203       {
204          new MBeanParameterInfo JavaDoc("MessagingType", Class JavaDoc.class.getName(), "The type of the message listener"),
205          new MBeanParameterInfo JavaDoc("ActivationConfig", Collection JavaDoc.class.getName(), "A collection of activation config properties")
206       };
207       ops[0] = new MBeanOperationInfo JavaDoc("createActivationSpec", "Create an activation spec",
208             createActivationSpecParams, ActivationSpec JavaDoc.class.getName(), MBeanOperationInfo.ACTION);
209
210       MBeanParameterInfo JavaDoc[] activationParams = new MBeanParameterInfo JavaDoc[]
211       {
212          new MBeanParameterInfo JavaDoc("MessageEndpointFactory", MessageEndpointFactory JavaDoc.class.getName(), "The message endpoint factory"),
213          new MBeanParameterInfo JavaDoc("ActivationSpec", ActivationSpec JavaDoc.class.getName(), "The activation spec")
214       };
215       ops[1] = new MBeanOperationInfo JavaDoc("endpointActivation", "Active the endpoint",
216             activationParams, Void JavaDoc.class.getName(), MBeanOperationInfo.ACTION);
217       ops[2] = new MBeanOperationInfo JavaDoc("endpointDeactivation", "Deactive the endpoint",
218             activationParams, Void JavaDoc.class.getName(), MBeanOperationInfo.ACTION);
219
220       return ops;
221    }
222    
223    public Object JavaDoc invoke(String JavaDoc actionName, Object JavaDoc[] params, String JavaDoc[] signature) throws MBeanException JavaDoc,
224          ReflectionException JavaDoc
225    {
226       Object JavaDoc result = null;
227       if ("createActivationSpec".equals(actionName))
228       {
229          if (params.length != 2)
230             throw new IllegalArgumentException JavaDoc("Wrong number of parameters for " + actionName);
231          Class JavaDoc messagingType = (Class JavaDoc) params[0];
232          Collection JavaDoc activationConfig = (Collection JavaDoc) params[1];
233          result = createActivationSpec(messagingType, activationConfig);
234       }
235       else if ("endpointActivation".equals(actionName))
236       {
237          if (params.length != 2)
238             throw new IllegalArgumentException JavaDoc("Wrong number of parameters for " + actionName);
239          MessageEndpointFactory JavaDoc messageEndpointFactory = (MessageEndpointFactory JavaDoc) params[0];
240          ActivationSpec JavaDoc activationSpec = (ActivationSpec JavaDoc) params[1];
241          endpointActivation(messageEndpointFactory, activationSpec);
242       }
243       else if ("endpointDeactivation".equals(actionName))
244       {
245          if (params.length != 2)
246             throw new IllegalArgumentException JavaDoc("Wrong number of parameters for " + actionName);
247          MessageEndpointFactory JavaDoc messageEndpointFactory = (MessageEndpointFactory JavaDoc) params[0];
248          ActivationSpec JavaDoc activationSpec = (ActivationSpec JavaDoc) params[1];
249          endpointDeactivation(messageEndpointFactory, activationSpec);
250       }
251       
252       return result;
253    }
254
255    protected ActivationSpec JavaDoc createActivationSpec(Class JavaDoc messagingType, Collection JavaDoc activationConfig) throws MBeanException JavaDoc
256    {
257       boolean trace = log.isTraceEnabled();
258       if (trace)
259          log.trace("CreateActivateSpec rar=" + getServiceName() + " messagingType=" + messagingType.getName() + " activationConfig=" + activationConfig);
260       
261       try
262       {
263          // Find the meta data
264
MessageListenerMetaData mlmd = cmd.getMessageListener(messagingType.getName());
265          if (mlmd == null)
266             throw new DeploymentException("MessagingType '" + messagingType.getName() + "' not found in resource deployment " + getServiceName());
267          
268          return ActivationSpecFactory.createActivationSpec(getServiceName(), messagingType.getName(), activationConfig, mlmd);
269       }
270       catch (Exception JavaDoc e)
271       {
272          throw new MBeanException JavaDoc(e, "Error in create activation spec " + getServiceName());
273       }
274    }
275
276    protected void endpointActivation(MessageEndpointFactory JavaDoc messageEndpointFactory, ActivationSpec JavaDoc activationSpec) throws MBeanException JavaDoc
277    {
278       boolean trace = log.isTraceEnabled();
279       if (trace)
280          log.trace("EndpointActivation rar=" + getServiceName() + " messagingEndpointFactory=" + messageEndpointFactory + " activationSpec=" + activationSpec);
281
282       try
283       {
284          activationSpec.setResourceAdapter(resourceAdapter);
285          resourceAdapter.endpointActivation(messageEndpointFactory, activationSpec);
286       }
287       catch (Exception JavaDoc e)
288       {
289          throw new MBeanException JavaDoc(e, "Error in endpoint activation " + getServiceName());
290       }
291    }
292
293    protected void endpointDeactivation(MessageEndpointFactory JavaDoc messageEndpointFactory, ActivationSpec JavaDoc activationSpec) throws MBeanException JavaDoc
294    {
295       boolean trace = log.isTraceEnabled();
296       if (trace)
297          log.trace("EndpointDeactivation rar=" + getServiceName() + " messagingEndpointFactory=" + messageEndpointFactory + " activationSpec=" + activationSpec);
298
299       try
300       {
301          resourceAdapter.endpointDeactivation(messageEndpointFactory, activationSpec);
302       }
303       catch (Exception JavaDoc e)
304       {
305          throw new MBeanException JavaDoc(e, "Error in endpoint deactivation " + getServiceName());
306       }
307    }
308 }
309
Popular Tags