001/*  ObjectReferenceTemplateSeqHelper.java --
002    Copyright (C) 2005 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038package org.omg.PortableInterceptor;
039
040import gnu.CORBA.Minor;
041import gnu.CORBA.typecodes.GeneralTypeCode;
042
043import org.omg.CORBA.Any;
044import org.omg.CORBA.BAD_OPERATION;
045import org.omg.CORBA.TCKind;
046import org.omg.CORBA.TypeCode;
047import org.omg.CORBA.portable.InputStream;
048import org.omg.CORBA.portable.OutputStream;
049import org.omg.CORBA.portable.Streamable;
050
051/**
052 * Provides static helper methods for working with the array of object reference
053 * templates.
054 * 
055 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
056 */
057public abstract class ObjectReferenceTemplateSeqHelper
058{
059  /**
060   * Extract the <code>ObjectReferenceTemplate[]</code> from the given
061   * {@link Any}. This implementation expects the {@link Any} to hold the
062   * instance of {@link ObjectReferenceTemplateSeqHolder} that is returned by
063   * {@link Any#extract_Streamable() }.
064   * 
065   * @param a an Any to extract the array from.
066   * 
067   * @return the extracted array.
068   * 
069   * @throws BAD_OPERATION if the Any contains something other than the the
070   * {@link ObjectReferenceTemplateSeqHolder}.
071   */
072  public static ObjectReferenceTemplate[] extract(Any a)
073  {
074    try
075      {
076        ObjectReferenceTemplateSeqHolder h = (ObjectReferenceTemplateSeqHolder) 
077          a.extract_Streamable();
078        return h.value;
079      }
080    catch (ClassCastException cex)
081      {
082        BAD_OPERATION bad = new BAD_OPERATION(
083          "ObjectReferenceTemplate[] expected");
084        bad.initCause(cex);
085        bad.minor = Minor.Any;
086        throw bad;
087      }
088  }
089
090  /**
091   * Returns the object reference template sequence repository Id.
092   * 
093   * @return "IDL:omg.org/PortableInterceptor/ObjectReferenceTemplateSeq:1.0",
094   * always.
095   */
096  public static String id()
097  {
098    return "IDL:omg.org/PortableInterceptor/ObjectReferenceTemplateSeq:1.0";
099  }
100
101  /**
102   * Insert into the given <code>ObjectReferenceTemplate[]</code> into the
103   * given {@link Any}. This implementation first creates a
104   * {@link ObjectReferenceTemplateSeqHolder} and then calls
105   * {@link Any#insert_Streamable(Streamable)}.
106   * 
107   * @param into the target Any.
108   * @param that the array to insert.
109   */
110  public static void insert(Any into, ObjectReferenceTemplate[] that)
111  {
112    ObjectReferenceTemplateSeqHolder holder = 
113      new ObjectReferenceTemplateSeqHolder(that);
114    into.insert_Streamable(holder);
115  }
116
117  /**
118   * Reads the <code>ObjectReferenceTemplate[]</code> from the CORBA input
119   * stream.
120   * 
121   * @param input the CORBA (not java.io) stream to read from.
122   * @return the value from the stream.
123   */
124  public static ObjectReferenceTemplate[] read(InputStream input)
125  {
126    ObjectReferenceTemplate[] value = 
127      new ObjectReferenceTemplate[input.read_long()];
128    for (int i = 0; i < value.length; i++)
129      value[i] = ObjectReferenceTemplateHelper.read(input);
130    return value;
131  }
132
133  /**
134   * Creates and returns a new instance of the TypeCode, corresponding the CORBA
135   * <code>ObjectReferenceTemplate[]</code>. The length of the sequence is
136   * left with the initial value 0.
137   */
138  public static TypeCode type()
139  {
140    GeneralTypeCode t = new GeneralTypeCode(TCKind.tk_sequence);
141    t.setId(id());
142    t.setLength(0);
143    t.setContentType(ObjectReferenceTemplateHelper.type());
144    return t;
145  }
146
147  /**
148   * Writes the <code>ObjectReferenceTemplate[]</code> into the given stream.
149   * 
150   * @param output the CORBA (not java.io) output stream to write.
151   * @param value the value that must be written.
152   */
153  public static void write(OutputStream output, ObjectReferenceTemplate[] value)
154  {
155    output.write_long(value.length);
156
157    for (int i = 0; i < value.length; i++)
158      ObjectReferenceTemplateHelper.write(output, value[i]);
159  }
160}