KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > iiop > rmi > ir > InterfaceRepository


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.iiop.rmi.ir;
23
24 import org.omg.CORBA.ORB JavaDoc;
25 import org.omg.CORBA.Any JavaDoc;
26 import org.omg.CORBA.TypeCode JavaDoc;
27 import org.omg.CORBA.TCKind JavaDoc;
28 import org.omg.CORBA.DefinitionKind JavaDoc;
29 import org.omg.CORBA.Repository JavaDoc;
30 import org.omg.CORBA.RepositoryHelper;
31 import org.omg.CORBA.ParameterDescription JavaDoc;
32 import org.omg.CORBA.ParameterMode JavaDoc;
33 import org.omg.CORBA.StructMember JavaDoc;
34 import org.omg.CORBA.ExceptionDef JavaDoc;
35 import org.omg.CORBA.ExceptionDefHelper;
36 import org.omg.PortableServer.POA JavaDoc;
37
38 import org.jboss.iiop.rmi.Util;
39 import org.jboss.iiop.rmi.ContainerAnalysis;
40 import org.jboss.iiop.rmi.InterfaceAnalysis;
41 import org.jboss.iiop.rmi.ExceptionAnalysis;
42 import org.jboss.iiop.rmi.ValueAnalysis;
43 import org.jboss.iiop.rmi.ValueMemberAnalysis;
44 import org.jboss.iiop.rmi.ConstantAnalysis;
45 import org.jboss.iiop.rmi.AttributeAnalysis;
46 import org.jboss.iiop.rmi.OperationAnalysis;
47 import org.jboss.iiop.rmi.ParameterAnalysis;
48 import org.jboss.iiop.rmi.RMIIIOPViolationException;
49 import org.jboss.iiop.rmi.RmiIdlUtil;
50
51 import java.util.ArrayList JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Map JavaDoc;
55
56 import javax.naming.InitialContext JavaDoc;
57 import javax.naming.NamingException JavaDoc;
58
59 /**
60  * An Interface Repository.
61  *
62  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
63  * @version $Revision: 37459 $
64  */

