001: /******************************************************************************
002: * LZClientObject.java
003: * ****************************************************************************/package org.openlaszlo.remote;
004:
005: import java.io.*;
006: import java.lang.reflect.*;
007: import javax.servlet.http.*;
008: import org.openlaszlo.iv.flash.util.*;
009: import org.openlaszlo.iv.flash.api.action.*;
010: import org.openlaszlo.iv.flash.api.*;
011: import org.openlaszlo.utils.*;
012: import org.openlaszlo.xml.internal.DataCommon;
013: import org.openlaszlo.xml.internal.DataContext;
014: import org.openlaszlo.sc.ScriptCompiler;
015: import org.apache.log4j.*;
016:
017: /**
018: * Utility class to create object SWF based on a server object.
019: */
020: public class LZClientObjectJSON {
021: public static Logger mLogger = Logger
022: .getLogger(LZClientObjectJSON.class);
023:
024: /**
025: */
026: public static String createObjectProgram(String classname,
027: String scope, Class c) {
028: if (mLogger.isDebugEnabled()) {
029: mLogger.debug("createObjectProgram (JSON)(" + classname
030: + "," + "," + scope + "," + c.getName() + ")");
031: }
032:
033: StringBuffer body = new StringBuffer();
034:
035: // Tells client data returned is an object
036: // this.__LZstubload = true
037: body.append("{");
038: body.append("__LZstubload: true, ");
039: // SOAP specific information
040: body.append("stubinfo: ");
041: body.append("{remoteClass: " + ScriptCompiler.quote(classname)
042: + "}, ");
043:
044: // object.stub
045: body.append("stub: ");
046: {
047:
048: body.append("{");
049:
050: int count = 0;
051: boolean onlyStatic = "staticobject".equals(scope);
052:
053: //------------------------------------------------------------
054: // Create methods for object
055: Method[] methods = c.getMethods();
056: for (int i = 0; i < methods.length; i++) {
057:
058: // Skip Object methods
059: if (methods[i].getDeclaringClass() == Object.class) {
060: continue;
061: }
062:
063: // Check if we only want static methods
064: if (onlyStatic
065: && !Modifier
066: .isStatic(methods[i].getModifiers())) {
067: continue;
068: }
069:
070: // Skip toString method.
071: String methodName = methods[i].getName();
072: if ("toString".equals(methodName)) {
073: continue;
074: }
075:
076: count++;
077:
078: if (i > 0) {
079: body.append(",");
080: }
081:
082: //------------------------------------------------------------
083: //
084: body.append(methodName + ": function (){");
085: {
086: // Check to see if method's last two parameters are
087: // HttpServletRequest and HttpServletResponse.
088: Class[] params = methods[i].getParameterTypes();
089: int len = params.length;
090: boolean doreq = // next to last or last argument
091: (len > 1 && params[len - 2] == HttpServletRequest.class)
092: || (len > 0 && params[len - 1] == HttpServletRequest.class);
093: boolean dores = // should be last argument
094: (len > 0 && params[len - 1] == HttpServletResponse.class);
095:
096: // arguments.callee.args gets set in the client and includes
097: // secure and secureport information.
098: //
099: body.append(" var args = arguments.callee.args;\n");
100: //
101:
102: // _root.LzRemote.invoke(
103: // arguments,
104: // delegate,
105: // classname,
106: // classname + "." + methodName,
107: // { op: 'session', doreq..., dores... },
108: // this.secure,
109: // this.secureport
110: // );
111:
112: body.append("return LzJavaRPCService.invoke(");
113: body.append("arguments[1], ");
114: body.append("arguments[0], ");
115:
116: body.append("{");
117: {
118: // { op: 'invoke', oname: varname, scope: scope,
119: // objectreturntype: objectreturntype,
120: // doreq: TBD, dores: TBD}
121:
122: body.append("op: 'invoke', ");
123: body
124: .append("objectreturntype: args.objectreturntype, ");
125: body.append("oname: args.attributename, ");
126: body.append("scope: args.scope ,");
127: body.append("classname: "
128: + ScriptCompiler.quote(classname)
129: + ", ");
130: body.append("methodname: "
131: + ScriptCompiler.quote(classname + "."
132: + methodName));
133:
134: if (doreq) {
135: body.append(", doreq: true");
136: }
137: if (dores) {
138: body.append(", dores: true");
139: }
140: }
141: body.append("}, \n");
142: body.append("args.secure, args.securport);\n");
143: }
144: body.append("}");
145: }
146: body.append("}");
147: }
148:
149: body.append("}\n");
150:
151: return body.toString();
152: }
153:
154: /**
155: Return JSON representation of CLASSNAME
156: */
157: public static byte[] createObject(String classname, String scope,
158: int ignore) throws IOException {
159: if (mLogger.isDebugEnabled()) {
160: mLogger.debug("createObject(JSON)(" + classname + ","
161: + scope + ")");
162: }
163:
164: Class c;
165: try {
166: c = Class.forName(classname);
167: } catch (ClassNotFoundException e) {
168: throw new RuntimeException("Can't find class " + classname);
169: }
170:
171: int i = 0;
172: try {
173: String buf = createObjectProgram(classname, scope, c);
174: mLogger.debug("ClientObject:");
175: mLogger.debug(buf);
176: return buf.getBytes("UTF-8");
177: } catch (IOException e) {
178: mLogger.error("io error creating object JSON: "
179: + e.getMessage());
180: throw e;
181: }
182: }
183:
184: }
|