package org.allcolor.services.xml.generators;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.allcolor.services.xml.BaseXMLSerializer;
import org.allcolor.services.xml.rest.Bind;
import org.allcolor.services.xml.rest.ResourceParameter;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class JSClientServiceGenerator {
public static class JSDescriptor {
private String httpMethod;
private boolean last = false;
private String name;
private String request;
private List<JSDescriptorResourceParameter> rps = new ArrayList<JSDescriptorResourceParameter>();
public JSDescriptor(final String name, final String httpMethod,
final String request) {
super();
this.name = name;
this.httpMethod = httpMethod;
this.request = request;
}
public String getHttpMethod() {
return this.httpMethod;
}
public String getName() {
return this.name;
}
public String getRequest() {
return this.request;
}
public List<JSDescriptorResourceParameter> getRps() {
return this.rps;
}
public boolean isLast() {
return this.last;
}
public void setHttpMethod(final String httpMethod) {
this.httpMethod = httpMethod;
}
public void setLast(final boolean last) {
this.last = last;
}
public void setName(final String name) {
this.name = name;
}
public void setRequest(final String request) {
this.request = request;
}
public void setRps(final List<JSDescriptorResourceParameter> rps) {
this.rps = rps;
}
}
public static class JSDescriptorResourceParameter {
private boolean last = false;
private String parameterName;
private String urlPrefix;
public JSDescriptorResourceParameter(final String urlPrefix,
final String parameterName) {
super();
this.urlPrefix = urlPrefix;
this.parameterName = parameterName;
}
public String getParameterName() {
return this.parameterName;
}
public String getUrlPrefix() {
return this.urlPrefix;
}
public boolean isLast() {
return this.last;
}
public void setLast(final boolean last) {
this.last = last;
}
public void setParameterName(final String parameterName) {
this.parameterName = parameterName;
}
public void setUrlPrefix(final String urlPrefix) {
this.urlPrefix = urlPrefix;
}
}
private static final JSClientServiceGenerator gen = new JSClientServiceGenerator();
public static String getJSClientService() {
try {
final Template tpl = JSClientServiceGenerator.gen.ve
.getTemplate("org/allcolor/services/xml/generators/JSClient.vm");
final VelocityContext vc = new VelocityContext();
final StringWriter sw = new StringWriter();
tpl.merge(vc, sw);
return sw.toString();
} catch (final Throwable t) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
pw.flush();
return sw.toString();
}
}
public static String toJSClientService(final Class<?> service,
final String serviceLocation) {
try {
final Template tpl = JSClientServiceGenerator.gen.ve
.getTemplate("org/allcolor/services/xml/generators/JSService.vm");
final VelocityContext vc = new VelocityContext();
final List<JSDescriptor> list = new ArrayList<JSDescriptor>();
final Method[] array = service.getDeclaredMethods();
for (final Method m : array) {
final Bind bind = m.getAnnotation(Bind.class);
if ((bind != null)
&& (m.getParameterTypes().length == 2)
&& (m.getParameterTypes()[0] != HttpServletRequest.class)) {
final JSDescriptor descr = new JSDescriptor(m.getName(),
bind.httpMethod().toString(), BaseXMLSerializer
.getXMLName(m.getParameterTypes()[0]));
for (final ResourceParameter rp : bind.resourceParameters()) {
final JSDescriptorResourceParameter jdrp = new JSDescriptorResourceParameter(
rp.urlPrefix(), rp.parameterName());
descr.getRps().add(jdrp);
}
if (descr.getRps().size() > 0) {
final JSDescriptorResourceParameter jdrp = descr
.getRps().get(descr.getRps().size() - 1);
jdrp.setLast(true);
}
list.add(descr);
}
}
if (list.size() > 0) {
final JSDescriptor descr = list.get(list.size() - 1);
descr.setLast(true);
}
vc.put("service", service.getSimpleName());
vc.put("service_location", serviceLocation);
vc.put("descriptors", list);
final StringWriter sw = new StringWriter();
tpl.merge(vc, sw);
return sw.toString();
} catch (final Throwable t) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
pw.flush();
return sw.toString();
}
}
private final VelocityEngine ve;
private JSClientServiceGenerator() {
this.ve = new VelocityEngine();
this.ve.setProperty("resource.loader", "class");
this.ve
.setProperty("class.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
try {
this.ve.init();
} catch (final Throwable t) {
throw new RuntimeException(t);
}
}
}
|