KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > deployment > ResourceAdapterFactory


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.deployment;
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.Iterator JavaDoc;
29
30 import javax.resource.spi.ResourceAdapter JavaDoc;
31
32 import org.jboss.deployment.DeploymentException;
33 import org.jboss.resource.metadata.ConfigPropertyMetaData;
34 import org.jboss.resource.metadata.ConnectorMetaData;
35 import org.jboss.resource.metadata.RARDeploymentMetaData;
36
37 /**
38  * A resource adapter factory
39  *
40  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
41  * @author <a HREF="weston.price@jboss.com">Weston Price</a>
42  * @version $Revision: 46109 $
43  */

44 public class ResourceAdapterFactory
45 {
46    /** The dummy resource adapter for old deployment */
47    public static final String JavaDoc DUMMY_RA_CLASS = DummyResourceAdapter.class.getName();
48    
49    /**
50     * Create a new ResourceAdapter
51     *
52     * @param ramd the ResourceAdapterMetaData
53     * @return the resource adapter
54     * @throws Exception
55     */

56    public static ResourceAdapter JavaDoc createResourceAdapter(RARDeploymentMetaData ramd) throws Exception JavaDoc
57    {
58       ResourceAdapter JavaDoc adapter = createResourceAdapter(ramd.getConnectorMetaData());
59       
60       for(Iterator JavaDoc iter = ramd.getRaXmlMetaData().getProperties().iterator(); iter.hasNext();)
61       {
62          ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData)iter.next();
63          applyProperty(cpmd, adapter.getClass(), adapter);
64          
65       }
66             
67       return adapter;
68    }
69    
70    /**
71     * Create a new resource adapter
72     *
73     * @param cmd the connector meta data
74     * @throws Exception for any error
75     */

76    public static ResourceAdapter JavaDoc createResourceAdapter(ConnectorMetaData cmd) throws Exception JavaDoc
77    {
78       // Determine the resource adapter class
79
String JavaDoc className = cmd.getRAClass();
80       if (className == null)
81       {
82          if (cmd.getVersion().equals("1.0"))
83             className = DUMMY_RA_CLASS;
84          else
85             throw new IllegalArgumentException JavaDoc("No resource adapter class name specified");
86       }
87       
88       // Load the class
89
Class JavaDoc raClass = Thread.currentThread().getContextClassLoader().loadClass(className);
90       if (ResourceAdapter JavaDoc.class.isAssignableFrom(raClass) == false)
91          throw new DeploymentException(raClass.getName() + " is not a resource adapter class");
92       ResourceAdapter JavaDoc result = (ResourceAdapter JavaDoc) raClass.newInstance();
93       
94       // Apply the properties
95
for (Iterator JavaDoc i = cmd.getProperties().iterator(); i.hasNext();)
96       {
97          ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData) i.next();
98          applyProperty(cpmd, raClass, result);
99          
100          
101       }
102       
103       return result;
104    }
105
106    private static void applyProperty(ConfigPropertyMetaData cpmd, Class JavaDoc clz, ResourceAdapter JavaDoc ra) throws Exception JavaDoc
107    {
108       
109       String JavaDoc name = cpmd.getName();
110       String JavaDoc type = cpmd.getType();
111       String JavaDoc value = cpmd.getValue();
112       
113       Class JavaDoc clazz = Thread.currentThread().getContextClassLoader().loadClass(type);
114       PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(clazz);
115       if (editor == null)
116          throw new IllegalArgumentException JavaDoc("No property editor found for property " + cpmd);
117       editor.setAsText(value);
118       Object JavaDoc object = editor.getValue();
119       
120       try
121       {
122          String JavaDoc setter = "set" + Character.toUpperCase(name.charAt(0));
123          if (name.length() > 1)
124             setter = setter.concat(name.substring(1));
125          Method JavaDoc method = clz.getMethod(setter, new Class JavaDoc[] { clazz });
126          method.invoke(ra, new Object JavaDoc[] { object });
127       }
128       catch (InvocationTargetException JavaDoc e)
129       {
130          DeploymentException.rethrowAsDeploymentException("Error for resource adapter class " + clz.getName() + " setting property " + cpmd, e.getTargetException());
131       }
132       catch (Throwable JavaDoc t)
133       {
134          DeploymentException.rethrowAsDeploymentException("Error for resource adapter class " + clz.getName() + " accessing property setter " + cpmd, t);
135       }
136       
137    }
138
139 }
140
Popular Tags