001    /* DynArrayHelper.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 DynArray}. Following the 1.5 JDK
053     * specifications, DynArray is always a local object, so the two methods of this
054     * helper ({@link #read} and {@link #write} are not in use, always throwing
055     * {@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 DynArrayHelper
063    {
064      /**
065       * Cast the passed object into the DynArray. As DynArray 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 DynArray.
071       */
072      public static DynArray narrow(org.omg.CORBA.Object obj)
073      {
074        try
075          {
076            return (DynArray) obj;
077          }
078        catch (ClassCastException cex)
079          {
080            throw new BAD_PARAM(obj.getClass().getName() + " is not a DynArray");
081          }
082      }
083      
084      /**
085       * Narrow the given object to the DynArray. 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 DynArray.
093       * 
094       * @since 1.5 
095       */ 
096      public static DynArray unchecked_narrow(org.omg.CORBA.Object obj)
097      {
098        return narrow(obj);
099      }    
100    
101      /**
102       * Get the type code of the {@link DynArray}.
103       */
104      public static TypeCode type()
105      {
106        return OrbRestricted.Singleton.create_interface_tc(id(), "DynArray");
107      }
108    
109      /**
110       * Insert the DynArray into the given Any.
111       * 
112       * @param any the Any to insert into.
113       * 
114       * @param that the DynArray to insert.
115       */
116      public static void insert(Any any, DynArray that)
117      {
118        any.insert_Object(that);
119      }
120    
121      /**
122       * Extract the DynArray from given Any.
123       * 
124       * @throws BAD_OPERATION if the passed Any does not contain DynArray.
125       */
126      public static DynArray extract(Any any)
127      {
128        return narrow(any.extract_Object());
129      }
130    
131      /**
132       * Get the DynArray repository id.
133       * 
134       * @return "IDL:omg.org/DynamicAny/DynArray:1.0", always.
135       */
136      public static String id()
137      {
138        return "IDL:omg.org/DynamicAny/DynArray:1.0";
139      }
140    
141      /**
142       * This should read DynArray from the CDR input stream, but (following the JDK
143       * 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 DynArray read(InputStream input)
152      {
153        throw new MARSHAL(DynAnyFactoryHelper.not_applicable(id()));
154      }
155    
156      /**
157       * This should read DynArray from the CDR input stream, but (following the JDK
158       * 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, DynArray value)
167      {
168        throw new MARSHAL(DynAnyFactoryHelper.not_applicable(id()));
169      }
170    }