1 15 package org.apache.tapestry.vlib.ejb.impl; 16 17 import java.rmi.RemoteException ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import javax.ejb.CreateException ; 22 import javax.ejb.EJBException ; 23 import javax.ejb.EntityBean ; 24 import javax.ejb.EntityContext ; 25 import javax.naming.Context ; 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 import javax.rmi.PortableRemoteObject ; 29 30 import org.apache.tapestry.contrib.ejb.XEJBException; 31 import org.apache.tapestry.vlib.ejb.IKeyAllocator; 32 import org.apache.tapestry.vlib.ejb.IKeyAllocatorHome; 33 import ognl.Ognl; 34 import ognl.OgnlException; 35 36 44 45 public abstract class AbstractEntityBean implements EntityBean 46 { 47 51 52 private EntityContext _context; 53 54 private transient String [] _attributePropertyNames; 55 56 private transient IKeyAllocatorHome _keyAllocatorHome; 57 58 63 64 private transient Context _environment; 65 66 public void setEntityContext(EntityContext context) 67 { 68 _context = context; 69 } 70 71 public void unsetEntityContext() 72 { 73 _context = null; 74 } 75 76 80 81 protected Object getEnvironmentObject(String name, Class objectClass) 82 throws RemoteException , NamingException 83 { 84 Object result = null; 85 86 if (_environment == null) 87 { 88 Context initial = new InitialContext (); 89 _environment = (Context ) initial.lookup("java:comp/env"); 90 } 91 92 Object raw = _environment.lookup(name); 93 94 try 95 { 96 result = PortableRemoteObject.narrow(raw, objectClass); 97 } 98 catch (ClassCastException ex) 99 { 100 throw new RemoteException ( 101 "Could not narrow " + raw + " (" + name + ") to class " + objectClass + "."); 102 } 103 104 return result; 105 } 106 107 111 112 public void ejbActivate() throws EJBException , RemoteException 113 { 114 } 116 117 121 122 public void ejbPassivate() throws EJBException , RemoteException 123 { 124 } 126 127 131 132 public void ejbRemove() throws EJBException , RemoteException 133 { 134 } 136 137 141 142 public void ejbLoad() throws EJBException , RemoteException 143 { 144 } 145 146 150 151 public void ejbStore() throws EJBException , RemoteException 152 { 153 } 154 155 159 160 protected Integer allocateKey() throws RemoteException 161 { 162 IKeyAllocator allocator; 163 164 if (_keyAllocatorHome == null) 165 { 166 try 167 { 168 Context initial = new InitialContext (); 169 Context environment = (Context ) initial.lookup("java:comp/env"); 170 171 Object raw = environment.lookup("ejb/KeyAllocator"); 172 _keyAllocatorHome = 173 (IKeyAllocatorHome) PortableRemoteObject.narrow(raw, IKeyAllocatorHome.class); 174 } 175 catch (NamingException ex) 176 { 177 throw new XEJBException("Unable to locate IKeyAllocatorHome.", ex); 178 } 179 } 180 181 184 try 185 { 186 allocator = _keyAllocatorHome.create(); 187 } 188 catch (CreateException ex) 189 { 190 throw new RemoteException ( 191 "Unable to create a KeyAllocator from " + _keyAllocatorHome + ".", 192 ex); 193 } 194 195 197 return allocator.allocateKey(); 198 } 199 200 205 206 protected abstract String [] getAttributePropertyNames(); 207 208 218 219 public Map getEntityAttributes() 220 { 221 Map result = new HashMap (); 222 223 if (_attributePropertyNames == null) 224 _attributePropertyNames = getAttributePropertyNames(); 225 226 for (int i = 0; i < _attributePropertyNames.length; i++) 227 { 228 String key = _attributePropertyNames[i]; 229 230 try 231 { 232 Object value = Ognl.getValue(key, this); 233 234 result.put(key, value); 235 } 236 catch (OgnlException ex) 237 { 238 } 239 } 240 241 return result; 242 } 243 244 254 255 public void updateEntityAttributes(Map update) 256 { 257 if (_attributePropertyNames == null) 258 _attributePropertyNames = getAttributePropertyNames(); 259 260 for (int i = 0; i < _attributePropertyNames.length; i++) 261 { 262 String key = _attributePropertyNames[i]; 263 264 if (update.containsKey(key)) 265 { 266 Object value = update.get(key); 267 268 try 269 { 270 Ognl.setValue(key, this, value); 271 } 272 catch (OgnlException ex) 273 { 274 } 275 276 } 277 278 } 279 280 } 281 282 protected void setContext(EntityContext context) 283 { 284 _context = context; 285 } 286 287 protected EntityContext geEntityContext() 288 { 289 return _context; 290 } 291 } | Popular Tags |