001: package org.jacorb.orb;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.io.IOException;
024: import java.lang.reflect.InvocationTargetException;
025: import java.lang.reflect.Method;
026:
027: import org.apache.avalon.framework.configuration.Configurable;
028: import org.apache.avalon.framework.logger.Logger;
029: import org.jacorb.ir.RepositoryID;
030: import org.jacorb.orb.giop.ReplyInputStream;
031: import org.jacorb.util.ObjectUtil;
032: import org.omg.CORBA.BAD_PARAM;
033: import org.omg.CORBA.ExceptionList;
034: import org.omg.CORBA.UserException;
035: import org.omg.GIOP.ReplyStatusType_1_2;
036:
037: /**
038: * JacORB-specific implementation of
039: * <code>org.omg.Messaging.ExceptionHolder</code>. An instance of this
040: * type is used to pass an exception to a reply handler.
041: *
042: * @author Andre Spiegel <spiegel@gnu.org>
043: * @version $Id: ExceptionHolderImpl.java,v 1.17 2006/07/13 08:57:36 nick.cross Exp $
044: */
045: public class ExceptionHolderImpl extends
046: org.omg.Messaging.ExceptionHolder implements Configurable {
047: private Logger logger = null;
048:
049: /**
050: * No-arg constructor for demarshaling.
051: */
052: public ExceptionHolderImpl() {
053: super ();
054: }
055:
056: /**
057: * Constructs an ExceptionHolderImpl object from an input stream.
058: * It is assumed that the reply status of this input stream is
059: * either USER_EXCEPTION or SYSTEM_EXCEPTION. If it has another
060: * status, a RuntimeException is thrown.
061: */
062: public ExceptionHolderImpl(ReplyInputStream inputStream) {
063: this ();
064:
065: int status = inputStream.getStatus().value();
066: if (status == ReplyStatusType_1_2._USER_EXCEPTION) {
067: is_system_exception = false;
068: } else if (status == ReplyStatusType_1_2._SYSTEM_EXCEPTION) {
069: is_system_exception = true;
070: } else {
071: throw new BAD_PARAM("attempt to create ExceptionHolder "
072: + "for non-exception reply");
073: }
074: byte_order = inputStream.littleEndian;
075: marshaled_exception = inputStream.getBody();
076: }
077:
078: public ExceptionHolderImpl(org.omg.CORBA.SystemException exception) {
079: this ();
080:
081: is_system_exception = true;
082: byte_order = false;
083:
084: final CDROutputStream output = new CDROutputStream();
085:
086: try {
087: SystemExceptionHelper.write(output, exception);
088: marshaled_exception = output.getBufferCopy();
089: } finally {
090: output.close();
091: }
092: }
093:
094: public void configure(
095: org.apache.avalon.framework.configuration.Configuration configuration)
096: throws org.apache.avalon.framework.configuration.ConfigurationException {
097: logger = ((org.jacorb.config.Configuration) configuration)
098: .getNamedLogger("jacorb.orb.exc_holder");
099: }
100:
101: public void raise_exception() throws UserException {
102: final CDRInputStream input = new CDRInputStream(
103: marshaled_exception, byte_order);
104:
105: try {
106: if (is_system_exception) {
107: throw SystemExceptionHelper.read(input);
108: }
109:
110: input.mark(0);
111: String id = input.read_string();
112:
113: try {
114: input.reset();
115: } catch (IOException e) {
116: logger.warn("Unexpected IOException: ", e);
117: }
118:
119: org.omg.CORBA.UserException result = null;
120: try {
121: result = exceptionFromHelper(id, input);
122: } catch (Exception e) {
123: throw new org.omg.CORBA.UnknownUserException();
124: }
125: throw result;
126: } finally {
127: input.close();
128: }
129: }
130:
131: public void raise_exception_with_list(ExceptionList exc_list)
132: throws UserException {
133: throw new org.omg.CORBA.NO_IMPLEMENT(
134: "raise_exception_with_list not yet implemented");
135: }
136:
137: /**
138: * For testing.
139: */
140: public String toString() {
141: StringBuffer result = new StringBuffer();
142: for (int i = 0; i < marshaled_exception.length; i++) {
143: result.append(marshaled_exception[i]);
144: result.append('(');
145: result.append((char) marshaled_exception[i]);
146: result.append(") ");
147: }
148: return result.toString();
149: }
150:
151: /**
152: * Given a repository id, tries to find a helper for the corresponding
153: * class and uses it to unmarshal an instance of this class from
154: * the given InputStream.
155: */
156: public org.omg.CORBA.UserException exceptionFromHelper(String id,
157: org.omg.CORBA.portable.InputStream input)
158: throws ClassNotFoundException, NoSuchMethodException,
159: IllegalAccessException, InvocationTargetException {
160: String name = RepositoryID.className(id, "Helper", null);
161:
162: // if class doesn't exist, let exception propagate
163: Class helperClazz = ObjectUtil.classForName(name);
164:
165: // helper must not be null from here on
166:
167: // get read method from helper and invoke it,
168: // i.e. read the object from the stream
169: Method readMethod = helperClazz
170: .getMethod(
171: "read",
172: new Class[] { ObjectUtil
173: .classForName("org.omg.CORBA.portable.InputStream") });
174: java.lang.Object result = readMethod.invoke(null,
175: new java.lang.Object[] { input });
176: return (org.omg.CORBA.UserException) result;
177: }
178:
179: /**
180: * Marshals this object into a new buffer and returns that buffer.
181: */
182: public byte[] marshal() {
183: final CDROutputStream output = new CDROutputStream();
184: try {
185: output.write_value(this ,
186: "IDL:omg.org/Messaging/ExceptionHolder:1.0");
187: return output.getBufferCopy();
188: } finally {
189: output.close();
190: }
191: }
192: }
|