KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > idl > ReplyHandler


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.idl;
22
23 import java.io.*;
24 import java.util.*;
25
26 /**
27  * A ReplyHandler receives replies of asynchronous invocations of
28  * another interface (we call this interface the "parent" of the
29  * ReplyHandler).
30  *
31  * @author Andre Spiegel
32  * $Id: ReplyHandler.java,v 1.7 2005/03/28 19:58:29 brose Exp $
33  */

34 public class ReplyHandler extends Interface
35 {
36     public ReplyHandler (Interface parent)
37     {
38         super (new_num());
39
40         name = "AMI_" + parent.name + "Handler";
41         pack_name = parent.pack_name;
42
43         createInheritanceSpec (parent.inheritanceSpec);
44
45         body = new InterfaceBody (new_num());
46         body.set_name (name);
47         body.my_interface = this;
48         body.setEnclosingSymbol (this);
49         body.inheritance_spec = this.inheritanceSpec;
50
51         createOperations (parent);
52     }
53
54     /**
55      * Creates an inheritance spec for this ReplyHandler, based
56      * on the inheritance spec of the parent interface.
57      */

58     private void createInheritanceSpec (SymbolList source)
59     {
60         inheritanceSpec = new SymbolList (new_num());
61         if (source.v.isEmpty())
62         {
63             ScopedName n = new ScopedName (new_num());
64             n.pack_name = "org.omg.Messaging";
65             n.typeName = "ReplyHandler";
66             inheritanceSpec.v.add (n);
67         }
68         else
69         {
70             for (Iterator i = source.v.iterator(); i.hasNext();)
71             {
72                 ScopedName n1 = (ScopedName)i.next();
73                 ScopedName n2 = new ScopedName (new_num());
74                 n2.pack_name = n1.pack_name;
75                 n2.typeName = "AMI_" + n1.name + "Handler";
76                 inheritanceSpec.v.add (n2);
77             }
78         }
79     }
80     
81     /**
82      * Creates the operations of this ReplyHandler and puts them into the body.
83      */

84     private void createOperations (Interface parent)
85     {
86         for (Iterator i = parent.body.v.iterator(); i.hasNext();)
87         {
88               Declaration d = ((Definition)i.next()).get_declaration();
89               if (d instanceof OpDecl)
90               {
91                     createOperationsFor ((OpDecl)d);
92               }
93               else if (d instanceof AttrDecl)
94               {
95                     createOperationsFor ((AttrDecl)d);
96               }
97         }
98     }
99
100     /**
101      * Creates the ReplyHandler operations for the given operation of the
102      * parent interface, and puts them into the body of this ReplyHandler.
103      */

104     private void createOperationsFor (OpDecl d)
105     {
106         // Create the parameter list for the NO_EXCEPTION reply operation
107
List paramDecls = new ArrayList();
108         if (!(d.opTypeSpec.type_spec instanceof VoidTypeSpec))
109         {
110             paramDecls.add (new ParamDecl (ParamDecl.MODE_IN,
111                                            d.opTypeSpec,
112                                            "ami_return_val"));
113         }
114         for (Iterator i = d.paramDecls.iterator(); i.hasNext();)
115         {
116             ParamDecl p = (ParamDecl)i.next();
117             if (p.paramAttribute != ParamDecl.MODE_IN)
118             {
119                 paramDecls.add (new ParamDecl (ParamDecl.MODE_IN,
120                                                p.paramTypeSpec,
121                                                p.simple_declarator));
122             }
123         }
124         body.addDefinition (new OpDecl (this, d.name, paramDecls));
125         body.addDefinition
126           (new OpDecl (this, d.name + "_excep", excepParameterList()));
127     }
128     
129     /**
130      * Creates the ReplyHandler operations for the given attribute declaration
131      * of the parent interface, and puts them into the body of this ReplyHandler.
132      */

133     private void createOperationsFor (AttrDecl d)
134     {
135         for (Iterator i = d.declarators.v.iterator(); i.hasNext();)
136         {
137             SimpleDeclarator decl = (SimpleDeclarator)i.next();
138             body.addDefinition
139               (new OpDecl (this, "get_" + decl.name,
140                            parameterList (d.param_type_spec, "ami_return_val")));
141             body.addDefinition
142               (new OpDecl (this, "get_" + decl.name + "_excep",
143                            excepParameterList()));
144             if (!d.readOnly)
145             {
146                 body.addDefinition
147                   (new OpDecl (this, "set_" + decl.name, new ArrayList()));
148                 body.addDefinition
149                   (new OpDecl (this, "set_" + decl.name + "_excep",
150                                excepParameterList()));
151             }
152         }
153     }
154
155     /**
156      * Returns a parameter list with a single "in" argument that has
157      * the given type and name.
158      */

159     private List parameterList(TypeSpec type, String JavaDoc name)
160     {
161         List result = new ArrayList();
162         result.add (new ParamDecl (ParamDecl.MODE_IN, type, name));
163         return result;
164     }
165     
166     private List excepParameterList()
167     {
168         return parameterList (new ExceptionHolderTypeSpec (new_num()),
169                               "excep_holder");
170     }
171
172     public String JavaDoc id()
173     {
174         return "IDL:" + full_name().replace('.', '/') + ":1.0";
175     }
176         
177     public void parse()
178     {
179         if (!NameTable.isDefined ("org.omg.Messaging.ReplyHandler"))
180         {
181             try
182             {
183                 NameTable.define ("org.omg.Messaging.ReplyHandler", "type");
184                 TypeMap.typedef ("org.omg.Messaging.ReplyHandler",
185                                  new ReplyHandlerTypeSpec (IdlSymbol.new_num()));
186             }
187             catch (Exception JavaDoc e)
188             {
189                 throw new RuntimeException JavaDoc (e.getMessage());
190             }
191         }
192
193         ConstrTypeSpec ctspec = new ConstrTypeSpec (this);
194         try
195         {
196             NameTable.define (full_name(), "interface");
197             TypeMap.typedef(full_name(), ctspec);
198         }
199         catch (NameAlreadyDefined e)
200         {
201             parser.error( "Interface " + typeName() + " already defined", token );
202         }
203         
204         body.parse();
205     }
206     
207     public void print (PrintWriter ps)
208     {
209         printInterface();
210         printOperations();
211         printStub();
212         printHelper();
213         printImplSkeleton();
214         printTieSkeleton();
215     }
216             
217
218 }
219
220
221
222
Popular Tags