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, Cambrigde, MA 02139, USA.
021: */
022:
023: import org.omg.PortableServer.*;
024:
025: import java.util.*;
026: import java.io.*;
027:
028: import org.apache.avalon.framework.logger.Logger;
029: import org.apache.avalon.framework.configuration.*;
030:
031: /**
032: * The Interface Repository.
033: * <p>
034: * This class represents the repository itself as
035: * well as the executable server.
036: * <p>
037: * Methods from the "write" interface to the IR
038: * are not supported.
039: * <p>
040: *
041: * @author Gerald Brose
042: * @version $Id: RepositoryImpl.java,v 1.12 2006/06/15 16:43:14 alphonse.bendt Exp $
043: */
044:
045: public class RepositoryImpl extends IRObject implements
046: org.omg.CORBA.RepositoryOperations, Configurable
047:
048: {
049: private Container[] containers;
050: private Container delegate;
051: private POA poa;
052:
053: /** the configuration object for this IR instance */
054: private org.jacorb.config.Configuration configuration = null;
055:
056: /** the IR logger instance */
057: private Logger logger = null;
058:
059: /**
060: * constructor to launch a repository with the contents of <tt>classpath</tt>
061: *
062: * @param classpath a classpath string made up of directories separated by ":"
063: */
064:
065: public RepositoryImpl(String classpath, String outfile,
066: //#ifjdk 1.2
067: java.net.URLClassLoader loader)
068: //#else
069: //# ClassLoader loader )
070: //#endif
071: throws Exception {
072: def_kind = org.omg.CORBA.DefinitionKind.dk_Repository;
073: name = "Repository";
074:
075: // parse classpath and create a top-level container for
076: // each directory in the path
077:
078: StringTokenizer strtok = new StringTokenizer(classpath,
079: java.io.File.pathSeparator);
080: // new StringTokenizer( classpath , ";" );
081:
082: String[] paths = new String[strtok.countTokens()];
083:
084: containers = new Container[paths.length];
085:
086: org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(new String[0],
087: null);
088:
089: this .configure(((org.jacorb.orb.ORB) orb).getConfiguration());
090:
091: poa = POAHelper.narrow(orb
092: .resolve_initial_references("RootPOA"));
093:
094: org.omg.CORBA.Repository myRef = org.omg.CORBA.RepositoryHelper
095: .narrow(poa
096: .servant_to_reference(new org.omg.CORBA.RepositoryPOATie(
097: this )));
098:
099: for (int i = 0; strtok.hasMoreTokens(); i++) {
100: paths[i] = strtok.nextToken();
101:
102: if (this .logger.isDebugEnabled()) {
103: logger.debug("found path: " + paths[i]);
104: }
105:
106: containers[i] = new Container(this , paths[i], null, loader,
107: poa, logger);
108: }
109:
110: // dummy
111: delegate = containers[0];
112:
113: PrintWriter out = new PrintWriter(
114: new FileOutputStream(outfile), true);
115: out.println(orb.object_to_string(myRef));
116: setReference(myRef);
117: out.close();
118: poa.the_POAManager().activate();
119:
120: if (this .logger.isInfoEnabled()) {
121: //#ifjdk 1.2
122: java.net.URL urls[] = loader.getURLs();
123: //#else
124: //# java.net.URL urls[] = new java.net.URL[0];
125: //#endif
126: StringBuffer sb = new StringBuffer(
127: "IR configured for class path: ");
128: for (int i = 0; i < urls.length; i++) {
129: sb.append(urls[i].toString() + "\n");
130: }
131:
132: logger.info(sb.toString());
133: }
134: }
135:
136: public void configure(Configuration myConfiguration)
137: throws ConfigurationException {
138: this .configuration = (org.jacorb.config.Configuration) myConfiguration;
139: this .logger = configuration.getNamedLogger("jacorb.ir");
140: }
141:
142: // Repository
143:
144: /**
145: * convert a repository ID to a scoped name
146: * @param id a string in Repository ID format, e.g. "IDL:myModule/MyInterface:1.0"
147: * @return a scoped name, e.g. "::myModule::MyInterface", or null
148: * if the id argument does not begin with "IDL:"
149: */
150:
151: private String idToScopedName(String id) {
152: String scoped = "";
153:
154: if (!id.startsWith("IDL:") || !id.endsWith(":1.0"))
155: return null;
156:
157: // strip "IDL:" and ":1.0")
158:
159: String base = id.substring(id.indexOf(':') + 1,
160: id.lastIndexOf(':')).replace(fileSeparator, '/');
161:
162: if (base.startsWith("omg.org"))
163: base = "org/omg" + base.substring(7);
164:
165: StringTokenizer strtok = new StringTokenizer(base, "/");
166:
167: for (int i = 0; strtok.hasMoreTokens(); i++)
168: scoped = scoped + "::" + strtok.nextToken();
169:
170: return scoped;
171:
172: }
173:
174: /**
175: * lookup a repository ID
176: * @param search_id a string in Repository ID format,
177: * e.g. "IDL:myModule/MyInterface:1.0"
178: * @return a reference to the object or null, if not found
179: */
180: public org.omg.CORBA.Contained lookup_id(String search_id) {
181: if (this .logger.isDebugEnabled()) {
182: this .logger.debug("IR lookup_id: " + search_id);
183: }
184:
185: String name = idToScopedName(search_id);
186: if (name == null) {
187: return null;
188: }
189: return lookup(name);
190: }
191:
192: public org.omg.CORBA.PrimitiveDef get_primitive(
193: org.omg.CORBA.PrimitiveKind kind) {
194: try {
195: return org.omg.CORBA.PrimitiveDefHelper
196: .narrow(poa
197: .servant_to_reference(new org.omg.CORBA.PrimitiveDefPOATie(
198: new org.jacorb.ir.PrimitiveDef(kind
199: .value()))));
200: } catch (Exception e) {
201: logger.error("unexpected exception", e);
202: return null;
203: }
204: }
205:
206: /**
207: * not supported
208: */
209:
210: public org.omg.CORBA.StringDef create_string(int bound) {
211: return null;
212: }
213:
214: /**
215: * not supported
216: */
217:
218: public org.omg.CORBA.WstringDef create_wstring(int bound) {
219: return null;
220: }
221:
222: /**
223: * not supported
224: */
225:
226: public org.omg.CORBA.FixedDef create_fixed(short digits, short scale) {
227: return null;
228: }
229:
230: /**
231: * not supported
232: */
233:
234: public org.omg.CORBA.SequenceDef create_sequence(int bound,
235: org.omg.CORBA.IDLType element_type) {
236: return null;
237: }
238:
239: /**
240: * not supported
241: */
242:
243: public org.omg.CORBA.ArrayDef create_array(int length,
244: org.omg.CORBA.IDLType element_type) {
245: return null;
246: }
247:
248: public org.omg.CORBA.TypeCode get_canonical_typecode(
249: org.omg.CORBA.TypeCode tc) {
250: return null;
251: }
252:
253: // container
254:
255: /**
256: * lookup a scoped name in the repository
257: *
258: * @param name the name to look for
259: * @return a reference to the item with the specified name
260: * or null, if not found
261: */
262:
263: public org.omg.CORBA.Contained lookup(String name) {
264: if (this .logger.isDebugEnabled()) {
265: this .logger.debug("IR lookup : " + name);
266: }
267:
268: org.omg.CORBA.Contained result = null;
269: for (int i = 0; i < containers.length; i++) {
270: result = containers[i].lookup(name);
271: if (result != null)
272: break;
273: }
274: return result;
275: }
276:
277: /**
278: * lookup a simple name in the repository
279: * (neither scoped nor ID formatted)
280: *
281: * @param search_name the name to look for
282: * @param levels_to_search if 1, search only this object, if -1, search
283: * all containers contained in this repository, else search
284: * until the specified depth is reached
285: * @param limit_type limit the description to objects of this type
286: * @param exclude_inherited exclude inherited items from the description
287: * @return an array of items with the specified name
288: */
289:
290: public org.omg.CORBA.Contained[] lookup_name(String search_name,
291: int levels_to_search,
292: org.omg.CORBA.DefinitionKind limit_type,
293: boolean exclude_inherited) {
294: if (this .logger.isDebugEnabled()) {
295: this .logger.debug("IR lookup_name: " + search_name);
296: }
297:
298: org.omg.CORBA.Contained[] result = null;
299: Vector intermediate = new Vector();
300:
301: for (int i = 0; i < containers.length; i++) {
302: intermediate.addElement(containers[i].lookup_name(
303: search_name, levels_to_search, limit_type,
304: exclude_inherited));
305: }
306:
307: int size = 0;
308: for (int i = 0; i < intermediate.size(); i++) {
309: size += ((Object[]) intermediate.elementAt(i)).length;
310: }
311: result = new org.omg.CORBA.Contained[size];
312: int start = 0;
313:
314: for (int i = 0; i < intermediate.size(); i++) {
315: org.omg.CORBA.Contained[] src = (org.omg.CORBA.Contained[]) intermediate
316: .elementAt(i);
317:
318: System.arraycopy(src, 0, result, start, src.length);
319: start += src.length;
320: }
321:
322: return result;
323: }
324:
325: /**
326: * list the contents of the repository
327: * @param limit_type limit the description to objects of this type
328: * @param exclude_inherited exclude inherited items from the description
329: * @return an array of items contained in this repository
330: */
331:
332: public org.omg.CORBA.Contained[] contents(
333: org.omg.CORBA.DefinitionKind limit_type,
334: boolean exclude_inherited) {
335: org.omg.CORBA.Contained[] result = null;
336: Vector intermediate = new Vector();
337: for (int i = 0; i < containers.length; i++) {
338: intermediate.addElement(containers[i].contents(limit_type,
339: exclude_inherited));
340: }
341:
342: int size = 0;
343: for (int i = 0; i < intermediate.size(); i++) {
344: size += ((Object[]) intermediate.elementAt(i)).length;
345: }
346:
347: result = new org.omg.CORBA.Contained[size];
348: int start = 0;
349:
350: // assemble result array
351: for (int i = 0; i < intermediate.size(); i++) {
352: org.omg.CORBA.Contained[] src = (org.omg.CORBA.Contained[]) intermediate
353: .elementAt(i);
354:
355: System.arraycopy(src, 0, result, start, src.length);
356: start += src.length;
357: }
358:
359: return result;
360: }
361:
362: /**
363: * describe the contents of the repository
364: * @param limit_type limit the description to objects of this type
365: * @param exclude_inherited exclude inherited items from the description
366: * @param max_returned_objs return only so many items
367: * @return an array of descriptions
368: */
369:
370: public org.omg.CORBA.ContainerPackage.Description[] describe_contents(
371: org.omg.CORBA.DefinitionKind limit_type,
372: boolean exclude_inherited, int max_returned_objs) {
373: org.omg.CORBA.Contained[] c = contents(limit_type,
374: exclude_inherited);
375: int size;
376: if (max_returned_objs > c.length)
377: size = max_returned_objs;
378: else
379: size = c.length;
380: org.omg.CORBA.ContainerPackage.Description[] result = new org.omg.CORBA.ContainerPackage.Description[size];
381: for (int i = 0; i < size; i++) {
382: result[i] = new org.omg.CORBA.ContainerPackage.Description();
383: org.omg.CORBA.ContainedPackage.Description cd_descr = c[i]
384: .describe();
385: result[i].contained_object = c[i];
386: result[i].kind = cd_descr.kind;
387: result[i].value = cd_descr.value;
388: }
389: return result;
390: }
391:
392: void define() {
393: // do nothing
394: }
395:
396: public void loadContents() {
397: if (this .logger.isInfoEnabled()) {
398: this .logger.info("Repository loads contents...");
399: }
400:
401: for (int i = 0; i < containers.length; i++) {
402: containers[i].loadContents();
403: }
404: for (int i = 0; i < containers.length; i++) {
405: containers[i].define();
406: }
407:
408: if (this .logger.isInfoEnabled()) {
409: this .logger.info("Repository contents loaded");
410: }
411: }
412:
413: public org.omg.CORBA.ModuleDef create_module(String id,
414: String name, String version) {
415: return delegate.create_module(id, name, version);
416: }
417:
418: public org.omg.CORBA.ConstantDef create_constant(String id,
419: String name, String version, org.omg.CORBA.IDLType type,
420: org.omg.CORBA.Any value) {
421: return delegate.create_constant(id, name, version, type, value);
422: }
423:
424: public org.omg.CORBA.StructDef create_struct(String id,
425: String name, String version,
426: org.omg.CORBA.StructMember[] members) {
427: return delegate.create_struct(id, name, version, members);
428: }
429:
430: public org.omg.CORBA.UnionDef create_union(String id, String name,
431: String version, org.omg.CORBA.IDLType discriminator_type,
432: org.omg.CORBA.UnionMember[] members) {
433: return delegate.create_union(id, name, version,
434: discriminator_type, members);
435: }
436:
437: public org.omg.CORBA.EnumDef create_enum(String id, String name,
438: String version, String[] members) {
439: return delegate.create_enum(id, name, version, members);
440: }
441:
442: public org.omg.CORBA.AliasDef create_alias(String id, String name,
443: String version, org.omg.CORBA.IDLType original_type) {
444: return delegate.create_alias(id, name, version, original_type);
445: }
446:
447: public org.omg.CORBA.ExceptionDef create_exception(String id,
448: String name, String version,
449: org.omg.CORBA.StructMember[] member) {
450: return delegate.create_exception(id, name, version, member);
451: }
452:
453: /**
454: * not supported
455: */
456:
457: public org.omg.CORBA.InterfaceDef create_interface(String id,
458: String name, String version,
459: org.omg.CORBA.InterfaceDef[] base_interfaces,
460: boolean is_abstract) {
461: return delegate.create_interface(id, name, version,
462: base_interfaces, is_abstract);
463: }
464:
465: /**
466: * not supported
467: */
468:
469: public org.omg.CORBA.ValueBoxDef create_value_box(String id,
470: String name, String version, org.omg.CORBA.IDLType type) {
471: return delegate.create_value_box(id, name, version, type);
472: }
473:
474: /**
475: * not supported
476: */
477:
478: public org.omg.CORBA.ValueDef create_value(String id, String name,
479: String version, boolean is_custom, boolean is_abstract,
480: org.omg.CORBA.ValueDef base_value, boolean is_truncatable,
481: org.omg.CORBA.ValueDef[] abstract_base_values,
482: org.omg.CORBA.InterfaceDef[] supported_interfaces,
483: org.omg.CORBA.Initializer[] initializers) {
484: return delegate.create_value(id, name, version, is_custom,
485: is_abstract, base_value, is_truncatable,
486: abstract_base_values, supported_interfaces,
487: initializers);
488: }
489:
490: /**
491: * not supported
492: */
493:
494: public org.omg.CORBA.NativeDef create_native(String id,
495: String name, String version) {
496: return delegate.create_native(id, name, version);
497: }
498:
499: public void destroy() {
500: delegate.destroy();
501: }
502:
503: }
|