001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1997-2004 Gerald Brose.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: */
020:
021: package org.jacorb.idl;
022:
023: /**
024: * @author Gerald Brose
025: * @version $Id: Method.java,v 1.20 2006/10/13 20:01:01 andre.spiegel Exp $
026: *
027: * This class is used to represent accessor operations
028: */
029:
030: import java.io.PrintWriter;
031:
032: public class Method implements Operation {
033: public TypeSpec resultType;
034: public TypeSpec parameterType;
035:
036: private String name;
037: private RaisesExpr raisesExpr;
038:
039: private boolean pseudo;
040:
041: public Method(TypeSpec res, TypeSpec params, String name,
042: RaisesExpr raisesExpr, boolean pseudo) {
043: resultType = res;
044: parameterType = params;
045: this .name = name;
046: this .raisesExpr = raisesExpr;
047: this .pseudo = pseudo;
048: }
049:
050: public boolean isGetter() {
051: return resultType != null;
052: }
053:
054: public String name() {
055: return name;
056: }
057:
058: public String opName() {
059: if (isGetter())
060: return "_get_" + name;
061: else
062: return "_set_" + name;
063: }
064:
065: public String signature() {
066: StringBuffer sb = new StringBuffer();
067: sb.append(name + "(");
068: if (parameterType != null) {
069: sb.append(parameterType.toString());
070: }
071: sb.append(")");
072: return sb.toString();
073: }
074:
075: public void printSignature(PrintWriter ps) {
076: printSignature(ps, pseudo);
077: }
078:
079: /**
080: * @param printModifiers whether "public abstract" should be added
081: */
082: public void printSignature(PrintWriter ps, boolean printModifiers) {
083: ps.print("\t");
084: if (printModifiers)
085: ps.print("public abstract ");
086:
087: if (isGetter()) {
088: ps.print(resultType.toString());
089: ps.print(" " + name + "()");
090: raisesExpr.print(ps);
091: ps.println(";");
092: } else {
093: ps.print("void " + name + "(");
094: ps.print(parameterType.toString());
095: ps.print(" arg)");
096: raisesExpr.print(ps);
097: ps.println(";");
098: }
099: }
100:
101: public void printMethod(PrintWriter ps, String classname,
102: boolean is_local, boolean is_abstract) {
103: ps.print("\tpublic ");
104:
105: if (isGetter()) {
106: // accessor method
107: ps.print(resultType.toString());
108: ps.print(" " + name + "()");
109: raisesExpr.print(ps);
110: ps.println();
111: ps.println("\t{");
112: ps.println("\t\twhile(true)");
113: ps.println("\t\t{");
114:
115: // remote part, not for locality constrained objects
116: //
117: if (!is_local) {
118: ps.println("\t\tif(! this._is_local())");
119: ps.println("\t\t{");
120:
121: ps
122: .println("\t\t\torg.omg.CORBA.portable.InputStream _is = null;");
123:
124: ps.println("\t\t\ttry");
125: ps.println("\t\t\t{");
126: ps
127: .println("\t\t\t\torg.omg.CORBA.portable.OutputStream _os = _request(\"_get_"
128: + name + "\",true);");
129: ps.println("\t\t\t\t_is = _invoke(_os);");
130: TypeSpec ts = resultType.typeSpec();
131: ps.println("\t\t\t\treturn "
132: + ts.printReadExpression("_is") + ";");
133: ps.println("\t\t\t}");
134: ps
135: .println("\t\t\tcatch( org.omg.CORBA.portable.RemarshalException _rx ){}");
136: ps
137: .println("\t\t\tcatch( org.omg.CORBA.portable.ApplicationException _ax )");
138: ps.println("\t\t\t{");
139: ps.println("\t\t\t\tString _id = _ax.getId();");
140:
141: if (!raisesExpr.empty()) {
142: String[] exceptIds = raisesExpr.getExceptionIds();
143: String[] classNames = raisesExpr
144: .getExceptionClassNames();
145: ps.print("\t\t\t\t");
146: for (int i = 0; i < exceptIds.length; i++) {
147: if (i > 0) {
148: ps.print("\t\t\t\telse ");
149: }
150: ps.println("if( _id.equals(\"" + exceptIds[i]
151: + "\"))");
152: ps.println("\t\t\t\t{");
153: ps.println("\t\t\t\t\tthrow " + classNames[i]
154: + "Helper.read(_ax.getInputStream());");
155: ps.println("\t\t\t\t}");
156: }
157: }
158:
159: ps
160: .println("\t\t\t\tthrow new RuntimeException(\"Unexpected exception \" + _id );");
161: ps.println("\t\t\t}");
162: ps.println("\t\t\tfinally");
163: ps.println("\t\t\t{");
164: ps.println("\t\t\t\tthis._releaseReply(_is);");
165: ps.println("\t\t\t}");
166: ps.println("\t\t}\n");
167:
168: // local part
169: ps.println("\t\telse");
170: ps.println("\t\t{");
171: }
172:
173: ps
174: .println("\t\torg.omg.CORBA.portable.ServantObject _so = _servant_preinvoke( \"_get_"
175: + name + "\", _opsClass);");
176:
177: ps.println("\t\tif( _so == null )");
178: ps
179: .println("\t\t\tthrow new org.omg.CORBA.UNKNOWN(\"local invocations not supported!\");");
180: if (is_abstract) {
181: ps.println("\t\t\t" + classname + " _localServant = ("
182: + classname + ")_so.servant;");
183: } else {
184: ps.println("\t\t\t" + classname
185: + "Operations _localServant = (" + classname
186: + "Operations)_so.servant;");
187: }
188:
189: ps.println("\t\t\t" + resultType + " _result;");
190:
191: ps.println("\t\ttry");
192: ps.println("\t\t{");
193: ps.println("\t\t\t_result = _localServant." + name + "();");
194: ps.println("\t\t}");
195: ps.println("\t\tfinally");
196: ps.println("\t\t{");
197: ps.println("\t\t\t_servant_postinvoke(_so);");
198: ps.println("\t\t}");
199: ps.println("\t\treturn _result;");
200: ps.println("\t\t}");
201: if (!is_local)
202: ps.println("\t\t}\n");
203: ps.println("\t}\n");
204: } else {
205: /** modifier */
206:
207: ps.print("void " + name + "(" + parameterType.toString());
208: ps.print(" a)");
209: raisesExpr.print(ps);
210: ps.println();
211: ps.println("\t{");
212: ps.println("\t\twhile(true)");
213: ps.println("\t\t{");
214: // remote part not for locality constrained objects
215: //
216: if (!is_local) {
217: ps.println("\t\tif(! this._is_local())");
218: ps.println("\t\t{");
219: ps
220: .println("\t\t\torg.omg.CORBA.portable.InputStream _is = null;");
221:
222: ps.println("\t\t\ttry");
223: ps.println("\t\t\t{");
224: ps
225: .println("\t\t\t\torg.omg.CORBA.portable.OutputStream _os = _request(\"_set_"
226: + name + "\",true);");
227: ps.println("\t\t\t\t"
228: + parameterType.typeSpec().printWriteStatement(
229: "a", "_os"));
230: ps.println("\t\t\t\t_is = _invoke(_os);");
231: ps.println("\t\t\t\treturn;");
232: ps.println("\t\t\t}");
233: ps
234: .println("\t\t\tcatch( org.omg.CORBA.portable.RemarshalException _rx ){}");
235: ps
236: .println("\t\t\tcatch( org.omg.CORBA.portable.ApplicationException _ax )");
237: ps.println("\t\t\t{");
238: ps.println("\t\t\t\tString _id = _ax.getId();");
239:
240: if (!raisesExpr.empty()) {
241: String[] exceptIds = raisesExpr.getExceptionIds();
242: String[] classNames = raisesExpr
243: .getExceptionClassNames();
244: ps.print("\t\t\t\t");
245: for (int i = 0; i < exceptIds.length; i++) {
246: if (i > 0) {
247: ps.print("\t\t\t\telse ");
248: }
249: ps.println("if( _id.equals(\"" + exceptIds[i]
250: + "\"))");
251: ps.println("\t\t\t\t{");
252: ps.println("\t\t\t\t\tthrow " + classNames[i]
253: + "Helper.read(_ax.getInputStream());");
254: ps.println("\t\t\t\t}");
255: }
256: }
257:
258: ps
259: .println("\t\t\t\tthrow new RuntimeException(\"Unexpected exception \" + _id );");
260: ps.println("\t\t\t}");
261: ps.println("\t\t\tfinally");
262: ps.println("\t\t\t{");
263: ps.println("\t\t\t\tthis._releaseReply(_is);");
264: ps.println("\t\t\t}");
265: ps.println("\t\t}\n");
266:
267: // local part
268: ps.println("\t\telse");
269: ps.println("\t\t{");
270:
271: }
272: ps
273: .println("\t\t\torg.omg.CORBA.portable.ServantObject _so = _servant_preinvoke( \"_set_"
274: + name + "\", _opsClass);");
275:
276: ps.println("\t\t\tif( _so == null )");
277: ps
278: .println("\t\t\t\tthrow new org.omg.CORBA.UNKNOWN(\"local invocations not supported!\");");
279: ps.println("\t\t\t" + classname
280: + "Operations _localServant = (" + classname
281: + "Operations)_so.servant;");
282:
283: ps.println("\t\t\t\ttry");
284: ps.println("\t\t\t\t{");
285: ps.println("\t\t\t\t\t_localServant." + name + "(a);");
286: ps.println("\t\t\t\t}");
287: ps.println("\t\t\t\tfinally");
288: ps.println("\t\t\t\t{");
289: ps.println("\t\t\t\t\t_servant_postinvoke(_so);");
290: ps.println("\t\t\t\t}");
291: ps.println("\t\t\t\treturn;");
292: ps.println("\t\t\t}");
293: if (!is_local)
294: ps.println("\t\t}\n");
295: ps.println("\t}\n");
296: }
297: }
298:
299: public void print_sendc_Method(PrintWriter ps, String classname) {
300: ps.print("\tpublic void sendc_");
301:
302: if (isGetter()) {
303: // accessor method
304: ps.print("get_" + name);
305: ps.println("(AMI_" + classname + "Handler ami_handler)");
306: ps.println("\t{");
307: ps.println("\t\twhile(true)");
308: ps.println("\t\t{");
309:
310: ps.println("\t\t\ttry");
311: ps.println("\t\t\t{");
312: ps
313: .println("\t\t\t\torg.omg.CORBA.portable.OutputStream _os = _request(\"_get_"
314: + name + "\",true);");
315: ps
316: .println("\t\t\t\t((org.jacorb.orb.Delegate)_get_delegate()).invoke(this, _os, ami_handler);");
317: ps.println("\t\t\t\treturn;");
318: ps.println("\t\t\t}");
319: ps
320: .println("\t\t\tcatch( org.omg.CORBA.portable.RemarshalException _rx ){}");
321: ps
322: .println("\t\t\tcatch( org.omg.CORBA.portable.ApplicationException _ax )");
323: ps.println("\t\t\t{");
324: ps.println("\t\t\t\tString _id = _ax.getId();");
325: ps
326: .println("\t\t\t\tthrow new RuntimeException(\"Unexpected exception \" + _id );");
327: ps.println("\t\t\t}");
328: ps.println("\t\t}");
329: ps.println("\t}\n");
330: } else {
331: // modifier
332: ps.print("set_" + name);
333: ps.print("(AMI_" + classname + "Handler ami_handler, ");
334: ps
335: .println(parameterType.toString() + " attr_" + name
336: + ")");
337: ps.println("\t{");
338: ps.println("\t\twhile(true)");
339: ps.println("\t\t{");
340: ps.println("\t\t\ttry");
341: ps.println("\t\t\t{");
342: ps
343: .println("\t\t\t\torg.omg.CORBA.portable.OutputStream _os = _request(\"_set_"
344: + name + "\",true);");
345: ps.println("\t\t\t\t"
346: + parameterType.typeSpec().printWriteStatement(
347: "attr_" + name, "_os"));
348: ps
349: .println("\t\t\t\t((org.jacorb.orb.Delegate)_get_delegate()).invoke(this, _os, ami_handler);");
350: ps.println("\t\t\t\treturn;");
351: ps.println("\t\t\t}");
352: ps
353: .println("\t\t\tcatch( org.omg.CORBA.portable.RemarshalException _rx ){}");
354: ps
355: .println("\t\t\tcatch( org.omg.CORBA.portable.ApplicationException _ax )");
356: ps.println("\t\t\t{");
357: ps.println("\t\t\t\tString _id = _ax.getId();");
358: ps
359: .println("\t\t\t\tthrow new RuntimeException(\"Unexpected exception \" + _id );");
360: ps.println("\t\t\t}");
361: ps.println("\t\t}");
362: ps.println("\t}\n");
363: }
364: }
365:
366: public void printDelegatedMethod(PrintWriter ps) {
367: ps.print("\tpublic ");
368: if (isGetter()) {
369: ps.print(resultType.toString());
370: ps.print(" " + name + "()");
371: raisesExpr.print(ps);
372: ps.println();
373: ps.println("\t{");
374: ps.println("\t\treturn _delegate." + name + "();");
375: ps.println("\t}\n");
376: } else {
377: /** modifier */
378:
379: ps.print("void " + name + "(" + parameterType.toString());
380: ps.print(" a)");
381: raisesExpr.print(ps);
382: ps.println();
383: ps.println("\t{");
384: ps.println("\t\t_delegate." + name + "(a);");
385: ps.println("\t}\n");
386: }
387: }
388:
389: public void printInvocation(PrintWriter ps) {
390: if (!raisesExpr.empty()) {
391: ps.println("\t\t\ttry");
392: ps.println("\t\t\t{");
393: }
394:
395: ps.println("\t\t\t_out = handler.createReply();");
396: ps.print("\t\t\t");
397:
398: if (isGetter()) {
399: ps.println(resultType.typeSpec().printWriteStatement(
400: name + "()", "_out"));
401: } else {
402: ps.println(name + "("
403: + parameterType.printReadExpression("_input")
404: + ");");
405: }
406:
407: if (!raisesExpr.empty()) {
408: ps.println("\t\t\t}");
409: String[] excepts = raisesExpr.getExceptionNames();
410: String[] classNames = raisesExpr.getExceptionClassNames();
411: for (int i = 0; i < excepts.length; i++) {
412: ps.println("\t\t\tcatch(" + excepts[i] + " _ex" + i
413: + ")");
414: ps.println("\t\t\t{");
415: ps
416: .println("\t\t\t\t_out = handler.createExceptionReply();");
417: ps.println("\t\t\t\t" + classNames[i]
418: + "Helper.write(_out, _ex" + i + ");");
419: ps.println("\t\t\t}");
420: }
421: }
422: }
423:
424: public void accept(IDLTreeVisitor visitor) {
425: visitor.visitMethod(this);
426: }
427:
428: }
|