001: package org.jacorb.ir;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.lang.reflect.*;
024: import java.util.*;
025: import java.io.*;
026:
027: import org.omg.CORBA.INTF_REPOS;
028: import org.omg.CORBA.TypeCode;
029: import org.omg.CORBA.Any;
030: import org.omg.PortableServer.POA;
031:
032: import org.apache.avalon.framework.logger.Logger;
033:
034: public class UnionDef extends TypedefDef implements
035: org.omg.CORBA.UnionDefOperations, ContainerType {
036: private org.omg.CORBA.UnionMember[] members;
037: private org.omg.CORBA.TypeCode discriminator_type;
038: private org.omg.CORBA.IDLType discriminator_type_def;
039:
040: private Method memberMethods[];
041: private int member_size;
042:
043: /* reference to my container as a contained object */
044: private org.omg.CORBA.Contained myContainer;
045: /** local references to contained objects */
046: private Hashtable containedLocals = new Hashtable();
047: /** CORBA references to contained objects */
048: private Hashtable contained = new Hashtable();
049:
050: private File my_dir;
051: private String path;
052: private Logger logger;
053: private ClassLoader loader;
054: private POA poa;
055:
056: public UnionDef(Class c, String path,
057: org.omg.CORBA.Container _defined_in,
058: org.omg.CORBA.Repository ir, ClassLoader loader,
059: Logger logger, POA poa) {
060: this .loader = loader;
061: this .logger = logger;
062: this .poa = poa;
063:
064: def_kind = org.omg.CORBA.DefinitionKind.dk_Union;
065: containing_repository = ir;
066: defined_in = _defined_in;
067: version = "1.0";
068: String classId = c.getName();
069: myContainer = org.omg.CORBA.ContainedHelper.narrow(defined_in);
070:
071: if (classId.indexOf('.') > 0) {
072: name(classId.substring(classId.lastIndexOf('.') + 1));
073: absolute_name = myContainer.absolute_name() + "::" + name;
074: } else {
075: name(classId);
076: defined_in = containing_repository;
077: absolute_name = "::" + name;
078: }
079:
080: Class helperClass;
081: try {
082: helperClass = this .loader.loadClass(classId + "Helper");
083: id((String) helperClass.getDeclaredMethod("id",
084: (Class[]) null).invoke(null, (Object[]) null));
085: type = TypeCodeUtil.getTypeCode(c, this .loader, null,
086: classId, this .logger);
087: members = new org.omg.CORBA.UnionMember[type.member_count()];
088: for (int i = 0; i < members.length; i++) {
089: members[i] = new org.omg.CORBA.UnionMember(type
090: .member_name(i), type.member_label(i), type
091: .member_type(i), null);
092: }
093: discriminator_type = type.discriminator_type();
094: } catch (Exception e) {
095: this .logger.error("Caught Exception", e);
096: }
097: }
098:
099: public void loadContents() {
100: // read from the class (operations and atributes)
101: if (getReference() == null) {
102: throw new INTF_REPOS("getReference returns null");
103: }
104:
105: org.omg.CORBA.UnionDef myReference = org.omg.CORBA.UnionDefHelper
106: .narrow(getReference());
107:
108: if (myReference == null) {
109: throw new INTF_REPOS("narrow failed for " + getReference());
110: }
111:
112: /* load nested definitions from interfacePackage directory */
113:
114: String[] classes = null;
115: if (my_dir != null) {
116: classes = my_dir.list(new IRFilenameFilter(".class"));
117:
118: // load class files in this interface's Package directory
119: if (classes != null) {
120: for (int j = 0; j < classes.length; j++) {
121: try {
122: if (this .logger.isDebugEnabled()) {
123: this .logger
124: .debug("Union "
125: + name
126: + " tries "
127: + full_name
128: + "Package."
129: + classes[j]
130: .substring(
131: 0,
132: classes[j]
133: .indexOf(".class")));
134: }
135:
136: ClassLoader loader = getClass()
137: .getClassLoader();
138: if (loader == null) {
139: loader = this .loader;
140: }
141:
142: Class cl = loader.loadClass((full_name
143: + "Package." + classes[j].substring(0,
144: classes[j].indexOf(".class"))));
145:
146: Contained containedObject = Contained
147: .createContained(cl, path, myReference,
148: containing_repository,
149: this .logger, this .loader,
150: this .poa);
151: if (containedObject == null)
152: continue;
153:
154: org.omg.CORBA.Contained containedRef = Contained
155: .createContainedReference(
156: containedObject, this .logger,
157: this .poa);
158:
159: if (containedObject instanceof ContainerType)
160: ((ContainerType) containedObject)
161: .loadContents();
162:
163: containedRef.move(myReference, containedRef
164: .name(), containedRef.version());
165:
166: if (this .logger.isDebugEnabled()) {
167: this .logger.debug("Union " + full_name
168: + " loads " + containedRef.name());
169: }
170:
171: contained
172: .put(containedRef.name(), containedRef);
173: containedLocals.put(containedRef.name(),
174: containedObject);
175: } catch (Exception e) {
176: this .logger.error("Caught Exception", e);
177: }
178: }
179: }
180: }
181: }
182:
183: public void define() {
184: if (this .logger.isDebugEnabled()) {
185: this .logger.debug("Union " + name + " defining...");
186: }
187:
188: discriminator_type_def = IDLType.create(discriminator_type,
189: containing_repository, this .logger, this .poa);
190:
191: for (Enumeration e = containedLocals.elements(); e
192: .hasMoreElements(); ((IRObject) e.nextElement())
193: .define())
194: ;
195:
196: try {
197: for (int i = 0; i < members.length; i++) {
198: members[i].type_def = IDLType.create(members[i].type,
199: containing_repository, this .logger, this .poa);
200: }
201: } catch (Exception e) {
202: this .logger.error("Caught Exception", e);
203: }
204:
205: if (this .logger.isDebugEnabled()) {
206: this .logger.debug("UnionDef " + name + " defined");
207: }
208: }
209:
210: public org.omg.CORBA.UnionMember[] members() {
211: return members;
212: }
213:
214: public void members(org.omg.CORBA.UnionMember[] m) {
215: members = m;
216: }
217:
218: public org.omg.CORBA.TypeCode discriminator_type() {
219: return discriminator_type;
220: }
221:
222: public org.omg.CORBA.IDLType discriminator_type_def() {
223: return discriminator_type_def;
224: }
225:
226: public void discriminator_type_def(org.omg.CORBA.IDLType dt) {
227: discriminator_type_def = dt;
228: }
229:
230: // from Contained
231:
232: public org.omg.CORBA.ContainedPackage.Description describe() {
233: org.omg.CORBA.Any a = orb.create_any();
234: String def_in_name;
235:
236: if (myContainer != null)
237: def_in_name = myContainer.id();
238: else
239: def_in_name = "IDL:/:1.0";
240:
241: org.omg.CORBA.TypeDescriptionHelper.insert(a,
242: new org.omg.CORBA.TypeDescription(name(), id(),
243: def_in_name, version(), type()));
244: return new org.omg.CORBA.ContainedPackage.Description(
245: org.omg.CORBA.DefinitionKind.dk_Union, a);
246: }
247:
248: // from IRObject
249:
250: public void destroy() {
251: }
252:
253: public org.omg.CORBA.Contained[] contents(
254: org.omg.CORBA.DefinitionKind limit_type,
255: boolean exclude_inherited) {
256: Hashtable filtered = new Hashtable();
257:
258: if (limit_type == org.omg.CORBA.DefinitionKind.dk_all) {
259: filtered = contained;
260: } else {
261: Enumeration f = contained.keys();
262: while (f.hasMoreElements()) {
263: Object k = f.nextElement();
264: org.omg.CORBA.Contained c = (org.omg.CORBA.Contained) contained
265: .get(k);
266: if (c.def_kind() == limit_type)
267: filtered.put(k, c);
268: }
269: }
270:
271: Enumeration e = filtered.elements();
272: org.omg.CORBA.Contained[] result = new org.omg.CORBA.Contained[filtered
273: .size()];
274:
275: for (int i = 0; i < filtered.size(); i++)
276: result[i] = (org.omg.CORBA.Contained) e.nextElement();
277:
278: return result;
279: }
280:
281: /**
282: * retrieves a contained object given a scoped name
283: */
284:
285: public org.omg.CORBA.Contained lookup(String scopedname) {
286: String top_level_name;
287: String rest_of_name;
288: String name;
289:
290: if (scopedname.startsWith("::")) {
291: name = scopedname.substring(2);
292: } else
293: name = scopedname;
294:
295: if (name.indexOf("::") > 0) {
296: top_level_name = name.substring(0, name.indexOf("::"));
297: rest_of_name = name.substring(name.indexOf("::") + 2);
298: } else {
299: top_level_name = name;
300: rest_of_name = null;
301: }
302:
303: org.omg.CORBA.Contained top = (org.omg.CORBA.Contained) contained
304: .get(top_level_name);
305: if (top == null) {
306: if (this .logger.isDebugEnabled()) {
307: this .logger.debug("Container " + this .name + " top "
308: + top_level_name + " not found ");
309: }
310:
311: return null;
312: }
313:
314: if (rest_of_name == null) {
315: return top;
316: } else {
317: org.omg.CORBA.Container topContainer = org.omg.CORBA.ContainerHelper
318: .narrow(top);
319: if (topContainer != null) {
320: return topContainer.lookup(rest_of_name);
321: } else {
322: if (this .logger.isDebugEnabled()) {
323: this .logger.debug("Container " + this .name + " "
324: + scopedname + " not found, top "
325: + top.getClass().getName());
326: }
327: return null;
328: }
329: }
330: }
331:
332: public org.omg.CORBA.Contained[] lookup_name(String search_name, /*Identifier*/
333: int levels_to_search, org.omg.CORBA.DefinitionKind limit_type,
334: boolean exclude_inherited) {
335: if (levels_to_search == 0)
336: return null;
337:
338: org.omg.CORBA.Contained[] c = contents(limit_type,
339: exclude_inherited);
340: Hashtable found = new Hashtable();
341:
342: for (int i = 0; i < c.length; i++)
343: if (c[i].name().equals(search_name))
344: found.put(c[i], "");
345:
346: if (levels_to_search > 1 || levels_to_search < 0) {
347: // search up to a specific depth or undefinitely
348: for (int i = 0; i < c.length; i++) {
349: if (c[i] instanceof org.omg.CORBA.Container) {
350: org.omg.CORBA.Contained[] tmp_seq = ((org.omg.CORBA.Container) c[i])
351: .lookup_name(search_name,
352: levels_to_search - 1, limit_type,
353: exclude_inherited);
354: if (tmp_seq != null)
355: for (int j = 0; j < tmp_seq.length; j++)
356: found.put(tmp_seq[j], "");
357: }
358: }
359: }
360:
361: org.omg.CORBA.Contained[] result = new org.omg.CORBA.Contained[found
362: .size()];
363: int idx = 0;
364:
365: for (Enumeration e = found.keys(); e.hasMoreElements();)
366: result[idx++] = (org.omg.CORBA.Contained) e.nextElement();
367:
368: return result;
369: }
370:
371: public org.omg.CORBA.ContainerPackage.Description[] describe_contents(
372: org.omg.CORBA.DefinitionKind limit_type,
373: boolean exclude_inherited, int max_returned_objs) {
374: return null;
375: }
376:
377: public org.omg.CORBA.ModuleDef create_module(
378: /*RepositoryId*/String id, /*Identifier*/String name, /*VersionSpec*/
379: String version) {
380: return null;
381: }
382:
383: public org.omg.CORBA.ConstantDef create_constant(
384: /*RepositoryId*/String id, /*Identifier*/String name, /*VersionSpec*/
385: String version, org.omg.CORBA.IDLType type,
386: org.omg.CORBA.Any value) {
387: return null;
388: }
389:
390: public org.omg.CORBA.StructDef create_struct(
391: /*RepositoryId*/String id, /*Identifier*/String name, /*VersionSpec*/
392: String version, /*StructMemberSeq*/
393: org.omg.CORBA.StructMember[] members) {
394: return null;
395: }
396:
397: public org.omg.CORBA.UnionDef create_union(
398: /*RepositoryId*/String id, /*Identifier*/String name, /*VersionSpec*/
399: String version, org.omg.CORBA.IDLType discriminator_type, /*UnionMemberSeq*/
400: org.omg.CORBA.UnionMember[] members) {
401: return null;
402: }
403:
404: public org.omg.CORBA.EnumDef create_enum(
405: /*RepositoryId*/String id, /*Identifier*/String name, /*VersionSpec*/
406: String version, /*EnumMemberSeq*//*Identifier*/
407: String[] members) {
408: return null;
409: }
410:
411: public org.omg.CORBA.AliasDef create_alias(
412: /*RepositoryId*/String id, /*Identifier*/String name, /*VersionSpec*/
413: String version, org.omg.CORBA.IDLType original_type) {
414: return null;
415: }
416:
417: /**
418: * not supported
419: */
420:
421: public org.omg.CORBA.ExceptionDef create_exception(
422: java.lang.String id, java.lang.String name,
423: java.lang.String version,
424: org.omg.CORBA.StructMember[] member) {
425: return null;
426: }
427:
428: /**
429: * not supported
430: */
431:
432: public org.omg.CORBA.InterfaceDef create_interface(
433: /*RepositoryId*/String id,
434: /*Identifier*/String name,
435: /*VersionSpec*/String version,
436: /*InterfaceDefSeq*/org.omg.CORBA.InterfaceDef[] base_interfaces,
437: boolean is_abstract) {
438: return null;
439: }
440:
441: /**
442: * not supported
443: */
444:
445: public org.omg.CORBA.ValueBoxDef create_value_box(
446: java.lang.String id, java.lang.String name,
447: java.lang.String version, org.omg.CORBA.IDLType type) {
448: return null;
449: }
450:
451: /**
452: * not supported
453: */
454:
455: public org.omg.CORBA.ValueDef create_value(java.lang.String id,
456: java.lang.String name, java.lang.String version,
457: boolean is_custom, boolean is_abstract,
458: org.omg.CORBA.ValueDef base_value, boolean is_truncatable,
459: org.omg.CORBA.ValueDef[] abstract_base_values,
460: org.omg.CORBA.InterfaceDef[] supported_interfaces,
461: org.omg.CORBA.Initializer[] initializers) {
462: return null;
463: }
464:
465: /**
466: * not supported
467: */
468:
469: public org.omg.CORBA.NativeDef create_native(java.lang.String id,
470: java.lang.String name, java.lang.String version) {
471: return null;
472: }
473:
474: }
|