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.orb;
031:
032: import org.omg.CORBA.*;
033:
034: import java.util.*;
035: import java.util.logging.*;
036: import java.io.*;
037:
038: import javax.transaction.*;
039: import javax.transaction.xa.*;
040:
041: import com.caucho.iiop.*;
042: import com.caucho.transaction.*;
043: import com.caucho.vfs.*;
044:
045: public class StubDelegateImpl extends org.omg.CORBA.portable.Delegate {
046: public static final Logger log = Logger
047: .getLogger(StubDelegateImpl.class.getName());
048:
049: private ORBImpl _orb;
050: private byte[] _oid;
051:
052: StubDelegateImpl(ORBImpl orb) {
053: _orb = orb;
054: }
055:
056: StubDelegateImpl(ORBImpl orb, byte[] oid) {
057: _orb = orb;
058: _oid = oid;
059: }
060:
061: @Override
062: public org.omg.CORBA.portable.OutputStream request(
063: org.omg.CORBA.Object self, String op,
064: boolean isResponseExpected) {
065: try {
066: Iiop12Writer writer = new Iiop12Writer();
067:
068: writer.setOrb(_orb);
069:
070: IiopSocketPool pool = _orb.getSocketPool();
071:
072: ReadWritePair pair = pool.open();
073:
074: MessageWriter out = new StreamMessageWriter(pair
075: .getWriteStream());
076:
077: IiopReader is = new IiopReader(pool, pair);
078:
079: is.setOrb(_orb);
080:
081: writer.init(out, is);
082:
083: byte[] oid;
084:
085: if (self instanceof StubImpl)
086: oid = ((StubImpl) self).getOid();
087: else
088: oid = _oid;
089:
090: ArrayList<ServiceContext> serviceList = null;
091:
092: try {
093: TransactionManagerImpl tm = TransactionManagerImpl
094: .getLocal();
095: TransactionImpl xa = (TransactionImpl) tm
096: .getTransaction();
097:
098: if (xa != null) {
099: if (serviceList == null)
100: serviceList = new ArrayList<ServiceContext>();
101:
102: serviceList.add(new TransactionServiceContext(xa));
103: }
104: } catch (Exception e) {
105: log.log(Level.FINER, e.toString(), e);
106: }
107:
108: writer.startRequest(oid, 0, oid.length, op, 1, serviceList);
109:
110: return writer;
111: } catch (IOException e) {
112: throw new RuntimeException(e);
113: }
114: }
115:
116: @Override
117: public org.omg.CORBA.portable.InputStream invoke(
118: org.omg.CORBA.Object self,
119: org.omg.CORBA.portable.OutputStream os) {
120: try {
121: IiopWriter writer = (IiopWriter) os;
122:
123: IiopReader reader = writer._call();
124:
125: return reader;
126: } catch (IOException e) {
127: throw new RuntimeException(e);
128: }
129: }
130:
131: @Override
132: public Request create_request(org.omg.CORBA.Object obj,
133: Context ctx, String op, NVList argList, NamedValue result) {
134: throw new UnsupportedOperationException();
135: }
136:
137: @Override
138: public Request create_request(org.omg.CORBA.Object obj,
139: Context ctx, String op, NVList argList, NamedValue result,
140: ExceptionList excList, ContextList ctxList) {
141: throw new UnsupportedOperationException();
142: }
143:
144: @Override
145: public org.omg.CORBA.Object duplicate(org.omg.CORBA.Object obj) {
146: return obj;
147: }
148:
149: @Override
150: public org.omg.CORBA.Object get_interface_def(
151: org.omg.CORBA.Object self) {
152: throw new UnsupportedOperationException();
153: }
154:
155: @Override
156: public int hash(org.omg.CORBA.Object obj, int max) {
157: throw new UnsupportedOperationException();
158: }
159:
160: @Override
161: public boolean is_a(org.omg.CORBA.Object obj, String repId) {
162: if (obj instanceof StubImpl) {
163: String typeId = ((StubImpl) obj).getIOR().getTypeId();
164:
165: if (typeId.equals(repId))
166: return true;
167: }
168:
169: return false;
170: }
171:
172: @Override
173: public boolean is_equivalent(org.omg.CORBA.Object obj,
174: org.omg.CORBA.Object other) {
175: return obj == other;
176: }
177:
178: @Override
179: public boolean non_existent(org.omg.CORBA.Object obj) {
180: throw new UnsupportedOperationException();
181: }
182:
183: @Override
184: public ORB orb(org.omg.CORBA.Object obj) {
185: return _orb;
186: }
187:
188: @Override
189: public void release(org.omg.CORBA.Object obj) {
190: }
191:
192: @Override
193: public void releaseReply(org.omg.CORBA.Object self,
194: org.omg.CORBA.portable.InputStream is) {
195: }
196:
197: @Override
198: public Request request(org.omg.CORBA.Object obj, String op) {
199: throw new UnsupportedOperationException();
200: }
201:
202: public String toString() {
203: return "StubDelegateImpl[]";
204: }
205:
206: static class TransactionServiceContext extends ServiceContext {
207: private TransactionImpl _xa;
208:
209: TransactionServiceContext(TransactionImpl xa) {
210: _xa = xa;
211: }
212:
213: public void write(IiopWriter out) {
214: try {
215: out.write_long(IiopReader.SERVICE_TRANSACTION);
216:
217: EncapsulationMessageWriter subOut = new EncapsulationMessageWriter();
218:
219: subOut.write(0); // endian
220:
221: subOut.align(4);
222: subOut.writeInt(_xa.getTransactionTimeout());
223: subOut.writeInt(0); // Coordinator
224: subOut.writeInt(0); // Terminator
225:
226: Xid xid = _xa.getXid();
227:
228: byte[] global = xid.getGlobalTransactionId();
229: byte[] local = xid.getBranchQualifier();
230:
231: // otid_t
232: subOut.writeInt(0); // format
233: subOut.writeInt(local.length); // bqual_length
234: subOut.writeInt(global.length + local.length);
235: subOut.write(global, 0, global.length);
236: subOut.write(local, 0, local.length);
237:
238: subOut.align(4);
239: subOut.writeInt(0); // parents
240: subOut.close();
241:
242: out.write_long(subOut.getOffset());
243: subOut.writeToWriter(out);
244: } catch (Exception e) {
245: throw new RuntimeException(e);
246: }
247: }
248: }
249: }
|