001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.iiop;
031:
032: import java.io.IOException;
033: import java.util.*;
034:
035: import com.caucho.vfs.*;
036:
037: public class Iiop12Writer extends Iiop10Writer {
038: private final static int KEY_ADDR = 0;
039: private final static int PROFILE_ADDR = 1;
040: private final static int REFERENCE_ADDR = 2;
041:
042: public Iiop12Writer() {
043: }
044:
045: public Iiop12Writer(Iiop12Writer parent, MessageWriter out) {
046: super (parent, out);
047: }
048:
049: /**
050: * Writes the header for a request
051: *
052: * @param operation the method to call
053: */
054: @Override
055: public void startRequest(byte[] oid, int off, int len,
056: String operation, int requestId,
057: ArrayList<ServiceContext> contextList) throws IOException {
058: _out.start12Message(IiopReader.MSG_REQUEST);
059:
060: _out.writeInt(requestId);
061:
062: int flags = 0x3;
063:
064: _out.write(flags); // response expected
065: _out.write(0);
066: _out.write(0);
067: _out.write(0);
068:
069: write_short((short) KEY_ADDR); // target
070: writeBytes(oid, off, len); // object id
071:
072: writeString(operation);
073:
074: if (contextList != null) {
075: write_long(contextList.size()); // service context list
076:
077: for (int i = 0; i < contextList.size(); i++)
078: contextList.get(i).write(this );
079: } else
080: write_long(0); // service context list
081:
082: // ejb/1231, ejb/1331
083: // see IiopRead12.read12Request
084: //_out.align(8);
085: }
086:
087: /**
088: * Writes the header for a request
089: */
090: public void startReplyOk(int requestId) throws IOException {
091: _out.start12Message(IiopReader.MSG_REPLY);
092:
093: write_long(requestId);
094: write_long(IiopReader.STATUS_NO_EXCEPTION); // okay
095: write_long(0); // service control list
096: }
097:
098: /**
099: * Writes the header for a request
100: */
101: @Override
102: public void startReplySystemException(int requestId,
103: String exceptionId, int minorStatus, int completionStatus,
104: Throwable cause) throws IOException {
105: _out.start12Message(IiopReader.MSG_REPLY);
106:
107: write_long(requestId);
108: write_long(IiopReader.STATUS_SYSTEM_EXCEPTION);
109:
110: if (cause == null) {
111: write_long(0); // service control list
112: } else {
113: write_long(1);
114: write_long(IiopReader.SERVICE_UNKNOWN_EXCEPTION_INFO);
115:
116: EncapsulationMessageWriter out = new EncapsulationMessageWriter();
117: Iiop12Writer writer = new Iiop12Writer(this , out);
118: writer.init(out);
119: writer.write(0); // endian
120: writer.write_value(cause);
121: //writer.close();
122: out.close();
123:
124: int len = out.getOffset();
125: write_long(len);
126:
127: out.writeToWriter(this );
128: }
129:
130: writeString(exceptionId);
131: write_long(minorStatus);
132: write_long(completionStatus);
133: }
134:
135: /**
136: * Writes the header for a request
137: */
138: public void startReplyUserException(int requestId)
139: throws IOException {
140: _out.start12Message(IiopReader.MSG_REPLY);
141:
142: write_long(requestId);
143: write_long(IiopReader.STATUS_USER_EXCEPTION);
144: write_long(0); // service control list
145: }
146:
147: public void alignMethodArgs() {
148: _out.align(8);
149: }
150:
151: /**
152: * Writes a 16-bit char.
153: */
154: public void write_wchar(char v) {
155: _out.write(2);
156: _out.write(v >> 8);
157: _out.write(v);
158: }
159:
160: /**
161: * Writes a sequence of 16-bit characters to the output stream.
162: */
163: public void write_wstring(String a) {
164: if (a == null) {
165: write_long(0);
166: return;
167: }
168:
169: int length = a.length();
170: write_long(2 * length);
171: for (int i = 0; i < length; i++)
172: _out.writeShort((int) a.charAt(i));
173: }
174: }
|