KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.ConfigException;
33 import com.caucho.ejb.AbstractServer;
34 import com.caucho.ejb.message.MessageServer;
35 import com.caucho.ejb.protocol.HandleEncoder;
36 import com.caucho.ejb.protocol.ProtocolContainer;
37 import com.caucho.ejb.protocol.Skeleton;
38 import com.caucho.hessian.io.HessianRemoteResolver;
39 import com.caucho.util.L10N;
40
41 import javax.ejb.EJBHome JavaDoc;
42 import javax.ejb.EJBObject JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47
48 /**
49  * Server containing all the EJBs for a given configuration.
50  *
51  * <p>Each protocol will extend the container to override Handle creation.
52  */

53 public class HessianProtocol extends ProtocolContainer
54 {
55   private static final L10N L = new L10N(HessianProtocol.class);
56   private static final Logger JavaDoc log
57     = Logger.getLogger(HessianProtocol.class.getName());
58
59   private HashMap JavaDoc<AbstractServer,Class JavaDoc> _homeSkeletonMap
60     = new HashMap JavaDoc<AbstractServer,Class JavaDoc>();
61
62   private HashMap JavaDoc<AbstractServer,Class JavaDoc> _objectSkeletonMap
63     = new HashMap JavaDoc<AbstractServer,Class JavaDoc>();
64
65   private HessianRemoteResolver _resolver;
66
67   /**
68    * Create a server with the given prefix name.
69    */

70   public HessianProtocol()
71   {
72     _resolver = new HessianStubFactory();
73   }
74
75   public String JavaDoc getName()
76   {
77     return "hessian";
78   }
79
80   /**
81    * Returns the handle encoder for hessian.
82    */

83   protected HandleEncoder createHandleEncoder(AbstractServer server,
84                                               Class JavaDoc primaryKeyClass)
85     throws ConfigException
86   {
87     String JavaDoc prefix = getURLPrefix();
88     String JavaDoc id = server.getProtocolId();
89
90     String JavaDoc url;
91     if (prefix.endsWith("/") || id.startsWith("/"))
92       url = prefix + id;
93     else
94       url = prefix + '/' + id;
95     
96       
97     return new HessianHandleEncoder(server, url, primaryKeyClass);
98   }
99
100   String JavaDoc calculateURL(String JavaDoc ejbName)
101   {
102     String JavaDoc prefix = getURLPrefix();
103
104     String JavaDoc url;
105     if (prefix.endsWith("/") || ejbName.startsWith("/"))
106       return prefix + ejbName;
107     else
108       return prefix + '/' + ejbName;
109   }
110
111   /**
112    * Adds a server to the protocol.
113    */

114   public void addServer(AbstractServer server)
115   {
116     log.fine("Hessian: add server " + server.getProtocolId());
117   }
118
119   /**
120    * Returns the skeleton
121    */

122   public Skeleton getSkeleton(String JavaDoc uri, String JavaDoc queryString)
123     throws Exception JavaDoc
124   {
125     String JavaDoc serverId = uri;
126     String JavaDoc objectId = null;
127
128     // decode ?id=my-instance-id
129
if (queryString != null) {
130       int p = queryString.indexOf('=');
131
132       if (p >= 0)
133         objectId = queryString.substring(p + 1);
134       else
135         objectId = queryString;
136     }
137     
138     if (log.isLoggable(Level.FINEST))
139       log.log(Level.FINEST, "uri=" + uri + ", queryString=" + queryString + ", serverId=" + serverId + ", objectId=" + objectId);
140
141     if ("/_ejb_xa_resource".equals(serverId))
142       return new XAResourceSkeleton(getProtocolManager().getServerManager().getTransactionManager());
143
144     AbstractServer server;
145
146     // XXX: should avoid this work for runtime
147
String JavaDoc name = serverId;
148     if (name == null)
149       name = "";
150     
151     while (name.startsWith("/"))
152       name = name.substring(1);
153
154     server = getProtocolManager().getServerByServerId(name);
155
156     if (server == null)
157       server = getProtocolManager().getServerByEJBName(name);
158
159     if (server == null) {
160       ArrayList JavaDoc children = getProtocolManager().getRemoteChildren(serverId);
161
162       if (children != null && children.size() > 0)
163         return new NameContextSkeleton(this, serverId);
164       else
165         return null; // XXX: should return error skeleton
166
}
167     else if (objectId != null) {
168       Object JavaDoc key = server.getHandleEncoder("hessian").objectIdToKey(objectId);
169
170       EJBObject JavaDoc obj = server.getContext(key, false).getRemoteView();
171
172       Class JavaDoc objectSkelClass = getObjectSkelClass(server);
173
174       HessianSkeleton skel = (HessianSkeleton) objectSkelClass.newInstance();
175       skel._setServer(server);
176       skel._setResolver(_resolver);
177       skel._setObject(obj);
178       return skel;
179     }
180     else if (server instanceof MessageServer) {
181       return new MessageSkeleton((MessageServer) server);
182     }
183     else {
184       Object JavaDoc remote = server.getRemoteObject();
185
186       if (remote instanceof EJBHome JavaDoc) {
187         Class JavaDoc homeSkelClass = getHomeSkelClass(server);
188
189         HessianSkeleton skel = (HessianSkeleton) homeSkelClass.newInstance();
190         skel._setServer(server);
191         skel._setResolver(_resolver);
192         skel._setObject(remote);
193
194         return skel;
195       }
196       else if (remote instanceof EJBObject JavaDoc) {
197         Class JavaDoc skelClass = getObjectSkelClass(server);
198
199         HessianSkeleton skel = (HessianSkeleton) skelClass.newInstance();
200         skel._setServer(server);
201         skel._setResolver(_resolver);
202         skel._setObject(remote);
203
204         return skel;
205       }
206
207       return null;
208     }
209   }
210
211   /**
212    * Returns the class for home skeletons.
213    */

214   protected Class JavaDoc getHomeSkelClass(AbstractServer server)
215     throws Exception JavaDoc
216   {
217     Class JavaDoc homeSkelClass = _homeSkeletonMap.get(server);
218
219     if (homeSkelClass != null)
220       return homeSkelClass;
221
222     Class JavaDoc remoteHomeClass = server.getRemoteHomeClass();
223
224     homeSkelClass = HessianSkeletonGenerator.generate(remoteHomeClass,
225                                                       getWorkPath());
226
227     _homeSkeletonMap.put(server, homeSkelClass);
228
229     return homeSkelClass;
230   }
231
232   /**
233    * Returns the class for home skeletons.
234    */

235   protected Class JavaDoc getObjectSkelClass(AbstractServer server)
236     throws Exception JavaDoc
237   {
238     Class JavaDoc objectSkelClass = _objectSkeletonMap.get(server);
239
240     if (objectSkelClass != null)
241       return objectSkelClass;
242
243     Class JavaDoc remoteObjectClass = server.getRemoteObjectClass();
244
245     objectSkelClass = HessianSkeletonGenerator.generate(remoteObjectClass,
246                                                         getWorkPath());
247
248     _objectSkeletonMap.put(server, objectSkelClass);
249
250     return objectSkelClass;
251   }
252 }
253
Popular Tags