1 28 29 package com.caucho.ejb.burlap; 30 31 import com.caucho.naming.AbstractModel; 32 import com.caucho.naming.NamingExceptionWrapper; 33 import com.caucho.services.name.NameServerRemote; 34 import com.caucho.util.L10N; 35 36 import javax.ejb.EJBHome ; 37 import javax.naming.NamingException ; 38 import java.util.ArrayList ; 39 import java.util.Hashtable ; 40 import java.util.List ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 49 public class BurlapModel extends AbstractModel { 50 private static final L10N L = new L10N(BurlapModel.class); 51 private static final Logger log 52 = Logger.getLogger(BurlapModel.class.getName()); 53 54 private String _urlPrefix; 55 private String _namePrefix; 56 private BurlapModel _root; 57 private Hashtable _cache; 58 private BurlapClientContainer _client; 59 private NameServerRemote _remoteRoot; 60 private NameServerRemote _remote; 61 62 65 public BurlapModel(String prefix) 66 { 67 if (! prefix.endsWith("/")) 68 prefix = prefix + '/'; 69 70 _urlPrefix = prefix; 71 _namePrefix = "/"; 72 _root = this; 73 _cache = new Hashtable (); 74 } 75 76 79 public BurlapModel(String namePrefix, BurlapModel root) 80 { 81 if (! namePrefix.endsWith("/")) 82 namePrefix = namePrefix + '/'; 83 84 _namePrefix = namePrefix; 85 _root = root; 86 } 87 88 void setRemote(NameServerRemote remote) 89 { 90 _remote = remote; 91 } 92 93 void setClientContainer(BurlapClientContainer client) 94 { 95 _root._client = client; 96 } 97 98 101 public AbstractModel copy() 102 { 103 return this; 104 } 105 106 109 String getURLPrefix() 110 { 111 return _root._urlPrefix; 112 } 113 114 117 String getURL() 118 { 119 return getURLPrefix() + _namePrefix; 120 } 121 122 132 public Object lookup(String name) 133 throws NamingException 134 { 135 try { 136 Object obj = _root._cache.get(getURLPrefix() + _namePrefix + name); 137 if (obj != null) 138 return obj; 139 140 if (_root._remoteRoot == null) { 141 if (_root._client == null) 142 _root._client = BurlapClientContainer.find(getURLPrefix()); 143 144 _root._remoteRoot = 145 (NameServerRemote) _client.createObjectStub(getURLPrefix()); 146 } 147 148 obj = _root._remoteRoot.lookup(_namePrefix + name); 149 150 if (obj instanceof EJBHome ) 151 _root._cache.put(name, obj); 152 else if (obj instanceof NameServerRemote) { 153 BurlapModel model = new BurlapModel(_namePrefix + name, _root); 154 _remote = (NameServerRemote) obj; 155 model.setRemote(_remote); 156 obj = model; 157 _root._cache.put(name, obj); 158 } 159 160 if (log.isLoggable(Level.FINE)) 161 log.fine(this + " lookup(" + name + ")->" + obj); 162 163 return obj; 164 } catch (Exception e) { 165 throw new NamingExceptionWrapper(e); 166 } 167 } 168 169 174 public List list() 175 throws NamingException 176 { 177 try { 178 if (_remote == null) { 179 if (_root._remoteRoot == null) { 180 if (_root._client == null) 181 _root._client = BurlapClientContainer.find(getURLPrefix()); 182 183 _root._remoteRoot = 184 (NameServerRemote) _client.createObjectStub(getURLPrefix()); 185 } 186 187 Object obj = _root._remoteRoot.lookup(_namePrefix); 188 if (obj instanceof NameServerRemote) 189 _remote = (NameServerRemote) obj; 190 } 191 192 if (_remote == null) 193 throw new NamingException (L.l("Burlap object `{0}' is not a context.", 194 getURLPrefix() + _namePrefix)); 195 196 String []list = _remote.list(); 197 198 ArrayList value = new ArrayList (); 199 for (int i = 0; list != null && i < list.length; i++) 200 value.add(list[i]); 201 202 return value; 203 } catch (NamingException e) { 204 throw e; 205 } catch (Exception e) { 206 throw new NamingExceptionWrapper(e); 207 } 208 } 209 210 public String toString() 211 { 212 return "BurlapModel[url=" + " " + getURLPrefix() + ",name=" + _namePrefix + "]"; 213 } 214 } 215 | Popular Tags |