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.ejb.hessian;
031:
032: import com.caucho.hessian.io.HessianRemoteObject;
033: import com.caucho.hessian.io.HessianRemoteResolver;
034: import com.caucho.transaction.TransactionImpl;
035: import com.caucho.transaction.TransactionManagerImpl;
036: import com.caucho.vfs.Path;
037: import com.caucho.vfs.ReadStream;
038: import com.caucho.vfs.ReadWritePair;
039: import com.caucho.vfs.Vfs;
040: import com.caucho.vfs.WriteStream;
041:
042: import javax.ejb.EJBException;
043: import javax.transaction.xa.Xid;
044: import java.io.IOException;
045: import java.util.logging.Level;
046: import java.util.logging.Logger;
047:
048: /**
049: * Base class for generated object stubs.
050: */
051: public abstract class HessianStub implements HessianRemoteObject {
052: private static final Logger log = Logger
053: .getLogger(HessianStub.class.getName());
054: protected String _url;
055:
056: protected transient Path _urlPath;
057: protected transient HessianClientContainer _client;
058: protected transient HessianRemoteResolver _resolver;
059:
060: /**
061: * Initializes the stub with its remote information.
062: *
063: * @param client the client container
064: * @param handle the corresponding handle for the stub
065: */
066: void _init(String url, HessianClientContainer client) {
067: _url = url;
068: _urlPath = Vfs.lookup(url);
069:
070: _hessian_setClientContainer(client);
071: }
072:
073: public String getHessianURL() {
074: return _url;
075: }
076:
077: void _hessian_setURL(String url) {
078: _url = url;
079: }
080:
081: void _hessian_setURLPath(Path urlPath) {
082: _urlPath = urlPath;
083: }
084:
085: void _hessian_setClientContainer(HessianRemoteResolver resolver) {
086: _resolver = resolver;
087: if (resolver instanceof HessianClientContainer)
088: _client = (HessianClientContainer) resolver;
089: }
090:
091: protected HessianWriter _hessian_openWriter() throws EJBException {
092: try {
093: ReadWritePair pair = _urlPath.openReadWrite();
094: ReadStream is = pair.getReadStream();
095: WriteStream os = pair.getWriteStream();
096:
097: if (_client != null) {
098: String auth = _client.getBasicAuthentication();
099: if (auth != null)
100: os.setAttribute("Authorization", auth);
101: }
102:
103: HessianWriter out = new HessianWriter(is, os);
104:
105: out.setRemoteResolver(_resolver);
106:
107: return out;
108: } catch (IOException e) {
109: throw new EJBException(e);
110: }
111: }
112:
113: protected void _hessian_writeHeaders(HessianWriter out)
114: throws IOException {
115: try {
116: TransactionImpl xa = (TransactionImpl) TransactionManagerImpl
117: .getInstance().getTransaction();
118:
119: if (xa != null) {
120: Xid xid = xa.getXid();
121:
122: String s = xidToString(xid.getGlobalTransactionId());
123:
124: out.writeHeader("xid");
125: out.writeString(s);
126: }
127: } catch (Throwable e) {
128: log.log(Level.FINE, e.toString(), e);
129: }
130: }
131:
132: protected void _hessian_freeWriter(HessianWriter out)
133: throws IOException {
134: out.close();
135: }
136:
137: private static String xidToString(byte[] id) {
138: StringBuilder sb = new StringBuilder();
139:
140: for (int i = 0; i < id.length; i++) {
141: byte b = id[i];
142:
143: sb.append(toHex((b >> 4) & 0xf));
144: sb.append(toHex(b & 0xf));
145: }
146:
147: return sb.toString();
148: }
149:
150: private static char toHex(int d) {
151: if (d < 10)
152: return (char) ('0' + d);
153: else
154: return (char) ('a' + d - 10);
155: }
156: }
|