001    /* DynStructHelper.java --
002       Copyright (C) 2005, 2006 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package org.omg.DynamicAny;
040    
041    import gnu.CORBA.OrbRestricted;
042    
043    import org.omg.CORBA.BAD_OPERATION;
044    import org.omg.CORBA.BAD_PARAM;
045    import org.omg.CORBA.TypeCode;
046    import org.omg.CORBA.Any;
047    import org.omg.CORBA.portable.InputStream;
048    import org.omg.CORBA.MARSHAL;
049    import org.omg.CORBA.portable.OutputStream;
050    
051    /**
052     * The helper operations for {@link DynStruct}. Following the 1.5 JDK
053     * specifications, DynStruct is always a local object, so the two methods of
054     * this helper ({@link #read} and {@link #write} are not in use, always
055     * throwing {@link MARSHAL}.
056     *
057     * @specnote always throwing MARSHAL in read and write ensures compatibility
058     * with other popular implementations like Sun's.
059     *
060     * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
061     */
062    public abstract class DynStructHelper
063    {
064      /**
065       * Cast the passed object into the DynStruct. As DynStruct is a local object,
066       * the method just uses java type cast.
067       *
068       * @param obj the object to narrow.
069       * @return narrowed instance.
070       * @throws BAD_PARAM if the passed object is not a DynStruct.
071       */
072      public static DynStruct narrow(org.omg.CORBA.Object obj)
073      {
074        try
075          {
076            return (DynStruct) obj;
077          }
078        catch (ClassCastException cex)
079          {
080            throw new BAD_PARAM(obj.getClass().getName() + " is not a DynStruct");
081          }
082      }
083    
084      /**
085       * Narrow the given object to the DynStruct. For the objects that are
086       * always local, this operation does not differ from the ordinary
087       * {@link #narrow} (ClassCastException will be thrown if narrowing something
088       * different). See OMG issue 4158.
089       *
090       * @param obj the object to cast.
091       *
092       * @return the casted DynStruct.
093       *
094       * @since 1.5
095       */
096      public static DynStruct unchecked_narrow(org.omg.CORBA.Object obj)
097      {
098        return narrow(obj);
099      }
100    
101      /**
102       * Get the type code of the {@link DynStruct}.
103       */
104      public static TypeCode type()
105      {
106        return OrbRestricted.Singleton.create_interface_tc(id(), "DynStruct");
107      }
108    
109      /**
110       * Insert the DynStruct into the given Any.
111       *
112       * @param any the Any to insert into.
113       *
114       * @param that the DynStruct to insert.
115       */
116      public static void insert(Any any, DynStruct that)
117      {
118        any.insert_Object(that);
119      }
120    
121      /**
122       * Extract the DynStruct from given Any.
123       *
124       * @throws BAD_OPERATION if the passed Any does not contain DynStruct.
125       */
126      public static DynStruct extract(Any any)
127      {
128        return narrow(any.extract_Object());
129      }
130    
131      /**
132       * Get the DynStruct repository id.
133       *
134       * @return "IDL:omg.org/DynamicAny/DynStruct:1.0", always.
135       */
136      public static String id()
137      {
138        return "IDL:omg.org/DynamicAny/DynStruct:1.0";
139      }
140    
141      /**
142       * This should read DynStruct from the CDR input stream, but (following the
143       * JDK 1.5 API) it does not.
144       *
145       * @param input a org.omg.CORBA.portable stream to read from.
146       *
147       * @specenote Sun throws the same exception.
148       *
149       * @throws MARSHAL always.
150       */
151      public static DynStruct read(InputStream input)
152      {
153        throw new MARSHAL(DynAnyFactoryHelper.not_applicable(id()));
154      }
155    
156      /**
157       * This should read DynStruct from the CDR input stream, but (following the
158       * JDK 1.5 API) it does not.
159       *
160       * @param output a org.omg.CORBA.portable stream to write into.
161       *
162       * @specenote Sun throws the same exception.
163       *
164       * @throws MARSHAL always.
165       */
166      public static void write(OutputStream output, DynStruct value)
167      {
168        throw new MARSHAL(DynAnyFactoryHelper.not_applicable(id()));
169      }
170    }