001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.webservice.jsonws;
021:
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import java.lang.reflect.Method;
025: import java.util.List;
026:
027: import de.schlund.pfixcore.example.webservices.CallTest;
028: import de.schlund.pfixcore.example.webservices.CallTestImpl;
029: import de.schlund.pfixcore.webservice.Constants;
030: import de.schlund.pfixcore.webservice.ServiceDescriptor;
031: import de.schlund.pfixcore.webservice.ServiceException;
032: import de.schlund.pfixcore.webservice.ServiceStubGenerator;
033: import de.schlund.pfixcore.webservice.config.ServiceConfig;
034: import de.schlund.pfixcore.webservice.jsgen.JsBlock;
035: import de.schlund.pfixcore.webservice.jsgen.JsClass;
036: import de.schlund.pfixcore.webservice.jsgen.JsMethod;
037: import de.schlund.pfixcore.webservice.jsgen.JsParam;
038: import de.schlund.pfixcore.webservice.jsgen.JsStatement;
039:
040: /**
041: * @author mleidig@schlund.de
042: */
043: public class JSONWSStubGenerator implements ServiceStubGenerator {
044:
045: public void generateStub(ServiceConfig service, OutputStream out)
046: throws ServiceException, IOException {
047: ServiceDescriptor desc = new ServiceDescriptor(service);
048: JsParam[] constParams = new JsParam[] { new JsParam("context") };
049:
050: String jsClassName = null;
051: if (service.getStubJSNamespace().equals(
052: Constants.STUBGEN_JSNAMESPACE_COMPAT)) {
053: jsClassName = Constants.STUBGEN_DEFAULT_JSNAMESPACE
054: + service.getName();
055: } else if (service.getStubJSNamespace().equals(
056: Constants.STUBGEN_JSNAMESPACE_COMPATUNIQUE)) {
057: jsClassName = Constants.STUBGEN_JSONWS_JSNAMESPACE
058: + service.getName();
059: } else if (service.getStubJSNamespace().equals(
060: Constants.STUBGEN_JSNAMESPACE_JAVANAME)) {
061: jsClassName = desc.getServiceClass().getName();
062: } else {
063: String prefix = service.getStubJSNamespace();
064: if (prefix.contains(".") && !prefix.endsWith("."))
065: prefix += ".";
066: jsClassName = prefix + service.getName();
067: }
068: JsClass jsClass = new JsClass(jsClassName,
069: "pfx.ws.json.BaseStub", constParams);
070: JsBlock block = jsClass.getConstructorBody();
071: block.addStatement(new JsStatement(
072: "pfx.ws.json.BaseStub.call(this,\"" + service.getName()
073: + "\",context)"));
074: for (String methName : desc.getMethods()) {
075: List<Method> meths = desc.getMethods(methName);
076: for (Method meth : meths) {
077: JsMethod jsMeth = new JsMethod(jsClass, meth.getName());
078: jsClass.addMethod(jsMeth);
079: JsBlock jsBody = jsMeth.getBody();
080: jsBody
081: .addStatement(new JsStatement(
082: "return this.callMethod(\""
083: + meth.getName()
084: + "\",arguments,"
085: + meth.getParameterTypes().length
086: + ")"));
087: }
088: }
089: jsClass.printCode(out);
090: }
091:
092: public static void main(String[] args) throws Exception {
093: ServiceConfig config = new ServiceConfig(null);
094: config.setInterfaceName(CallTest.class.getName());
095: config.setImplementationName(CallTestImpl.class.getName());
096: config.setName("Test");
097: config.setStubJSNamespace(Constants.STUBGEN_JSNAMESPACE_COMPAT);
098: //config.setStubJSNamespace(Constants.STUBGEN_JSNAMESPACE_COMPATUNIQUE);
099: //config.setStubJSNamespace(Constants.STUBGEN_JSNAMESPACE_JAVANAME);
100: //config.setStubJSNamespace("My");
101: //config.setStubJSNamespace("foo.");
102: //config.setStubJSNamespace("foo.bar");
103: (new JSONWSStubGenerator()).generateStub(config, System.out);
104: }
105:
106: }
|