1 28 29 package com.caucho.ejb.hessian; 30 31 import com.caucho.ejb.AbstractServer; 32 import com.caucho.ejb.protocol.EjbProtocolManager; 33 import com.caucho.ejb.protocol.Skeleton; 34 import com.caucho.hessian.io.HessianInput; 35 import com.caucho.hessian.io.HessianOutput; 36 import com.caucho.hessian.io.HessianProtocolException; 37 import com.caucho.log.Log; 38 import com.caucho.services.name.NameServerRemote; 39 40 import javax.ejb.EJBHome ; 41 import java.io.InputStream ; 42 import java.io.OutputStream ; 43 import java.util.ArrayList ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 47 55 public class NameContextSkeleton extends Skeleton { 56 protected static final Logger log = Log.open(NameContextSkeleton.class); 57 58 private HessianProtocol _protocol; 59 private String _prefix; 60 61 NameContextSkeleton(HessianProtocol protocol, String prefix) 62 { 63 _protocol = protocol; 64 _prefix = prefix; 65 } 66 67 70 public void _service(InputStream is, OutputStream os) 71 throws Exception 72 { 73 HessianInput in = new HessianReader(is); 74 HessianOutput out = new HessianWriter(os); 75 76 in.startCall(); 77 78 String method = in.getMethod(); 79 80 try { 81 if (method.equals("lookup") || 82 method.equals("lookup_string") || 83 method.equals("lookup_1")) 84 executeLookup(in, out); 85 else if (method.equals("list")) 86 executeList(in, out); 87 else 88 executeUnknown(method, in, out); 89 } catch (HessianProtocolException e) { 90 throw e; 91 } catch (Throwable e) { 92 log.log(Level.WARNING, e.toString(), e); 93 94 out.startReply(); 95 out.writeFault("ServiceException", e.getMessage(), e); 96 out.completeReply(); 97 } 98 } 99 100 private void executeLookup(HessianInput in, HessianOutput out) 101 throws Throwable 102 { 103 String name = in.readString(); 104 in.completeCall(); 105 106 while (name.startsWith("/")) 107 name = name.substring(1); 108 109 EjbProtocolManager container = _protocol.getProtocolManager(); 110 111 AbstractServer server; 112 113 server = container.getServerByServerId(name); 114 115 if (server == null) 116 server = container.getServerByEJBName(name); 117 118 if (server != null) { 119 EJBHome home = server.getEJBHome(); 120 121 out.startReply(); 122 123 if (home != null) 124 out.writeObject(home); 125 else out.writeObject(server.getRemoteObject()); 127 128 out.completeReply(); 129 } 130 else if (container.getRemoteChildren(name) != null) { 131 out.startReply(); 132 133 String serverId; 134 135 if (_prefix.endsWith("/") || name.startsWith("/")) 136 serverId = _prefix + name; 137 else 138 serverId = _prefix + '/' + name; 139 140 if (serverId.startsWith("/")) 141 serverId = serverId.substring(1); 142 143 String url; 144 String prefix = _protocol.getURLPrefix(); 145 if (prefix.endsWith("/")) 146 url = prefix + serverId; 147 else 148 url = prefix + '/' + serverId; 149 150 out.writeRemote(NameServerRemote.class.getName(), url); 151 152 out.completeReply(); 153 } 154 else { 155 out.startReply(); 156 157 out.writeNull(); 158 out.completeReply(); 159 } 160 } 161 162 private void executeList(HessianInput in, HessianOutput out) 163 throws Throwable 164 { 165 in.completeCall(); 166 167 EjbProtocolManager container = _protocol.getProtocolManager(); 168 169 AbstractServer server = container.getServerByEJBName(_prefix); 170 171 ArrayList children; 172 173 if (server != null) { 174 EJBHome home = server.getEJBHome(); 175 176 out.startReply(); 177 178 out.writeNull(); 179 180 out.completeReply(); 181 } 182 else if ((children = container.getRemoteChildren(_prefix)) != null) { 183 out.startReply(); 184 185 out.writeObject(children.toArray(new String [children.size()])); 186 187 out.completeReply(); 188 } 189 else { 190 out.startReply(); 191 192 out.writeNull(); 193 out.completeReply(); 194 } 195 } 196 197 204 protected void executeUnknown(String method, 205 HessianInput in, HessianOutput out) 206 throws Exception 207 { 208 if (method.equals("_hessian_getAttribute")) { 209 String key = in.readString(); 210 in.completeCall(); 211 212 out.startReply(); 213 214 if ("java.api.class".equals(key)) 215 out.writeString(NameServerRemote.class.getName()); 216 else if ("java.home.class".equals(key)) 217 out.writeString(NameServerRemote.class.getName()); 218 else if ("java.object.class".equals(key)) 219 out.writeString(NameServerRemote.class.getName()); 220 else if ("home-class".equals(key)) 221 out.writeString(NameServerRemote.class.getName()); 222 else if ("remote-class".equals(key)) 223 out.writeString(NameServerRemote.class.getName()); 224 else 225 out.writeNull(); 226 227 out.completeReply(); 228 } 229 else { 230 out.startReply(); 231 out.writeFault("NoMethod", "no such method: " + method, null); 232 out.completeReply(); 233 } 234 } 235 } 236 | Popular Tags |