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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.hessian;
030:
031: import com.caucho.ejb.protocol.Skeleton;
032: import com.caucho.ejb.xa.EjbTransactionManager;
033: import com.caucho.hessian.io.HessianInput;
034: import com.caucho.hessian.io.HessianOutput;
035: import com.caucho.hessian.io.HessianProtocolException;
036:
037: import javax.transaction.xa.XAResource;
038: import java.io.InputStream;
039: import java.io.OutputStream;
040: import java.util.logging.Level;
041: import java.util.logging.Logger;
042:
043: /**
044: * Skeleton for XA Resource.
045: */
046: public class XAResourceSkeleton extends Skeleton {
047: protected static final Logger log = Logger
048: .getLogger(XAResourceSkeleton.class.getName());
049:
050: private EjbTransactionManager _tm;
051:
052: XAResourceSkeleton(EjbTransactionManager tm) {
053: _tm = tm;
054: }
055:
056: /**
057: * Services the request.
058: */
059: public void _service(InputStream is, OutputStream os)
060: throws Exception {
061: HessianInput in = new HessianReader(is);
062: HessianOutput out = new HessianWriter(os);
063:
064: in.startCall();
065:
066: String method = in.getMethod();
067:
068: try {
069: if (method.equals("commitOnePhase")
070: || method.equals("commitOnePhase_string")
071: || method.equals("commitOnePhase_1")) {
072: executeCommitOnePhase(in, out);
073: } else if (method.equals("commit")
074: || method.equals("commit_string")
075: || method.equals("commit_1")) {
076: executeCommit(in, out);
077: } else if (method.equals("rollback")
078: || method.equals("rollback_string")
079: || method.equals("rollback_1")) {
080: executeRollback(in, out);
081: } else if (method.equals("prepare")
082: || method.equals("prepare_string")
083: || method.equals("prepare_1")) {
084: executePrepare(in, out);
085: } else
086: executeUnknown(method, in, out);
087: } catch (HessianProtocolException e) {
088: throw e;
089: } catch (Throwable e) {
090: log.log(Level.WARNING, e.toString(), e);
091:
092: out.startReply();
093: out.writeFault("ServiceException", e.getMessage(), e);
094: out.completeReply();
095: }
096: }
097:
098: private void executeCommitOnePhase(HessianInput in,
099: HessianOutput out) throws Throwable {
100: String xid = in.readString();
101: in.completeCall();
102:
103: _tm.commitTransaction(xid);
104:
105: out.startReply();
106: out.writeNull();
107: out.completeReply();
108: }
109:
110: private void executeCommit(HessianInput in, HessianOutput out)
111: throws Throwable {
112: String xid = in.readString();
113: in.completeCall();
114:
115: _tm.commitTransaction(xid);
116:
117: out.startReply();
118: out.writeNull();
119: out.completeReply();
120: }
121:
122: private void executePrepare(HessianInput in, HessianOutput out)
123: throws Throwable {
124: String xid = in.readString();
125: in.completeCall();
126:
127: // XXX: _tm.commitTransaction(xid);
128:
129: out.startReply();
130: out.writeInt(XAResource.XA_OK);
131: out.completeReply();
132: }
133:
134: private void executeRollback(HessianInput in, HessianOutput out)
135: throws Throwable {
136: String xid = in.readString();
137: in.completeCall();
138:
139: _tm.rollbackTransaction(xid);
140:
141: out.startReply();
142: out.writeNull();
143: out.completeReply();
144: }
145:
146: /**
147: * Executes an unknown method.
148: *
149: * @param method the method name to match.
150: * @param in the hessian input stream
151: * @param out the hessian output stream
152: */
153: protected void executeUnknown(String method, HessianInput in,
154: HessianOutput out) throws Exception {
155: if (method.equals("_hessian_getAttribute")) {
156: String key = in.readString();
157: in.completeCall();
158:
159: out.startReply();
160:
161: /*
162: if ("java.api.class".equals(key))
163: out.writeString(NameServerRemote.class.getName());
164: else if ("java.home.class".equals(key))
165: out.writeString(NameServerRemote.class.getName());
166: else if ("java.object.class".equals(key))
167: out.writeString(NameServerRemote.class.getName());
168: else if ("home-class".equals(key))
169: out.writeString(NameServerRemote.class.getName());
170: else if ("remote-class".equals(key))
171: out.writeString(NameServerRemote.class.getName());
172: else
173: out.writeNull();
174: */
175: out.writeNull();
176:
177: out.completeReply();
178: } else {
179: out.startReply();
180: out.writeFault("NoMethod", "no such method: " + method,
181: null);
182: out.completeReply();
183: }
184: }
185: }
|