[jboss-cvs] JBossAS SVN: r90953 - in projects/jboss-jca/trunk: sjc/src/main/java/org/jboss/jca/sjc and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Jul 8 16:50:38 EDT 2009
Author: jesper.pedersen
Date: 2009-07-08 16:50:38 -0400 (Wed, 08 Jul 2009)
New Revision: 90953
Modified:
projects/jboss-jca/trunk/doc/developerguide/en/modules/standalone.xml
projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/Main.java
projects/jboss-jca/trunk/sjc/src/main/resources/boot.xsd
Log:
[JBJCA-108] Add support for <constructor>
Modified: projects/jboss-jca/trunk/doc/developerguide/en/modules/standalone.xml
===================================================================
--- projects/jboss-jca/trunk/doc/developerguide/en/modules/standalone.xml 2009-07-08 20:48:35 UTC (rev 90952)
+++ projects/jboss-jca/trunk/doc/developerguide/en/modules/standalone.xml 2009-07-08 20:50:38 UTC (rev 90953)
@@ -89,6 +89,14 @@
<code><inject></code>
<para>injects a bean or a property from a bean.</para>
</listitem>
+ <listitem>
+ <code><constructor></code>
+ <para>which defines the constructor method to use for a bean.</para>
+ </listitem>
+ <listitem>
+ <code><parameter></code>
+ <para>defines a parameter value for a constructor.</para>
+ </listitem>
</itemizedlist>
<para>In order to define locations relative to the install root of the JBoss JCA
Modified: projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/Main.java
===================================================================
--- projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/Main.java 2009-07-08 20:48:35 UTC (rev 90952)
+++ projects/jboss-jca/trunk/sjc/src/main/java/org/jboss/jca/sjc/Main.java 2009-07-08 20:50:38 UTC (rev 90953)
@@ -23,7 +23,9 @@
package org.jboss.jca.sjc;
import org.jboss.jca.sjc.boot.BeanType;
+import org.jboss.jca.sjc.boot.ConstructorType;
import org.jboss.jca.sjc.boot.InjectType;
+import org.jboss.jca.sjc.boot.ParameterType;
import org.jboss.jca.sjc.boot.PropertyType;
import org.jboss.jca.sjc.deployers.Deployer;
@@ -37,8 +39,10 @@
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -240,9 +244,103 @@
*/
private static Object createBean(BeanType bt, ClassLoader cl, File deployDirectory) throws Exception
{
- Class<?> clz = Class.forName(bt.getClazz(), true, cl);
- Object instance = clz.newInstance();
+ Class<?> clz = null;
+ Object instance = null;
+ if (bt.getClazz() != null)
+ {
+ clz = Class.forName(bt.getClazz(), true, cl);
+ instance = clz.newInstance();
+ }
+ else
+ {
+ ConstructorType ct = bt.getConstructor();
+ Class factoryClass = Class.forName(ct.getFactoryClass(), true, cl);
+ Method factoryMethod = null;
+ Object[] args = null;
+
+ if (ct.getParameter() == null)
+ {
+ factoryMethod = factoryClass.getMethod(ct.getFactoryMethod(), (Class[])null);
+ }
+ else
+ {
+ Method[] factoryMethods = factoryClass.getMethods();
+
+ List<Method> candidates = new ArrayList<Method>();
+
+ for (Method m : factoryMethods)
+ {
+ if (ct.getFactoryMethod().equals(m.getName()))
+ {
+ if (ct.getParameter().size() == m.getParameterTypes().length)
+ {
+ boolean include = true;
+ for (int i = 0; include && i < m.getParameterTypes().length; i++)
+ {
+ Class<?> parameterClass = m.getParameterTypes()[i];
+
+ if (!(parameterClass.equals(String.class) ||
+ parameterClass.equals(byte.class) || parameterClass.equals(Byte.class) ||
+ parameterClass.equals(short.class) || parameterClass.equals(Short.class) ||
+ parameterClass.equals(int.class) || parameterClass.equals(Integer.class) ||
+ parameterClass.equals(long.class) || parameterClass.equals(Long.class) ||
+ parameterClass.equals(float.class) || parameterClass.equals(Float.class) ||
+ parameterClass.equals(double.class) || parameterClass.equals(Double.class) ||
+ parameterClass.equals(boolean.class) || parameterClass.equals(Boolean.class) ||
+ parameterClass.equals(char.class) || parameterClass.equals(Character.class) ||
+ parameterClass.equals(InetAddress.class)))
+ {
+ include = false;
+ }
+ }
+
+ if (include)
+ candidates.add(m);
+ }
+ }
+ }
+
+ if (candidates.size() == 1)
+ {
+ factoryMethod = candidates.get(0);
+ args = new Object[ct.getParameter().size()];
+ for (int i = 0; i < ct.getParameter().size(); i++)
+ {
+ args[i] = getValue(ct.getParameter().get(i).getValue(), factoryMethod.getParameterTypes()[i]);
+ }
+ }
+ else
+ {
+ boolean found = false;
+ Iterator<Method> it = candidates.iterator();
+ while (!found && it.hasNext())
+ {
+ try
+ {
+ Method m = it.next();
+ args = new Object[ct.getParameter().size()];
+
+ for (int i = 0; i < ct.getParameter().size(); i++)
+ {
+ args[i] = getValue(ct.getParameter().get(i).getValue(), factoryMethod.getParameterTypes()[i]);
+ }
+
+ factoryMethod = m;
+ found = true;
+ }
+ catch (Throwable t)
+ {
+ // ok - not this one...
+ }
+ }
+ }
+ }
+
+ instance = factoryMethod.invoke((Object)null, args);
+ clz = instance.getClass();
+ }
+
if (bt.getProperty() != null)
{
for (PropertyType pt : bt.getProperty())
@@ -310,6 +408,60 @@
}
/**
+ * Get a value from a string
+ * @param s The string representation
+ * @param clz The class
+ * @return The value
+ * @exception Exception If the string cant be converted
+ */
+ private static Object getValue(String s, Class<?> clz) throws Exception
+ {
+ if (clz.equals(String.class))
+ {
+ return s;
+ }
+ else if (clz.equals(byte.class) || clz.equals(Byte.class))
+ {
+ return Byte.valueOf(s);
+ }
+ else if (clz.equals(short.class) || clz.equals(Short.class))
+ {
+ return Short.valueOf(s);
+ }
+ else if (clz.equals(int.class) || clz.equals(Integer.class))
+ {
+ return Integer.valueOf(s);
+ }
+ else if (clz.equals(long.class) || clz.equals(Long.class))
+ {
+ return Long.valueOf(s);
+ }
+ else if (clz.equals(float.class) || clz.equals(Float.class))
+ {
+ return Float.valueOf(s);
+ }
+ else if (clz.equals(double.class) || clz.equals(Double.class))
+ {
+ return Double.valueOf(s);
+ }
+ else if (clz.equals(boolean.class) || clz.equals(Boolean.class))
+ {
+ return Boolean.valueOf(s);
+ }
+ else if (clz.equals(char.class) || clz.equals(Character.class))
+ {
+ return Character.valueOf(s.charAt(0));
+ }
+ else if (clz.equals(InetAddress.class))
+ {
+ return InetAddress.getByName(s);
+ }
+
+ throw new Exception("Unknown class " + clz.getName() + " for " + s);
+ }
+
+
+ /**
* Set a property on an object instance
* @param instance The object instance
* @param pt The property type definition
Modified: projects/jboss-jca/trunk/sjc/src/main/resources/boot.xsd
===================================================================
--- projects/jboss-jca/trunk/sjc/src/main/resources/boot.xsd 2009-07-08 20:48:35 UTC (rev 90952)
+++ projects/jboss-jca/trunk/sjc/src/main/resources/boot.xsd 2009-07-08 20:50:38 UTC (rev 90953)
@@ -6,11 +6,12 @@
<xsd:complexType name="beanType">
<xsd:sequence>
+ <xsd:element name="constructor" type="constructorType" maxOccurs="1" minOccurs="0"/>
<xsd:element name="property" type="propertyType" maxOccurs="unbounded" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:token" use="required"/>
<xsd:attribute name="interface" type="xsd:token"/>
- <xsd:attribute name="class" type="xsd:token" use="required"/>
+ <xsd:attribute name="class" type="xsd:token"/>
</xsd:complexType>
<xsd:complexType name="propertyType" mixed="true">
@@ -30,12 +31,27 @@
</xsd:simpleContent>
</xsd:complexType>
- <xsd:element name="deployment">
- <xsd:complexType>
- <xsd:sequence>
- <xsd:element name="bean" type="beanType" maxOccurs="unbounded" minOccurs="0"/>
- </xsd:sequence>
- </xsd:complexType>
- </xsd:element>
+ <xsd:complexType name="constructorType">
+ <xsd:sequence>
+ <xsd:element name="parameter" type="parameterType" maxOccurs="unbounded" minOccurs="0"/>
+ </xsd:sequence>
+ <xsd:attribute name="factoryMethod" type="xsd:token" use="required"/>
+ <xsd:attribute name="factoryClass" type="xsd:token" use="required"/>
+ </xsd:complexType>
+ <xsd:complexType name="parameterType">
+ <xsd:simpleContent>
+ <xsd:extension base="xsd:token">
+ </xsd:extension>
+ </xsd:simpleContent>
+ </xsd:complexType>
+
+ <xsd:element name="deployment">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="bean" type="beanType" maxOccurs="unbounded" minOccurs="0"/>
+ </xsd:sequence>
+ </xsd:complexType>
+ </xsd:element>
+
</xsd:schema>
More information about the jboss-cvs-commits
mailing list