001/* ServiceDetailHelper.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
038
039package org.omg.CORBA;
040
041import gnu.CORBA.Minor;
042import gnu.CORBA.OrbRestricted;
043import gnu.CORBA.ServiceDetailHolder;
044
045import org.omg.CORBA.portable.InputStream;
046import org.omg.CORBA.portable.OutputStream;
047
048/**
049 * The helper operations on the Service Detail.
050 *
051 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
052 */
053public abstract class ServiceDetailHelper
054{
055  /**
056   * The service detail repository id.
057   */
058  private static String _id = "IDL:omg.org/CORBA/ServiceDetail:1.0";
059
060  /**
061   * Extract the service detail info from the given {@link Any}
062   *
063   * @param a the Any to extract from.
064   *
065   * @return the extracted detail.
066   *
067   * @throws BAD_OPERATION if the parameter holds something different
068   * from the ServiceDetail.
069   */
070  public static ServiceDetail extract(Any a)
071  {
072    try
073      {
074        return ((ServiceDetailHolder) a.extract_Streamable()).value;
075      }
076    catch (ClassCastException ex)
077      {
078        BAD_OPERATION bad = new BAD_OPERATION();
079        bad.initCause(ex);
080        bad.minor = Minor.Any;
081        throw bad;
082      }
083  }
084
085  /**
086   * Get the service detail repository id.
087   *
088   * @return the service detail repository id,
089   * <code>IDL:omg.org/CORBA/ServiceDetail:1.0</code>, always.
090   */
091  public static String id()
092  {
093    return _id;
094  }
095
096  /**
097   * Insert the service detail into the given {@link Any}.
098   *
099   * @param a the Any to insert into.
100   *
101   * @param that the detail to insert.
102   */
103  public static void insert(Any a, ServiceDetail that)
104  {
105    a.insert_Streamable(new ServiceDetailHolder(that));
106  }
107
108  /**
109   * Read the service detail information from the given CDR
110   * intput stream. First reads the type, then the flexible
111   * length byte sequence.
112   *
113   * @param istream a stram to read from.
114   *
115   * @return the loaded service detail.
116   */
117  public static ServiceDetail read(InputStream istream)
118  {
119    ServiceDetail value = new ServiceDetail();
120    value.service_detail_type = istream.read_ulong();
121
122    int l = istream.read_long();
123    value.service_detail = new byte[ l ];
124    istream.read_octet_array(value.service_detail, 0, l);
125    return value;
126  }
127
128  /**
129   * Get the typecode of the service detail, assuming to be it
130   * a structure with the two fields.
131   *
132   * @return the newly created or cached typecode value.
133   */
134  public static TypeCode type()
135  {
136    ORB orb = OrbRestricted.Singleton;
137
138    StructMember[] members = new StructMember[ 2 ];
139
140    TypeCode type =
141      orb.create_alias_tc(_id, "ServiceDetailType",
142                          orb.get_primitive_tc(TCKind.tk_ulong)
143      );
144    members [ 0 ] = new StructMember("service_detail_type", type, null);
145
146    TypeCode data =
147      orb.create_sequence_tc(0, orb.get_primitive_tc(TCKind.tk_octet));
148    members [ 1 ] = new StructMember("service_detail", data, null);
149
150    return orb.create_struct_tc(id(), "ServiceDetail", members);
151  }
152
153  /**
154   * Write the service detail data to the given CDR output stream.
155   * Writes the detail type first, then the detail type data
156   * as the variable length byte sequence.
157   *
158   * @param ostream a stream to write into.
159   * @param value a value to write.
160   */
161  public static void write(OutputStream ostream, ServiceDetail value)
162  {
163    ostream.write_ulong(value.service_detail_type);
164    ostream.write_long(value.service_detail.length);
165    ostream.write_octet_array(value.service_detail, 0,
166      value.service_detail.length
167    );
168  }
169}