65 public class InterfaceRepository
66 {
67    // Constants -----------------------------------------------------
68

69    // Attributes ----------------------------------------------------
70

71    // Static --------------------------------------------------------
72

73    /**
74     * Maps java classes to IDL TypeCodes for primitives.
75     */

76    private static Map JavaDoc primitiveTypeCodeMap;
77
78    /**
79     * Maps java classes to IDL TypeCodes for constants.
80     */

81    private static Map JavaDoc constantTypeCodeMap;
82
83    static {
84       // Get an ORB for creating type codes.
85
ORB JavaDoc orb;
86       try {
87          orb = (ORB JavaDoc)new InitialContext JavaDoc().lookup("java:/JBossCorbaORB");
88       } catch (NamingException JavaDoc ex) {
89          throw new RuntimeException JavaDoc("Cannot lookup java:/JBossCorbaORB: "+ex);
90       }
91
92       // TypeCodes for primitive types
93
primitiveTypeCodeMap = new HashMap JavaDoc();
94       primitiveTypeCodeMap.put(Void.TYPE,
95                                orb.get_primitive_tc(TCKind.tk_void));
96       primitiveTypeCodeMap.put(Boolean.TYPE,
97                                orb.get_primitive_tc(TCKind.tk_boolean));
98       primitiveTypeCodeMap.put(Character.TYPE,
99                                orb.get_primitive_tc(TCKind.tk_wchar));
100       primitiveTypeCodeMap.put(Byte.TYPE,
101                                orb.get_primitive_tc(TCKind.tk_octet));
102       primitiveTypeCodeMap.put(Short.TYPE,
103                                orb.get_primitive_tc(TCKind.tk_short));
104       primitiveTypeCodeMap.put(Integer.TYPE,
105                                orb.get_primitive_tc(TCKind.tk_long));
106       primitiveTypeCodeMap.put(Long.TYPE,
107                                orb.get_primitive_tc(TCKind.tk_longlong));
108       primitiveTypeCodeMap.put(Float.TYPE,
109                                orb.get_primitive_tc(TCKind.tk_float));
110       primitiveTypeCodeMap.put(Double.TYPE,
111                                orb.get_primitive_tc(TCKind.tk_double));
112
113       // TypeCodes for constant types
114
constantTypeCodeMap = new HashMap JavaDoc(primitiveTypeCodeMap);
115       constantTypeCodeMap.put(String JavaDoc.class, orb.create_wstring_tc(0));
116    }
117
118    /**
119     * Static logger used by the interface repository.
120     */

121    private static final org.jboss.logging.Logger logger =
122                org.jboss.logging.Logger.getLogger(InterfaceRepository.class);
123
124    // Constructors --------------------------------------------------
125

126    public InterfaceRepository(ORB JavaDoc orb, POA JavaDoc poa, String JavaDoc name)
127    {
128       this.orb = orb;
129       this.poa = poa;
130       impl = new RepositoryImpl(orb, poa, name);
131    }
132
133    // Public --------------------------------------------------------
134

135    /**
136     * Add mapping for a class.
137     */

138    public void mapClass(Class JavaDoc cls)
139       throws RMIIIOPViolationException, IRConstructionException
140    {
141       // Just lookup a TypeCode for the class: That will provoke
142
// mapping the class and adding it to the IR.
143
getTypeCode(cls);
144    }
145
146
147    /**
148     * Finish the build.
149     */

150    public void finishBuild()
151       throws IRConstructionException
152    {
153       impl.allDone();
154    }
155
156    /**
157     * Return a CORBA reference to this IR.
158     */

159    public Repository JavaDoc getReference()
160    {
161       return RepositoryHelper.narrow(impl.getReference());
162    }
163
164    /**
165     * Deactivate all CORBA objects in this IR.
166     */

167    public void shutdown()
168    {
169       impl.shutdown();
170    }
171
172    // Z implementation ----------------------------------------------
173

174    // Y overrides ---------------------------------------------------
175

176    // Package protected ---------------------------------------------
177

178    // Protected -----------------------------------------------------
179

180    // Private -------------------------------------------------------
181

182    /**
183     * The repository implementation.
184     */

185    RepositoryImpl impl;
186
187    /**
188     * The ORB that I use.
189     */

190    private ORB JavaDoc orb = null;
191  
192    /**
193     * The POA that I use.
194     */

195    private POA JavaDoc poa = null;
196  
197    /**
198     * Maps java classes to IDL TypeCodes for parameter, result, attribute
199     * and value member types.
200     */

201    private Map JavaDoc typeCodeMap = new HashMap JavaDoc(primitiveTypeCodeMap);
202
203    /**
204     * Maps java classes to <code>InterfaceDefImpl</code>s for interfaces.
205     */

206    private Map JavaDoc interfaceMap = new HashMap JavaDoc();
207
208    /**
209     * Maps java classes to <code>ValueDefImpl</code>s for values.
210     */

211    private Map JavaDoc valueMap = new HashMap JavaDoc();
212
213    /**
214     * Maps java classes to <code>ExceptionDefImpl</code>s for exceptions.
215     */

216    private Map JavaDoc exceptionMap = new HashMap JavaDoc();
217
218    /**
219     * Maps java classes to <code>ValueBoxDefImpl</code>s for arrays.
220     */

221    private Map JavaDoc arrayMap = new HashMap JavaDoc();
222
223
224    /**
225     * java.io.Serializable special mapping, as per section 1.3.10.1.
226     * Do not use this variable directly, use the
227     * <code>getJavaIoSerializable()</code> method instead, as that will
228     * create the typedef in the IR on demand.
229     */

230    private AliasDefImpl javaIoSerializable = null;
231  
232    /**
233     * java.io.Externalizable special mapping, as per section 1.3.10.1.
234     * Do not use this variable directly, use the
235     * <code>getJavaIoExternalizable()</code> method instead, as that will
236     * create the typedef in the IR on demand.
237     */

238    private AliasDefImpl javaIoExternalizable = null;
239
240    /**
241     * java.lang.Object special mapping, as per section 1.3.10.2.
242     * Do not use this variable directly, use the
243     * <code>getJavaLang_Object()</code> method instead, as that will
244     * create the typedef in the IR on demand.
245     */

246    private AliasDefImpl javaLang_Object = null;
247  
248    /**
249     * java.lang.String special mapping, as per section 1.3.5.10.
250     * Do not use this variable directly, use the
251     * <code>getJavaLangString()</code> method instead, as that will
252     * create the value type in the IR on demand.
253     */

254    private ValueDefImpl javaLangString = null;
255  
256    /**
257     * java.lang.Class special mapping, as per section 1.3.5.11.
258     * Do not use this variable directly, use the
259     * <code>getJavaxRmiCORBAClassDesc()</code> method instead, as that will
260     * create the value type in the IR on demand.
261     */

262    private ValueDefImpl javaxRmiCORBAClassDesc = null;
263  
264
265    /**
266     * Returns the TypeCode suitable for an IDL constant.
267     * @param cls The Java class denoting the type of the constant.
268     */

269    private TypeCode JavaDoc getConstantTypeCode(Class JavaDoc cls)
270       throws IRConstructionException
271    {
272       if (cls == null)
273          throw new IllegalArgumentException JavaDoc("Null class");
274
275       TypeCode JavaDoc ret = (TypeCode JavaDoc)constantTypeCodeMap.get(cls);
276
277       if (ret == null)
278          throw new IRConstructionException("Bad class \"" + cls.getName() +
279                                            "\" for a constant.");
280
281       return ret;
282    }
283
284    /**
285     * Returns the TypeCode IDL TypeCodes for parameter, result, attribute
286     * and value member types.
287     * This may provoke a mapping of the class argument.
288     *
289     * Exception classes map to both values and exceptions. For these, this
290     * method returns the typecode for the value, and you can use the
291     * <code>getExceptionTypeCode</code> TODO method to get the typecode for the
292     * mapping to exception.
293     *
294     * @param cls The Java class denoting the java type.
295     */

296    private TypeCode JavaDoc getTypeCode(Class JavaDoc cls)
297       throws IRConstructionException, RMIIIOPViolationException
298    {
299       if (cls == null)
300          throw new IllegalArgumentException JavaDoc("Null class");
301
302       TypeCode JavaDoc ret = (TypeCode JavaDoc)typeCodeMap.get(cls);
303
304       if (ret == null) {
305          if (cls == java.lang.String JavaDoc.class)
306             ret = getJavaLangString().type();
307          else if (cls == java.lang.Object JavaDoc.class)
308             ret = getJavaLang_Object().type();
309          else if (cls == java.lang.Class JavaDoc.class)
310             ret = getJavaxRmiCORBAClassDesc().type();
311          else if (cls == java.io.Serializable JavaDoc.class)
312             ret = getJavaIoSerializable().type();
313          else if (cls == java.io.Externalizable JavaDoc.class)
314             ret = getJavaIoExternalizable().type();
315          else {
316             // Try adding a mapping of the the class to the IR
317
addClass(cls);
318
319             // Lookup again, it should be there now.
320
ret = (TypeCode JavaDoc)typeCodeMap.get(cls);
321
322             if (ret == null)
323               throw new IRConstructionException("TypeCode for class " +
324                                                 cls.getName() + " unknown.");
325             else
326                return ret;
327          }
328
329          typeCodeMap.put(cls, ret);
330       }
331
332       return ret;
333    }
334
335    /**
336     * Add a new IDL TypeCode for a mapped class.
337     *
338     * @param cls The Java class denoting the java type.
339     * @param typeCode The IDL type code of the mapped java class.
340     */

341    private void addTypeCode(Class JavaDoc cls, TypeCode JavaDoc typeCode)
342       throws IRConstructionException
343    {
344       if (cls == null)
345          throw new IllegalArgumentException JavaDoc("Null class");
346
347       TypeCode JavaDoc tc = (TypeCode JavaDoc)typeCodeMap.get(cls);
348
349       if (tc != null)
350          throw new IllegalArgumentException JavaDoc("Class " + cls.getName() +
351                                             " already has TypeCode.");
352
353       logger.trace("InterfaceRepository: added typecode for " + cls.getName());
354       typeCodeMap.put(cls, typeCode);
355    }
356
357    /**
358     * Get a reference to the special case mapping for java.io.Serializable.
359     * This is according to "Java(TM) Language to IDL Mapping Specification",
360     * section 1.3.10.1
361     */

362    private AliasDefImpl getJavaIoSerializable()
363       throws IRConstructionException
364    {
365       if (javaIoSerializable == null) {
366          final String JavaDoc id = "IDL:java/io/Serializable:1.0";
367          final String JavaDoc name = "Serializable";
368          final String JavaDoc version = "1.0";
369  
370          // Get module to add typedef to.
371
ModuleDefImpl m = ensurePackageExists("java.io");
372  
373          TypeCode JavaDoc typeCode = orb.create_alias_tc(id, name,
374                                           orb.get_primitive_tc(TCKind.tk_any));
375 // TypeCode typeCode = new TypeCodeImpl(TCKind._tk_alias, id, name,
376
// new TypeCodeImpl(TCKind.tk_any));
377

378          javaIoSerializable = new AliasDefImpl(id, name, version, m,
379                                                typeCode, impl);
380          m.add(name, javaIoSerializable);
381       }
382  
383       return javaIoSerializable;
384    }
385  
386    /**
387     * Get a reference to the special case mapping for java.io.Externalizable.
388     * This is according to "Java(TM) Language to IDL Mapping Specification",
389     * section 1.3.10.1
390     */

391    private AliasDefImpl getJavaIoExternalizable()
392       throws IRConstructionException
393    {
394       if (javaIoExternalizable == null) {
395          final String JavaDoc id = "IDL:java/io/Externalizable:1.0";
396          final String JavaDoc name = "Externalizable";
397          final String JavaDoc version = "1.0";
398  
399          // Get module to add typedef to.
400
ModuleDefImpl m = ensurePackageExists("java.io");
401  
402          TypeCode JavaDoc typeCode = orb.create_alias_tc(id, name,
403                                           orb.get_primitive_tc(TCKind.tk_any));
404 // TypeCode typeCode = new TypeCodeImpl(TCKind._tk_alias, id, name,
405
// new TypeCodeImpl(TCKind.tk_any));
406

407          javaIoExternalizable = new AliasDefImpl(id, name, version, m,
408                                                typeCode, impl);
409          m.add(name, javaIoExternalizable);
410       }
411  
412       return javaIoExternalizable;
413    }
414
415    /**
416     * Get a reference to the special case mapping for java.lang.Object.
417     * This is according to "Java(TM) Language to IDL Mapping Specification",
418     * section 1.3.10.2
419     */

420    private AliasDefImpl getJavaLang_Object()
421       throws IRConstructionException
422    {
423       if (javaLang_Object == null) {
424          final String JavaDoc id = "IDL:java/lang/_Object:1.0";
425          final String JavaDoc name = "_Object";
426          final String JavaDoc version = "1.0";
427  
428          // Get module to add typedef to.
429
ModuleDefImpl m = ensurePackageExists("java.lang");
430  
431          TypeCode JavaDoc typeCode = orb.create_alias_tc(id, name,
432                                           orb.get_primitive_tc(TCKind.tk_any));
433 // TypeCode typeCode = new TypeCodeImpl(TCKind._tk_alias, id, name,
434
// new TypeCodeImpl(TCKind.tk_any));
435

436          javaLang_Object = new AliasDefImpl(id, name, version, m,
437                                             typeCode, impl);
438          m.add(name, javaLang_Object);
439       }
440  
441       return javaLang_Object;
442    }
443
444    /**
445     * Get a reference to the special case mapping for java.lang.String.
446     * This is according to "Java(TM) Language to IDL Mapping Specification",
447     * section 1.3.5.10
448     */

449    private ValueDefImpl getJavaLangString()
450       throws IRConstructionException
451    {
452       if (javaLangString == null) {
453          ModuleDefImpl m = ensurePackageExists("org.omg.CORBA");
454          ValueDefImpl val =
455                          new ValueDefImpl("IDL:omg.org/CORBA/WStringValue:1.0",
456                                           "WStringValue", "1.0",
457                                           m, false, false,
458                                           new String JavaDoc[0], new String JavaDoc[0],
459                                           orb.get_primitive_tc(TCKind.tk_null),
460                                           impl);
461          ValueMemberDefImpl vmdi =
462               new ValueMemberDefImpl("IDL:omg.org/CORBA/WStringValue.data:1.0",
463                                      "data", "1.0", orb.create_wstring_tc(0),
464                                      true, val, impl);
465          val.add("data", vmdi);
466          m.add("WStringValue", val);
467
468          javaLangString = val;
469       }
470  
471       return javaLangString;
472    }
473
474    /**
475     * Get a reference to the special case mapping for java.lang.Class.
476     * This is according to "Java(TM) Language to IDL Mapping Specification",
477     * section 1.3.5.11.
478     */

479    private ValueDefImpl getJavaxRmiCORBAClassDesc()
480       throws IRConstructionException, RMIIIOPViolationException
481    {
482       if (javaxRmiCORBAClassDesc == null) {
483          // Just map the right value class
484
ValueAnalysis va = ValueAnalysis.getValueAnalysis(javax.rmi.CORBA.ClassDesc JavaDoc.class);
485          ValueDefImpl val = addValue(va);
486
487          // Warn if it does not conform to the specification.
488
if (!"RMI:javax.rmi.CORBA.ClassDesc:B7C4E3FC9EBDC311:CFBF02CF5294176B".equals(val.id()) )
489             logger.debug("Compatibility problem: Class " +
490                          "javax.rmi.CORBA.ClassDesc does not conform " +
491                          "to the Java(TM) Language to IDL Mapping "+
492                          "Specification (01-06-07), section 1.3.5.11.");
493
494          javaxRmiCORBAClassDesc = val;
495       }
496  
497       return javaxRmiCORBAClassDesc;
498    }
499
500
501    /**
502     * Ensure that a package exists in the IR.
503     * This will create modules in the IR as needed.
504     *
505     * @param pkg
506     * The package that needs to be defined as a module in the IR.
507     *
508     * @return A reference to the IR module that represents the package.
509     */

510    private ModuleDefImpl ensurePackageExists(String JavaDoc pkgName)
511       throws IRConstructionException
512    {
513       return ensurePackageExists(impl, "", pkgName);
514    }
515
516    /**
517     * Ensure that a package exists in the IR.
518     * This will create modules in the IR as needed.
519     *
520     * @param c
521     * The container that the remainder of modules should be defined in.
522     * @param previous
523     * The IDL module name, from root to <code>c</code>.
524     * @param remainder
525     * The java package name, relative to <code>c</code>.
526     *
527     * @return A reference to the IR module that represents the package.
528     */

529    private ModuleDefImpl ensurePackageExists(LocalContainer c,
530                                              String JavaDoc previous,
531                                              String JavaDoc remainder)
532       throws IRConstructionException
533    {
534       if ("".equals(remainder))
535          return (ModuleDefImpl)c; // done
536

537       int idx = remainder.indexOf('.');
538       String JavaDoc base;
539  
540       if (idx == -1)
541          base = remainder;
542       else
543          base = remainder.substring(0, idx);
544       base = Util.javaToIDLName(base);
545  
546       if (previous.equals(""))
547          previous = base;
548       else
549          previous = previous + "/" + base;
550       if (idx == -1)
551          remainder = "";
552       else
553          remainder = remainder.substring(idx + 1);
554  
555       LocalContainer next = null;
556       LocalContained contained = (LocalContained)c._lookup(base);
557
558       if (contained instanceof LocalContainer)
559          next = (LocalContainer)contained;
560       else if (contained != null)
561          throw new IRConstructionException("Name collision while creating package.");
562  
563       if (next == null) {
564          String JavaDoc id = "IDL:" + previous + ":1.0";
565  
566          // Create module
567
ModuleDefImpl m = new ModuleDefImpl(id, base, "1.0", c, impl);
568          logger.trace("Created module \"" + id + "\".");
569  
570          c.add(base, m);
571  
572          if (idx == -1)
573             return m; // done
574

575          next = (LocalContainer)c._lookup(base); // Better be there now...
576
} else // Check that next _is_ a module
577
if (next.def_kind() != DefinitionKind.dk_Module)
578             throw new IRConstructionException("Name collision while creating package.");
579  
580       return ensurePackageExists(next, previous, remainder);
581    }
582
583    /**
584     * Add a set of constants to a container (interface or value class).
585     */

586    private void addConstants(LocalContainer container,
587                              ContainerAnalysis ca)
588       throws RMIIIOPViolationException, IRConstructionException
589    {
590       ConstantAnalysis[] consts = ca.getConstants();
591       for (int i = 0; i < consts.length; ++i) {
592          ConstantDefImpl cDef;
593          String JavaDoc cid = ca.getMemberRepositoryId(consts[i].getJavaName());
594          String JavaDoc cName = consts[i].getIDLName();
595  
596          Class JavaDoc cls = consts[i].getType();
597          logger.trace("Constant["+i+"] class: " + cls.getName());
598          TypeCode JavaDoc typeCode = getConstantTypeCode(cls);
599  
600          Any JavaDoc value = orb.create_any();
601          consts[i].insertValue(value);
602
603          logger.trace("Adding constant: " + cid);
604          cDef = new ConstantDefImpl(cid, cName, "1.0",
605                                     typeCode, value, container, impl);
606          container.add(cName, cDef);
607       }
608    }
609
610    /**
611     * Add a set of attributes to a container (interface or value class).
612     */

613    private void addAttributes(LocalContainer container,
614                               ContainerAnalysis ca)
615       throws RMIIIOPViolationException, IRConstructionException
616    {
617       AttributeAnalysis[] attrs = ca.getAttributes();
618       logger.trace("Attribute count: " + attrs.length);
619       for (int i = 0; i < attrs.length; ++i) {
620          AttributeDefImpl aDef;
621          String JavaDoc aid = ca.getMemberRepositoryId(attrs[i].getJavaName());
622          String JavaDoc aName = attrs[i].getIDLName();
623  
624          Class JavaDoc cls = attrs[i].getCls();
625          logger.trace("Attribute["+i+"] class: " + cls.getName());
626
627          TypeCode JavaDoc typeCode = getTypeCode(cls);
628  
629          logger.trace("Adding: " + aid);
630          aDef = new AttributeDefImpl(aid, aName, "1.0", attrs[i].getMode(),
631                                      typeCode, container, impl);
632          container.add(aName, aDef);
633       }
634    }
635
636    /**
637     * Add a set of operations to a container (interface or value class).
638     */

639    private void addOperations(LocalContainer container,
640                               ContainerAnalysis ca)
641       throws RMIIIOPViolationException, IRConstructionException
642    {
643       OperationAnalysis[] ops = ca.getOperations();
644       logger.debug("Operation count: " + ops.length);
645       for (int i = 0; i < ops.length; ++i) {
646          OperationDefImpl oDef;
647          String JavaDoc oName = ops[i].getIDLName();
648          String JavaDoc oid = ca.getMemberRepositoryId(oName);
649  
650          Class JavaDoc cls = ops[i].getReturnType();
651          logger.debug("Operation["+i+"] return type class: " + cls.getName());
652          TypeCode JavaDoc typeCode = getTypeCode(cls);
653  
654          ParameterAnalysis[] ps = ops[i].getParameters();
655          ParameterDescription JavaDoc[] params = new ParameterDescription JavaDoc[ps.length];
656          for (int j = 0; j < ps.length; ++j) {
657             params[j] = new ParameterDescription JavaDoc(ps[j].getIDLName(),
658                                                  getTypeCode(ps[j].getCls()),
659                                                  null, // filled in later
660
ParameterMode.PARAM_IN);
661          }
662
663          ExceptionAnalysis[] exc = ops[i].getMappedExceptions();
664          ExceptionDef JavaDoc[] exceptions = new ExceptionDef JavaDoc[exc.length];
665          for (int j = 0; j < exc.length; ++j) {
666             ExceptionDefImpl e = addException(exc[j]);
667             exceptions[j] = ExceptionDefHelper.narrow(e.getReference());
668          }
669  
670          logger.debug("Adding: " + oid);
671          oDef = new OperationDefImpl(oid, oName, "1.0", container,
672                                      typeCode, params, exceptions, impl);
673          container.add(oName, oDef);
674       }
675    }
676
677    /**
678     * Add a set of interfaces to the IR.
679     *
680     * @return An array of the IR IDs of the interfaces.
681     */

682    private String JavaDoc[] addInterfaces(ContainerAnalysis ca)
683       throws RMIIIOPViolationException, IRConstructionException
684    {
685       logger.trace("Adding interfaces: ");
686       InterfaceAnalysis[] interfaces = ca.getInterfaces();
687       List JavaDoc base_interfaces = new ArrayList JavaDoc();
688       for (int i = 0; i < interfaces.length; ++i) {
689          InterfaceDefImpl idi = addInterface(interfaces[i]);
690          base_interfaces.add(idi.id());
691          logger.trace(" " + idi.id());
692       }
693       String JavaDoc[] strArr = new String JavaDoc[base_interfaces.size()];
694       return (String JavaDoc[])base_interfaces.toArray(strArr);
695    }
696
697    /**
698     * Add a set of abstract valuetypes to the IR.
699     *
700     * @return An array of the IR IDs of the abstract valuetypes.
701     */

702    private String JavaDoc[] addAbstractBaseValuetypes(ContainerAnalysis ca)
703       throws RMIIIOPViolationException, IRConstructionException
704    {
705       logger.trace("Adding abstract valuetypes: ");
706       ValueAnalysis[] abstractValuetypes = ca.getAbstractBaseValuetypes();
707       List JavaDoc abstract_base_valuetypes = new ArrayList JavaDoc();
708       for (int i = 0; i < abstractValuetypes.length; ++i) {
709          ValueDefImpl vdi = addValue(abstractValuetypes[i]);
710          abstract_base_valuetypes.add(vdi.id());
711          logger.trace(" " + vdi.id());
712       }
713       String JavaDoc[] strArr = new String JavaDoc[abstract_base_valuetypes.size()];
714       return (String JavaDoc[])abstract_base_valuetypes.toArray(strArr);
715    }
716
717    /**
718     * Map the class and add its IIOP mapping to the repository.
719     */

720    private void addClass(Class JavaDoc cls)
721       throws RMIIIOPViolationException, IRConstructionException
722    {
723       if (cls.isPrimitive())
724          return; // No need to add primitives.
725

726       if (cls.isArray()) {
727          // Add array mapping
728
addArray(cls);
729       } else if (cls.isInterface()) {
730          if (!RmiIdlUtil.isAbstractValueType(cls)) {
731             // Analyse the interface
732
InterfaceAnalysis ia = InterfaceAnalysis.getInterfaceAnalysis(cls);
733         
734             // Add analyzed interface (which may be abstract)
735
addInterface(ia);
736          }
737          else {
738             // Analyse the value
739
ValueAnalysis va = ValueAnalysis.getValueAnalysis(cls);
740             
741             // Add analyzed value
742
addValue(va);
743          }
744       } else if (Exception JavaDoc.class.isAssignableFrom(cls)) { // Exception type.
745
// Analyse the exception
746
ExceptionAnalysis ea = ExceptionAnalysis.getExceptionAnalysis(cls);
747
748          // Add analyzed exception
749
addException(ea);
750       } else { // Got to be a value type.
751
// Analyse the value
752
ValueAnalysis va = ValueAnalysis.getValueAnalysis(cls);
753
754          // Add analyzed value
755
addValue(va);
756       }
757    }
758
759    /**
760     * Add an array.
761     */

762    private ValueBoxDefImpl addArray(Class JavaDoc cls)
763       throws RMIIIOPViolationException, IRConstructionException
764    {
765       if (!cls.isArray())
766          throw new IllegalArgumentException JavaDoc("Not an array class.");
767
768       ValueBoxDefImpl vbDef;
769
770       // Lookup: Has it already been added?
771
vbDef = (ValueBoxDefImpl)arrayMap.get(cls);
772       if (vbDef != null)
773          return vbDef; // Yes, just return it.
774

775       int dimensions = 0;
776       Class JavaDoc compType = cls;
777
778       do {
779          compType = compType.getComponentType();
780          ++dimensions;
781       } while (compType.isArray());
782
783       String JavaDoc typeName;
784       String JavaDoc moduleName;
785       TypeCode JavaDoc typeCode;
786
787       if (compType.isPrimitive()) {
788          if (compType == Boolean.TYPE) {
789             typeName = "boolean";
790             typeCode = orb.get_primitive_tc(TCKind.tk_boolean);
791          } else if (compType == Character.TYPE) {
792             typeName = "wchar";
793             typeCode = orb.get_primitive_tc(TCKind.tk_wchar);
794          } else if (compType == Byte.TYPE) {
795             typeName = "octet";
796             typeCode = orb.get_primitive_tc(TCKind.tk_octet);
797          } else if (compType == Short.TYPE) {
798             typeName = "short";
799             typeCode = orb.get_primitive_tc(TCKind.tk_short);
800          } else if (compType == Integer.TYPE) {
801             typeName = "long";
802             typeCode = orb.get_primitive_tc(TCKind.tk_long);
803          } else if (compType == Long.TYPE) {
804             typeName = "long_long";
805             typeCode = orb.get_primitive_tc(TCKind.tk_longlong);
806          } else if (compType == Float.TYPE) {
807             typeName = "float";
808             typeCode = orb.get_primitive_tc(TCKind.tk_float);
809          } else if (compType == Double.TYPE) {
810             typeName = "double";
811             typeCode = orb.get_primitive_tc(TCKind.tk_double);
812          } else {
813             throw new IRConstructionException("Unknown primitive type for " +
814                                               "array type: " + cls.getName());
815          }
816
817          moduleName = "org.omg.boxedRMI";
818       } else {
819          typeCode = getTypeCode(compType); // map the component type.
820

821          if (compType == java.lang.String JavaDoc.class)
822             typeName = getJavaLangString().name();
823          else if (compType == java.lang.Object JavaDoc.class)
824             typeName = getJavaLang_Object().name();
825          else if (compType == java.lang.Class JavaDoc.class)
826             typeName = getJavaxRmiCORBAClassDesc().name();
827          else if (compType == java.io.Serializable JavaDoc.class)
828             typeName = getJavaIoSerializable().name();
829          else if (compType == java.io.Externalizable JavaDoc.class)
830             typeName = getJavaIoExternalizable().name();
831          else if (compType.isInterface() &&
832                   !RmiIdlUtil.isAbstractValueType(compType))
833             typeName = ((InterfaceDefImpl)interfaceMap.get(compType)).name();
834          else if (Exception JavaDoc.class.isAssignableFrom(compType)) // exception type
835
typeName = ((ExceptionDefImpl)exceptionMap.get(compType)).name();
836          else // must be value type
837
typeName = ((ValueDefImpl)valueMap.get(compType)).name();
838
839          moduleName = "org.omg.boxedRMI." + compType.getPackage().getName();
840       }
841
842       // Get module to add array to.
843
ModuleDefImpl m = ensurePackageExists(moduleName);
844  
845       // Create an array of the types for the dimensions
846
Class JavaDoc[] types = new Class JavaDoc[dimensions];
847       types[dimensions-1] = cls;
848       for (int i = dimensions - 2; i >= 0; --i)
849          types[i] = types[i+1].getComponentType();
850
851       // Create boxed sequences for all dimensions.
852
for (int i = 0; i < dimensions; ++i) {
853          Class JavaDoc type = types[i];
854
855          typeCode = orb.create_sequence_tc(0, typeCode);
856          vbDef = (ValueBoxDefImpl)arrayMap.get(type);
857          if (vbDef == null) {
858             String JavaDoc id = Util.getIRIdentifierOfClass(type);
859
860             SequenceDefImpl sdi = new SequenceDefImpl(typeCode, impl);
861
862             String JavaDoc name = "seq" + (i+1) + "_" + typeName;
863 // TypeCode boxTypeCode = new TypeCodeImpl(TCKind._tk_value_box,
864
// id, name, typeCode);
865
TypeCode JavaDoc boxTypeCode = orb.create_value_box_tc(id, name, typeCode);
866             vbDef = new ValueBoxDefImpl(id, name, "1.0", m, boxTypeCode, impl);
867
868             addTypeCode(type, vbDef.type());
869             m.add(name, vbDef);
870             impl.putSequenceImpl(id, typeCode, sdi, vbDef);
871
872             arrayMap.put(type, vbDef); // Remember we mapped this.
873

874             typeCode = boxTypeCode;
875          } else
876             typeCode = vbDef.type();
877       }
878
879       // Return the box of higest dimension.
880
return vbDef;
881    }
882
883    /**
884     * Add an interface.
885     */

886    private InterfaceDefImpl addInterface(InterfaceAnalysis ia)
887       throws RMIIIOPViolationException, IRConstructionException
888    {
889       InterfaceDefImpl iDef;
890       Class JavaDoc cls = ia.getCls();
891
892       // Lookup: Has it already been added?
893
iDef = (InterfaceDefImpl)interfaceMap.get(cls);
894       if (iDef != null)
895          return iDef; // Yes, just return it.
896

897       if (ia.isAbstractInterface())
898          logger.trace("Adding abstract interface: " + ia.getRepositoryId());
899
900       // Get module to add interface to.
901
ModuleDefImpl m = ensurePackageExists(cls.getPackage().getName());
902  
903       // Add superinterfaces
904
String JavaDoc[] base_interfaces = addInterfaces(ia);
905  
906       // Create the interface
907
String JavaDoc base = cls.getName();
908       base = base.substring(base.lastIndexOf('.')+1);
909       base = Util.javaToIDLName(base);
910  
911       iDef = new InterfaceDefImpl(ia.getRepositoryId(),
912                                   base, "1.0", m,
913                                   base_interfaces, impl);
914       addTypeCode(cls, iDef.type());
915       m.add(base, iDef);
916       interfaceMap.put(cls, iDef); // Remember we mapped this.
917

918       // Fill in constants
919
addConstants(iDef, ia);
920  
921       // Add attributes
922
addAttributes(iDef, ia);
923  
924       // Fill in operations
925
addOperations(iDef, ia);
926  
927       logger.trace("Added interface: " + ia.getRepositoryId());
928       return iDef;
929    }
930
931    /**
932     * Add a value type.
933     */

934    private ValueDefImpl addValue(ValueAnalysis va)
935       throws RMIIIOPViolationException, IRConstructionException
936    {
937       ValueDefImpl vDef;
938       Class JavaDoc cls = va.getCls();
939
940       // Lookup: Has it already been added?
941
vDef = (ValueDefImpl)valueMap.get(cls);
942       if (vDef != null)
943          return vDef; // Yes, just return it.
944

945       // Get module to add value to.
946
ModuleDefImpl m = ensurePackageExists(cls.getPackage().getName());
947  
948       // Add implemented interfaces
949
String JavaDoc[] supported_interfaces = addInterfaces(va);
950
951       // Add abstract base valuetypes
952
String JavaDoc[] abstract_base_valuetypes = addAbstractBaseValuetypes(va);
953
954       // Add superclass
955
ValueDefImpl superValue = null;
956       ValueAnalysis superAnalysis = va.getSuperAnalysis();
957       if (superAnalysis != null)
958          superValue = addValue(superAnalysis);
959
960       // Create the value
961
String JavaDoc base = cls.getName();
962       base = base.substring(base.lastIndexOf('.')+1);
963       base = Util.javaToIDLName(base);
964
965       TypeCode JavaDoc baseTypeCode;
966       if (superValue == null)
967          baseTypeCode = orb.get_primitive_tc(TCKind.tk_null);
968       else
969          baseTypeCode = superValue.type();
970  
971       vDef = new ValueDefImpl(va.getRepositoryId(), base, "1.0",
972                               m,
973                               va.isAbstractValue(),
974                               va.isCustom(),
975                               supported_interfaces,
976                               abstract_base_valuetypes,
977                               baseTypeCode,
978                               impl);
979       addTypeCode(cls, vDef.type());
980       logger.debug("Value: base=" + base);
981       m.add(base, vDef);
982       valueMap.put(cls, vDef); // Remember we mapped this.
983

984       // Fill in constants.
985
addConstants(vDef, va);
986  
987       // Add value members
988
ValueMemberAnalysis[] vmas = va.getMembers();
989       logger.debug("Value member count: " + vmas.length);
990       for (int i = 0; i < vmas.length; ++i) {
991          ValueMemberDefImpl vmDef;
992          String JavaDoc vmid = va.getMemberRepositoryId(vmas[i].getJavaName());
993          String JavaDoc vmName = vmas[i].getIDLName();
994  
995          Class JavaDoc vmCls = vmas[i].getCls();
996          logger.debug("ValueMembers["+i+"] class: " + vmCls.getName());
997          TypeCode JavaDoc typeCode = getTypeCode(vmCls);
998  
999          boolean vmPublic = vmas[i].isPublic();
1000
1001         logger.debug("Adding value member: " + vmid);
1002         vmDef = new ValueMemberDefImpl(vmid, vmName, "1.0",
1003                                        typeCode, vmPublic, vDef, impl);
1004         vDef.add(vmName, vmDef);
1005      }
1006 
1007      // Add attributes
1008
addAttributes(vDef, va);
1009 
1010      // TODO: Fill in operations.
1011

1012      return vDef;
1013   }
1014
1015   /**
1016    * Add an exception type.
1017    */

1018   private ExceptionDefImpl addException(ExceptionAnalysis ea)
1019      throws RMIIIOPViolationException, IRConstructionException
1020   {
1021      ExceptionDefImpl eDef;
1022      Class JavaDoc cls = ea.getCls();
1023
1024      // Lookup: Has it already been added?
1025
eDef = (ExceptionDefImpl)exceptionMap.get(cls);
1026      if (eDef != null)
1027         return eDef; // Yes, just return it.
1028

1029      // 1.3.7.1: map to value
1030
ValueDefImpl vDef = addValue(ea);
1031
1032      // 1.3.7.2: map to exception
1033
ModuleDefImpl m = ensurePackageExists(cls.getPackage().getName());
1034      String JavaDoc base = cls.getName();
1035      base = base.substring(base.lastIndexOf('.')+1);
1036      if (base.endsWith("Exception"))
1037         base = base.substring(0, base.length()-9);
1038      base = Util.javaToIDLName(base + "Ex");
1039
1040      StructMember JavaDoc[] members = new StructMember JavaDoc[1];
1041      members[0] = new StructMember JavaDoc("value", vDef.type(), null/*ignored*/);
1042      TypeCode JavaDoc typeCode
1043                       = orb.create_exception_tc(ea.getExceptionRepositoryId(),
1044                                                 base, members);
1045
1046      eDef = new ExceptionDefImpl(ea.getExceptionRepositoryId(), base, "1.0",
1047                                  typeCode, vDef, m, impl);
1048      logger.debug("Exception: base=" + base);
1049      m.add(base, eDef);
1050      exceptionMap.put(cls, eDef); // Remember we mapped this.
1051

1052      return eDef;
1053   }
1054
1055
1056   // Inner classes -------------------------------------------------
1057
}
1058
Popular Tags