001: /*
002: * Copyright 1999-2002 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: /*
026: * COMPONENT_NAME: idl.toJava
027: *
028: * ORIGINS: 27
029: *
030: * Licensed Materials - Property of IBM
031: * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
032: * RMI-IIOP v1.0
033: *
034: * @(#)Stub.java 1.26 07/05/05
035: */
036:
037: package com.sun.tools.corba.se.idl.toJavaPortable;
038:
039: // NOTES:
040:
041: import java.io.File;
042: import java.io.PrintWriter;
043: import java.util.Enumeration;
044: import java.util.Hashtable;
045: import java.util.Vector;
046:
047: import com.sun.tools.corba.se.idl.AttributeEntry;
048: import com.sun.tools.corba.se.idl.GenFileStream;
049: import com.sun.tools.corba.se.idl.InterfaceEntry;
050: import com.sun.tools.corba.se.idl.MethodEntry;
051: import com.sun.tools.corba.se.idl.SymtabEntry;
052:
053: /**
054: *
055: **/
056: public class Stub implements AuxGen {
057: /**
058: * Public zero-argument constructor.
059: **/
060: public Stub() {
061: } // ctor
062:
063: /**
064: *
065: **/
066: public void generate(Hashtable symbolTable, SymtabEntry entry) {
067: this .symbolTable = symbolTable;
068: this .i = (InterfaceEntry) entry;
069: this .localStub = i.isLocalServant();
070: this .isAbstract = i.isAbstract();
071: init();
072:
073: openStream();
074: if (stream == null)
075: return;
076: writeHeading();
077: writeBody();
078: writeClosing();
079: closeStream();
080: } // generate
081:
082: /**
083: * Initialize unique members of this generator.
084: **/
085: protected void init() {
086: classSuffix = "Stub";
087: } // init
088:
089: /**
090: *
091: **/
092: protected void openStream() {
093: String name = '_' + i.name() + classSuffix;
094: String pkg = Util.containerFullName(i.container());
095: if (pkg != null && !pkg.equals("")) {
096: Util.mkdir(pkg);
097: name = pkg + '/' + name;
098: }
099: stream = Util.getStream(name.replace('/', File.separatorChar)
100: + ".java", i);
101: } // openStream
102:
103: /**
104: *
105: **/
106: protected void writeHeading() {
107: Util.writePackage(stream, i, Util.StubFile);
108: Util.writeProlog(stream, ((GenFileStream) stream).name());
109:
110: // Transfer interface comment to target <31jul1997>.
111: if (i.comment() != null)
112: i.comment().generate("", stream);
113:
114: writeClassDeclaration();
115: stream.println('{');
116: } // writeHeading
117:
118: /**
119: *
120: **/
121: protected void writeClassDeclaration() {
122: stream.print("public class _" + i.name() + classSuffix
123: + " extends org.omg.CORBA.portable.ObjectImpl");
124: stream.println(" implements " + Util.javaName(i));
125: } // writeClassDeclaration
126:
127: /**
128: * Steps done within writeBody include:
129: * 1.) makeCtors ();
130: * 2.) buildMethodList ();
131: * 3.) makeMethods ();
132: * 4.) makeCORBAObjectMethods ()
133: **/
134: protected void writeBody() {
135: writeCtors();
136: buildMethodList();
137: writeMethods();
138: writeCORBAObjectMethods();
139: writeSerializationMethods();
140: } // writeBody
141:
142: /**
143: *
144: **/
145: protected void writeClosing() {
146: stream.println("} // class _" + i.name() + classSuffix);
147: } // writeClosing
148:
149: /**
150: *
151: **/
152: protected void closeStream() {
153: stream.close();
154: } // closeStream
155:
156: /**
157: *
158: **/
159: protected void writeCtors() {
160: String name = i.name();
161:
162: /*** the constructors are not generated as per ptc/00-01-08.pdf
163: * since these are non-standard APIs, and same can be accomplished
164: * programatically, we need to comment this out, in order to
165: * be able to generate standard stubs
166: */
167:
168: /*************
169: stream.println (" // Constructors");
170: stream.println (" // NOTE: If the default constructor is used, the");
171: stream.println (" // object is useless until _set_delegate (...)");
172: stream.println (" // is called.");
173: stream.println (" public _" + name + classSuffix + " ()");
174: stream.println (" {");
175: stream.println (" super ();");
176: stream.println (" }");
177: stream.println ();
178: stream.println (" public _" + name + classSuffix + " (org.omg.CORBA.portable.Delegate delegate)");
179: stream.println (" {");
180: stream.println (" super ();");
181: stream.println (" _set_delegate (delegate);");
182: stream.println (" }");
183: ***************/
184: // This is confusing since we have localOptimization flag as well.
185: // We have left this code because JCK team filed a P1 bug for changing
186: // _opsClass to $opsClass. Will clean it up in Tiger
187: // _REVISIT_ (Hemanth 03/05/2002)
188: if (localStub) {
189: stream
190: .println(" final public static java.lang.Class _opsClass = "
191: + name + "Operations.class;");
192: stream.println();
193: }
194: stream.println();
195: } // writeCtors
196:
197: /**
198: * Build a list of all of the methods, keeping out duplicates.
199: **/
200: protected void buildMethodList() {
201: // Start from scratch
202: methodList = new Vector();
203:
204: buildMethodList(i);
205: } // buildMethodList
206:
207: /**
208: *
209: **/
210: private void buildMethodList(InterfaceEntry entry) {
211: // Add the local methods
212: Enumeration locals = entry.methods().elements();
213: while (locals.hasMoreElements())
214: addMethod((MethodEntry) locals.nextElement());
215:
216: // Add the inherited methods
217: Enumeration parents = entry.derivedFrom().elements();
218: while (parents.hasMoreElements()) {
219: InterfaceEntry parent = (InterfaceEntry) parents
220: .nextElement();
221: if (!parent.name().equals("Object"))
222: buildMethodList(parent);
223: }
224: } // buildMethodList
225:
226: /**
227: *
228: **/
229: private void addMethod(MethodEntry method) {
230: if (!methodList.contains(method))
231: methodList.addElement(method);
232: } // addMethod
233:
234: /**
235: *
236: **/
237: protected void writeMethods() {
238: // Count the methods, attributes which are not readonly are
239: // counted as 2 methods.
240: int count = methodList.size();
241: Enumeration e = methodList.elements();
242: while (e.hasMoreElements()) {
243: Object method = e.nextElement();
244: if (method instanceof AttributeEntry
245: && !((AttributeEntry) method).readOnly())
246: ++count;
247: }
248:
249: if ((((Arguments) Compile.compiler.arguments).LocalOptimization)
250: && !isAbstract) {
251: stream
252: .println(" final public static java.lang.Class _opsClass =");
253: stream.println(" " + this .i.name()
254: + "Operations.class;");
255: }
256:
257: // Write the methods
258: int realI = 0;
259: for (int i = 0; i < methodList.size(); ++i) {
260: MethodEntry method = (MethodEntry) methodList.elementAt(i);
261: if (!localStub) {
262: ((MethodGen) method.generator()).stub(this .i.name(),
263: isAbstract, symbolTable, method, stream, realI);
264: } else {
265: ((MethodGen) method.generator()).localstub(symbolTable,
266: method, stream, realI, this .i);
267: }
268: if (method instanceof AttributeEntry
269: && !((AttributeEntry) method).readOnly())
270: realI += 2;
271: else
272: ++realI;
273: }
274: } // writeMethods
275:
276: /**
277: *
278: **/
279: private void buildIDList(InterfaceEntry entry, Vector list) {
280: if (!entry.fullName().equals("org/omg/CORBA/Object")) {
281: String id = Util.stripLeadingUnderscoresFromID(entry
282: .repositoryID().ID());
283: if (!list.contains(id))
284: list.addElement(id);
285: Enumeration e = entry.derivedFrom().elements();
286: while (e.hasMoreElements())
287: buildIDList((InterfaceEntry) e.nextElement(), list);
288: }
289: } // buildIDList
290:
291: /**
292: *
293: **/
294: private void writeIDs() {
295: Vector list = new Vector();
296: buildIDList(i, list);
297: Enumeration e = list.elements();
298: boolean first = true;
299: while (e.hasMoreElements()) {
300: if (first)
301: first = false;
302: else
303: stream.println(", ");
304: stream.print(" \"" + (String) e.nextElement() + '"');
305: }
306: } // writeIDs
307:
308: /**
309: *
310: **/
311: protected void writeCORBAObjectMethods() {
312: stream.println(" // Type-specific CORBA::Object operations");
313: stream.println(" private static String[] __ids = {");
314: writeIDs();
315: stream.println("};");
316: stream.println();
317: stream.println(" public String[] _ids ()");
318: stream.println(" {");
319: stream.println(" return (String[])__ids.clone ();");
320: stream.println(" }");
321: stream.println();
322: } // writeCORBAObjectMethods
323:
324: /**
325: *
326: **/
327: protected void writeSerializationMethods() {
328: stream
329: .println(" private void readObject (java.io.ObjectInputStream s) throws java.io.IOException");
330: stream.println(" {");
331: stream.println(" String str = s.readUTF ();");
332: stream.println(" String[] args = null;");
333: stream.println(" java.util.Properties props = null;");
334: stream
335: .println(" org.omg.CORBA.Object obj = org.omg.CORBA.ORB.init (args, props).string_to_object (str);");
336: stream
337: .println(" org.omg.CORBA.portable.Delegate delegate = ((org.omg.CORBA.portable.ObjectImpl) obj)._get_delegate ();");
338: stream.println(" _set_delegate (delegate);");
339: stream.println(" }");
340: stream.println();
341: stream
342: .println(" private void writeObject (java.io.ObjectOutputStream s) throws java.io.IOException");
343: stream.println(" {");
344: stream.println(" String[] args = null;");
345: stream.println(" java.util.Properties props = null;");
346: stream
347: .println(" String str = org.omg.CORBA.ORB.init (args, props).object_to_string (this);");
348: stream.println(" s.writeUTF (str);");
349: stream.println(" }");
350: }
351:
352: protected Hashtable symbolTable = null;
353: protected InterfaceEntry i = null;
354: protected PrintWriter stream = null;
355:
356: // Unique to this generator
357: protected Vector methodList = null;
358: protected String classSuffix = "";
359: protected boolean localStub = false;
360: private boolean isAbstract = false;
361: } // class Stub
|