001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.deployment;
023:
024: import java.beans.PropertyEditor;
025: import java.beans.PropertyEditorManager;
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.Method;
028: import java.util.Iterator;
029:
030: import javax.resource.spi.ResourceAdapter;
031:
032: import org.jboss.deployment.DeploymentException;
033: import org.jboss.resource.metadata.ConfigPropertyMetaData;
034: import org.jboss.resource.metadata.ConnectorMetaData;
035:
036: /**
037: * A resource adapter factory
038: *
039: * @author <a href="adrian@jboss.com">Adrian Brock</a>
040: * @version $Revision: 57189 $
041: */
042: public class ResourceAdapterFactory {
043: /** The dummy resource adapter for old deployment */
044: public static final String DUMMY_RA_CLASS = DummyResourceAdapter.class
045: .getName();
046:
047: /**
048: * Create a new resource adapter
049: *
050: * @param cmd the connector meta data
051: * @throws Exception for any error
052: */
053: public static ResourceAdapter createResourceAdapter(
054: ConnectorMetaData cmd) throws Exception {
055: // Determine the resource adapter class
056: String className = cmd.getRAClass();
057: if (className == null) {
058: if (cmd.getVersion().equals("1.0"))
059: className = DUMMY_RA_CLASS;
060: else
061: throw new IllegalArgumentException(
062: "No resource adapter class name specified");
063: }
064:
065: // Load the class
066: Class raClass = Thread.currentThread().getContextClassLoader()
067: .loadClass(className);
068: if (ResourceAdapter.class.isAssignableFrom(raClass) == false)
069: throw new DeploymentException(raClass.getName()
070: + " is not a resource adapter class");
071: ResourceAdapter result = (ResourceAdapter) raClass
072: .newInstance();
073:
074: // Apply the properties
075: for (Iterator i = cmd.getProperties().iterator(); i.hasNext();) {
076: ConfigPropertyMetaData cpmd = (ConfigPropertyMetaData) i
077: .next();
078: String name = cpmd.getName();
079: String type = cpmd.getType();
080: String value = cpmd.getValue();
081:
082: Class clazz = Thread.currentThread()
083: .getContextClassLoader().loadClass(type);
084: PropertyEditor editor = PropertyEditorManager
085: .findEditor(clazz);
086: if (editor == null)
087: throw new IllegalArgumentException(
088: "No property editor found for property " + cpmd);
089: editor.setAsText(value);
090: Object object = editor.getValue();
091:
092: try {
093: String setter = "set"
094: + Character.toUpperCase(name.charAt(0));
095: if (name.length() > 1)
096: setter = setter.concat(name.substring(1));
097: Method method = raClass.getMethod(setter,
098: new Class[] { clazz });
099: method.invoke(result, new Object[] { object });
100: } catch (InvocationTargetException e) {
101: DeploymentException.rethrowAsDeploymentException(
102: "Error for resource adapter class "
103: + raClass.getName()
104: + " setting property " + cpmd, e
105: .getTargetException());
106: } catch (Throwable t) {
107: DeploymentException.rethrowAsDeploymentException(
108: "Error for resource adapter class "
109: + raClass.getName()
110: + " accessing property setter " + cpmd,
111: t);
112: }
113: }
114:
115: return result;
116: }
117: }
|