KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > ejb > cmp > CMPBeanHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * CMPBeanHelper.java
26  *
27  * Created on May 28, 2003
28  */

29 package com.sun.jdo.spi.persistence.support.ejb.cmp;
30
31 import java.util.Collection JavaDoc;
32 import java.util.ResourceBundle JavaDoc;
33
34 import javax.ejb.DuplicateKeyException JavaDoc;
35 import javax.ejb.EJBException JavaDoc;
36 import javax.ejb.ObjectNotFoundException JavaDoc;
37
38 import com.sun.jdo.api.persistence.support.JDOHelper;
39 import com.sun.jdo.api.persistence.support.JDOException;
40 import com.sun.jdo.api.persistence.support.JDOFatalInternalException;
41 import com.sun.jdo.api.persistence.support.PersistenceCapable;
42 import com.sun.jdo.api.persistence.support.PersistenceManager;
43
44 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.EJBHelper;
45
46 import com.sun.jdo.spi.persistence.utility.I18NHelper;
47 import com.sun.jdo.spi.persistence.utility.logging.Logger;
48
49 /** Provides static helper methods for CMP bean implementation to simplify
50  * the generated code.
51  */

52 public class CMPBeanHelper {
53
54     /** I18N message handlers */
55     private final static ResourceBundle JavaDoc cmpMessages = I18NHelper.loadBundle(
56         "com.sun.jdo.spi.persistence.support.ejb.ejbc.Bundle", // NOI18N
57
CMPBeanHelper.class.getClassLoader());
58
59     /**
60      * The lifecycle logger used to log messages from ejbCreate(), ejbRemove()
61      * and other lifecycle methods.
62      */

63     private static Logger cmpLifecycleLogger = LogHelperEntityLifecycle.getLogger();
64
65     /**
66      * The finder logger used to log messages from ejbFindXXX and/or ejbSelectXXX
67      * methods.
68      */

69     private static Logger cmpFinderLogger = LogHelperEntityFinder.getLogger();
70
71     /**
72      * The internal logger used to log messages from setters and getter and other
73      * generated methods.
74      */

75     private static Logger cmpInternalLogger = LogHelperEntityInternal.getLogger();
76
77     /**
78      * Called from a CMP bean to log JDOException message
79      * with the LifecycleLogger.
80      *
81      * @param key the key for the corresponding Bundle.
82      * @param beanName the name of the calling bean.
83      * @param ex the JDOException.
84      */

85     public static void logJDOExceptionWithLifecycleLogger(
86             String JavaDoc key, String JavaDoc beanName, JDOException ex) {
87
88         cmpLifecycleLogger.log(Logger.WARNING,
89                 I18NHelper.getMessage(cmpMessages, key,
90                         beanName, findCallingMethodName()), ex);
91     }
92
93     /**
94      * Called from a CMP bean to log JDOException message
95      * with the LifecycleLogger.
96      *
97      * @param key the key for the corresponding Bundle.
98      * @param beanName the name of the calling bean.
99      * @param paramList the list of the concatenated parameters.
100      * @param ex the JDOException.
101      */

102     public static void logJDOExceptionWithLifecycleLogger(
103             String JavaDoc key, String JavaDoc beanName, String JavaDoc paramList,
104             JDOException ex) {
105
106         cmpLifecycleLogger.log(Logger.WARNING,
107                 I18NHelper.getMessage(cmpMessages, key,
108                         beanName, findCallingMethodName(), paramList),
109                 ex);
110     }
111
112     /**
113      * Called from a CMP bean to log JDOException message thrown
114      * from a any getter or setter method, with the InternalLogger.
115      *
116      * @param beanName the name of the calling bean.
117      * @param ex the JDOException.
118      */

119     public static void logJDOExceptionWithInternalLogger(
120             String JavaDoc beanName, JDOException ex) {
121
122         cmpInternalLogger.log(Logger.WARNING,
123                 I18NHelper.getMessage(cmpMessages,
124                 "GEN.generic_method_exception", // NOI18N
125
beanName, findCallingMethodName()), ex);
126     }
127
128     /**
129      * Called from a CMP bean to log JDOException message thrown
130      * from a any finder or selector method, with the FinderLogger.
131      *
132      * @param beanName the name of the calling bean.
133      * @param params the Object[] of the parameter values for the
134      * finder or selector method.
135      * @param ex the JDOException.
136      */

137     public static void logJDOExceptionWithFinderLogger(
138             String JavaDoc beanName, Object JavaDoc[] params, JDOException ex) {
139
140         String JavaDoc msg = null;
141         if (params != null) {
142             msg = I18NHelper.getMessage(cmpMessages,
143                     "GEN.ejbSSReturnBody_exception", beanName, // NOI18N
144
findCallingMethodName(),
145                     java.util.Arrays.asList(params).toString());
146         } else {
147             msg = I18NHelper.getMessage(cmpMessages,
148                     "GEN.ejbSSReturnBody_exception_woparams", beanName, // NOI18N
149
findCallingMethodName());
150         }
151         cmpFinderLogger.log(Logger.WARNING, msg, ex);
152     }
153
154     /**
155      * Called from a CMP bean to log JDOException message thrown
156      * from a any finder or selector method, with the FinderLogger.
157      *
158      * @param level the logging level as int.
159      * @param beanName the name of the calling bean.
160      * @param ex the Exception.
161      */

162     public static void logFinderException(int level, String JavaDoc beanName,
163             Exception JavaDoc ex) {
164
165         if (cmpFinderLogger.isLoggable(level)) {
166             cmpFinderLogger.log(level,
167                     I18NHelper.getMessage(cmpMessages,
168                             "GEN.generic_method_exception", // NOI18N
169
beanName, findCallingMethodName()), ex);
170         }
171     }
172
173     /**
174      * Called from a CMP bean to log JDOException message thrown
175      * from a PK setter method, with the InternalLogger.
176      * Returns generated message to the caller to be used for a
177      * IllegalStateException.
178      *
179      * @param beanName the name of the calling bean.
180      * @param ex the JDOException.
181      * @return logged message as String.
182      */

183     public static String JavaDoc logJDOExceptionFromPKSetter(
184             String JavaDoc beanName, JDOException ex) {
185
186         String JavaDoc msg = I18NHelper.getMessage(cmpMessages, "EXC_PKUpdate", // NOI18N
187
beanName, findCallingMethodName());
188         if (cmpInternalLogger.isLoggable(Logger.FINE)) {
189             cmpInternalLogger.log(Logger.FINE, msg, ex);
190         }
191
192         return msg;
193     }
194
195     /**
196      * Called from a CMP bean to verify that the PersistenceCapable
197      * instance is already persistent. Throws IllegalStateException
198      * otherwise.
199      *
200      * @param pc the PersistenceCapable instance to be checked.
201      * @param beanName the name of the caller bean.
202      * @throws IllegalStateException if the instance is not persistent.
203      */

204     public static void assertPersistent(PersistenceCapable pc, String JavaDoc beanName) {
205         if (!JDOHelper.isPersistent(pc)) {
206             String JavaDoc msg = I18NHelper.getMessage(cmpMessages,
207                 "GEN.cmrgettersetter_exception", beanName, findCallingMethodName()); // NOI18N
208

209             cmpInternalLogger.log(Logger.SEVERE, msg);
210             throw new IllegalStateException JavaDoc(msg);
211         }
212     }
213
214     /**
215      * Called from a CMP bean to verify that the argument for
216      * a Collection set method is not null. Throws IllegalArgumentException
217      * if the argument is null.
218      *
219      * @param c the Collection to check.
220      * @param beanName the name of the caller bean.
221      * @throws IllegalArgumentException if the argument is null.
222      */

223     public static void assertCollectionNotNull(Collection JavaDoc c, String JavaDoc beanName) {
224         if (c == null) {
225             String JavaDoc msg = I18NHelper.getMessage(cmpMessages,
226                 "GEN.cmrsettercol_nullexception", beanName, findCallingMethodName()); // NOI18N
227

228             cmpInternalLogger.log(Logger.SEVERE, msg);
229             throw new IllegalArgumentException JavaDoc(msg);
230         }
231     }
232
233     /**
234      * Called from a CMP bean to verify that the PersistenceManager
235      * is not null. Throws IllegalStateException if the argument is null.
236      *
237      * @param pm the PersistenceManager to check.
238      * @param bean the calling bean instance.
239      * @throws IllegalStateException if the PersistenceManager is null.
240      */

241     public static void assertPersistenceManagerNotNull(PersistenceManager pm,
242         Object JavaDoc bean) {
243         if (pm == null) {
244             String JavaDoc msg = I18NHelper.getMessage(cmpMessages,
245                 "JDO.beannotloaded_exception", bean); // NOI18N
246

247             cmpInternalLogger.log(Logger.SEVERE, msg);
248             throw new IllegalStateException JavaDoc(msg);
249         }
250     }
251
252     /**
253      * Called from a CMP bean to verify that the PersistenceManager
254      * is null. Throws IllegalStateException if the argument is not null.
255      *
256      * @param pm the PersistenceManager to check.
257      * @param bean the calling bean instance.
258      * @throws IllegalStateException if the PersistenceManager is not null.
259      */

260     public static void assertPersistenceManagerIsNull(PersistenceManager pm,
261         Object JavaDoc bean) {
262         if (pm != null) {
263             String JavaDoc msg = I18NHelper.getMessage(cmpMessages,
264                 "JDO.beaninuse_exception", bean); // NOI18N
265

266             cmpInternalLogger.log(Logger.SEVERE, msg);
267             throw new IllegalStateException JavaDoc(msg);
268         }
269     }
270
271     /**
272      * Called from a 1.1 CMP bean to verify that the bean method is not called
273      * in a container transaction. Throws IllegalStateException otherwise.
274      *
275      * @param bean the calling bean instance.
276      * @throws IllegalStateException if the bean method is called in a container transaction.
277      */

278     public static void assertNotContainerTransaction(Object JavaDoc bean) {
279         if (EJBHelper.getTransaction() != null) {
280             String JavaDoc msg = I18NHelper.getMessage(cmpMessages,
281                 "JDO.containertransaction_exception", bean); // NOI18N
282

283             cmpInternalLogger.log(Logger.SEVERE, msg);
284             throw new IllegalStateException JavaDoc(msg);
285         }
286     }
287
288     /**
289      * Called from a CMP bean to process JDODuplicateObjectIdException.
290      * Logs the message and throws DuplicateKeyException.
291      *
292      * @param beanName the name of the calling bean.
293      * @param paramList the list of the concatenated parameters.
294      * @param ex the JDOException.
295      * @throws DuplicateKeyException.
296      */

297     public static void handleJDODuplicateObjectIdAsDuplicateKeyException(
298         String JavaDoc beanName, String JavaDoc paramList, JDOException ex)
299         throws DuplicateKeyException JavaDoc {
300
301         String JavaDoc msg = I18NHelper.getMessage(cmpMessages,
302             "GEN.ejbcreate_exception_dup", beanName, // NOI18N
303
findCallingMethodName(), paramList);
304
305         cmpLifecycleLogger.log(Logger.FINER, msg, ex);
306         throw new DuplicateKeyException JavaDoc(msg);
307     }
308
309     /**
310      * Called from a CMP bean to process JDODuplicateObjectIdException.
311      * Logs the message and throws EJBException.
312      *
313      * @param beanName the name of the calling bean.
314      * @param paramList the list of the concatenated parameters.
315      * @param ex the JDOException.
316      * @throws EJBException.
317      */

318     public static void handleJDODuplicateObjectIdAsEJBException(
319         String JavaDoc beanName, String JavaDoc paramList, JDOException ex) {
320
321         String JavaDoc msg = I18NHelper.getMessage(cmpMessages,
322             "GEN.ejbcreate_exception_dup", beanName, // NOI18N
323
findCallingMethodName(), paramList);
324
325         cmpLifecycleLogger.log(Logger.FINER, msg, ex);
326         throw new EJBException JavaDoc(msg);
327     }
328
329    /**
330      * Called from a CMP bean to process JDOObjectNotFoundException.
331      * Logs the message and throws ObjectNotFoundException
332      *
333      * @param primaryKey the PrimaryKey instance.
334      * @param beanName the name of the calling bean.
335      * @param ex the JDOException.
336      * @throws ObjectNotFoundException.
337      */

338     public static void handleJDOObjectNotFoundException(
339         Object JavaDoc primaryKey, String JavaDoc beanName, JDOException ex)
340         throws ObjectNotFoundException JavaDoc {
341
342         String JavaDoc msg = I18NHelper.getMessage(cmpMessages,
343             "GEN.findbypk_exception_notfound", beanName, // NOI18N
344
primaryKey.toString());
345
346         cmpLifecycleLogger.log(Logger.FINER, msg, ex);
347         throw new ObjectNotFoundException JavaDoc(msg);
348     }
349
350    /**
351      * Throws EJBException on attempted updates to the
352      * calling bean.
353      * @param beanName the name of the calling bean.
354      * @throws EJBException.
355      */

356     public static void handleUpdateNotAllowedException(
357         String JavaDoc beanName) {
358         String JavaDoc msg = I18NHelper.getMessage(cmpMessages,
359             "GEN.update_not_allowed", beanName, // NOI18N
360
findCallingMethodName());
361
362         cmpLifecycleLogger.log(Logger.SEVERE, msg);
363         throw new EJBException JavaDoc(msg);
364     }
365  
366    /**
367      * Throws EJBException on failed clone of persistence state
368      * in read-only beans.
369      *
370      * @param primaryKey the PrimaryKey instance.
371      * @param beanName the name of the calling bean.
372      * @param ex the Exception.
373      * @throws EJBException.
374      */

375     public static void handleCloneException(
376         Object JavaDoc primaryKey, String JavaDoc beanName, Exception JavaDoc ex) {
377  
378         String JavaDoc msg = I18NHelper.getMessage(cmpMessages,
379             "GEN.clone_exception", beanName, // NOI18N
380
primaryKey.toString());
381  
382         cmpLifecycleLogger.log(Logger.SEVERE, msg, ex);
383         throw new EJBException JavaDoc(msg);
384     }
385
386     /**
387      * Calculates the method name of the calling method.
388      *
389      * @return method name as String.
390      */

391     private static String JavaDoc findCallingMethodName() {
392         StackTraceElement JavaDoc[] ste = (new Throwable JavaDoc()).getStackTrace();
393         return ste[2].getMethodName();
394     }
395 }
396
Popular Tags