KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > connectionmanager > 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.resource.connectionmanager;
23
24 import java.beans.PropertyEditor JavaDoc;
25 import java.beans.PropertyEditorManager JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import javax.management.Notification JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.resource.ResourceException JavaDoc;
33 import javax.resource.spi.ConnectionManager JavaDoc;
34 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
35 import javax.resource.spi.ManagedConnection JavaDoc;
36 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
37 import javax.resource.spi.ResourceAdapter JavaDoc;
38 import javax.resource.spi.ResourceAdapterAssociation JavaDoc;
39
40 import org.jboss.deployment.DeploymentException;
41 import org.jboss.logging.Logger;
42 import org.jboss.metadata.MetaData;
43 import org.jboss.resource.metadata.ConfigPropertyMetaData;
44 import org.jboss.resource.metadata.ConnectionDefinitionMetaData;
45 import org.jboss.resource.metadata.ConnectorMetaData;
46 import org.jboss.system.ServiceMBeanSupport;
47 import org.jboss.util.Classes;
48 import org.jboss.util.NestedRuntimeException;
49 import org.w3c.dom.Element JavaDoc;
50 import org.w3c.dom.Node JavaDoc;
51 import org.w3c.dom.NodeList JavaDoc;
52 /**
53  * The RARDeployment mbean manages instantiation and configuration of a
54  * ManagedConnectionFactory instance. It is intended to be configured
55  * primarily by xslt transformation of the ra.xml from a jca adapter.
56  * Until that is implemented, it uses the old RARDeployment and RARDeployer
57  * mechanism to obtain information from the ra.xml. Properties for the
58  * ManagedConectionFactory should be supplied with their values in the
59  * ManagedConnectionFactoryProperties element.
60  *
61  * @author <a HREF="toby.allsopp@peace.com">Toby Allsopp</a>
62  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
63  * @version $Revision: 38338 $
64  */

