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