001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-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: import java.io.*;
024: import java.util.*;
025:
026: /**
027: * A ReplyHandler receives replies of asynchronous invocations of
028: * another interface (we call this interface the "parent" of the
029: * ReplyHandler).
030: *
031: * @author Andre Spiegel
032: * $Id: ReplyHandler.java,v 1.9 2006/05/17 12:53:42 alphonse.bendt Exp $
033: */
034: public class ReplyHandler extends Interface {
035: public ReplyHandler(Interface parent) {
036: super (new_num());
037:
038: name = "AMI_" + parent.name + "Handler";
039: pack_name = parent.pack_name;
040:
041: createInheritanceSpec(parent.inheritanceSpec);
042:
043: body = new InterfaceBody(new_num());
044: body.set_name(name);
045: body.my_interface = this ;
046: body.setEnclosingSymbol(this );
047: body.inheritance_spec = this .inheritanceSpec;
048:
049: createOperations(parent);
050: }
051:
052: /**
053: * Creates an inheritance spec for this ReplyHandler, based
054: * on the inheritance spec of the parent interface.
055: */
056: private void createInheritanceSpec(SymbolList source) {
057: inheritanceSpec = new SymbolList(new_num());
058: if (source.v.isEmpty()) {
059: ScopedName n = new ScopedName(new_num());
060: n.pack_name = "org.omg.Messaging";
061: n.typeName = "ReplyHandler";
062: inheritanceSpec.v.add(n);
063: } else {
064: for (Iterator i = source.v.iterator(); i.hasNext();) {
065: ScopedName n1 = (ScopedName) i.next();
066: ScopedName n2 = new ScopedName(new_num());
067: StringBuffer typeName = new StringBuffer(n1.typeName());
068: int nameStart = typeName.lastIndexOf(".") + 1;
069: typeName.insert(nameStart, "AMI_");
070: typeName.append("Handler");
071: n2.typeName = typeName.toString();
072: inheritanceSpec.v.add(n2);
073: }
074: }
075: }
076:
077: /**
078: * Creates the operations of this ReplyHandler and puts them into the body.
079: */
080: private void createOperations(Interface parent) {
081: for (Iterator i = parent.body.v.iterator(); i.hasNext();) {
082: Declaration d = ((Definition) i.next()).get_declaration();
083: if (d instanceof OpDecl) {
084: createOperationsFor((OpDecl) d);
085: } else if (d instanceof AttrDecl) {
086: createOperationsFor((AttrDecl) d);
087: }
088: }
089: }
090:
091: /**
092: * Creates the ReplyHandler operations for the given operation of the
093: * parent interface, and puts them into the body of this ReplyHandler.
094: */
095: private void createOperationsFor(OpDecl d) {
096: // Create the parameter list for the NO_EXCEPTION reply operation
097: List paramDecls = new ArrayList();
098: if (!(d.opTypeSpec.type_spec instanceof VoidTypeSpec)) {
099: paramDecls.add(new ParamDecl(ParamDecl.MODE_IN,
100: d.opTypeSpec, "ami_return_val"));
101: }
102: for (Iterator i = d.paramDecls.iterator(); i.hasNext();) {
103: ParamDecl p = (ParamDecl) i.next();
104: if (p.paramAttribute != ParamDecl.MODE_IN) {
105: paramDecls.add(new ParamDecl(ParamDecl.MODE_IN,
106: p.paramTypeSpec, p.simple_declarator));
107: }
108: }
109: body.addDefinition(new OpDecl(this , d.name, paramDecls));
110: body.addDefinition(new OpDecl(this , d.name + "_excep",
111: excepParameterList()));
112: }
113:
114: /**
115: * Creates the ReplyHandler operations for the given attribute declaration
116: * of the parent interface, and puts them into the body of this ReplyHandler.
117: */
118: private void createOperationsFor(AttrDecl d) {
119: for (Iterator i = d.declarators.v.iterator(); i.hasNext();) {
120: SimpleDeclarator decl = (SimpleDeclarator) i.next();
121: body
122: .addDefinition(new OpDecl(this , "get_" + decl.name,
123: parameterList(d.param_type_spec,
124: "ami_return_val")));
125: body.addDefinition(new OpDecl(this , "get_" + decl.name
126: + "_excep", excepParameterList()));
127: if (!d.readOnly) {
128: body.addDefinition(new OpDecl(this , "set_" + decl.name,
129: new ArrayList()));
130: body.addDefinition(new OpDecl(this , "set_" + decl.name
131: + "_excep", excepParameterList()));
132: }
133: }
134: }
135:
136: /**
137: * Returns a parameter list with a single "in" argument that has
138: * the given type and name.
139: */
140: private List parameterList(TypeSpec type, String name) {
141: List result = new ArrayList();
142: result.add(new ParamDecl(ParamDecl.MODE_IN, type, name));
143: return result;
144: }
145:
146: private List excepParameterList() {
147: return parameterList(new ExceptionHolderTypeSpec(new_num()),
148: "excep_holder");
149: }
150:
151: public String id() {
152: return "IDL:" + full_name().replace('.', '/') + ":1.0";
153: }
154:
155: public void parse() {
156: if (!NameTable.isDefined("org.omg.Messaging.ReplyHandler")) {
157: try {
158: NameTable.define("org.omg.Messaging.ReplyHandler",
159: "type");
160: TypeMap.typedef("org.omg.Messaging.ReplyHandler",
161: new ReplyHandlerTypeSpec(IdlSymbol.new_num()));
162: } catch (Exception e) {
163: throw new RuntimeException(e.toString());
164: }
165: }
166:
167: ConstrTypeSpec ctspec = new ConstrTypeSpec(this );
168: try {
169: NameTable.define(full_name(), "interface");
170: TypeMap.typedef(full_name(), ctspec);
171: } catch (NameAlreadyDefined e) {
172: parser.error(
173: "Interface " + typeName() + " already defined",
174: token);
175: }
176:
177: body.parse();
178: }
179:
180: public void print(PrintWriter ps) {
181: printInterface();
182: printOperations();
183: printStub();
184: printHelper();
185: printImplSkeleton();
186: printTieSkeleton();
187: }
188:
189: }
|