1 29 30 package com.caucho.ejb.hessian; 31 32 import com.caucho.naming.AbstractModel; 33 import com.caucho.naming.NamingExceptionWrapper; 34 import com.caucho.services.name.NameServerRemote; 35 import com.caucho.util.L10N; 36 37 import javax.ejb.EJBHome ; 38 import javax.naming.NamingException ; 39 import java.util.ArrayList ; 40 import java.util.Hashtable ; 41 import java.util.List ; 42 43 48 public class HessianModel extends AbstractModel { 49 private static L10N L = new L10N(HessianModel.class); 50 51 private String _urlPrefix; 52 private String _namePrefix; 53 private HessianModel _root; 54 private Hashtable _cache; 55 private HessianClientContainer _client; 56 private NameServerRemote _remoteRoot; 57 private NameServerRemote _remote; 58 59 62 public HessianModel(String prefix) 63 { 64 if (! prefix.endsWith("/")) 65 prefix = prefix + '/'; 66 67 _urlPrefix = prefix; 68 _namePrefix = "/"; 69 _root = this; 70 _cache = new Hashtable (); 71 } 72 73 76 public HessianModel(String namePrefix, HessianModel root) 77 { 78 if (! namePrefix.endsWith("/")) 79 namePrefix = namePrefix + '/'; 80 81 _namePrefix = namePrefix; 82 _root = root; 83 } 84 85 void setRemote(NameServerRemote remote) 86 { 87 _remote = remote; 88 } 89 90 void setClientContainer(HessianClientContainer client) 91 { 92 _root._client = client; 93 } 94 95 98 public AbstractModel copy() 99 { 100 return this; 101 } 102 103 106 String getURLPrefix() 107 { 108 return _root._urlPrefix; 109 } 110 111 121 public Object lookup(String name) 122 throws NamingException 123 { 124 try { 125 String urlPrefix = getURLPrefix(); 126 String cacheName = urlPrefix + _namePrefix + name; 127 128 Object obj = _root._cache.get(cacheName); 129 if (obj != null) 130 return obj; 131 132 if (_root._remoteRoot == null) { 133 if (_root._client == null) 134 _root._client = HessianClientContainer.find(urlPrefix); 135 136 Object stub = _client.createObjectStub(urlPrefix); 137 138 _root._remoteRoot = (NameServerRemote) stub; 139 } 140 141 obj = _root._remoteRoot.lookup(_namePrefix + name); 142 143 if (obj instanceof EJBHome ) 144 _root._cache.put(cacheName, obj); 145 else if (obj instanceof NameServerRemote) { 146 HessianModel model = new HessianModel(_namePrefix + name, _root); 147 NameServerRemote remote = (NameServerRemote) obj; 148 model.setRemote(remote); 149 obj = model; 150 _root._cache.put(cacheName, obj); 151 } 152 153 return obj; 154 } catch (Exception e) { 155 throw new NamingExceptionWrapper(e); 156 } 157 } 158 159 164 public List list() 165 throws NamingException 166 { 167 try { 168 if (_remote == null) { 169 if (_root._remoteRoot == null) { 170 if (_root._client == null) 171 _root._client = HessianClientContainer.find(getURLPrefix()); 172 173 _root._remoteRoot = 174 (NameServerRemote) _client.createObjectStub(getURLPrefix()); 175 } 176 177 Object obj = _root._remoteRoot.lookup(_namePrefix); 178 if (obj instanceof NameServerRemote) 179 _remote = (NameServerRemote) obj; 180 } 181 182 if (_remote == null) 183 throw new NamingException (L.l("Hessian object `{0}' is not a context.", 184 getURLPrefix() + _namePrefix)); 185 186 String []list = _remote.list(); 187 188 ArrayList value = new ArrayList (); 189 for (int i = 0; list != null && i < list.length; i++) 190 value.add(list[i]); 191 192 return value; 193 } catch (NamingException e) { 194 throw e; 195 } catch (Exception e) { 196 throw new NamingExceptionWrapper(e); 197 } 198 } 199 200 public String toString() 201 { 202 return "HessianModel[url=" + " " + getURLPrefix() + ",name=" + _namePrefix + "]"; 203 } 204 } 205 | Popular Tags |