[jboss-cvs] JBossAS SVN: r104517 - in projects/jboss-jca/trunk/codegenerator/src: main/resources and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu May 6 10:18:21 EDT 2010
Author: jeff.zhang
Date: 2010-05-06 10:18:19 -0400 (Thu, 06 May 2010)
New Revision: 104517
Added:
projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/AbstractCodeGen.java
projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/ConnImplCodeGen.java
projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/ConnInterfaceCodeGen.java
projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/McCodeGen.java
projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/McfCodeGen.java
projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/RaCodeGen.java
Modified:
projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Definition.java
projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/JCA16AnnoProfile.java
projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Main.java
projects/jboss-jca/trunk/codegenerator/src/main/resources/codegenerator.properties
projects/jboss-jca/trunk/codegenerator/src/main/resources/codegenerator_zh_CN.properties
projects/jboss-jca/trunk/codegenerator/src/test/java/org/jboss/jca/codegenerator/JCA16AnnoProfileTestCase.java
Log:
[JBJCA-307] Generate java files of rar package, RA, MCF, MC, ConnInterface, ConnImpl
Added: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/AbstractCodeGen.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/AbstractCodeGen.java (rev 0)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/AbstractCodeGen.java 2010-05-06 14:18:19 UTC (rev 104517)
@@ -0,0 +1,222 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jca.codegenerator;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.net.URL;
+
+/**
+ * Abstract CodeGenerator.
+ *
+ * @author Jeff Zhang
+ * @version $Revision:$
+ */
+public abstract class AbstractCodeGen
+{
+ /**
+ * generate code
+ * @param def Definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ public void generate(Definition def, Writer out) throws IOException
+ {
+ writeheader(def, out);
+ writeImport(def, out);
+ writeClassComment(def, out);
+ writeClassBody(def, out);
+ }
+
+
+ /**
+ * Output class head, for example license
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ void writeheader(Definition def, Writer out) throws IOException
+ {
+ URL headerFile = AbstractCodeGen.class.getResource("/header.template");
+ String headerString = Utils.readFileIntoString(headerFile);
+ out.write(headerString);
+ writeEol(out);
+ }
+
+ /**
+ * Output class comment
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ void writeClassComment(Definition def, Writer out) throws IOException
+ {
+ out.write("/**");
+ writeEol(out);
+ out.write(" * " + getClassName(def));
+ writeEol(out);
+ out.write(" * @version $Revision: $");
+ writeEol(out);
+ out.write(" */");
+ writeEol(out);
+ }
+
+ /**
+ * get this class name
+ * @param def definition
+ * @return String class name
+ */
+ public abstract String getClassName(Definition def);
+
+ /**
+ * Output class import
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ public abstract void writeImport(Definition def, Writer out) throws IOException;
+
+
+ /**
+ * Output ResourceAdapater class
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ public abstract void writeClassBody(Definition def, Writer out) throws IOException;
+
+ /**
+ * Output eol
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ void writeEol(Writer out) throws IOException
+ {
+ out.write("\n");
+ }
+
+ /**
+ * Output left curly bracket
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ void writeLeftCurlyBracket(Writer out, int indent) throws IOException
+ {
+ writeEol(out);
+ writeIndent(out, indent);
+ out.write("{");
+ writeEol(out);
+ }
+
+ /**
+ * Output right curly bracket
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ void writeRightCurlyBracket(Writer out, int indent) throws IOException
+ {
+ writeEol(out);
+ writeIndent(out, indent);
+ out.write("}");
+ writeEol(out);
+ }
+
+ /**
+ * Output space
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ void writeIndent(Writer out, int indent) throws IOException
+ {
+ for (int i = 0; i < indent; i++)
+ out.write(" ");
+ }
+
+ /**
+ * Upcase first letter
+ * @param name string
+ * @return String name string
+ */
+ String upcaseFisrt(String name)
+ {
+ StringBuilder sb = new StringBuilder();
+ sb.append(name.substring(0, 1).toUpperCase());
+ sb.append(name.substring(1));
+ return sb.toString();
+ }
+
+
+ /**
+ * Output hashCode method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ void writeHashCode(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("@Override");
+ writeEol(out);
+ writeIndent(out, indent);
+ out.write("public int hashCode()");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("return 42;");
+
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+
+
+ /**
+ * Output equals method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ void writeEquals(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("@Override");
+ writeEol(out);
+ writeIndent(out, indent);
+ out.write("public boolean equals(Object other)");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("if (other == null)");
+ writeEol(out);
+ writeIndent(out, indent + 2);
+ out.write("return false;");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return getClass().equals(other.getClass());");
+
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+
+}
Added: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/ConnImplCodeGen.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/ConnImplCodeGen.java (rev 0)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/ConnImplCodeGen.java 2010-05-06 14:18:19 UTC (rev 104517)
@@ -0,0 +1,89 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jca.codegenerator;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * A connection impl class CodeGen.
+ *
+ * @author Jeff Zhang
+ * @version $Revision: $
+ */
+public class ConnImplCodeGen extends AbstractCodeGen
+{
+
+ /**
+ * Output ResourceAdapater class
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ @Override
+ public void writeClassBody(Definition def, Writer out) throws IOException
+ {
+ int indent = 1;
+ out.write("public class " + getClassName(def) + " implements " + def.getConnInterfaceClass());
+ writeLeftCurlyBracket(out, 0);
+ writeIndent(out, indent);
+ out.write("private static Logger log = Logger.getLogger(" + getClassName(def) + ".class);");
+ writeEol(out);
+ writeEol(out);
+ writeIndent(out, indent);
+ out.write("public void callMe()");
+ writeLeftCurlyBracket(out, 0);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call callMe\");");
+
+ writeRightCurlyBracket(out, indent);
+ writeRightCurlyBracket(out, 0);
+ }
+
+ /**
+ * Output class import
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ @Override
+ public void writeImport(Definition def, Writer out) throws IOException
+ {
+ out.write("package " + def.getRaPackage() + ";");
+ writeEol(out);
+ writeEol(out);
+ out.write("import org.jboss.logging.Logger;");
+ writeEol(out);
+ writeEol(out);
+ }
+
+ /**
+ * get this class name
+ * @param def definition
+ * @return String class name
+ */
+ @Override
+ public String getClassName(Definition def)
+ {
+ return def.getConnImplClass();
+ }
+}
Added: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/ConnInterfaceCodeGen.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/ConnInterfaceCodeGen.java (rev 0)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/ConnInterfaceCodeGen.java 2010-05-06 14:18:19 UTC (rev 104517)
@@ -0,0 +1,80 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jca.codegenerator;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * A connection interface CodeGen.
+ *
+ * @author Jeff Zhang
+ * @version $Revision: $
+ */
+public class ConnInterfaceCodeGen extends AbstractCodeGen
+{
+
+ /**
+ * Output ResourceAdapater class
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ @Override
+ public void writeClassBody(Definition def, Writer out) throws IOException
+ {
+ int indent = 1;
+ out.write("public interface " + getClassName(def));
+ writeLeftCurlyBracket(out, 0);
+
+ writeIndent(out, indent);
+ out.write("public void callMe();");
+ writeEol(out);
+
+ writeRightCurlyBracket(out, 0);
+ }
+
+ /**
+ * Output class import
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ @Override
+ public void writeImport(Definition def, Writer out) throws IOException
+ {
+ out.write("package " + def.getRaPackage() + ";");
+ writeEol(out);
+ writeEol(out);
+ }
+
+ /**
+ * get this class name
+ * @param def definition
+ * @return String class name
+ */
+ @Override
+ public String getClassName(Definition def)
+ {
+ return def.getConnInterfaceClass();
+ }
+}
Modified: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Definition.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Definition.java 2010-05-06 07:52:13 UTC (rev 104516)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Definition.java 2010-05-06 14:18:19 UTC (rev 104517)
@@ -37,9 +37,17 @@
private String raPackage;
/** resource adapter class name */
private String raClass;
- /** resource adapter config properties */
+ /** resource adapter configuration properties */
private List<ConfigPropType> raConfigProps;
-
+ /** managed connection factory class name */
+ private String mcfClass;
+ /** managed connection class name */
+ private String mcClass;
+ /** connection interface name */
+ private String connInterfaceClass;
+ /** connection impl class name */
+ private String connImplClass;
+
/**
* Set the outputDir.
*
@@ -119,4 +127,85 @@
{
return raConfigProps;
}
+
+ /**
+ * Set the mcfClass.
+ *
+ * @param mcfClass The mcfClass to set.
+ */
+ public void setMcfClass(String mcfClass)
+ {
+ this.mcfClass = mcfClass;
+ }
+
+ /**
+ * Get the mcfClass.
+ *
+ * @return the mcfClass.
+ */
+ public String getMcfClass()
+ {
+ return mcfClass;
+ }
+
+ /**
+ * Set the mcClass.
+ *
+ * @param mcClass The mcClass to set.
+ */
+ public void setMcClass(String mcClass)
+ {
+ this.mcClass = mcClass;
+ }
+
+ /**
+ * Get the mcClass.
+ *
+ * @return the mcClass.
+ */
+ public String getMcClass()
+ {
+ return mcClass;
+ }
+
+ /**
+ * Set the connInterfaceClass.
+ *
+ * @param connInterfaceClass The connInterfaceClass to set.
+ */
+ public void setConnInterfaceClass(String connInterfaceClass)
+ {
+ this.connInterfaceClass = connInterfaceClass;
+ }
+
+ /**
+ * Get the connInterfaceClass.
+ *
+ * @return the connInterfaceClass.
+ */
+ public String getConnInterfaceClass()
+ {
+ return connInterfaceClass;
+ }
+
+ /**
+ * Set the connImplClass.
+ *
+ * @param connImplClass The connImplClass to set.
+ */
+ public void setConnImplClass(String connImplClass)
+ {
+ this.connImplClass = connImplClass;
+ }
+
+ /**
+ * Get the connImplClass.
+ *
+ * @return the connImplClass.
+ */
+ public String getConnImplClass()
+ {
+ return connImplClass;
+ }
+
}
Modified: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/JCA16AnnoProfile.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/JCA16AnnoProfile.java 2010-05-06 07:52:13 UTC (rev 104516)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/JCA16AnnoProfile.java 2010-05-06 14:18:19 UTC (rev 104517)
@@ -22,9 +22,6 @@
package org.jboss.jca.codegenerator;
import java.io.FileWriter;
-import java.io.IOException;
-import java.io.Writer;
-import java.net.URL;
/**
* A JCA16AnnoProfile.
@@ -51,458 +48,42 @@
@Override
public void generate(Definition def, String packageName)
{
- FileWriter fw = null;
-
- try
- {
- fw = Utils.createSrcFile(def.getRaClass() + ".java", packageName, def.getOutputDir());
-
- writeDown(def, fw);
- fw.flush();
- fw.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
+ generateClassCode(def, "Ra");
+ generateClassCode(def, "Mcf");
+ generateClassCode(def, "Mc");
+ generateClassCode(def, "ConnInterface");
+ generateClassCode(def, "ConnImpl");
}
/**
- * Output generation code
- * @param def definition
- * @param out Writer
- * @throws IOException ioException
+ * generate ResourceAdapater code
+ * @param def Definition
+ * @param packageName the writer to output the text to.
*/
- public void writeDown(Definition def, Writer out) throws IOException
+ private void generateClassCode(Definition def, String className)
{
- writeheader(def, out);
- writeImport(def, out);
- writeClassComment(def, out);
- writeClassBody(def, out);
-
- }
-
- /**
- * Output class head, for example license
- * @param def definition
- * @param out Writer
- * @throws IOException ioException
- */
- private void writeheader(Definition def, Writer out) throws IOException
- {
- URL headerFile = JCA16AnnoProfile.class.getResource("/header.template");
- String headerString = Utils.readFileIntoString(headerFile);
- out.write(headerString);
- writeEol(out);
- }
-
- /**
- * Output class comment
- * @param def definition
- * @param out Writer
- * @throws IOException ioException
- */
- private void writeClassComment(Definition def, Writer out) throws IOException
- {
- out.write("/**");
- writeEol(out);
- out.write(" * " + def.getRaClass());
- writeEol(out);
- out.write(" * @version $Revision: $");
- writeEol(out);
- out.write(" */");
- writeEol(out);
- }
-
- /**
- * Output class import
- * @param def definition
- * @param out Writer
- * @throws IOException ioException
- */
- private void writeImport(Definition def, Writer out) throws IOException
- {
- out.write("package " + def.getRaPackage() + ";");
- writeEol(out);
- writeEol(out);
- out.write("import javax.resource.ResourceException;");
- writeEol(out);
- out.write("import javax.resource.spi.ActivationSpec;");
- writeEol(out);
- out.write("import javax.resource.spi.BootstrapContext;");
- writeEol(out);
- out.write("import javax.resource.spi.ConfigProperty;");
- writeEol(out);
- out.write("import javax.resource.spi.Connector;");
- writeEol(out);
- out.write("import javax.resource.spi.ResourceAdapter;");
- writeEol(out);
- out.write("import javax.resource.spi.ResourceAdapterInternalException;");
- writeEol(out);
- out.write("import javax.resource.spi.endpoint.MessageEndpointFactory;");
- writeEol(out);
- writeEol(out);
- out.write("import javax.transaction.xa.XAResource;");
- writeEol(out);
- writeEol(out);
- out.write("import org.jboss.logging.Logger;");
- writeEol(out);
- writeEol(out);
- }
-
- /**
- * Output eol
- * @param out Writer
- * @throws IOException ioException
- */
- private void writeEol(Writer out) throws IOException
- {
- out.write("\n");
- }
-
- /**
- * Output left curly bracket
- * @param out Writer
- */
- private void writeLeftCurlyBracket(Writer out, int indent) throws IOException
- {
- writeEol(out);
- writeIndent(out, indent);
- out.write("{");
- writeEol(out);
- }
-
- /**
- * Output right curly bracket
- * @param out Writer
- */
- private void writeRightCurlyBracket(Writer out, int indent) throws IOException
- {
- writeEol(out);
- writeIndent(out, indent);
- out.write("}");
- writeEol(out);
- }
-
- /**
- * Output space
- * @param out Writer
- */
- private void writeIndent(Writer out, int indent) throws IOException
- {
- for (int i = 0; i < indent; i++)
- out.write(" ");
- }
-
- /**
- * Output class body
- * @param def definition
- * @param out Writer
- */
- private void writeClassBody(Definition def, Writer out) throws IOException
- {
- out.write("@Connector");
- writeEol(out);
- out.write("public class " + def.getRaClass() + " implements ResourceAdapter");
- writeLeftCurlyBracket(out, 0);
- writeEol(out);
-
- int indent = 1;
- writeIndent(out, indent);
- out.write("private static Logger log = Logger.getLogger(" + def.getRaClass() + ".class);");
- writeEol(out);
- writeEol(out);
-
- //constructor
- writeIndent(out, indent);
- out.write("public " + def.getRaClass() + "()");
- writeLeftCurlyBracket(out, indent);
- writeRightCurlyBracket(out, indent);
- writeEol(out);
-
- writeConfigProps(def, out, indent);
- writeEndpointLifecycle(def, out, indent);
- writeLifecycle(def, out, indent);
- writeXAResource(def, out, indent);
- writeHashCode(def, out, indent);
- writeEquals(def, out, indent);
-
- writeRightCurlyBracket(out, 0);
- }
-
- /**
- * Output Configuration Properties
- * @param def definition
- * @param out Writer
- * @param indent space number
- */
- private void writeConfigProps(Definition def, Writer out, int indent) throws IOException
- {
- if (def.getRaConfigProps() == null)
+ if (className.equals("") || className == null)
return;
- for (int i = 0; i < def.getRaConfigProps().size(); i++)
+ try
{
- writeIndent(out, indent);
- out.write("@ConfigProperty(defaultValue=\"" + def.getRaConfigProps().get(i).getValue() + "\")");
- writeEol(out);
- writeIndent(out, indent);
- out.write("private " +
- def.getRaConfigProps().get(i).getType() +
- " " +
- def.getRaConfigProps().get(i).getName() +
- ";");
- writeEol(out);
- writeEol(out);
- }
- for (int i = 0; i < def.getRaConfigProps().size(); i++)
- {
- String name = def.getRaConfigProps().get(i).getName();
- String upcaseName = upcaseFisrt(name);
- //set
- writeIndent(out, indent);
- out.write("public void set" +
- upcaseName +
- "(" +
- def.getRaConfigProps().get(i).getType() +
- " " +
- name +
- ")");
- writeLeftCurlyBracket(out, indent);
- writeIndent(out, indent + 1);
- out.write("this." + name + " = " + name + ";");
- writeRightCurlyBracket(out, indent);
- writeEol(out);
+ String clazzName = this.getClass().getPackage().getName() + "." + className + "CodeGen";
+ String javaFile = (String)Definition.class.getMethod(
+ "get" + className + "Class").invoke(def, (Object[])null) + ".java";
+ FileWriter fw = Utils.createSrcFile(javaFile, def.getRaPackage(), def.getOutputDir());
+
+ Class<?> clazz = Class.forName(clazzName, true, Thread.currentThread().getContextClassLoader());
+ AbstractCodeGen codeGen = (AbstractCodeGen)clazz.newInstance();
- //get
- writeIndent(out, indent);
- out.write("public " +
- def.getRaConfigProps().get(i).getType() +
- " get" +
- upcaseName +
- "()");
- writeLeftCurlyBracket(out, indent);
- writeIndent(out, indent + 1);
- out.write("return " + name + ";");
- writeRightCurlyBracket(out, indent);
- writeEol(out);
+ codeGen.generate(def, fw);
+
+ fw.flush();
+ fw.close();
}
- }
-
- /**
- * Upcase first letter
- * @param name string
- * @param out Writer
- * @return String name string
- */
- private String upcaseFisrt(String name)
- {
- StringBuilder sb = new StringBuilder();
- sb.append(name.substring(0, 1).toUpperCase());
- sb.append(name.substring(1));
- return sb.toString();
- }
-
-
- /**
- * Output hashCode method
- * @param def definition
- * @param out Writer
- * @param indent space number
- */
- private void writeHashCode(Definition def, Writer out, int indent) throws IOException
- {
- writeIndent(out, indent);
- out.write("@Override");
- writeEol(out);
- writeIndent(out, indent);
- out.write("public int hashCode()");
- writeLeftCurlyBracket(out, indent);
- writeIndent(out, indent + 1);
- out.write("int result = 17;");
- writeEol(out);
- for (int i = 0; i < def.getRaConfigProps().size(); i++)
+ catch (Exception e)
{
- writeIndent(out, indent + 1);
- String type = def.getRaConfigProps().get(i).getType();
- if (type.equals("int"))
- {
- out.write("result = 31 * result + " + def.getRaConfigProps().get(i).getName() + ";");
- }
- else if (type.equals("short") || type.equals("char") || type.equals("byte"))
- {
- out.write("result = 31 * result + (int)" + def.getRaConfigProps().get(i).getName() + ";");
- }
- else if (type.equals("boolean"))
- {
- out.write("result = 31 * result + (" + def.getRaConfigProps().get(i).getName() + " ? 0 : 1);");
- }
- else if (type.equals("long"))
- {
- out.write("result = 31 * result + (int)(" + def.getRaConfigProps().get(i).getName() +
- " ^ (" + def.getRaConfigProps().get(i).getName() + " >>> 32));");
- }
- else if (type.equals("float"))
- {
- out.write("result = 31 * result + Float.floatToIntBits(" + def.getRaConfigProps().get(i).getName() + ");");
- }
- else if (type.equals("double"))
- {
- out.write("long tolong = Double.doubleToLongBits(" + def.getRaConfigProps().get(i).getName() + ");");
- writeEol(out);
- writeIndent(out, indent + 1);
- out.write("result = 31 * result + (int)(tolong ^ (tolong >>> 32));");
- }
- else
- {
- out.write("result = 31 * result + " + def.getRaConfigProps().get(i).getName() + ".hashCode();");
- }
- writeEol(out);
+ e.printStackTrace();
}
- writeIndent(out, indent + 1);
- out.write("return result;");
- writeRightCurlyBracket(out, indent);
- writeEol(out);
}
-
-
- /**
- * Output equals method
- * @param def definition
- * @param out Writer
- * @param indent space number
- */
- private void writeEquals(Definition def, Writer out, int indent) throws IOException
- {
- writeIndent(out, indent);
- out.write("@Override");
- writeEol(out);
- writeIndent(out, indent);
- out.write("public boolean equals(Object other)");
- writeLeftCurlyBracket(out, indent);
- writeIndent(out, indent + 1);
- out.write("if (other == null)");
- writeEol(out);
- writeIndent(out, indent + 2);
- out.write("return false;");
- writeEol(out);
- writeIndent(out, indent + 1);
- out.write("if (other == this)");
- writeEol(out);
- writeIndent(out, indent + 2);
- out.write("return true;");
- writeEol(out);
- writeIndent(out, indent + 1);
- out.write("if (!(other instanceof " + def.getRaClass() + "))");
- writeEol(out);
- writeIndent(out, indent + 2);
- out.write("return false;");
- writeEol(out);
- writeIndent(out, indent + 1);
- out.write(def.getRaClass() + " ra = (" + def.getRaClass() + ")other;");
- writeEol(out);
- writeIndent(out, indent + 1);
- out.write("return ");
- if (def.getRaConfigProps().size() == 0)
- {
- out.write("true");
- }
- for (int i = 0; i < def.getRaConfigProps().size(); i++)
- {
- if (i != 0)
- {
- writeEol(out);
- writeIndent(out, indent + 2);
- out.write("&& ");
- }
- out.write(def.getRaConfigProps().get(i).getName() + " == ra." + def.getRaConfigProps().get(i).getName());
- }
- out.write(";");
- writeRightCurlyBracket(out, indent);
- writeEol(out);
- }
-
- /**
- * Output getXAResources method
- * @param def definition
- * @param out Writer
- * @param indent space number
- */
- private void writeXAResource(Definition def, Writer out, int indent) throws IOException
- {
- writeIndent(out, indent);
- out.write("public XAResource[] getXAResources(ActivationSpec[] specs)");
- writeEol(out);
- writeIndent(out, indent + 1);
- out.write("throws ResourceException");
- writeLeftCurlyBracket(out, indent);
- writeIndent(out, indent + 1);
- out.write("log.debug(\"call getXAResources\");");
- writeEol(out);
- writeIndent(out, indent + 1);
- out.write("return null;");
- writeRightCurlyBracket(out, indent);
- writeEol(out);
- }
-
- /**
- * Output Lifecycle method
- * @param def definition
- * @param out Writer
- * @param indent space number
- */
- private void writeLifecycle(Definition def, Writer out, int indent) throws IOException
- {
- writeIndent(out, indent);
- out.write("public void start(BootstrapContext ctx)");
- writeEol(out);
- writeIndent(out, indent + 1);
- out.write("throws ResourceAdapterInternalException");
- writeLeftCurlyBracket(out, indent);
- writeIndent(out, indent + 1);
- out.write("log.debug(\"call start\");");
- writeRightCurlyBracket(out, indent);
- writeEol(out);
-
- writeIndent(out, indent);
- out.write("public void stop()");
- writeLeftCurlyBracket(out, indent);
- writeIndent(out, indent + 1);
- out.write("log.debug(\"call stop\");");
- writeRightCurlyBracket(out, indent);
- writeEol(out);
- }
-
- /**
- * Output EndpointLifecycle method
- * @param def definition
- * @param out Writer
- * @param indent space number
- */
- private void writeEndpointLifecycle(Definition def, Writer out, int indent) throws IOException
- {
- writeIndent(out, indent);
- out.write("public void endpointActivation(MessageEndpointFactory endpointFactory,");
- writeEol(out);
- writeIndent(out, indent + 1);
- out.write("ActivationSpec spec) throws ResourceException");
- writeLeftCurlyBracket(out, indent);
- writeIndent(out, indent + 1);
- out.write("log.debug(\"call endpointActivation\");");
- writeRightCurlyBracket(out, indent);
- writeEol(out);
-
- writeIndent(out, indent);
- out.write("public void endpointDeactivation(MessageEndpointFactory endpointFactory,");
- writeEol(out);
- writeIndent(out, indent + 1);
- out.write("ActivationSpec spec)");
- writeLeftCurlyBracket(out, indent);
- writeIndent(out, indent + 1);
- out.write("log.debug(\"call endpointDeactivation\");");
- writeRightCurlyBracket(out, indent);
- writeEol(out);
- }
-
}
Modified: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Main.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Main.java 2010-05-06 07:52:13 UTC (rev 104516)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Main.java 2010-05-06 14:18:19 UTC (rev 104517)
@@ -83,19 +83,19 @@
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print(dbconf.getString("package.name"));
String packageName = in.readLine();
- System.out.print(dbconf.getString("class.name"));
- String className = in.readLine();
+ System.out.print(dbconf.getString("ra.class.name"));
+ String raClassName = in.readLine();
- Profile template = new JCA16AnnoProfile();
+ Profile profile = new JCA16AnnoProfile();
Definition def = new Definition();
def.setRaPackage(packageName);
- def.setRaClass(className);
+ def.setRaClass(raClassName);
List<ConfigPropType> props = new ArrayList<ConfigPropType>();
while (true)
{
- System.out.println(dbconf.getString("config.properties"));
+ System.out.println(dbconf.getString("ra.config.properties"));
System.out.print(" " + dbconf.getString("config.properties.name"));
String name = in.readLine();
if (name == null || name.equals(""))
@@ -111,15 +111,27 @@
}
def.setRaConfigProps(props);
+ System.out.print(dbconf.getString("mcf.class.name"));
+ String mcfClassName = in.readLine();
+ def.setMcfClass(mcfClassName);
+ System.out.print(dbconf.getString("mc.class.name"));
+ String mcClassName = in.readLine();
+ def.setMcClass(mcClassName);
+
+ System.out.print(dbconf.getString("conn.interface.name"));
+ String connInterfaceName = in.readLine();
+ def.setConnInterfaceClass(connInterfaceName);
+ System.out.print(dbconf.getString("conn.class.name"));
+ String connImplName = in.readLine();
+ def.setConnImplClass(connImplName);
+
def.setOutputDir(outputDir);
- //FileWriter fw = Utils.createSrcFile(className + ".java", packageName, outputDir);
- template.generate(def, packageName);
- //fw.close();
+ profile.generate(def, packageName);
generateAntXml(outputDir);
- System.out.println(dbconf.getString("java.wrote"));
+ System.out.println(dbconf.getString("code.wrote"));
}
catch (Exception e)
{
Added: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/McCodeGen.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/McCodeGen.java (rev 0)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/McCodeGen.java 2010-05-06 14:18:19 UTC (rev 104517)
@@ -0,0 +1,293 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jca.codegenerator;
+
+import java.io.IOException;
+import java.io.Writer;
+
+
+/**
+ * A managed connection CodeGen.
+ *
+ * @author Jeff Zhang
+ * @version $Revision: $
+ */
+public class McCodeGen extends AbstractCodeGen
+{
+
+ /**
+ * Output ResourceAdapater class
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ @Override
+ public void writeClassBody(Definition def, Writer out) throws IOException
+ {
+ int indent = 1;
+ out.write("public class " + getClassName(def) + " implements ManagedConnection");
+ writeLeftCurlyBracket(out, 0);
+ writeEol(out);
+
+ writeIndent(out, indent);
+ out.write("private static Logger log = Logger.getLogger(" + getClassName(def) + ".class);");
+ writeEol(out);
+ writeEol(out);
+
+ //constructor
+ writeIndent(out, indent);
+ out.write("public " + getClassName(def) + "()");
+ writeLeftCurlyBracket(out, indent);
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeConnection(def, out, indent);
+ writeLifecycle(def, out, indent);
+ writeConnectionEventListener(def, out, indent);
+ writeLogWriter(def, out, indent);
+ writeTransaction(def, out, indent);
+ writeMetaData(def, out, indent);
+
+ writeRightCurlyBracket(out, 0);
+ }
+
+ /**
+ * Output class import
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ @Override
+ public void writeImport(Definition def, Writer out) throws IOException
+ {
+ out.write("package " + def.getRaPackage() + ";");
+ writeEol(out);
+ writeEol(out);
+ out.write("import java.io.PrintWriter;");
+ writeEol(out);
+ writeEol(out);
+ out.write("import javax.resource.ResourceException;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ConnectionEventListener;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ConnectionDefinition;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ConnectionRequestInfo;");
+ writeEol(out);
+ out.write("import javax.resource.spi.LocalTransaction;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ManagedConnection;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ManagedConnectionMetaData;");
+ writeEol(out);
+ writeEol(out);
+ out.write("import javax.security.auth.Subject;");
+ writeEol(out);
+ out.write("import javax.transaction.xa.XAResource;");
+ writeEol(out);
+ writeEol(out);
+ out.write("import org.jboss.logging.Logger;");
+ writeEol(out);
+ writeEol(out);
+ }
+
+ /**
+ * get this class name
+ * @param def definition
+ * @return String class name
+ */
+ @Override
+ public String getClassName(Definition def)
+ {
+ return def.getMcClass();
+ }
+
+ /**
+ * Output Connection method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeConnection(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("public Object getConnection(Subject subject,");
+ writeEol(out);
+ writeIndent(out, indent + 2);
+ out.write("ConnectionRequestInfo cxRequestInfo) throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call getConnection\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return null;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeIndent(out, indent);
+ out.write("public void associateConnection(Object connection) throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call associateConnection\");");
+
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+
+ /**
+ * Output Lifecycle method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeLifecycle(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("public void cleanup() throws ResourceException");
+ writeEol(out);
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call cleanup\");");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeIndent(out, indent);
+ out.write("public void destroy() throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call destroy\");");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+
+ /**
+ * Output ConnectionEventListener method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeConnectionEventListener(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("public void addConnectionEventListener(ConnectionEventListener listener)");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call addConnectionEventListener\");");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeIndent(out, indent);
+ out.write("public void removeConnectionEventListener(ConnectionEventListener listener)");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call removeConnectionEventListener\");");
+
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+ /**
+ * Output LogWriter method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeLogWriter(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("public PrintWriter getLogWriter() throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call getLogWriter\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return null;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeIndent(out, indent);
+ out.write("public void setLogWriter(PrintWriter out) throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call setLogWriter\");");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+
+ /**
+ * Output Transaction method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeTransaction(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("public LocalTransaction getLocalTransaction() throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call getLocalTransaction\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return null;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeIndent(out, indent);
+ out.write("public XAResource getXAResource() throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call getXAResource\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return null;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+
+ /**
+ * Output MetaData method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeMetaData(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("public ManagedConnectionMetaData getMetaData() throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call getMetaData\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return null;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+}
Added: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/McfCodeGen.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/McfCodeGen.java (rev 0)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/McfCodeGen.java 2010-05-06 14:18:19 UTC (rev 104517)
@@ -0,0 +1,303 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jca.codegenerator;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * A McfCodeGen.
+ *
+ * @author Jeff Zhang
+ * @version $Revision: $
+ */
+public class McfCodeGen extends AbstractCodeGen
+{
+
+ /**
+ * Output ResourceAdapater class
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ @Override
+ public void writeClassBody(Definition def, Writer out) throws IOException
+ {
+ int indent = 1;
+ out.write("@ConnectionDefinition(connectionFactory = ManagedConnection.class,");
+ writeEol(out);
+ writeIndent(out, indent);
+ out.write("connectionFactoryImpl = " + def.getMcClass() + ".class,");
+ writeEol(out);
+ writeIndent(out, indent);
+ out.write("connection = " + def.getConnInterfaceClass() + ".class,");
+ writeEol(out);
+ writeIndent(out, indent);
+ out.write("connectionImpl = " + def.getConnImplClass() + ".class)");
+ writeEol(out);
+ out.write("public class " + getClassName(def) +
+ " implements ManagedConnectionFactory, ResourceAdapterAssociation");
+ writeLeftCurlyBracket(out, 0);
+ writeEol(out);
+
+ writeIndent(out, indent);
+ out.write("private static Logger log = Logger.getLogger(" + getClassName(def) + ".class);");
+ writeEol(out);
+ writeEol(out);
+ writeIndent(out, indent);
+ out.write("private ResourceAdapter ra;");
+ writeEol(out);
+ writeEol(out);
+ writeIndent(out, indent);
+ out.write("private PrintWriter logwriter;");
+ writeEol(out);
+ writeEol(out);
+
+
+ //constructor
+ writeIndent(out, indent);
+ out.write("public " + getClassName(def) + "()");
+ writeLeftCurlyBracket(out, indent);
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeConnectionFactory(def, out, indent);
+ writeManagedConnection(def, out, indent);
+ writeLogWriter(def, out, indent);
+ writeResourceAdapter(def, out, indent);
+
+ writeHashCode(def, out, indent);
+ writeEquals(def, out, indent);
+
+ writeRightCurlyBracket(out, 0);
+ }
+
+ /**
+ * Output class import
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ @Override
+ public void writeImport(Definition def, Writer out) throws IOException
+ {
+ out.write("package " + def.getRaPackage() + ";");
+ writeEol(out);
+ writeEol(out);
+ out.write("import java.io.PrintWriter;");
+ writeEol(out);
+ out.write("import java.util.Set;");
+ writeEol(out);
+ writeEol(out);
+ out.write("import javax.resource.ResourceException;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ConnectionDefinition;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ConnectionManager;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ConnectionRequestInfo;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ManagedConnection;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ManagedConnectionFactory;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ResourceAdapter;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ResourceAdapterAssociation;");
+ writeEol(out);
+ writeEol(out);
+ out.write("import javax.security.auth.Subject;");
+ writeEol(out);
+ writeEol(out);
+ out.write("import org.jboss.logging.Logger;");
+ writeEol(out);
+ writeEol(out);
+ }
+
+ /**
+ * get this class name
+ * @param def definition
+ * @return String class name
+ */
+ @Override
+ public String getClassName(Definition def)
+ {
+ return def.getMcfClass();
+ }
+
+ /**
+ * Output ConnectionFactory method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeConnectionFactory(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("public Object createConnectionFactory(ConnectionManager cxManager) throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("if (ra == null)");
+ writeEol(out);
+ writeIndent(out, indent + 2);
+ out.write("throw new IllegalStateException(\"RA is null\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call createConnectionFactory\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return null;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeIndent(out, indent);
+ out.write("public Object createConnectionFactory() throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("if (ra == null)");
+ writeEol(out);
+ writeIndent(out, indent + 2);
+ out.write("throw new IllegalStateException(\"RA is null\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call createConnectionFactory\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return null;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+
+ /**
+ * Output ConnectionFactory method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeManagedConnection(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("public ManagedConnection createManagedConnection(Subject subject,");
+ writeEol(out);
+ writeIndent(out, indent + 2);
+ out.write("ConnectionRequestInfo cxRequestInfo) throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("if (ra == null)");
+ writeEol(out);
+ writeIndent(out, indent + 2);
+ out.write("throw new IllegalStateException(\"RA is null\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call createManagedConnection\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return null;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeIndent(out, indent);
+ out.write("public ManagedConnection matchManagedConnections(Set connectionSet,");
+ writeEol(out);
+ writeIndent(out, indent + 2);
+ out.write("Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("if (ra == null)");
+ writeEol(out);
+ writeIndent(out, indent + 2);
+ out.write("throw new IllegalStateException(\"RA is null\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call matchManagedConnections\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return null;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+ /**
+ * Output LogWriter method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeLogWriter(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("public PrintWriter getLogWriter() throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call getLogWriter\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return logwriter;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeIndent(out, indent);
+ out.write("public void setLogWriter(PrintWriter out) throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call setLogWriter\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("logwriter = out;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+
+ /**
+ * Output ResourceAdapter method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeResourceAdapter(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("public ResourceAdapter getResourceAdapter()");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call getResourceAdapter\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return ra;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeIndent(out, indent);
+ out.write("public void setResourceAdapter(ResourceAdapter ra)");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call setResourceAdapter\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("this.ra = ra;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+}
Added: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/RaCodeGen.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/RaCodeGen.java (rev 0)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/RaCodeGen.java 2010-05-06 14:18:19 UTC (rev 104517)
@@ -0,0 +1,388 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jca.codegenerator;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * A resource adapter code generator
+ *
+ * @author Jeff Zhang
+ * @version $Revision: 1.1 $
+ */
+public class RaCodeGen extends AbstractCodeGen
+{
+ /**
+ * Output ResourceAdapater class
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ @Override
+ public void writeClassBody(Definition def, Writer out) throws IOException
+ {
+ out.write("@Connector");
+ writeEol(out);
+ out.write("public class " + getClassName(def) + " implements ResourceAdapter");
+ writeLeftCurlyBracket(out, 0);
+ writeEol(out);
+
+ int indent = 1;
+ writeIndent(out, indent);
+ out.write("private static Logger log = Logger.getLogger(" + getClassName(def) + ".class);");
+ writeEol(out);
+ writeEol(out);
+
+ //constructor
+ writeIndent(out, indent);
+ out.write("public " + getClassName(def) + "()");
+ writeLeftCurlyBracket(out, indent);
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeConfigProps(def, out, indent);
+ writeEndpointLifecycle(def, out, indent);
+ writeLifecycle(def, out, indent);
+ writeXAResource(def, out, indent);
+ writeHashCode(def, out, indent);
+ writeEquals(def, out, indent);
+
+ writeRightCurlyBracket(out, 0);
+ }
+
+ /**
+ * Output class import
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ @Override
+ public void writeImport(Definition def, Writer out) throws IOException
+ {
+ out.write("package " + def.getRaPackage() + ";");
+ writeEol(out);
+ writeEol(out);
+ out.write("import javax.resource.ResourceException;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ActivationSpec;");
+ writeEol(out);
+ out.write("import javax.resource.spi.BootstrapContext;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ConfigProperty;");
+ writeEol(out);
+ out.write("import javax.resource.spi.Connector;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ResourceAdapter;");
+ writeEol(out);
+ out.write("import javax.resource.spi.ResourceAdapterInternalException;");
+ writeEol(out);
+ out.write("import javax.resource.spi.endpoint.MessageEndpointFactory;");
+ writeEol(out);
+ writeEol(out);
+ out.write("import javax.transaction.xa.XAResource;");
+ writeEol(out);
+ writeEol(out);
+ out.write("import org.jboss.logging.Logger;");
+ writeEol(out);
+ writeEol(out);
+ }
+
+ /**
+ * Output Configuration Properties
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeConfigProps(Definition def, Writer out, int indent) throws IOException
+ {
+ if (def.getRaConfigProps() == null)
+ return;
+
+ for (int i = 0; i < def.getRaConfigProps().size(); i++)
+ {
+ writeIndent(out, indent);
+ out.write("@ConfigProperty(defaultValue=\"" + def.getRaConfigProps().get(i).getValue() + "\")");
+ writeEol(out);
+ writeIndent(out, indent);
+ out.write("private " +
+ def.getRaConfigProps().get(i).getType() +
+ " " +
+ def.getRaConfigProps().get(i).getName() +
+ ";");
+ writeEol(out);
+ writeEol(out);
+ }
+
+ for (int i = 0; i < def.getRaConfigProps().size(); i++)
+ {
+ String name = def.getRaConfigProps().get(i).getName();
+ String upcaseName = upcaseFisrt(name);
+ //set
+ writeIndent(out, indent);
+ out.write("public void set" +
+ upcaseName +
+ "(" +
+ def.getRaConfigProps().get(i).getType() +
+ " " +
+ name +
+ ")");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("this." + name + " = " + name + ";");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ //get
+ writeIndent(out, indent);
+ out.write("public " +
+ def.getRaConfigProps().get(i).getType() +
+ " get" +
+ upcaseName +
+ "()");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("return " + name + ";");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+ }
+
+ /**
+ * Output hashCode method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ @Override
+ void writeHashCode(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("@Override");
+ writeEol(out);
+ writeIndent(out, indent);
+ out.write("public int hashCode()");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("int result = 17;");
+ writeEol(out);
+ for (int i = 0; i < def.getRaConfigProps().size(); i++)
+ {
+ writeIndent(out, indent + 1);
+ String type = def.getRaConfigProps().get(i).getType();
+ if (type.equals("int"))
+ {
+ out.write("result = 31 * result + " + def.getRaConfigProps().get(i).getName() + ";");
+ }
+ else if (type.equals("short") || type.equals("char") || type.equals("byte"))
+ {
+ out.write("result = 31 * result + (int)" + def.getRaConfigProps().get(i).getName() + ";");
+ }
+ else if (type.equals("boolean"))
+ {
+ out.write("result = 31 * result + (" + def.getRaConfigProps().get(i).getName() + " ? 0 : 1);");
+ }
+ else if (type.equals("long"))
+ {
+ out.write("result = 31 * result + (int)(" + def.getRaConfigProps().get(i).getName() +
+ " ^ (" + def.getRaConfigProps().get(i).getName() + " >>> 32));");
+ }
+ else if (type.equals("float"))
+ {
+ out.write("result = 31 * result + Float.floatToIntBits(" + def.getRaConfigProps().get(i).getName() + ");");
+ }
+ else if (type.equals("double"))
+ {
+ out.write("long tolong = Double.doubleToLongBits(" + def.getRaConfigProps().get(i).getName() + ");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("result = 31 * result + (int)(tolong ^ (tolong >>> 32));");
+ }
+ else
+ {
+ out.write("result = 31 * result + " + def.getRaConfigProps().get(i).getName() + ".hashCode();");
+ }
+ writeEol(out);
+ }
+ writeIndent(out, indent + 1);
+ out.write("return result;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+
+
+ /**
+ * Output equals method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ @Override
+ void writeEquals(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("@Override");
+ writeEol(out);
+ writeIndent(out, indent);
+ out.write("public boolean equals(Object other)");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("if (other == null)");
+ writeEol(out);
+ writeIndent(out, indent + 2);
+ out.write("return false;");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("if (other == this)");
+ writeEol(out);
+ writeIndent(out, indent + 2);
+ out.write("return true;");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("if (!(other instanceof " + def.getRaClass() + "))");
+ writeEol(out);
+ writeIndent(out, indent + 2);
+ out.write("return false;");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write(def.getRaClass() + " ra = (" + def.getRaClass() + ")other;");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return ");
+ if (def.getRaConfigProps().size() == 0)
+ {
+ out.write("true");
+ }
+ for (int i = 0; i < def.getRaConfigProps().size(); i++)
+ {
+ if (i != 0)
+ {
+ writeEol(out);
+ writeIndent(out, indent + 2);
+ out.write("&& ");
+ }
+ out.write(def.getRaConfigProps().get(i).getName() + " == ra." + def.getRaConfigProps().get(i).getName());
+ }
+ out.write(";");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+
+ /**
+ * Output getXAResources method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeXAResource(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("public XAResource[] getXAResources(ActivationSpec[] specs)");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call getXAResources\");");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("return null;");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+
+ /**
+ * Output Lifecycle method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeLifecycle(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("public void start(BootstrapContext ctx)");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("throws ResourceAdapterInternalException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call start\");");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeIndent(out, indent);
+ out.write("public void stop()");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call stop\");");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+
+ /**
+ * Output EndpointLifecycle method
+ * @param def definition
+ * @param out Writer
+ * @param indent space number
+ * @throws IOException ioException
+ */
+ private void writeEndpointLifecycle(Definition def, Writer out, int indent) throws IOException
+ {
+ writeIndent(out, indent);
+ out.write("public void endpointActivation(MessageEndpointFactory endpointFactory,");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("ActivationSpec spec) throws ResourceException");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call endpointActivation\");");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+
+ writeIndent(out, indent);
+ out.write("public void endpointDeactivation(MessageEndpointFactory endpointFactory,");
+ writeEol(out);
+ writeIndent(out, indent + 1);
+ out.write("ActivationSpec spec)");
+ writeLeftCurlyBracket(out, indent);
+ writeIndent(out, indent + 1);
+ out.write("log.debug(\"call endpointDeactivation\");");
+ writeRightCurlyBracket(out, indent);
+ writeEol(out);
+ }
+
+ /**
+ * get this class name
+ * @param def definition
+ * @return String class name
+ */
+ @Override
+ public String getClassName(Definition def)
+ {
+ return def.getRaClass();
+ }
+}
Modified: projects/jboss-jca/trunk/codegenerator/src/main/resources/codegenerator.properties
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/resources/codegenerator.properties 2010-05-06 07:52:13 UTC (rev 104516)
+++ projects/jboss-jca/trunk/codegenerator/src/main/resources/codegenerator.properties 2010-05-06 14:18:19 UTC (rev 104517)
@@ -1,8 +1,12 @@
package.name=Package name:
-class.name=Resource adapter class name:
-config.properties=Resource adapter config properties [enter to quit]:
+ra.class.name=Resource adapter class name:
+mcf.class.name=Managed connection factory class name:
+mc.class.name=Managed connection class name:
+conn.interface.name=Connection interface name:
+conn.class.name=Connection implementation name:
+ra.config.properties=Resource adapter config properties [enter to quit]:
config.properties.name=Name:
config.properties.type=Type:
config.properties.value=Value:
output.dir=Output directory:
-java.wrote=Code generated
\ No newline at end of file
+code.wrote=Code generated
\ No newline at end of file
Modified: projects/jboss-jca/trunk/codegenerator/src/main/resources/codegenerator_zh_CN.properties
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/resources/codegenerator_zh_CN.properties 2010-05-06 07:52:13 UTC (rev 104516)
+++ projects/jboss-jca/trunk/codegenerator/src/main/resources/codegenerator_zh_CN.properties 2010-05-06 14:18:19 UTC (rev 104517)
@@ -1,9 +1,2 @@
#zh_CN
-package.name=请输入ResourceAdapter包名:
-class.name=请输入ResourceAdapter 类名:
-config.properties=请输入 config properties [回车退出]:
-config.properties.name=名字:
-config.properties.type=类型:
-config.properties.value=赋值:
-output.dir=请输入代码生成目录:
-java.wrote=Java文件已经生成
+
Modified: projects/jboss-jca/trunk/codegenerator/src/test/java/org/jboss/jca/codegenerator/JCA16AnnoProfileTestCase.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/test/java/org/jboss/jca/codegenerator/JCA16AnnoProfileTestCase.java 2010-05-06 07:52:13 UTC (rev 104516)
+++ projects/jboss-jca/trunk/codegenerator/src/test/java/org/jboss/jca/codegenerator/JCA16AnnoProfileTestCase.java 2010-05-06 14:18:19 UTC (rev 104517)
@@ -49,7 +49,6 @@
@Test
public void testProcessFile() throws Throwable
{
- JCA16AnnoProfile profile = new JCA16AnnoProfile();
Definition def = new Definition();
def.setRaPackage("org.jboss.jca.test");
def.setRaClass("BaseResourceAdapter");
@@ -60,8 +59,10 @@
def.setRaConfigProps(props);
StringWriter writer = new StringWriter();
- profile.writeDown(def, writer);
- assertTrue(writer.toString().indexOf("org.jboss.jca.test") > 0);
+
+ RaCodeGen codeGen = new RaCodeGen();
+ codeGen.writeClassBody(def, writer);
+ assertTrue(writer.toString().indexOf("BaseResourceAdapter") > 0);
assertTrue(writer.toString().indexOf("getMyProp") > 0);
}
}
More information about the jboss-cvs-commits
mailing list