KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > hessian > HessianStub


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb.hessian;
31
32 import com.caucho.hessian.io.HessianRemoteObject;
33 import com.caucho.hessian.io.HessianRemoteResolver;
34 import com.caucho.transaction.TransactionImpl;
35 import com.caucho.transaction.TransactionManagerImpl;
36 import com.caucho.vfs.Path;
37 import com.caucho.vfs.ReadStream;
38 import com.caucho.vfs.ReadWritePair;
39 import com.caucho.vfs.Vfs;
40 import com.caucho.vfs.WriteStream;
41
42 import javax.ejb.EJBException JavaDoc;
43 import javax.transaction.xa.Xid JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47
48 /**
49  * Base class for generated object stubs.
50  */

51 public abstract class HessianStub implements HessianRemoteObject {
52   private static final Logger JavaDoc log
53     = Logger.getLogger(HessianStub.class.getName());
54   protected String JavaDoc _url;
55   
56   protected transient Path _urlPath;
57   protected transient HessianClientContainer _client;
58   protected transient HessianRemoteResolver _resolver;
59   
60   /**
61    * Initializes the stub with its remote information.
62    *
63    * @param client the client container
64    * @param handle the corresponding handle for the stub
65    */

66   void _init(String JavaDoc url, HessianClientContainer client)
67   {
68     _url = url;
69     _urlPath = Vfs.lookup(url);
70
71     _hessian_setClientContainer(client);
72   }
73   
74   public String JavaDoc getHessianURL()
75   {
76     return _url;
77   }
78
79   void _hessian_setURL(String JavaDoc url)
80   {
81     _url = url;
82   }
83
84   void _hessian_setURLPath(Path urlPath)
85   {
86     _urlPath = urlPath;
87   }
88
89   void _hessian_setClientContainer(HessianRemoteResolver resolver)
90   {
91     _resolver = resolver;
92     if (resolver instanceof HessianClientContainer)
93       _client = (HessianClientContainer) resolver;
94   }
95
96   protected HessianWriter _hessian_openWriter()
97     throws EJBException JavaDoc
98   {
99     try {
100       ReadWritePair pair = _urlPath.openReadWrite();
101       ReadStream is = pair.getReadStream();
102       WriteStream os = pair.getWriteStream();
103
104       if (_client != null) {
105     String JavaDoc auth = _client.getBasicAuthentication();
106     if (auth != null)
107       os.setAttribute("Authorization", auth);
108       }
109
110       HessianWriter out = new HessianWriter(is, os);
111
112       out.setRemoteResolver(_resolver);
113
114       return out;
115     } catch (IOException JavaDoc e) {
116       throw new EJBException JavaDoc(e);
117     }
118   }
119
120   protected void _hessian_writeHeaders(HessianWriter out)
121     throws IOException JavaDoc
122   {
123     try {
124       TransactionImpl xa = (TransactionImpl) TransactionManagerImpl.getInstance().getTransaction();
125
126       if (xa != null) {
127     Xid JavaDoc xid = xa.getXid();
128
129     String JavaDoc s = xidToString(xid.getGlobalTransactionId());
130
131     out.writeHeader("xid");
132     out.writeString(s);
133       }
134     } catch (Throwable JavaDoc e) {
135       log.log(Level.FINE, e.toString(), e);
136     }
137   }
138
139   protected void _hessian_freeWriter(HessianWriter out)
140     throws IOException JavaDoc
141   {
142     out.close();
143   }
144
145   private static String JavaDoc xidToString(byte []id)
146   {
147     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
148
149     for (int i = 0; i < id.length; i++) {
150       byte b = id[i];
151
152       sb.append(toHex((b >> 4) & 0xf));
153       sb.append(toHex(b & 0xf));
154     }
155
156     return sb.toString();
157   }
158
159   private static char toHex(int d)
160   {
161     if (d < 10)
162       return (char) ('0' + d);
163     else
164       return (char) ('a' + d - 10);
165   }
166 }
167
Popular Tags