65 public class RARDeployment extends ServiceMBeanSupport
66    implements RARDeploymentMBean, ManagedConnectionFactory JavaDoc
67 {
68    static final long serialVersionUID = -294341806721616790L;
69
70    public final static String JavaDoc MCF_ATTRIBUTE_CHANGED_NOTIFICATION = "jboss.mcfattributechangednotification";
71
72    private Logger log = Logger.getLogger(getClass());
73
74    //Hack to use previous ra.xml parsing code until xslt deployment is written.
75
private ObjectName JavaDoc oldRarDeployment;
76
77    private String JavaDoc rarName;
78
79    private String JavaDoc connectionDefinition;
80
81    private String JavaDoc vendorName;
82
83    private String JavaDoc specVersion;
84
85    private String JavaDoc eisType;
86
87    private String JavaDoc version;
88
89    private String JavaDoc managedConnectionFactoryClass;
90
91    private String JavaDoc connectionFactoryInterface;
92
93    private String JavaDoc connectionFactoryImplClass;
94
95    private String JavaDoc connectionInterface;
96
97    private String JavaDoc connectionImplClass;
98
99    private String JavaDoc transactionSupport;
100
101    private Element JavaDoc managedConnectionFactoryProperties;
102
103    private String JavaDoc authenticationMechanismType;
104
105    private String JavaDoc credentialInterface;
106
107    private boolean reauthenticationSupport;
108
109    private Class JavaDoc mcfClass;
110
111    private ManagedConnectionFactory JavaDoc mcf;
112
113    /**
114     * Default managed constructor for RARDeployment mbeans.
115     */

116    public RARDeployment()
117    {
118    }
119
120    public ObjectName JavaDoc getOldRarDeployment()
121    {
122       return oldRarDeployment;
123    }
124
125    public void setOldRarDeployment(final ObjectName JavaDoc oldRarDeployment)
126    {
127       this.oldRarDeployment = oldRarDeployment;
128    }
129
130    public String JavaDoc getRARName()
131    {
132       return rarName;
133    }
134
135    public void setRARName(String JavaDoc rarName)
136    {
137       this.rarName = rarName;
138    }
139
140    public String JavaDoc getConnectionDefinition()
141    {
142       return connectionDefinition;
143    }
144
145    public void setConnectionDefinition(String JavaDoc connectionDefinition)
146    {
147       this.connectionDefinition = connectionDefinition;
148    }
149
150    public String JavaDoc getVendorName()
151    {
152       return vendorName;
153    }
154
155    public void setVendorName(String JavaDoc vendorName)
156    {
157       this.vendorName = vendorName;
158    }
159
160    public String JavaDoc getSpecVersion()
161    {
162       return specVersion;
163    }
164
165    public void setSpecVersion(String JavaDoc specVersion)
166    {
167       this.specVersion = specVersion;
168    }
169
170    public String JavaDoc getEisType()
171    {
172       return eisType;
173    }
174
175    public void setEisType(String JavaDoc eisType)
176    {
177       this.eisType = eisType;
178    }
179
180    public String JavaDoc getVersion()
181    {
182       return version;
183    }
184
185    public void setVersion(String JavaDoc version)
186    {
187       this.version = version;
188    }
189
190    public String JavaDoc getManagedConnectionFactoryClass()
191    {
192       return managedConnectionFactoryClass;
193    }
194
195    public void setManagedConnectionFactoryClass(final String JavaDoc managedConnectionFactoryClass)
196    {
197       this.managedConnectionFactoryClass = managedConnectionFactoryClass;
198    }
199
200    public String JavaDoc getConnectionFactoryInterface()
201    {
202       return connectionFactoryInterface;
203    }
204
205    public void setConnectionFactoryInterface(String JavaDoc connectionFactoryInterface)
206    {
207       this.connectionFactoryInterface = connectionFactoryInterface;
208    }
209
210    public String JavaDoc getConnectionFactoryImplClass()
211    {
212       return connectionFactoryImplClass;
213    }
214
215    public void setConnectionFactoryImplClass(String JavaDoc connectionFactoryImplClass)
216    {
217       this.connectionFactoryImplClass = connectionFactoryImplClass;
218    }
219
220    public String JavaDoc getConnectionInterface()
221    {
222       return connectionInterface;
223    }
224
225    public void setConnectionInterface(String JavaDoc connectionInterface)
226    {
227       this.connectionInterface = connectionInterface;
228    }
229
230    public String JavaDoc getConnectionImplClass()
231    {
232       return connectionImplClass;
233    }
234
235    public void setConnectionImplClass(String JavaDoc connectionImplClass)
236    {
237       this.connectionImplClass = connectionImplClass;
238    }
239
240    public String JavaDoc getTransactionSupport()
241    {
242       return transactionSupport;
243    }
244
245    public void setTransactionSupport(String JavaDoc transactionSupport)
246    {
247       this.transactionSupport = transactionSupport;
248    }
249
250    public Element JavaDoc getManagedConnectionFactoryProperties()
251    {
252       return managedConnectionFactoryProperties;
253    }
254
255    public void setManagedConnectionFactoryProperties(Element JavaDoc managedConnectionFactoryProperties)
256    {
257       this.managedConnectionFactoryProperties = managedConnectionFactoryProperties;
258    }
259
260    public String JavaDoc getAuthenticationMechanismType()
261    {
262       return authenticationMechanismType;
263    }
264
265    public void setAuthenticationMechanismType(String JavaDoc authenticationMechanismType)
266    {
267       this.authenticationMechanismType = authenticationMechanismType;
268    }
269
270    public String JavaDoc getCredentialInterface()
271    {
272       return credentialInterface;
273    }
274
275    public void setCredentialInterface(String JavaDoc credentialInterface)
276    {
277       this.credentialInterface = credentialInterface;
278    }
279
280    public boolean isReauthenticationSupport()
281    {
282       return reauthenticationSupport;
283    }
284
285    public void setReauthenticationSupport(boolean reauthenticationSupport)
286    {
287       this.reauthenticationSupport = reauthenticationSupport;
288    }
289
290    public ManagedConnectionFactory JavaDoc getMcfInstance()
291    {
292       return mcf;
293    }
294
295    protected void startService() throws Exception JavaDoc
296    {
297       if (mcf != null)
298          throw new DeploymentException("Stop the RARDeployment before restarting it");
299
300       ConnectorMetaData cmd = null;
301       ConnectionDefinitionMetaData cdmd = null;
302       ResourceAdapter JavaDoc resourceAdapter = null;
303       if (oldRarDeployment != null)
304       {
305          try
306          {
307             resourceAdapter = (ResourceAdapter JavaDoc) getServer().getAttribute(oldRarDeployment, "ResourceAdapter");
308             cmd = (ConnectorMetaData) getServer().getAttribute(oldRarDeployment, "MetaData");
309             cdmd = cmd.getConnectionDefinition(connectionDefinition);
310             if (cdmd == null)
311                throw new DeploymentException("ConnectionDefinition '" + connectionDefinition + "' not found in rar '" + rarName + "'");
312             setManagedConnectionFactoryClass(cdmd.getManagedConnectionFactoryClass());
313             setReauthenticationSupport(cmd.getReauthenticationSupport());
314          }
315          catch (Exception JavaDoc e)
316          {
317             throw new DeploymentException("couldn't get oldRarDeployment! " + oldRarDeployment, e);
318          }
319       }
320       try
321       {
322          mcfClass = Thread.currentThread().getContextClassLoader().loadClass(managedConnectionFactoryClass);
323       }
324       catch (ClassNotFoundException JavaDoc cnfe)
325       {
326          log.error("Could not find ManagedConnectionFactory class: " + managedConnectionFactoryClass, cnfe);
327          throw new DeploymentException("Could not find ManagedConnectionFactory class: "
328                + managedConnectionFactoryClass);
329       }
330       try
331       {
332          mcf = (ManagedConnectionFactory JavaDoc) mcfClass.newInstance();
333       }
334       catch (Exception JavaDoc e)
335       {
336          log.error("Could not instantiate ManagedConnectionFactory: " + managedConnectionFactoryClass, e);
337          throw new DeploymentException("Could not instantiate ManagedConnectionFactory: "
338                + managedConnectionFactoryClass);
339       }
340       if (cmd != null)
341       {
342          // Set the resource adapter properties
343
setMcfProperties(cmd.getProperties(), false);
344          // Set the connection definition properties
345
setMcfProperties(cdmd.getProperties(), true);
346       }
347       //set overridden properties;
348
setMcfProperties(managedConnectionFactoryProperties);
349       
350       if (resourceAdapter != null && mcf instanceof ResourceAdapterAssociation JavaDoc)
351       {
352          ResourceAdapterAssociation JavaDoc raa = (ResourceAdapterAssociation JavaDoc) mcf;
353          raa.setResourceAdapter(resourceAdapter);
354       }
355    }
356
357    protected void stopService()
358    {
359       mcf = null;
360       mcfClass = null;
361    }
362
363    public void setManagedConnectionFactoryAttribute(String JavaDoc name, Class JavaDoc clazz, Object JavaDoc value)
364    {
365       setManagedConnectionFactoryAttribute(name, clazz, value, false);
366    }
367    
368    protected void setManagedConnectionFactoryAttribute(String JavaDoc name, Class JavaDoc clazz, Object JavaDoc value, boolean mustExist)
369    {
370       if (name == null || name.length() == 0)
371          throw new IllegalArgumentException JavaDoc("Null or empty attribute name " + name);
372       String JavaDoc setterName = "set" + Character.toUpperCase(name.charAt(0));
373       if (name.length() > 1)
374          setterName = setterName.concat(name.substring(1));
375       Method JavaDoc setter;
376       try
377       {
378          setter = mcfClass.getMethod(setterName, new Class JavaDoc[] {clazz});
379       }
380       catch (NoSuchMethodException JavaDoc nsme)
381       {
382          String JavaDoc error = "The class '" + mcfClass.toString() + "' has no setter for config property '" + name + "'";
383          if (mustExist)
384             throw new IllegalArgumentException JavaDoc(error);
385          else
386          {
387             log.trace(error, nsme);
388             return;
389          }
390       }
391       try
392       {
393          setter.invoke(mcf, new Object JavaDoc[] {value});
394          log.debug("set property " + name + " to value " + value);
395       }
396       catch (Exception JavaDoc e)
397       {
398          String JavaDoc error = "Unable to invoke setter method '" + setter + "' " + "on object '" + mcf + "'";
399          if (e instanceof InvocationTargetException JavaDoc)
400             throw new NestedRuntimeException(error, ((InvocationTargetException JavaDoc) e).getCause());
401          else
402             throw new NestedRuntimeException(error, e);
403       }
404       sendNotification(new Notification JavaDoc(MCF_ATTRIBUTE_CHANGED_NOTIFICATION, getServiceName(),
405             getNextNotificationSequenceNumber()));
406    }
407
408    public Object JavaDoc getManagedConnectionFactoryAttribute(String JavaDoc name)
409    {
410       if (name == null || name.length() == 0)
411          throw new IllegalArgumentException JavaDoc("Null or empty attribute name " + name);
412       String JavaDoc getterName = "get" + Character.toUpperCase(name.charAt(0));
413       if (name.length() > 1)
414          getterName = getterName.concat(name.substring(1));
415       Method JavaDoc getter;
416       try
417       {
418          getter = mcfClass.getMethod(getterName, new Class JavaDoc[] {});
419       }
420       catch (NoSuchMethodException JavaDoc e)
421       {
422          String JavaDoc msg = "The class '" + mcfClass + "' has no getter("
423             + getterName + ") for config property '" + name + "'";
424          log.debug(msg, e);
425          throw new IllegalArgumentException JavaDoc(msg);
426       }
427       try
428       {
429          Object JavaDoc value = getter.invoke(mcf, new Object JavaDoc[]{});
430          log.debug("get property " + name + ": value " + value);
431          return value;
432       }
433       catch (Exception JavaDoc e)
434       {
435          String JavaDoc error = "Unable to invoke getter method '" + getter + "' " + "on object '" + mcf + "'";
436          log.debug(error, e);
437          if (e instanceof InvocationTargetException JavaDoc)
438             throw new NestedRuntimeException(error, ((InvocationTargetException JavaDoc) e).getCause());
439          else
440             throw new NestedRuntimeException(error, e);
441       }
442    }
443
444    protected void setMcfProperties(Collection JavaDoc properties, boolean mustExist) throws DeploymentException
445    {
446       for (Iterator JavaDoc i = properties.iterator(); i.hasNext();)
447       {
448          ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData) i.next();
449          String JavaDoc name = cpmd.getName();
450          String JavaDoc type = cpmd.getType();
451          String JavaDoc value = cpmd.getValue();
452          if (name == null || name.length() == 0 || value == null || value.length() == 0)
453          {
454             log.debug("Not setting config property '" + name + "'");
455             continue;
456          }
457          // see if it is a primitive type first
458
Class JavaDoc clazz = Classes.getPrimitiveTypeForName(type);
459          if (clazz == null)
460          {
461             //not primitive, look for it.
462
try
463             {
464                clazz = Thread.currentThread().getContextClassLoader().loadClass(type);
465             }
466             catch (ClassNotFoundException JavaDoc cnfe)
467             {
468                log.warn("Unable to find class '" + type + "' for " + "property '" + name + "' - skipping property.");
469                continue;
470             }
471          }
472          PropertyEditor JavaDoc pe = PropertyEditorManager.findEditor(clazz);
473          if (pe == null)
474          {
475             log.warn("Unable to find a PropertyEditor for class '" + clazz + "' of property '" + name + "' - "
476                   + "skipping property");
477             continue;
478          }
479          log.debug("setting property: " + name + " to value " + value);
480          try
481          {
482             pe.setAsText(value);
483          }
484          catch (IllegalArgumentException JavaDoc iae)
485          {
486             log.warn("Value '" + value + "' is not valid for property '" + name + "' of class '" + clazz
487                   + "' - skipping " + "property");
488             continue;
489          }
490          Object JavaDoc v = pe.getValue();
491          setManagedConnectionFactoryAttribute(name, clazz, v, mustExist);
492       }
493    }
494
495    protected void setMcfProperties(Element JavaDoc mcfProps) throws DeploymentException
496    {
497       if (mcfProps == null)
498          return;
499       // the properties that the deployment descriptor says we need to set
500
NodeList JavaDoc props = mcfProps.getChildNodes();
501       for (int i = 0; i < props.getLength(); i++)
502       {
503          if (props.item(i).getNodeType() == Node.ELEMENT_NODE)
504          {
505             Element JavaDoc prop = (Element JavaDoc) props.item(i);
506             if (prop.getTagName().equals("config-property"))
507             {
508                String JavaDoc name = null;
509                String JavaDoc type = null;
510                String JavaDoc value = null;
511                //Support for more friendly config style
512
//<config-property name="" type=""></config-property>
513
if (prop.hasAttribute("name"))
514                {
515                   name = prop.getAttribute("name");
516                   type = prop.getAttribute("type");
517                   value = MetaData.getElementContent(prop, null, false);
518                }
519                else
520                {
521                   name = MetaData.getElementContent(MetaData.getUniqueChild(prop, "config-property-name"));
522                   type = MetaData.getElementContent(MetaData.getUniqueChild(prop, "config-property-type"));
523                   value = MetaData.getElementContent(MetaData.getOptionalChild(prop, "config-property-value"), null, false);
524                }
525                if (name == null || name.length() == 0 || value == null || value.length() == 0)
526                {
527                   log.debug("Not setting config property '" + name + "'");
528                   continue;
529                }
530                if (type == null || type.length() == 0)
531                {
532                   // Default to String for convenience.
533
type = "java.lang.String";
534                }
535                // see if it is a primitive type first
536
Class JavaDoc clazz = Classes.getPrimitiveTypeForName(type);
537                if (clazz == null)
538                {
539                   //not primitive, look for it.
540
try
541                   {
542                      clazz = Thread.currentThread().getContextClassLoader().loadClass(type);
543                   }
544                   catch (ClassNotFoundException JavaDoc cnfe)
545                   {
546                      log.warn("Unable to find class '" + type + "' for " + "property '" + name
547                            + "' - skipping property.");
548                      continue;
549                   }
550                }
551                PropertyEditor JavaDoc pe = PropertyEditorManager.findEditor(clazz);
552                if (pe == null)
553                {
554                   log.warn("Unable to find a PropertyEditor for class '" + clazz + "' of property '" + name + "' - "
555                         + "skipping property");
556                   continue;
557                }
558                log.debug("setting property: " + name + " to value " + value);
559                try
560                {
561                   pe.setAsText(value);
562                }
563                catch (IllegalArgumentException JavaDoc iae)
564                {
565                   log.warn("Value '" + value + "' is not valid for property '" + name + "' of class '" + clazz
566                         + "' - skipping " + "property");
567                   continue;
568                }
569                Object JavaDoc v = pe.getValue();
570                setManagedConnectionFactoryAttribute(name, clazz, v);
571             }
572          }
573       }
574    }
575
576    public Object JavaDoc createConnectionFactory() throws ResourceException JavaDoc
577    {
578       return mcf.createConnectionFactory();
579    }
580
581    public Object JavaDoc createConnectionFactory(ConnectionManager JavaDoc cxManager) throws ResourceException JavaDoc
582    {
583       return mcf.createConnectionFactory(cxManager);
584    }
585
586    public ManagedConnection JavaDoc createManagedConnection(javax.security.auth.Subject JavaDoc subject,
587          ConnectionRequestInfo JavaDoc cxRequestInfo) throws ResourceException JavaDoc
588    {
589       return mcf.createManagedConnection(subject, cxRequestInfo);
590    }
591
592    public boolean equals(Object JavaDoc other)
593    {
594       return mcf.equals(other);
595    }
596
597    public java.io.PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc
598    {
599       return mcf.getLogWriter();
600    }
601
602    public String JavaDoc toString()
603    {
604       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
605       buffer.append(getClass().getName());
606       buffer.append('@');
607       buffer.append(Integer.toHexString(System.identityHashCode(this)));
608       return buffer.toString();
609    }
610    
611    public int hashCode()
612    {
613       return mcf.hashCode();
614    }
615
616    public ManagedConnection JavaDoc matchManagedConnections(java.util.Set JavaDoc connectionSet, javax.security.auth.Subject JavaDoc subject,
617          ConnectionRequestInfo JavaDoc cxRequestInfo) throws ResourceException JavaDoc
618    {
619       return mcf.matchManagedConnections(connectionSet, subject, cxRequestInfo);
620    }
621
622    public void setLogWriter(java.io.PrintWriter JavaDoc out) throws ResourceException JavaDoc
623    {
624       mcf.setLogWriter(out);
625    }
626 }
627
Popular Tags