KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > ejb > impl > AbstractEntityBean


1 // Copyright 2004 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.vlib.ejb.impl;
16
17 import java.rmi.RemoteException JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.ejb.CreateException JavaDoc;
22 import javax.ejb.EJBException JavaDoc;
23 import javax.ejb.EntityBean JavaDoc;
24 import javax.ejb.EntityContext JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import javax.rmi.PortableRemoteObject JavaDoc;
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 /**
37  * Provides basic support for the entity context, empty or minimal
38  * implementations of the required methods, and some utilties.
39  *
40  * @version $Id: AbstractEntityBean.java,v 1.4 2004/02/19 17:38:07 hlship Exp $
41  * @author Howard Lewis Ship
42  *
43  **/

44
45 public abstract class AbstractEntityBean implements EntityBean JavaDoc
46 {
47     /**
48      * The EntityContext provided by the application server.
49      *
50      **/

51
52     private EntityContext JavaDoc _context;
53
54     private transient String JavaDoc[] _attributePropertyNames;
55
56     private transient IKeyAllocatorHome _keyAllocatorHome;
57
58     /**
59      * The environment naming context, which is configured for this bean
60      * in the deployment descriptor.
61      *
62      **/

63
64     private transient Context JavaDoc _environment;
65
66     public void setEntityContext(EntityContext JavaDoc context)
67     {
68         _context = context;
69     }
70
71     public void unsetEntityContext()
72     {
73         _context = null;
74     }
75
76     /**
77      * Gets a named object from the bean's environment naming context.
78      *
79      **/

80
81     protected Object JavaDoc getEnvironmentObject(String JavaDoc name, Class JavaDoc objectClass)
82         throws RemoteException JavaDoc, NamingException JavaDoc
83     {
84         Object JavaDoc result = null;
85
86         if (_environment == null)
87         {
88             Context JavaDoc initial = new InitialContext JavaDoc();
89             _environment = (Context JavaDoc) initial.lookup("java:comp/env");
90         }
91
92         Object JavaDoc raw = _environment.lookup(name);
93
94         try
95         {
96             result = PortableRemoteObject.narrow(raw, objectClass);
97         }
98         catch (ClassCastException JavaDoc ex)
99         {
100             throw new RemoteException JavaDoc(
101                 "Could not narrow " + raw + " (" + name + ") to class " + objectClass + ".");
102         }
103
104         return result;
105     }
106
107     /**
108      * Empty implementation; subclasses may override.
109      *
110      **/

111
112     public void ejbActivate() throws EJBException JavaDoc, RemoteException JavaDoc
113     {
114         // does nothing
115
}
116
117     /**
118      * Empty implementation; subclasses may override.
119      *
120      **/

121
122     public void ejbPassivate() throws EJBException JavaDoc, RemoteException JavaDoc
123     {
124         // does nothing
125
}
126
127     /**
128      * Empty implementation; subclasses may override.
129      *
130      **/

131
132     public void ejbRemove() throws EJBException JavaDoc, RemoteException JavaDoc
133     {
134         // does nothing
135
}
136
137     /**
138      * Does nothing.
139      *
140      **/

141
142     public void ejbLoad() throws EJBException JavaDoc, RemoteException JavaDoc
143     {
144     }
145
146     /**
147      * Does nothing.
148      *
149      **/

150
151     public void ejbStore() throws EJBException JavaDoc, RemoteException JavaDoc
152     {
153     }
154
155     /**
156      * Uses the KeyAllocator session bean to allocate a necessary key.
157      *
158      **/

159
160     protected Integer JavaDoc allocateKey() throws RemoteException JavaDoc
161     {
162         IKeyAllocator allocator;
163
164         if (_keyAllocatorHome == null)
165         {
166             try
167             {
168                 Context JavaDoc initial = new InitialContext JavaDoc();
169                 Context JavaDoc environment = (Context JavaDoc) initial.lookup("java:comp/env");
170
171                 Object JavaDoc raw = environment.lookup("ejb/KeyAllocator");
172                 _keyAllocatorHome =
173                     (IKeyAllocatorHome) PortableRemoteObject.narrow(raw, IKeyAllocatorHome.class);
174             }
175             catch (NamingException JavaDoc ex)
176             {
177                 throw new XEJBException("Unable to locate IKeyAllocatorHome.", ex);
178             }
179         }
180
181         // Get a reference to *some* KeyAllocator bean ... it may be fresh,
182
// or one reused from a pool.
183

184         try
185         {
186             allocator = _keyAllocatorHome.create();
187         }
188         catch (CreateException JavaDoc ex)
189         {
190             throw new RemoteException JavaDoc(
191                 "Unable to create a KeyAllocator from " + _keyAllocatorHome + ".",
192                 ex);
193         }
194
195         // Finally, invoke the method that gets a key.
196

197         return allocator.allocateKey();
198     }
199
200     /**
201      * Implemented in subclasses to provide a list of property names to be included
202      * in the entity attributes map.
203      *
204      **/

205
206     protected abstract String JavaDoc[] getAttributePropertyNames();
207
208     /**
209      * Returns a {@link Map} of the properties of the bean. This Map is
210      * returned to the client, where it can be modified and then used to update
211      * the entity bean in a single method
212      *
213      * <p>The properties included in the Map are defined by the
214      * {@link #getAttributePropertyNames()} method, which is implemented
215      * by concrete subclasses.
216      *
217      **/

218
219     public Map JavaDoc getEntityAttributes()
220     {
221         Map JavaDoc result = new HashMap JavaDoc();
222
223         if (_attributePropertyNames == null)
224             _attributePropertyNames = getAttributePropertyNames();
225
226         for (int i = 0; i < _attributePropertyNames.length; i++)
227         {
228             String JavaDoc key = _attributePropertyNames[i];
229
230             try
231             {
232                 Object JavaDoc 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     /**
245      * Updates the bean with property changes from the update {@link Map}.
246      * Only the keys defined by {@link #getAttributePropertyNames()} will be
247      * accessed (keys and values that are not in that list are ignored).
248      *
249      * <p>The corresponding bean property will only be updated
250      * if the key is present ... this means that the update may contain just
251      * the <em>changed</em> keys. Remember that a Map may store null values.
252      *
253      **/

254
255     public void updateEntityAttributes(Map JavaDoc update)
256     {
257         if (_attributePropertyNames == null)
258             _attributePropertyNames = getAttributePropertyNames();
259
260         for (int i = 0; i < _attributePropertyNames.length; i++)
261         {
262             String JavaDoc key = _attributePropertyNames[i];
263
264             if (update.containsKey(key))
265             {
266                 Object JavaDoc 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 JavaDoc context)
283     {
284         _context = context;
285     }
286
287     protected EntityContext JavaDoc geEntityContext()
288     {
289         return _context;
290     }
291 }
Popular Tags