001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-2004 Gerald Brose
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: *
020: */
021:
022: package org.jacorb.orb;
023:
024: import java.lang.reflect.*;
025:
026: import org.jacorb.orb.giop.ReplyInputStream;
027: import org.jacorb.util.ObjectUtil;
028: import org.omg.IOP.*;
029:
030: /**
031: * @author Gerald Brose
032: * @version $Id: SystemExceptionHelper.java,v 1.15 2006/07/13 09:33:12 nick.cross Exp $
033: */
034:
035: public class SystemExceptionHelper {
036: private SystemExceptionHelper() {
037: // utility class
038: }
039:
040: private static final String className(String repId) {
041: // cut "IDL:" and version
042: String id_base = repId.substring(4, repId.lastIndexOf(':'));
043: return ir2scopes("org.omg", id_base.substring(7));
044: }
045:
046: private static final String ir2scopes(String prefix, String s) {
047: if (s.indexOf('/') < 0) {
048: return s;
049: }
050: java.util.StringTokenizer strtok = new java.util.StringTokenizer(
051: s, "/");
052:
053: int count = strtok.countTokens();
054: StringBuffer buffer = new StringBuffer();
055: buffer.append(prefix);
056:
057: for (int i = 0; strtok.hasMoreTokens(); i++) {
058: String nextToken = strtok.nextToken();
059: try {
060: if (buffer.length() > 0) {
061: ObjectUtil.classForName(buffer.toString() + "."
062: + nextToken);
063: } else {
064: ObjectUtil.classForName(nextToken);
065: }
066:
067: if (i < count - 1) {
068: buffer.append('.');
069: buffer.append(nextToken);
070: buffer.append("Package");
071: } else {
072: buffer.append('.');
073: buffer.append(nextToken);
074: }
075: } catch (ClassNotFoundException cnfe) {
076: if (buffer.length() > 0) {
077: buffer.append('.');
078: buffer.append(nextToken);
079: } else {
080: buffer.append(nextToken);
081: }
082: }
083: }
084:
085: return buffer.toString();
086: }
087:
088: private static final String repId(Class clazz) {
089: String className = clazz.getName();
090: String body = className.substring(7);
091: return "IDL:omg.org/" + scopesToIR(body) + ":1.0";
092: }
093:
094: private static final String scopesToIR(String s) {
095: if (s.indexOf('.') < 0) {
096: return s;
097: }
098: java.util.StringTokenizer strtok = new java.util.StringTokenizer(
099: s, ".");
100: String scopes[] = new String[strtok.countTokens()];
101: for (int i = 0; strtok.hasMoreTokens(); i++) {
102: String nextToken = strtok.nextToken();
103: if (nextToken.endsWith("Package")) {
104: scopes[i] = nextToken.substring(0, nextToken
105: .indexOf("Package"));
106: } else {
107: scopes[i] = nextToken;
108: }
109: }
110: StringBuffer buffer = new StringBuffer();
111: if (scopes.length > 1) {
112: for (int i = 0; i < scopes.length - 1; i++) {
113: buffer.append(scopes[i]);
114: buffer.append('/');
115: }
116: }
117:
118: buffer.append(scopes[scopes.length - 1]);
119: return buffer.toString();
120: }
121:
122: public static void insert(org.omg.CORBA.Any any,
123: org.omg.CORBA.SystemException exception) {
124: any.type(type(exception));
125: write(any.create_output_stream(), exception);
126: }
127:
128: public static org.omg.CORBA.TypeCode type(
129: org.omg.CORBA.SystemException exception) {
130: String name = exception.getClass().getName();
131: name = name.substring(name.lastIndexOf('.') + 1);
132: org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init();
133:
134: org.omg.CORBA.TypeCode _type = orb
135: .create_struct_tc(
136: "IDL:omg.org/CORBA/" + name + ":1.0",
137: name,
138: new org.omg.CORBA.StructMember[] {
139: new org.omg.CORBA.StructMember(
140: "minor",
141: orb
142: .get_primitive_tc(org.omg.CORBA.TCKind
143: .from_int(3)),
144: null),
145: new org.omg.CORBA.StructMember(
146: "completed",
147: orb
148: .create_enum_tc(
149: "IDL:omg.org/CORBA/CompletionStatus:1.0",
150: "CompletionStatus",
151: new String[] {
152: "COMPLETED_YES",
153: "COMPLETED_NO",
154: "COMPLETED_MAYBE" }),
155: null) });
156: return _type;
157: }
158:
159: public static org.omg.CORBA.SystemException read(
160: org.omg.CORBA.portable.InputStream in) {
161: final String className = SystemExceptionHelper.className(in
162: .read_string());
163: final int minor = in.read_long();
164: final org.omg.CORBA.CompletionStatus completed = org.omg.CORBA.CompletionStatusHelper
165: .read(in);
166:
167: String message = null;
168:
169: if (in instanceof ReplyInputStream) {
170: final ReplyInputStream input = (ReplyInputStream) in;
171:
172: try {
173: final ServiceContext context = input
174: .getServiceContext(ExceptionDetailMessage.value);
175: if (context != null) {
176: final CDRInputStream data = new CDRInputStream(
177: null, context.context_data);
178:
179: try {
180: data.openEncapsulatedArray();
181: message = data.read_wstring();
182: } finally {
183: data.close();
184: }
185: }
186: } finally {
187: input.close();
188: }
189: }
190:
191: try {
192: Class clazz = ObjectUtil.classForName(className);
193: Constructor ctor = clazz.getConstructor(new Class[] {
194: String.class, int.class,
195: org.omg.CORBA.CompletionStatus.class });
196:
197: return (org.omg.CORBA.SystemException) ctor
198: .newInstance(new Object[] {
199: "Server-side Exception: " + message,
200: ObjectUtil.newInteger(minor), completed });
201: } catch (Exception e) {
202: return (org.omg.CORBA.SystemException) new org.omg.CORBA.UNKNOWN(
203: className).initCause(e);
204: }
205: }
206:
207: public static void write(org.omg.CORBA.portable.OutputStream out,
208: org.omg.CORBA.SystemException exception) {
209: out.write_string(repId(exception.getClass()));
210: out.write_long(exception.minor);
211: org.omg.CORBA.CompletionStatusHelper.write(out,
212: exception.completed);
213: }
214: }
|