[jboss-cvs] JBossAS SVN: r104507 - in projects/jboss-jca/trunk/codegenerator/src: test/java/org/jboss/jca/codegenerator and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed May 5 21:36:02 EDT 2010
Author: jeff.zhang
Date: 2010-05-05 21:36:01 -0400 (Wed, 05 May 2010)
New Revision: 104507
Added:
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/Profile.java
projects/jboss-jca/trunk/codegenerator/src/test/java/org/jboss/jca/codegenerator/JCA16AnnoProfileTestCase.java
Removed:
projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/SimpleTemplate.java
projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Template.java
projects/jboss-jca/trunk/codegenerator/src/test/java/org/jboss/jca/codegenerator/SimpleTemplateTestCase.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/Main.java
projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Utils.java
Log:
[JBJCA-309] refactor code to support different profile
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 01:03:56 UTC (rev 104506)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Definition.java 2010-05-06 01:36:01 UTC (rev 104507)
@@ -31,6 +31,8 @@
*/
public class Definition
{
+ /** output directory */
+ private String outputDir;
/** resource adapter package name */
private String raPackage;
/** resource adapter class name */
@@ -39,6 +41,26 @@
private List<ConfigPropType> raConfigProps;
/**
+ * Set the outputDir.
+ *
+ * @param outputDir The outputDir to set.
+ */
+ public void setOutputDir(String outputDir)
+ {
+ this.outputDir = outputDir;
+ }
+
+ /**
+ * Get the outputDir.
+ *
+ * @return the outputDir.
+ */
+ public String getOutputDir()
+ {
+ return outputDir;
+ }
+
+ /**
* Set the raPackage.
*
* @param raPackage The raPackage to set.
Copied: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/JCA16AnnoProfile.java (from rev 104499, projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/SimpleTemplate.java)
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/JCA16AnnoProfile.java (rev 0)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/JCA16AnnoProfile.java 2010-05-06 01:36:01 UTC (rev 104507)
@@ -0,0 +1,508 @@
+/*
+ * 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.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+import java.net.URL;
+
+/**
+ * A JCA16AnnoProfile.
+ *
+ * @author Jeff Zhang
+ * @version $Revision: $
+ */
+public class JCA16AnnoProfile implements Profile
+{
+
+ /**
+ * JCA16AnnoProfile
+ */
+ public JCA16AnnoProfile()
+ {
+ }
+
+
+ /**
+ * generate code
+ * @param def Definition
+ * @param packageName the writer to output the text to.
+ */
+ @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();
+ }
+ }
+
+ /**
+ * Output generation code
+ * @param def definition
+ * @param out Writer
+ * @throws IOException ioException
+ */
+ public void writeDown(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
+ */
+ 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)
+ 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);
+ }
+ }
+
+ /**
+ * 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++)
+ {
+ 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
+ */
+ 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 01:03:56 UTC (rev 104506)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Main.java 2010-05-06 01:36:01 UTC (rev 104507)
@@ -22,7 +22,6 @@
package org.jboss.jca.codegenerator;
import java.io.BufferedReader;
-import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
@@ -88,7 +87,7 @@
String className = in.readLine();
- Template template = new SimpleTemplate();
+ Profile template = new JCA16AnnoProfile();
Definition def = new Definition();
def.setRaPackage(packageName);
def.setRaClass(className);
@@ -111,17 +110,14 @@
props.add(config);
}
def.setRaConfigProps(props);
+
+ def.setOutputDir(outputDir);
- FileWriter fw = createSrcFile(className + ".java", packageName, outputDir);
- template.process(def, fw);
- fw.close();
+ //FileWriter fw = Utils.createSrcFile(className + ".java", packageName, outputDir);
+ template.generate(def, packageName);
+ //fw.close();
- //ant build.xml
- FileWriter antfw = createFile("build.xml", outputDir);
- URL headerFile = SimpleTemplate.class.getResource("/build.xml.template");
- String headerString = Utils.readFileIntoString(headerFile);
- antfw.write(headerString);
- antfw.close();
+ generateAntXml(outputDir);
System.out.println(dbconf.getString("java.wrote"));
}
@@ -130,57 +126,21 @@
e.printStackTrace();
}
}
-
- /**
- * Create source file
- * @param name The name of the class
- * @param packageName The package name
- * @param outDir output directory
- * @return The file
- * @exception IOException Thrown if an error occurs
- */
- private static FileWriter createSrcFile(String name, String packageName, String outDir) throws IOException
- {
- String directory = "src";
- if (packageName != null && !packageName.trim().equals(""))
- {
- directory = directory + File.separatorChar +
- packageName.replace('.', File.separatorChar);
- }
-
- File path = new File(outDir, directory);
- if (!path.exists())
- path.mkdirs();
-
- File file = new File(path.getAbsolutePath() + File.separatorChar + name);
-
- if (file.exists())
- file.delete();
-
- return new FileWriter(file);
- }
-
/**
- * Create file
- * @param name The name of the class
- * @param outDir output directory
- * @return The file
- * @exception IOException Thrown if an error occurs
+ * generateAnt build.xml
+ * @param outputDir output directory
*/
- private static FileWriter createFile(String name, String outDir) throws IOException
+ private static void generateAntXml(String outputDir) throws IOException
{
- File path = new File(outDir);
- if (!path.exists())
- path.mkdirs();
-
- File file = new File(path.getAbsolutePath() + File.separatorChar + name);
-
- if (file.exists())
- file.delete();
-
- return new FileWriter(file);
+ //ant build.xml
+ FileWriter antfw = Utils.createFile("build.xml", outputDir);
+ URL headerFile = Main.class.getResource("/build.xml.template");
+ String headerString = Utils.readFileIntoString(headerFile);
+ antfw.write(headerString);
+ antfw.close();
}
+
/**
* Tool usage
Copied: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Profile.java (from rev 104499, projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Template.java)
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Profile.java (rev 0)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Profile.java 2010-05-06 01:36:01 UTC (rev 104507)
@@ -0,0 +1,39 @@
+/*
+ * 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;
+
+
+/**
+ * Profile interface.
+ *
+ * @author Jeff Zhang
+ * @version $Revision: $
+ */
+public interface Profile
+{
+ /**
+ * generate code
+ * @param def Definition
+ * @param packageName the writer to output the text to.
+ */
+ public void generate(Definition def, String packageName);
+}
Deleted: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/SimpleTemplate.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/SimpleTemplate.java 2010-05-06 01:03:56 UTC (rev 104506)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/SimpleTemplate.java 2010-05-06 01:36:01 UTC (rev 104507)
@@ -1,498 +0,0 @@
-/*
- * 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;
-
-/**
- * A SimpleTemplate.
- *
- * @author Jeff Zhang
- * @version $Revision: $
- */
-public class SimpleTemplate implements Template
-{
-
- /**
- * SimpleTemplate
- */
- public SimpleTemplate()
- {
- }
-
-
- /**
- * Processes the template
- * @param def Definition
- * @param out the writer to output the text to.
- */
- @Override
- public void process(Definition def, Writer out)
- {
- try
- {
- writeDown(def, out);
- out.flush();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
-
- /**
- * Output generation code
- * @param def definition
- * @param out Writer
- * @throws IOException ioException
- */
- public void writeDown(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
- */
- private void writeheader(Definition def, Writer out) throws IOException
- {
- URL headerFile = SimpleTemplate.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)
- 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);
- }
- }
-
- /**
- * 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++)
- {
- 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
- */
- 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 ");
- 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);
- }
-
-}
Deleted: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Template.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Template.java 2010-05-06 01:03:56 UTC (rev 104506)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Template.java 2010-05-06 01:36:01 UTC (rev 104507)
@@ -1,40 +0,0 @@
-/*
- * 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.Writer;
-
-/**
- * Template interface.
- *
- * @author Jeff Zhang
- * @version $Revision: $
- */
-public interface Template
-{
- /**
- * Processes the template
- * @param def Definition
- * @param out the writer to output the text to.
- */
- public void process(Definition def, Writer out);
-}
Modified: projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Utils.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Utils.java 2010-05-06 01:03:56 UTC (rev 104506)
+++ projects/jboss-jca/trunk/codegenerator/src/main/java/org/jboss/jca/codegenerator/Utils.java 2010-05-06 01:36:01 UTC (rev 104507)
@@ -21,6 +21,8 @@
*/
package org.jboss.jca.codegenerator;
+import java.io.File;
+import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -82,4 +84,56 @@
}
return s.toString();
}
+
+
+ /**
+ * Create source file
+ * @param name The name of the class
+ * @param packageName The package name
+ * @param outDir output directory
+ * @return The file
+ * @exception IOException Thrown if an error occurs
+ */
+ public static FileWriter createSrcFile(String name, String packageName, String outDir) throws IOException
+ {
+ String directory = "src";
+
+ if (packageName != null && !packageName.trim().equals(""))
+ {
+ directory = directory + File.separatorChar +
+ packageName.replace('.', File.separatorChar);
+ }
+
+ File path = new File(outDir, directory);
+ if (!path.exists())
+ path.mkdirs();
+
+ File file = new File(path.getAbsolutePath() + File.separatorChar + name);
+
+ if (file.exists())
+ file.delete();
+
+ return new FileWriter(file);
+ }
+
+ /**
+ * Create file
+ * @param name The name of the class
+ * @param outDir output directory
+ * @return The file
+ * @exception IOException Thrown if an error occurs
+ */
+ public static FileWriter createFile(String name, String outDir) throws IOException
+ {
+ File path = new File(outDir);
+ if (!path.exists())
+ path.mkdirs();
+
+ File file = new File(path.getAbsolutePath() + File.separatorChar + name);
+
+ if (file.exists())
+ file.delete();
+
+ return new FileWriter(file);
+ }
}
Copied: projects/jboss-jca/trunk/codegenerator/src/test/java/org/jboss/jca/codegenerator/JCA16AnnoProfileTestCase.java (from rev 104499, projects/jboss-jca/trunk/codegenerator/src/test/java/org/jboss/jca/codegenerator/SimpleTemplateTestCase.java)
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/test/java/org/jboss/jca/codegenerator/JCA16AnnoProfileTestCase.java (rev 0)
+++ projects/jboss-jca/trunk/codegenerator/src/test/java/org/jboss/jca/codegenerator/JCA16AnnoProfileTestCase.java 2010-05-06 01:36:01 UTC (rev 104507)
@@ -0,0 +1,67 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008-2009, 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.StringWriter;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.logging.Logger;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ * A JCA16AnnoProfile test case.
+ *
+ * @author Jeff Zhang
+ * @version $Revision: $
+ */
+public class JCA16AnnoProfileTestCase
+{
+ private static Logger log = Logger.getLogger(JCA16AnnoProfileTestCase.class);
+
+
+ /**
+ * test process
+ * @throws Throwable throwable exception
+ */
+ @Test
+ public void testProcessFile() throws Throwable
+ {
+ JCA16AnnoProfile profile = new JCA16AnnoProfile();
+ Definition def = new Definition();
+ def.setRaPackage("org.jboss.jca.test");
+ def.setRaClass("BaseResourceAdapter");
+
+ List<ConfigPropType> props = new ArrayList<ConfigPropType>();
+ ConfigPropType config = new ConfigPropType("myProp", "String", "Hello");
+ props.add(config);
+ def.setRaConfigProps(props);
+
+ StringWriter writer = new StringWriter();
+ profile.writeDown(def, writer);
+ assertTrue(writer.toString().indexOf("org.jboss.jca.test") > 0);
+ assertTrue(writer.toString().indexOf("getMyProp") > 0);
+ }
+}
Deleted: projects/jboss-jca/trunk/codegenerator/src/test/java/org/jboss/jca/codegenerator/SimpleTemplateTestCase.java
===================================================================
--- projects/jboss-jca/trunk/codegenerator/src/test/java/org/jboss/jca/codegenerator/SimpleTemplateTestCase.java 2010-05-06 01:03:56 UTC (rev 104506)
+++ projects/jboss-jca/trunk/codegenerator/src/test/java/org/jboss/jca/codegenerator/SimpleTemplateTestCase.java 2010-05-06 01:36:01 UTC (rev 104507)
@@ -1,67 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2008-2009, 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.StringWriter;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.jboss.logging.Logger;
-
-import org.junit.Test;
-import static org.junit.Assert.*;
-
-/**
- * A SimpleTemplate test case.
- *
- * @author Jeff Zhang
- * @version $Revision: $
- */
-public class SimpleTemplateTestCase
-{
- private static Logger log = Logger.getLogger(SimpleTemplateTestCase.class);
-
-
- /**
- * test process
- * @throws Throwable throwable exception
- */
- @Test
- public void testProcessFile() throws Throwable
- {
- SimpleTemplate template = new SimpleTemplate();
- Definition def = new Definition();
- def.setRaPackage("org.jboss.jca.test");
- def.setRaClass("BaseResourceAdapter");
-
- List<ConfigPropType> props = new ArrayList<ConfigPropType>();
- ConfigPropType config = new ConfigPropType("myProp", "String", "Hello");
- props.add(config);
- def.setRaConfigProps(props);
-
- StringWriter writer = new StringWriter();
- template.process(def, writer);
- assertTrue(writer.toString().indexOf("org.jboss.jca.test") > 0);
- assertTrue(writer.toString().indexOf("getMyProp") > 0);
- }
-}
More information about the jboss-cvs-commits
mailing list