KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > ExceptionHolderImpl


1 package org.jacorb.orb;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.io.*;
24 import java.lang.reflect.*;
25
26 import org.apache.avalon.framework.logger.Logger;
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.ConfigurationException;
29
30 import org.omg.GIOP.*;
31 import org.omg.Messaging.ExceptionHolder;
32 import org.omg.CORBA.ExceptionList JavaDoc;
33 import org.omg.CORBA.SystemException JavaDoc;
34 import org.omg.CORBA.UnknownUserException JavaDoc;
35 import org.omg.CORBA.UserException JavaDoc;
36
37 import org.jacorb.ir.*;
38 import org.jacorb.orb.giop.*;
39 import org.jacorb.util.ObjectUtil;
40
41 /**
42  * JacORB-specific implementation of
43  * <code>org.omg.Messaging.ExceptionHolder</code>. An instance of this
44  * type is used to pass an exception to a reply handler.
45  *
46  * @author Andre Spiegel <spiegel@gnu.org>
47  * @version $Id: ExceptionHolderImpl.java,v 1.11 2004/05/06 12:40:00 nicolas Exp $
48  */

49 public class ExceptionHolderImpl
50     extends org.omg.Messaging.ExceptionHolder
51     implements Configurable
52 {
53     private Logger logger = null;
54
55     /**
56      * Constructs an ExceptionHolderImpl object from an input stream.
57      * It is assumed that the reply status of this input stream is
58      * either USER_EXCEPTION or SYSTEM_EXCEPTION. If it has another
59      * status, a RuntimeException is thrown.
60      */

61     public ExceptionHolderImpl( ReplyInputStream is )
62     {
63         int status = is.getStatus().value();
64         if ( status == ReplyStatusType_1_2._USER_EXCEPTION )
65         {
66             is_system_exception = false;
67         }
68         else if ( status == ReplyStatusType_1_2._SYSTEM_EXCEPTION )
69         {
70             is_system_exception = true;
71         }
72         else
73         {
74             throw new RuntimeException JavaDoc( "attempt to create ExceptionHolder " +
75                                         "for non-exception reply" );
76         }
77         byte_order = is.littleEndian;
78         marshaled_exception = is.getBody();
79     }
80
81     public ExceptionHolderImpl(org.omg.CORBA.SystemException JavaDoc ex)
82     {
83         is_system_exception = true;
84         byte_order = false;
85         
86         CDROutputStream output = new CDROutputStream();
87         SystemExceptionHelper.write(output, ex);
88         marshaled_exception = output.getBufferCopy();
89     }
90
91     /**
92      * No-arg constructor for demarshaling.
93      */

94     public ExceptionHolderImpl()
95     {
96         super();
97     }
98
99     public void configure(org.apache.avalon.framework.configuration.Configuration configuration)
100         throws org.apache.avalon.framework.configuration.ConfigurationException
101     {
102         logger =
103             ((org.jacorb.config.Configuration)configuration).getNamedLogger("jacorb.orb.exc_holder");
104     }
105
106
107     public void raise_exception()
108         throws UserException JavaDoc
109     {
110         CDRInputStream input =
111             new CDRInputStream (null, marshaled_exception, byte_order);
112         if ( is_system_exception )
113         {
114             throw SystemExceptionHelper.read( input );
115         }
116         else
117         {
118             input.mark( 0 );
119             String JavaDoc id = input.read_string();
120             try
121             {
122                 input.reset();
123             }
124             catch( IOException ioe )
125             {
126                 if (logger.isWarnEnabled())
127                     logger.warn( "Unexpected IOException: " + ioe.getMessage() );
128             }
129
130             org.omg.CORBA.UserException JavaDoc result = null;
131             try
132             {
133                 result = exceptionFromHelper( id, input );
134             }
135             catch( Exception JavaDoc e )
136             {
137                 throw new org.omg.CORBA.UnknownUserException JavaDoc();
138             }
139             throw result;
140         }
141     }
142
143     public void raise_exception_with_list( ExceptionList JavaDoc exc_list )
144         throws UserException JavaDoc
145     {
146         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc(
147            "raise_exception_with_list not yet implemented" );
148     }
149
150     /**
151      * For testing.
152      */

153     public String JavaDoc toString()
154     {
155         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
156         for (int i=0; i<marshaled_exception.length; i++)
157         {
158             result.append (marshaled_exception[i] +
159                            "(" + (char)marshaled_exception[i] + ") ");
160         }
161         return result.toString();
162     }
163     
164     /**
165      * Given a repository id, tries to find a helper for the corresponding
166      * class and uses it to unmarshal an instance of this class from
167      * the given InputStream.
168      */

169     public org.omg.CORBA.UserException JavaDoc exceptionFromHelper
170                                 ( String JavaDoc id,
171                                   org.omg.CORBA.portable.InputStream JavaDoc input )
172         throws ClassNotFoundException JavaDoc,
173                NoSuchMethodException JavaDoc,
174                IllegalAccessException JavaDoc,
175                InvocationTargetException
176     {
177         String JavaDoc name = RepositoryID.className(id, "Helper", null);
178
179         // if class doesn't exist, let exception propagate
180
Class JavaDoc helper = ObjectUtil.classForName (name);
181
182         // helper must not be null from here on
183

184         // get read method from helper and invoke it,
185
// i.e. read the object from the stream
186
Method readMethod =
187             helper.getMethod( "read",
188                                new Class JavaDoc[]{
189                                    ObjectUtil.classForName("org.omg.CORBA.portable.InputStream")
190                                } );
191         java.lang.Object JavaDoc result =
192             readMethod.invoke( null,
193                                new java.lang.Object JavaDoc[]{ input }
194                              );
195         return ( org.omg.CORBA.UserException JavaDoc ) result;
196     }
197
198     /**
199      * Marshals this object into a new buffer and returns that buffer.
200      */

201     public byte[] marshal()
202     {
203          byte[] buffer =
204              BufferManager.getInstance()
205                           .getBuffer( marshaled_exception.length + 128 );
206          CDROutputStream output = new CDROutputStream( buffer );
207          output.write_value( this, "IDL:omg.org/Messaging/ExceptionHolder:1.0" );
208          return buffer;
209     }
210 }
211
Popular Tags