1 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 ; 42 import javax.ejb.EJBObject ; 43 import java.util.ArrayList ; 44 import java.util.HashMap ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 48 53 public class HessianProtocol extends ProtocolContainer 54 { 55 private static final L10N L = new L10N(HessianProtocol.class); 56 private static final Logger log 57 = Logger.getLogger(HessianProtocol.class.getName()); 58 59 private HashMap <AbstractServer,Class > _homeSkeletonMap 60 = new HashMap <AbstractServer,Class >(); 61 62 private HashMap <AbstractServer,Class > _objectSkeletonMap 63 = new HashMap <AbstractServer,Class >(); 64 65 private HessianRemoteResolver _resolver; 66 67 70 public HessianProtocol() 71 { 72 _resolver = new HessianStubFactory(); 73 } 74 75 public String getName() 76 { 77 return "hessian"; 78 } 79 80 83 protected HandleEncoder createHandleEncoder(AbstractServer server, 84 Class primaryKeyClass) 85 throws ConfigException 86 { 87 String prefix = getURLPrefix(); 88 String id = server.getProtocolId(); 89 90 String 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 calculateURL(String ejbName) 101 { 102 String prefix = getURLPrefix(); 103 104 String url; 105 if (prefix.endsWith("/") || ejbName.startsWith("/")) 106 return prefix + ejbName; 107 else 108 return prefix + '/' + ejbName; 109 } 110 111 114 public void addServer(AbstractServer server) 115 { 116 log.fine("Hessian: add server " + server.getProtocolId()); 117 } 118 119 122 public Skeleton getSkeleton(String uri, String queryString) 123 throws Exception 124 { 125 String serverId = uri; 126 String objectId = null; 127 128 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 String 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 children = getProtocolManager().getRemoteChildren(serverId); 161 162 if (children != null && children.size() > 0) 163 return new NameContextSkeleton(this, serverId); 164 else 165 return null; } 167 else if (objectId != null) { 168 Object key = server.getHandleEncoder("hessian").objectIdToKey(objectId); 169 170 EJBObject obj = server.getContext(key, false).getRemoteView(); 171 172 Class 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 remote = server.getRemoteObject(); 185 186 if (remote instanceof EJBHome ) { 187 Class 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 ) { 197 Class 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 214 protected Class getHomeSkelClass(AbstractServer server) 215 throws Exception 216 { 217 Class homeSkelClass = _homeSkeletonMap.get(server); 218 219 if (homeSkelClass != null) 220 return homeSkelClass; 221 222 Class remoteHomeClass = server.getRemoteHomeClass(); 223 224 homeSkelClass = HessianSkeletonGenerator.generate(remoteHomeClass, 225 getWorkPath()); 226 227 _homeSkeletonMap.put(server, homeSkelClass); 228 229 return homeSkelClass; 230 } 231 232 235 protected Class getObjectSkelClass(AbstractServer server) 236 throws Exception 237 { 238 Class objectSkelClass = _objectSkeletonMap.get(server); 239 240 if (objectSkelClass != null) 241 return objectSkelClass; 242 243 Class 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 |