Author: fdesbois Date: 2010-04-01 15:30:48 +0200 (Thu, 01 Apr 2010) New Revision: 1867 Log: [ServiceTransformer] - Manage defaultValue for primitive returnType - Resolve issue for methods with no parameter - Add throw generated exception on interfaces - Add time log Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/ServiceTransformer.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/ServiceTransformer.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/ServiceTransformer.java 2010-04-01 13:28:35 UTC (rev 1866) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/ServiceTransformer.java 2010-04-01 13:30:48 UTC (rev 1867) @@ -3,6 +3,7 @@ import java.util.ArrayList; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.time.DurationFormatUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.eugene.GeneratorUtil; @@ -187,7 +188,7 @@ protected String defaultPackageName; protected String getContextInterfaceName() { - return modelName + "Context"; + return StringUtils.capitalize(modelName) + "Context"; } protected String getContextImplementorInterfaceName() { @@ -195,7 +196,7 @@ } protected String getExceptionClassName() { - return modelName + "Exception"; + return StringUtils.capitalize(modelName) + "Exception"; } protected String getServiceAbstractClassName(String serviceName) { @@ -219,67 +220,68 @@ ObjectModelClass exception = createExceptionClass(); ObjectModelInterface newContextImplementor = - this.createInterface(getContextImplementorInterfaceName(), + createInterface(getContextImplementorInterfaceName(), defaultPackageName); ObjectModelInterface newContext = - this.createInterface(getContextInterfaceName(), + createInterface(getContextInterfaceName(), defaultPackageName); - this.addInterface(newContextImplementor, + addInterface(newContextImplementor, newContext.getQualifiedName()); if (contextImplementor != null) { // Copy of defined operations // interfaces of contextImplementor are not copied - copyInterfaceOperations(contextImplementor, newContextImplementor); + copyInterfaceOperations( + contextImplementor, newContextImplementor, false); } if (context != null) { // Copy of defined operations // interfaces of context are not copied - copyInterfaceOperations(context, newContext); + copyInterfaceOperations(context, newContext, false); } ObjectModelOperation beginTransaction = - this.addOperation(newContextImplementor, + addOperation(newContextImplementor, "beginTransaction", TopiaContext.class); - this.addException(beginTransaction, TopiaException.class); + addException(beginTransaction, TopiaException.class); ObjectModelOperation doCatch1 = - this.addOperation(newContextImplementor, "doCatch", "void"); - this.addParameter(doCatch1, Exception.class, "eee"); - this.addParameter(doCatch1, String.class, "message"); - this.addParameter(doCatch1, "Object...", "args"); - this.addException(doCatch1, exception.getQualifiedName()); + addOperation(newContextImplementor, "doCatch", "void"); + addParameter(doCatch1, Exception.class, "eee"); + addParameter(doCatch1, String.class, "message"); + addParameter(doCatch1, "Object...", "args"); + addException(doCatch1, exception.getQualifiedName()); ObjectModelOperation doCatch2 = - this.addOperation(newContextImplementor, "doCatch", "void"); - this.addParameter(doCatch2, TopiaContext.class, "transaction"); - this.addParameter(doCatch2, Exception.class, "eee"); - this.addParameter(doCatch2, String.class, "message"); - this.addParameter(doCatch2, "Object...", "args"); - this.addException(doCatch2, exception.getQualifiedName()); + addOperation(newContextImplementor, "doCatch", "void"); + addParameter(doCatch2, TopiaContext.class, "transaction"); + addParameter(doCatch2, Exception.class, "eee"); + addParameter(doCatch2, String.class, "message"); + addParameter(doCatch2, "Object...", "args"); + addException(doCatch2, exception.getQualifiedName()); ObjectModelOperation doFinally = - this.addOperation(newContextImplementor, "doFinally", "void"); - this.addParameter(doFinally, TopiaContext.class, "transaction"); + addOperation(newContextImplementor, "doFinally", "void"); + addParameter(doFinally, TopiaContext.class, "transaction"); } protected ObjectModelClass createExceptionClass() { ObjectModelClass exception = - this.createClass(getExceptionClassName(), defaultPackageName); + createClass(getExceptionClassName(), defaultPackageName); - this.setSuperClass(exception, RuntimeException.class); - this.addAttribute(exception, "args", "Object[]", null, + setSuperClass(exception, RuntimeException.class); + addAttribute(exception, "args", "Object[]", null, ObjectModelModifier.PROTECTED); ObjectModelOperation constructor = - this.addConstructor(exception, ObjectModelModifier.PUBLIC); + addConstructor(exception, ObjectModelModifier.PUBLIC); - this.addParameter(constructor, Throwable.class, "eee"); - this.addParameter(constructor, String.class, "message"); - this.addParameter(constructor, "Object...", "args"); + addParameter(constructor, Throwable.class, "eee"); + addParameter(constructor, String.class, "message"); + addParameter(constructor, "Object...", "args"); setOperationBody(constructor, "" /*{ @@ -289,7 +291,7 @@ ); ObjectModelOperation getArgs = - this.addOperation(exception, "getArgs", "Object[]", + addOperation(exception, "getArgs", "Object[]", ObjectModelModifier.PUBLIC); setOperationBody(getArgs, "" @@ -299,7 +301,7 @@ ); ObjectModelOperation hasArgs = - this.addOperation(exception, "hasArgs", "boolean", + addOperation(exception, "hasArgs", "boolean", ObjectModelModifier.PUBLIC); setOperationBody(hasArgs, "" @@ -317,25 +319,32 @@ * * @param source interface * @param dest interface + * @param throwException if generated exception is thrown */ protected void copyInterfaceOperations(ObjectModelInterface source, - ObjectModelInterface dest) { + ObjectModelInterface dest, boolean throwException) { setDocumentation(dest, source.getDocumentation()); + if (throwException) { + addImport(dest, defaultPackageName + "." + getExceptionClassName()); + } for (ObjectModelOperation op : source.getOperations()) { ObjectModelOperation newOp = - this.addOperation(dest, + addOperation(dest, op.getName(), op.getReturnType()); setDocumentation(newOp.getReturnParameter(), op.getReturnParameter().getDocumentation()); for (ObjectModelParameter param : op.getParameters()) { ObjectModelParameter newParam = - this.addParameter(newOp, param.getType(), + addParameter(newOp, param.getType(), param.getName()); setDocumentation(newParam, param.getDocumentation()); } for (String ex : op.getExceptions()) { - this.addException(newOp, ex); + addException(newOp, ex); } + if (throwException) { + addException(newOp, getExceptionClassName()); + } setDocumentation(newOp, op.getDocumentation()); } } @@ -349,59 +358,57 @@ // Create INTERFACE ObjectModelInterface serviceInterface = - this.createInterface(interfacez.getName(), + createInterface(interfacez.getName(), interfacez.getPackageName()); - copyInterfaceOperations(interfacez, serviceInterface); + copyInterfaceOperations(interfacez, serviceInterface, true); // Create ABSTRACT CLASS - ObjectModelClass service = this.createAbstractClass( + ObjectModelClass service = createAbstractClass( getServiceAbstractClassName(interfacez.getName()), interfacez.getPackageName()); - this.addInterface(service, serviceInterface.getQualifiedName()); + addInterface(service, serviceInterface.getQualifiedName()); // Add Logger // FIXME in EUGene, we want the default value not to be managed // for import. -// this.addAttribute(service, "log", +// addAttribute(service, "log", // Log.class, // "LogFactory.getLog(" + interfacez.getName() + ".class)", // ObjectModelModifier.PRIVATE, // ObjectModelModifier.STATIC, // ObjectModelModifier.FINAL); - this.addAttribute(service, "log", + addAttribute(service, "log", Log.class, null, ObjectModelModifier.PRIVATE, ObjectModelModifier.FINAL); - this.addImport(service, Log.class); - this.addImport(service, LogFactory.class); + addImport(service, Log.class); + addImport(service, LogFactory.class); String contextFqn = defaultPackageName + "." + getContextImplementorInterfaceName(); // Add Context Attribute + constructor - this.addAttribute(service, "context", contextFqn, null, + addAttribute(service, "context", contextFqn, null, ObjectModelModifier.PROTECTED); // Constructor ObjectModelOperation constructor = - this.addConstructor(service, ObjectModelModifier.PUBLIC); - //this.addParameter(constructor, contextFqn, "context"); + addConstructor(service, ObjectModelModifier.PUBLIC); setOperationBody(constructor, "" /*{ - //this.context = context; // FIXME : must be fixed attribute value in EUGene this.log = LogFactory.getLog(<%=interfacez.getName()%>.class); }*/ ); ObjectModelOperation setContext = - this.addOperation(service, "setContext", "void", + addOperation(service, "setContext", "void", ObjectModelModifier.PUBLIC); - this.addParameter(setContext, contextFqn, "context"); + addParameter(setContext, contextFqn, "context"); setOperationBody(setContext, "" /*{ this.context = context; @@ -413,9 +420,10 @@ String serviceName = GeneratorUtil.toLowerCaseFirstLetter(interfacez.getName()); - this.addImport(service, TopiaContext.class); - this.addImport(service, I18n.class); - this.addImport(service, ArrayList.class); + addImport(service, TopiaContext.class); + addImport(service, I18n.class); + addImport(service, ArrayList.class); + addImport(service, DurationFormatUtils.class); for (ObjectModelOperation op : interfacez.getOperations()) { @@ -445,27 +453,27 @@ // Implementation of interface operation ObjectModelOperation implOp = - this.addOperation(service, + addOperation(service, op.getName(), op.getReturnType(), ObjectModelModifier.PUBLIC); - this.addAnnotation(service, implOp, Override.class.getSimpleName()); + addAnnotation(service, implOp, Override.class.getSimpleName()); String opName = StringUtils.capitalize(op.getName()); // Abstract operation to execute method content ObjectModelOperation abstOp = - this.addOperation(service, "execute" + opName, + addOperation(service, "execute" + opName, op.getReturnType(), ObjectModelModifier.ABSTRACT, ObjectModelModifier.PROTECTED); // Throw all exception from abstract method // They will be catched by interface method to use doCatch - this.addException(abstOp, Exception.class); + addException(abstOp, Exception.class); if (needTransaction) { - this.addParameter(abstOp, TopiaContext.class, "transaction"); - this.addException(abstOp, TopiaException.class); + addParameter(abstOp, TopiaContext.class, "transaction"); + addException(abstOp, TopiaException.class); } String toStringAppend = ""; @@ -477,15 +485,15 @@ opParams += "errorArgs"; separatorParams = ", "; // Add errorArgs to abstract operation - this.addParameter(abstOp, + addParameter(abstOp, "java.util.List<Object>", "errorArgs"); } // Copy other operation parameters for (ObjectModelParameter param : op.getParameters()) { String paramName = param.getName(); - this.addParameter(implOp, param.getType(), param.getName()); - this.addParameter(abstOp, param.getType(), param.getName()); + addParameter(implOp, param.getType(), param.getName()); + addParameter(abstOp, param.getType(), param.getName()); // Prepare Log toStringAppend += @@ -509,11 +517,15 @@ doCatchParams += needErrorArgs ? ", errorArgs.toArray()" : ""; // Return managment + String opReturnType = ""; String opReturn = ""; String finalReturn = ""; if (!op.getReturnType().equals("void")) { - opReturn = "return "; - finalReturn = "return null;"; + opReturnType = GeneratorUtil.getSimpleName(op.getReturnType()) + + " result = "; + opReturn = "return result;"; + finalReturn = "return " + + getReturnValue(op.getReturnType()) + ";"; } if (needErrorArgs) { @@ -535,7 +547,7 @@ ); // Add transaction in the execute operation parameters // and doCatch parameters - opParams = "transaction, " + opParams; + opParams = "transaction" + separatorParams + opParams; doCatchParams = "transaction, " + doCatchParams; } else { buffer.append("" @@ -547,18 +559,29 @@ buffer.append("" /*{ - if (log.isDebugEnabled()) { + if (log.isInfoEnabled()) { String message = new StringBuilder("<%=first%>:[ <%=opName%> ]")<%=toStringAppend%>. toString(); - log.debug(message); + log.info(message); } + long startTime = 0; + if (log.isDebugEnabled()) { + startTime = System.currentTimeMillis(); + } - <%=opReturn%>execute<%=opName%>(<%=opParams%>); }*/); + <%=opReturnType%>execute<%=opName%>(<%=opParams%>); + if (log.isDebugEnabled()) { + long stopTime = System.currentTimeMillis(); + log.debug("<%=first%>:[ <%=opName%> ] Time = " + + DurationFormatUtils.formatDurationHMS( + stopTime - startTime)); + } + <%=opReturn%> }*/); // Copy exceptions for (String ex : op.getExceptions()) { - this.addException(implOp, ex); - this.addException(abstOp, ex); + addException(implOp, ex); + addException(abstOp, ex); // Add catch block for known exceptions we want to throw String exName = GeneratorUtil.getSimpleName(ex); buffer.append("" @@ -587,8 +610,41 @@ }*/ ); - this.setOperationBody(implOp, buffer.toString()); + setOperationBody(implOp, buffer.toString()); } } + + + protected String getReturnValue(String returnType) { + try { + Primitive prim = + Primitive.valueOf(StringUtils.upperCase(returnType)); + return prim.getValue(); + // If not defined in Primitive enum, return null + } catch (IllegalArgumentException eee) { + return null; + } + } + + public enum Primitive { + BYTE("0"), + SHORT("0"), + INT("0"), + LONG("0"), + FLOAT("0."), + DOUBLE("0."), + CHAR("''"), + BOOLEAN("false"); + + private String value; + + Primitive(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + } }