001: package org.jacorb.orb.dynany;
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.IOException;
024:
025: import org.omg.CORBA.*;
026: import org.omg.CORBA.portable.InputStream;
027: import org.omg.DynamicAny.DynAnyPackage.*;
028: import org.apache.avalon.framework.logger.Logger;
029: import org.jacorb.orb.TypeCode;
030:
031: /**
032: * CORBA DynAny
033: *
034: * @author Gerald Brose
035: * @version $Id: DynAny.java,v 1.26 2006/06/27 09:34:10 alphonse.bendt Exp $
036: */
037: public class DynAny extends org.omg.CORBA.LocalObject implements
038: org.omg.DynamicAny.DynAny {
039: protected org.omg.CORBA.TypeCode typeCode;
040: protected int pos = -1;
041: protected int limit = 0;
042: protected final org.omg.DynamicAny.DynAnyFactory dynFactory;
043: protected final org.omg.CORBA.ORB orb;
044: private final Logger logger;
045:
046: /**
047: * our representation of a primitive type any is the any itself
048: */
049: private org.omg.CORBA.Any anyRepresentation;
050:
051: protected DynAny(org.omg.DynamicAny.DynAnyFactory factory,
052: org.omg.CORBA.ORB orb, Logger logger) {
053: super ();
054: this .orb = orb;
055: this .dynFactory = factory;
056: this .logger = logger;
057: }
058:
059: DynAny(org.omg.DynamicAny.DynAnyFactory dynFactory,
060: org.omg.CORBA.TypeCode type, org.omg.CORBA.ORB orb,
061: Logger logger) throws TypeMismatch {
062: this (dynFactory, orb, logger);
063: typeCode = TypeCode.originalType(type);
064: anyRepresentation = defaultValue(typeCode);
065: }
066:
067: public org.omg.CORBA.TypeCode type() {
068: checkDestroyed();
069: return typeCode;
070: }
071:
072: public void assign(org.omg.DynamicAny.DynAny dyn_any)
073: throws TypeMismatch {
074: checkDestroyed();
075: if (dyn_any.type().equivalent(this .type())) {
076: try {
077: from_any(dyn_any.to_any());
078: } catch (InvalidValue iv) {
079: throw unexpectedException(iv);
080: }
081: } else {
082: throw new TypeMismatch();
083: }
084: }
085:
086: public boolean equal(org.omg.DynamicAny.DynAny dyn_any) {
087: checkDestroyed();
088:
089: if (getRepresentation() == null) {
090: throw new BAD_INV_ORDER("DynAny not initialized");
091: }
092:
093: return dyn_any.to_any().equal(getRepresentation());
094: }
095:
096: public void from_any(org.omg.CORBA.Any value) throws InvalidValue,
097: TypeMismatch {
098: checkDestroyed();
099: if (!value.type().equivalent(type())) {
100: throw new TypeMismatch();
101: }
102:
103: typeCode = TypeCode.originalType(value.type());
104:
105: try {
106: anyRepresentation = (org.jacorb.orb.Any) orb.create_any();
107: anyRepresentation.read_value(value.create_input_stream(),
108: type());
109: } catch (Exception e) {
110: throw new InvalidValue(e.toString());
111: }
112: }
113:
114: public org.omg.CORBA.Any to_any() {
115: checkDestroyed();
116: final org.omg.CORBA.Any out_any = orb.create_any();
117: out_any.type(type());
118: final InputStream in = getRepresentation()
119: .create_input_stream();
120: try {
121: out_any.read_value(in, type());
122: return out_any;
123: } finally {
124: try {
125: in.close();
126: } catch (IOException e) {
127: logger.error("unable to close stream", e);
128: }
129: }
130: }
131:
132: public void destroy() {
133: checkDestroyed();
134: anyRepresentation = null;
135: typeCode = null;
136: }
137:
138: public org.omg.DynamicAny.DynAny copy() {
139: checkDestroyed();
140: try {
141: return dynFactory.create_dyn_any(to_any());
142: } catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode tm) {
143: throw unexpectedException(tm);
144: }
145: }
146:
147: /**
148: * returns the DynAny's internal any representation,
149: * overwritten in subclasses that represent constructed
150: * types and need to traverse structures.
151: */
152:
153: protected org.omg.CORBA.Any getRepresentation() {
154: return anyRepresentation;
155: }
156:
157: public void insert_boolean(boolean value) throws TypeMismatch {
158: checkDestroyed();
159: org.omg.CORBA.Any any = getRepresentation();
160: if (any.type().kind() != org.omg.CORBA.TCKind.tk_boolean) {
161: throw new TypeMismatch();
162: }
163: any.insert_boolean(value);
164: }
165:
166: public void insert_octet(byte value) throws TypeMismatch {
167: checkDestroyed();
168: org.omg.CORBA.Any any = getRepresentation();
169: if (any.type().kind() != org.omg.CORBA.TCKind.tk_octet) {
170: throw new TypeMismatch();
171: }
172: any.insert_octet(value);
173: }
174:
175: public void insert_char(char value) throws TypeMismatch {
176: checkDestroyed();
177: org.omg.CORBA.Any any = getRepresentation();
178: if (any.type().kind() != org.omg.CORBA.TCKind.tk_char) {
179: throw new TypeMismatch();
180: }
181: any.insert_char(value);
182: }
183:
184: public void insert_short(short value) throws TypeMismatch {
185: checkDestroyed();
186: org.omg.CORBA.Any any = getRepresentation();
187: if (any.type().kind() != org.omg.CORBA.TCKind.tk_short) {
188: throw new TypeMismatch();
189: }
190: any.insert_short(value);
191: }
192:
193: public void insert_ushort(short value) throws TypeMismatch {
194: checkDestroyed();
195: org.omg.CORBA.Any any = getRepresentation();
196: if (any.type().kind() != org.omg.CORBA.TCKind.tk_ushort) {
197: throw new TypeMismatch();
198: }
199: any.insert_ushort(value);
200: }
201:
202: public void insert_long(int value) throws TypeMismatch {
203: checkDestroyed();
204: org.omg.CORBA.Any any = getRepresentation();
205: if (any.type().kind() != org.omg.CORBA.TCKind.tk_long) {
206: throw new TypeMismatch();
207: }
208: any.insert_long(value);
209: }
210:
211: public void insert_ulong(int value) throws TypeMismatch {
212: checkDestroyed();
213: org.omg.CORBA.Any any = getRepresentation();
214: if (any.type().kind() != org.omg.CORBA.TCKind.tk_ulong) {
215: throw new TypeMismatch();
216: }
217: any.insert_ulong(value);
218: }
219:
220: public void insert_float(float value) throws TypeMismatch {
221: checkDestroyed();
222: org.omg.CORBA.Any any = getRepresentation();
223: if (any.type().kind() != org.omg.CORBA.TCKind.tk_float) {
224: throw new TypeMismatch();
225: }
226: any.insert_float(value);
227: }
228:
229: public void insert_double(double value) throws TypeMismatch {
230: checkDestroyed();
231: org.omg.CORBA.Any any = getRepresentation();
232: if (any.type().kind() != org.omg.CORBA.TCKind.tk_double) {
233: throw new TypeMismatch();
234: }
235: any.insert_double(value);
236: }
237:
238: public void insert_string(java.lang.String value)
239: throws TypeMismatch {
240: checkDestroyed();
241: org.omg.CORBA.Any any = getRepresentation();
242: if (any.type().kind() != org.omg.CORBA.TCKind.tk_string) {
243: throw new TypeMismatch();
244: }
245: any.insert_string(value);
246: }
247:
248: public void insert_reference(org.omg.CORBA.Object value)
249: throws TypeMismatch {
250: checkDestroyed();
251: org.omg.CORBA.Any any = getRepresentation();
252: if (any.type().kind() != org.omg.CORBA.TCKind.tk_objref) {
253: throw new TypeMismatch();
254: }
255: any.insert_Object(value);
256: }
257:
258: public void insert_typecode(org.omg.CORBA.TypeCode value)
259: throws TypeMismatch {
260: checkDestroyed();
261: org.omg.CORBA.Any any = getRepresentation();
262: if (any.type().kind() != org.omg.CORBA.TCKind.tk_TypeCode) {
263: throw new TypeMismatch();
264: }
265: any.insert_TypeCode(value);
266: }
267:
268: public void insert_longlong(long value) throws TypeMismatch {
269: checkDestroyed();
270: org.omg.CORBA.Any any = getRepresentation();
271: if (any.type().kind() != org.omg.CORBA.TCKind.tk_longlong) {
272: throw new TypeMismatch();
273: }
274: any.insert_longlong(value);
275: }
276:
277: public void insert_ulonglong(long value) throws TypeMismatch {
278: checkDestroyed();
279: org.omg.CORBA.Any any = getRepresentation();
280: if (any.type().kind() != org.omg.CORBA.TCKind.tk_ulonglong) {
281: throw new TypeMismatch();
282: }
283: any.insert_ulonglong(value);
284: }
285:
286: public void insert_wchar(char value) throws TypeMismatch {
287: checkDestroyed();
288: org.omg.CORBA.Any any = getRepresentation();
289: if (any.type().kind() != org.omg.CORBA.TCKind.tk_wchar) {
290: throw new TypeMismatch();
291: }
292: any.insert_wchar(value);
293: }
294:
295: public void insert_wstring(java.lang.String value)
296: throws TypeMismatch {
297: checkDestroyed();
298: org.omg.CORBA.Any any = getRepresentation();
299: if (any.type().kind() != org.omg.CORBA.TCKind.tk_wstring) {
300: throw new TypeMismatch();
301: }
302: any.insert_wstring(value);
303: }
304:
305: public void insert_any(org.omg.CORBA.Any value) throws TypeMismatch {
306: checkDestroyed();
307: org.omg.CORBA.Any any = getRepresentation();
308: if (any.type().kind() != org.omg.CORBA.TCKind.tk_any) {
309: throw new TypeMismatch();
310: }
311: any.insert_any(value);
312: }
313:
314: public void insert_dyn_any(org.omg.DynamicAny.DynAny value)
315: throws TypeMismatch {
316: checkDestroyed();
317: insert_any(value.to_any());
318: }
319:
320: public boolean get_boolean() throws TypeMismatch {
321: checkDestroyed();
322: org.omg.CORBA.Any any = getRepresentation();
323: try {
324: return any.extract_boolean();
325: } catch (org.omg.CORBA.BAD_OPERATION b) {
326: throw new TypeMismatch();
327: }
328: }
329:
330: public byte get_octet() throws TypeMismatch {
331: checkDestroyed();
332: org.omg.CORBA.Any any = getRepresentation();
333: try {
334: return any.extract_octet();
335: } catch (org.omg.CORBA.BAD_OPERATION b) {
336: throw new TypeMismatch();
337: }
338: }
339:
340: public char get_char() throws TypeMismatch {
341: checkDestroyed();
342: org.omg.CORBA.Any any = getRepresentation();
343: try {
344: return any.extract_char();
345: } catch (org.omg.CORBA.BAD_OPERATION b) {
346: throw new TypeMismatch();
347: }
348: }
349:
350: public short get_short() throws TypeMismatch {
351: checkDestroyed();
352: org.omg.CORBA.Any any = getRepresentation();
353: try {
354: return any.extract_short();
355: } catch (org.omg.CORBA.BAD_OPERATION b) {
356: throw new TypeMismatch();
357: }
358: }
359:
360: public short get_ushort() throws TypeMismatch {
361: checkDestroyed();
362: org.omg.CORBA.Any any = getRepresentation();
363: try {
364: return any.extract_ushort();
365: } catch (org.omg.CORBA.BAD_OPERATION b) {
366: throw new TypeMismatch();
367: }
368: }
369:
370: public int get_long() throws TypeMismatch {
371: checkDestroyed();
372: org.omg.CORBA.Any any = getRepresentation();
373: try {
374: return any.extract_long();
375: } catch (org.omg.CORBA.BAD_OPERATION b) {
376: throw new TypeMismatch();
377: }
378: }
379:
380: public int get_ulong() throws TypeMismatch {
381: checkDestroyed();
382: org.omg.CORBA.Any any = getRepresentation();
383: try {
384: return any.extract_ulong();
385: } catch (org.omg.CORBA.BAD_OPERATION b) {
386: throw new TypeMismatch();
387: }
388: }
389:
390: public float get_float() throws TypeMismatch {
391: checkDestroyed();
392: org.omg.CORBA.Any any = getRepresentation();
393: try {
394: return any.extract_float();
395: } catch (org.omg.CORBA.BAD_OPERATION b) {
396: throw new TypeMismatch();
397: }
398: }
399:
400: public double get_double() throws TypeMismatch {
401: checkDestroyed();
402: org.omg.CORBA.Any any = getRepresentation();
403: try {
404: return any.extract_double();
405: } catch (org.omg.CORBA.BAD_OPERATION b) {
406: throw new TypeMismatch();
407: }
408: }
409:
410: public java.lang.String get_string() throws TypeMismatch {
411: checkDestroyed();
412: org.omg.CORBA.Any any = getRepresentation();
413: try {
414: return any.extract_string();
415: } catch (org.omg.CORBA.BAD_OPERATION b) {
416: throw new TypeMismatch();
417: }
418: }
419:
420: public org.omg.CORBA.Object get_reference() throws TypeMismatch {
421: checkDestroyed();
422: org.omg.CORBA.Any any = getRepresentation();
423: try {
424: return any.extract_Object();
425: } catch (org.omg.CORBA.BAD_OPERATION b) {
426: throw new TypeMismatch();
427: }
428: }
429:
430: public org.omg.CORBA.TypeCode get_typecode() throws TypeMismatch {
431: checkDestroyed();
432: org.omg.CORBA.Any any = getRepresentation();
433: try {
434: return any.extract_TypeCode();
435: } catch (org.omg.CORBA.BAD_OPERATION b) {
436: throw new TypeMismatch();
437: }
438: }
439:
440: public long get_longlong() throws TypeMismatch {
441: checkDestroyed();
442: org.omg.CORBA.Any any = getRepresentation();
443: try {
444: return any.extract_longlong();
445: } catch (org.omg.CORBA.BAD_OPERATION b) {
446: throw new TypeMismatch();
447: }
448: }
449:
450: public long get_ulonglong() throws TypeMismatch {
451: checkDestroyed();
452: org.omg.CORBA.Any any = getRepresentation();
453: try {
454: return any.extract_ulonglong();
455: } catch (org.omg.CORBA.BAD_OPERATION b) {
456: throw new TypeMismatch();
457: }
458: }
459:
460: public char get_wchar() throws TypeMismatch {
461: checkDestroyed();
462: org.omg.CORBA.Any any = getRepresentation();
463: try {
464: return any.extract_wchar();
465: } catch (org.omg.CORBA.BAD_OPERATION b) {
466: throw new TypeMismatch();
467: }
468: }
469:
470: public java.lang.String get_wstring() throws TypeMismatch {
471: checkDestroyed();
472: org.omg.CORBA.Any any = getRepresentation();
473: try {
474: return any.extract_wstring();
475: } catch (org.omg.CORBA.BAD_OPERATION b) {
476: throw new TypeMismatch();
477: }
478: }
479:
480: public org.omg.CORBA.Any get_any() throws TypeMismatch {
481: checkDestroyed();
482: org.omg.CORBA.Any any = getRepresentation();
483: try {
484: return any.extract_any();
485: } catch (org.omg.CORBA.BAD_OPERATION b) {
486: throw new TypeMismatch();
487: }
488: }
489:
490: public org.omg.DynamicAny.DynAny get_dyn_any() throws TypeMismatch {
491: checkDestroyed();
492: try {
493: return dynFactory.create_dyn_any(get_any());
494: } catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode tm) {
495: throw unexpectedException(tm);
496: }
497: }
498:
499: public int component_count() {
500: checkDestroyed();
501: return limit;
502: }
503:
504: public org.omg.DynamicAny.DynAny current_component()
505: throws TypeMismatch {
506: checkDestroyed();
507: throw new TypeMismatch();
508: }
509:
510: public boolean next() {
511: checkDestroyed();
512: if (pos < limit - 1) {
513: pos++;
514: return true;
515: }
516: pos = -1;
517: return false;
518: }
519:
520: public boolean seek(int index) {
521: checkDestroyed();
522: if (index < 0) {
523: pos = -1;
524: return false;
525: }
526: if (index < limit) {
527: pos = index;
528: return true;
529: }
530: pos = -1;
531:
532: return false;
533: }
534:
535: public void rewind() {
536: checkDestroyed();
537: seek(0);
538: }
539:
540: protected void checkDestroyed() {
541: if (anyRepresentation == null && typeCode == null) {
542: throw new OBJECT_NOT_EXIST();
543: }
544: }
545:
546: private org.omg.CORBA.Any defaultValue(org.omg.CORBA.TypeCode type)
547: throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch {
548: org.omg.CORBA.Any _any = orb.create_any();
549: _any.type(type);
550: switch (type.kind().value()) {
551: case TCKind._tk_boolean:
552: _any.insert_boolean(false);
553: break;
554: case TCKind._tk_short:
555: _any.insert_short((short) 0);
556: break;
557: case TCKind._tk_ushort:
558: _any.insert_ushort((short) 0);
559: break;
560: case TCKind._tk_long:
561: _any.insert_long(0);
562: break;
563: case TCKind._tk_double:
564: _any.insert_double(0);
565: break;
566: case TCKind._tk_ulong:
567: _any.insert_ulong(0);
568: break;
569: case TCKind._tk_longlong:
570: _any.insert_longlong(0);
571: break;
572: case TCKind._tk_ulonglong:
573: _any.insert_ulonglong(0);
574: break;
575: case TCKind._tk_float:
576: _any.insert_float(0);
577: break;
578: case TCKind._tk_char:
579: _any.insert_char((char) 0);
580: break;
581: case TCKind._tk_wchar:
582: _any.insert_wchar((char) 0);
583: break;
584: case TCKind._tk_octet:
585: _any.insert_octet((byte) 0);
586: break;
587: case TCKind._tk_string:
588: _any.insert_string("");
589: break;
590: case TCKind._tk_wstring:
591: _any.insert_wstring("");
592: break;
593: case TCKind._tk_objref:
594: _any.insert_Object(null);
595: break;
596: case TCKind._tk_TypeCode:
597: _any.insert_TypeCode(orb.get_primitive_tc(TCKind.tk_null));
598: break;
599: case TCKind._tk_any:
600: org.jacorb.orb.Any any = (org.jacorb.orb.Any) orb
601: .create_any();
602: any.type(orb.get_primitive_tc(TCKind.tk_null));
603: _any.insert_any(any);
604: break;
605: case TCKind._tk_null:
606: case TCKind._tk_void:
607: // legal TypeCodes that have no associated value
608: break;
609: default:
610: throw new TypeMismatch();
611: }
612: return _any;
613: }
614:
615: // methods below suggested by <Philippe.Merle@lifl.fr>,
616: // to allow compilation on J2SDK 1.4.2
617:
618: public void insert_val(java.io.Serializable value) {
619: throw new NO_IMPLEMENT("DynAny::insert_val not implemented.");
620: }
621:
622: public java.io.Serializable get_val() {
623: throw new NO_IMPLEMENT("DynAny::get_val not implemented");
624: }
625:
626: protected final INTERNAL unexpectedException(Exception cause) {
627: logger.debug("An unexpected error occured", cause);
628: return new INTERNAL(cause.toString());
629: }
630: }
|