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.io.File;
024: import java.util.Enumeration;
025: import java.util.Hashtable;
026:
027: import org.apache.avalon.framework.logger.Logger;
028: import org.omg.CORBA.INTF_REPOS;
029: import org.omg.PortableServer.POA;
030:
031: public class StructDef extends TypedefDef implements
032: org.omg.CORBA.StructDefOperations, ContainerType {
033: private org.omg.CORBA.TypeCode type;
034: private Class myClass;
035: private Class helperClass;
036: private org.omg.CORBA.StructMember[] members;
037:
038: /** local references to contained objects */
039: private Hashtable containedLocals = new Hashtable();
040: /** CORBA references to contained objects */
041: private Hashtable contained = new Hashtable();
042:
043: private File my_dir;
044: private String path;
045:
046: private boolean defined = false;
047:
048: private Logger logger;
049: private ClassLoader loader;
050: private POA poa;
051:
052: public StructDef(Class c, String path,
053: org.omg.CORBA.Container _defined_in,
054: org.omg.CORBA.Repository ir, Logger logger,
055: ClassLoader loader, POA poa) {
056: this .logger = logger;
057: this .loader = loader;
058: this .poa = poa;
059: def_kind = org.omg.CORBA.DefinitionKind.dk_Struct;
060: containing_repository = ir;
061: defined_in = _defined_in;
062: this .path = path;
063: if (defined_in == null) {
064: throw new INTF_REPOS("defined_in = null");
065: }
066: if (containing_repository == null) {
067: throw new INTF_REPOS("containing_repository = null");
068: }
069:
070: try {
071: String classId = c.getName();
072: myClass = c;
073: version("1.0");
074: full_name = classId;
075:
076: if (classId.indexOf('.') > 0) {
077: name(classId.substring(classId.lastIndexOf('.') + 1));
078: absolute_name = org.omg.CORBA.ContainedHelper.narrow(
079: defined_in).absolute_name()
080: + "::" + name;
081: } else {
082: name(classId);
083: absolute_name = "::" + name;
084: }
085:
086: helperClass = this .loader.loadClass(classId + "Helper");
087: id((String) helperClass.getDeclaredMethod("id",
088: (Class[]) null).invoke(null, (Object[]) null));
089:
090: type = (org.omg.CORBA.TypeCode) helperClass
091: .getDeclaredMethod("type", (Class[]) null).invoke(
092: null, (Object[]) null);
093:
094: members = new org.omg.CORBA.StructMember[type
095: .member_count()];
096: for (int i = 0; i < members.length; i++) {
097: org.omg.CORBA.TypeCode type_code = type.member_type(i);
098: String member_name = type.member_name(i);
099:
100: if (this .logger.isDebugEnabled()) {
101: this .logger.debug("StructDef " + absolute_name
102: + " member " + member_name);
103: }
104:
105: members[i] = new org.omg.CORBA.StructMember(
106: member_name, type_code, null);
107: }
108: /* get directory for nested definitions' classes */
109: File f = new File(path + fileSeparator
110: + classId.replace('.', fileSeparator) + "Package");
111:
112: if (f.exists() && f.isDirectory())
113: my_dir = f;
114:
115: if (this .logger.isDebugEnabled()) {
116: this .logger.debug("StructDef: " + absolute_name);
117: }
118: } catch (Exception e) {
119: logger.error("Caught Exception", e);
120: throw new INTF_REPOS(ErrorMsg.IR_Not_Implemented,
121: org.omg.CORBA.CompletionStatus.COMPLETED_NO);
122: }
123: }
124:
125: public void loadContents() {
126: // read from the class (operations and atributes)
127: if (getReference() == null) {
128: throw new INTF_REPOS("getReference returns null");
129: }
130:
131: org.omg.CORBA.StructDef myReference = org.omg.CORBA.StructDefHelper
132: .narrow(getReference());
133:
134: if (myReference == null) {
135: throw new INTF_REPOS("narrow failed for " + getReference());
136: }
137:
138: /* load nested definitions from interfacePackage directory */
139:
140: String[] classes = null;
141: if (my_dir != null) {
142: classes = my_dir.list(new IRFilenameFilter(".class"));
143:
144: // load class files in this interface's Package directory
145: if (classes != null) {
146: for (int j = 0; j < classes.length; j++) {
147: try {
148: if (this .logger.isDebugEnabled()) {
149: this .logger
150: .debug("Struct "
151: + name
152: + " tries "
153: + full_name
154: + "Package."
155: + classes[j]
156: .substring(
157: 0,
158: classes[j]
159: .indexOf(".class")));
160: }
161:
162: ClassLoader loader = getClass()
163: .getClassLoader();
164: if (loader == null) {
165: loader = this .loader;
166: }
167:
168: Class cl = loader.loadClass((full_name
169: + "Package." + classes[j].substring(0,
170: classes[j].indexOf(".class"))));
171:
172: Contained containedObject = Contained
173: .createContained(cl, path, myReference,
174: containing_repository,
175: this .logger, this .loader,
176: this .poa);
177: if (containedObject == null)
178: continue;
179:
180: org.omg.CORBA.Contained containedRef = Contained
181: .createContainedReference(
182: containedObject, this .logger,
183: this .poa);
184:
185: if (containedObject instanceof ContainerType)
186: ((ContainerType) containedObject)
187: .loadContents();
188:
189: containedRef.move(myReference, containedRef
190: .name(), containedRef.version());
191:
192: if (this .logger.isDebugEnabled()) {
193: this .logger.debug("Struct " + full_name
194: + " loads " + containedRef.name());
195: }
196:
197: contained
198: .put(containedRef.name(), containedRef);
199: containedLocals.put(containedRef.name(),
200: containedObject);
201: } catch (Exception e) {
202: logger.error("Caught Exception", e);
203: }
204: }
205: }
206: }
207: }
208:
209: /**
210: */
211:
212: public void define() {
213: if (this .logger.isDebugEnabled()) {
214: this .logger.debug("Struct " + name + " defining...");
215: }
216:
217: for (Enumeration e = containedLocals.elements(); e
218: .hasMoreElements(); ((IRObject) e.nextElement())
219: .define())
220: ;
221:
222: for (int i = 0; i < members.length; i++) {
223: members[i].type_def = IDLType.create(members[i].type,
224: containing_repository, this .logger, this .poa);
225:
226: if (members[i].type_def == null) {
227: throw new INTF_REPOS("No type_def for member "
228: + members[i].name + " in struct " + full_name);
229: }
230: }
231: defined = true;
232:
233: if (this .logger.isDebugEnabled()) {
234: this .logger.debug("Struct " + name + " defined");
235: }
236: }
237:
238: /**
239: */
240:
241: org.omg.CORBA.TypeDescription describe_struct() {
242: if (!defined) {
243: throw new INTF_REPOS("Struct " + full_name
244: + " not defined! ");
245: }
246:
247: return new org.omg.CORBA.TypeDescription(name(), id(),
248: org.omg.CORBA.ContainedHelper.narrow(defined_in).id(),
249: version(), type());
250: }
251:
252: public org.omg.CORBA.TypeCode type() {
253: if (type == null) {
254: throw new INTF_REPOS("Struct TypeCode is null");
255: }
256:
257: return type;
258: }
259:
260: public org.omg.CORBA.Contained lookup(String scopedname) {
261: if (this .logger.isDebugEnabled()) {
262: this .logger.debug("Struct " + this .name + " lookup "
263: + scopedname);
264: }
265:
266: String top_level_name;
267: String rest_of_name;
268: String name;
269:
270: if (scopedname.startsWith("::")) {
271: name = scopedname.substring(2);
272: } else
273: name = scopedname;
274:
275: if (name.indexOf("::") > 0) {
276: top_level_name = name.substring(0, name.indexOf("::"));
277: rest_of_name = name.substring(name.indexOf("::") + 2);
278: } else {
279: top_level_name = name;
280: rest_of_name = null;
281: }
282:
283: try {
284: org.omg.CORBA.Contained top = (org.omg.CORBA.Contained) contained
285: .get(top_level_name);
286:
287: if (top == null) {
288: if (this .logger.isDebugEnabled()) {
289: this .logger.debug("Interface " + this .name
290: + " top " + top_level_name + " not found ");
291: }
292: return null;
293: }
294:
295: if (rest_of_name == null) {
296: return top;
297: } else {
298: if (top instanceof org.omg.CORBA.Container) {
299: return ((org.omg.CORBA.Container) top)
300: .lookup(rest_of_name);
301: } else {
302: if (this .logger.isDebugEnabled()) {
303: this .logger.debug("Interface " + this .name
304: + " " + scopedname + " not found");
305: }
306: return null;
307: }
308: }
309: } catch (Exception e) {
310: logger.error("Caught Exception", e);
311: return null;
312: }
313: }
314:
315: public org.omg.CORBA.StructMember[] members() {
316: if (!defined) {
317: throw new INTF_REPOS("Struct " + full_name
318: + " not defined! ");
319: }
320:
321: return members;
322: }
323:
324: // write interface not supported!
325:
326: public void members(org.omg.CORBA.StructMember[] a) {
327: }
328:
329: public org.omg.CORBA.ModuleDef create_module(String id,
330: String name, String version) {
331: return null;
332: }
333:
334: public org.omg.CORBA.ConstantDef create_constant(
335: java.lang.String id, java.lang.String name,
336: java.lang.String version, org.omg.CORBA.IDLType type,
337: org.omg.CORBA.Any value) {
338: return null;
339: }
340:
341: public org.omg.CORBA.StructDef create_struct(String id,
342: String name, String version,
343: org.omg.CORBA.StructMember[] members) {
344: return null;
345: }
346:
347: public org.omg.CORBA.UnionDef create_union(String id, String name,
348: String version, org.omg.CORBA.IDLType discriminator_type,
349: org.omg.CORBA.UnionMember[] members) {
350: return null;
351: }
352:
353: public org.omg.CORBA.EnumDef create_enum(String id, String name,
354: String version, String[] members) {
355: return null;
356: }
357:
358: public org.omg.CORBA.AliasDef create_alias(String id, String name,
359: String version, org.omg.CORBA.IDLType original_type) {
360: return null;
361: }
362:
363: /**
364: * not supported
365: */
366:
367: public org.omg.CORBA.ExceptionDef create_exception(
368: java.lang.String id, java.lang.String name,
369: java.lang.String version,
370: org.omg.CORBA.StructMember[] member) {
371: return null;
372: }
373:
374: /**
375: * not supported
376: */
377:
378: public org.omg.CORBA.InterfaceDef create_interface(String id,
379: String name, String version,
380: org.omg.CORBA.InterfaceDef[] base_interfaces,
381: boolean is_abstract) {
382: return null;
383: }
384:
385: /**
386: * not supported
387: */
388:
389: public org.omg.CORBA.ValueBoxDef create_value_box(
390: java.lang.String id, java.lang.String name,
391: java.lang.String version, org.omg.CORBA.IDLType type) {
392: return null;
393: }
394:
395: /**
396: * not supported
397: */
398:
399: public org.omg.CORBA.ValueDef create_value(java.lang.String id,
400: java.lang.String name, java.lang.String version,
401: boolean is_custom, boolean is_abstract,
402: org.omg.CORBA.ValueDef base_value, boolean is_truncatable,
403: org.omg.CORBA.ValueDef[] abstract_base_values,
404: org.omg.CORBA.InterfaceDef[] supported_interfaces,
405: org.omg.CORBA.Initializer[] initializers) {
406: return null;
407: }
408:
409: /**
410: * not supported
411: */
412:
413: public org.omg.CORBA.NativeDef create_native(java.lang.String id,
414: java.lang.String name, java.lang.String version) {
415: return null;
416: }
417:
418: public void destroy() {
419: throw new INTF_REPOS(ErrorMsg.IR_Not_Implemented,
420: org.omg.CORBA.CompletionStatus.COMPLETED_NO);
421: }
422:
423: public org.omg.CORBA.Contained[] lookup_name(String search_name,
424: int levels_to_search,
425: org.omg.CORBA.DefinitionKind limit_type,
426: boolean exclude_inherited) {
427: if (levels_to_search == 0)
428: return null;
429:
430: org.omg.CORBA.Contained[] c = contents(limit_type,
431: exclude_inherited);
432: Hashtable found = new Hashtable();
433:
434: for (int i = 0; i < c.length; i++)
435: if (c[i].name().equals(search_name))
436: found.put(c[i], "");
437:
438: if (levels_to_search > 1 || levels_to_search == -1) {
439: // search up to a specific depth or undefinitely
440: for (int i = 0; i < c.length; i++) {
441: if (c[i] instanceof org.omg.CORBA.Container) {
442: org.omg.CORBA.Contained[] tmp_seq = ((org.omg.CORBA.Container) c[i])
443: .lookup_name(search_name,
444: levels_to_search - 1, limit_type,
445: exclude_inherited);
446: if (tmp_seq != null)
447: for (int j = 0; j < tmp_seq.length; j++)
448: found.put(tmp_seq[j], "");
449: }
450: }
451: }
452:
453: org.omg.CORBA.Contained[] result = new org.omg.CORBA.Contained[found
454: .size()];
455: int idx = 0;
456:
457: for (Enumeration e = found.keys(); e.hasMoreElements();)
458: result[idx++] = (org.omg.CORBA.Contained) e.nextElement();
459:
460: return result;
461: }
462:
463: public org.omg.CORBA.ContainerPackage.Description[] describe_contents(
464: org.omg.CORBA.DefinitionKind limit_type,
465: boolean exclude_inherited, int max_returned_objs) {
466: return null;
467: }
468:
469: public org.omg.CORBA.Contained[] contents(
470: org.omg.CORBA.DefinitionKind limit_type,
471: boolean exclude_inherited) {
472: Hashtable limited = new Hashtable();
473:
474: // analog constants, exceptions etc.
475:
476: for (Enumeration e = contained.elements(); e.hasMoreElements();) {
477: org.omg.CORBA.Contained c = (org.omg.CORBA.Contained) e
478: .nextElement();
479: if (limit_type == org.omg.CORBA.DefinitionKind.dk_all
480: || limit_type == c.def_kind()) {
481: limited.put(c, "");
482: }
483: }
484:
485: org.omg.CORBA.Contained[] c = new org.omg.CORBA.Contained[limited
486: .size()];
487: int i;
488: Enumeration e;
489: for (e = limited.keys(), i = 0; e.hasMoreElements(); i++)
490: c[i] = (org.omg.CORBA.Contained) e.nextElement();
491: return c;
492: }
493:
494: // from Contained
495:
496: public org.omg.CORBA.ContainedPackage.Description describe() {
497: if (!defined) {
498: throw new INTF_REPOS("Struct " + full_name
499: + " not defined! ");
500: }
501:
502: org.omg.CORBA.Any a = orb.create_any();
503: org.omg.CORBA.TypeDescription ed = describe_struct();
504: org.omg.CORBA.TypeDescriptionHelper.insert(a, ed);
505: return new org.omg.CORBA.ContainedPackage.Description(
506: org.omg.CORBA.DefinitionKind.dk_Struct, a);
507: }
508:
509: }
|