1 23 24 29 package com.sun.jdo.spi.persistence.support.ejb.cmp; 30 31 import java.util.Collection ; 32 import java.util.ResourceBundle ; 33 34 import javax.ejb.DuplicateKeyException ; 35 import javax.ejb.EJBException ; 36 import javax.ejb.ObjectNotFoundException ; 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 52 public class CMPBeanHelper { 53 54 55 private final static ResourceBundle cmpMessages = I18NHelper.loadBundle( 56 "com.sun.jdo.spi.persistence.support.ejb.ejbc.Bundle", CMPBeanHelper.class.getClassLoader()); 58 59 63 private static Logger cmpLifecycleLogger = LogHelperEntityLifecycle.getLogger(); 64 65 69 private static Logger cmpFinderLogger = LogHelperEntityFinder.getLogger(); 70 71 75 private static Logger cmpInternalLogger = LogHelperEntityInternal.getLogger(); 76 77 85 public static void logJDOExceptionWithLifecycleLogger( 86 String key, String beanName, JDOException ex) { 87 88 cmpLifecycleLogger.log(Logger.WARNING, 89 I18NHelper.getMessage(cmpMessages, key, 90 beanName, findCallingMethodName()), ex); 91 } 92 93 102 public static void logJDOExceptionWithLifecycleLogger( 103 String key, String beanName, String paramList, 104 JDOException ex) { 105 106 cmpLifecycleLogger.log(Logger.WARNING, 107 I18NHelper.getMessage(cmpMessages, key, 108 beanName, findCallingMethodName(), paramList), 109 ex); 110 } 111 112 119 public static void logJDOExceptionWithInternalLogger( 120 String beanName, JDOException ex) { 121 122 cmpInternalLogger.log(Logger.WARNING, 123 I18NHelper.getMessage(cmpMessages, 124 "GEN.generic_method_exception", beanName, findCallingMethodName()), ex); 126 } 127 128 137 public static void logJDOExceptionWithFinderLogger( 138 String beanName, Object [] params, JDOException ex) { 139 140 String msg = null; 141 if (params != null) { 142 msg = I18NHelper.getMessage(cmpMessages, 143 "GEN.ejbSSReturnBody_exception", beanName, findCallingMethodName(), 145 java.util.Arrays.asList(params).toString()); 146 } else { 147 msg = I18NHelper.getMessage(cmpMessages, 148 "GEN.ejbSSReturnBody_exception_woparams", beanName, findCallingMethodName()); 150 } 151 cmpFinderLogger.log(Logger.WARNING, msg, ex); 152 } 153 154 162 public static void logFinderException(int level, String beanName, 163 Exception ex) { 164 165 if (cmpFinderLogger.isLoggable(level)) { 166 cmpFinderLogger.log(level, 167 I18NHelper.getMessage(cmpMessages, 168 "GEN.generic_method_exception", beanName, findCallingMethodName()), ex); 170 } 171 } 172 173 183 public static String logJDOExceptionFromPKSetter( 184 String beanName, JDOException ex) { 185 186 String msg = I18NHelper.getMessage(cmpMessages, "EXC_PKUpdate", beanName, findCallingMethodName()); 188 if (cmpInternalLogger.isLoggable(Logger.FINE)) { 189 cmpInternalLogger.log(Logger.FINE, msg, ex); 190 } 191 192 return msg; 193 } 194 195 204 public static void assertPersistent(PersistenceCapable pc, String beanName) { 205 if (!JDOHelper.isPersistent(pc)) { 206 String msg = I18NHelper.getMessage(cmpMessages, 207 "GEN.cmrgettersetter_exception", beanName, findCallingMethodName()); 209 cmpInternalLogger.log(Logger.SEVERE, msg); 210 throw new IllegalStateException (msg); 211 } 212 } 213 214 223 public static void assertCollectionNotNull(Collection c, String beanName) { 224 if (c == null) { 225 String msg = I18NHelper.getMessage(cmpMessages, 226 "GEN.cmrsettercol_nullexception", beanName, findCallingMethodName()); 228 cmpInternalLogger.log(Logger.SEVERE, msg); 229 throw new IllegalArgumentException (msg); 230 } 231 } 232 233 241 public static void assertPersistenceManagerNotNull(PersistenceManager pm, 242 Object bean) { 243 if (pm == null) { 244 String msg = I18NHelper.getMessage(cmpMessages, 245 "JDO.beannotloaded_exception", bean); 247 cmpInternalLogger.log(Logger.SEVERE, msg); 248 throw new IllegalStateException (msg); 249 } 250 } 251 252 260 public static void assertPersistenceManagerIsNull(PersistenceManager pm, 261 Object bean) { 262 if (pm != null) { 263 String msg = I18NHelper.getMessage(cmpMessages, 264 "JDO.beaninuse_exception", bean); 266 cmpInternalLogger.log(Logger.SEVERE, msg); 267 throw new IllegalStateException (msg); 268 } 269 } 270 271 278 public static void assertNotContainerTransaction(Object bean) { 279 if (EJBHelper.getTransaction() != null) { 280 String msg = I18NHelper.getMessage(cmpMessages, 281 "JDO.containertransaction_exception", bean); 283 cmpInternalLogger.log(Logger.SEVERE, msg); 284 throw new IllegalStateException (msg); 285 } 286 } 287 288 297 public static void handleJDODuplicateObjectIdAsDuplicateKeyException( 298 String beanName, String paramList, JDOException ex) 299 throws DuplicateKeyException { 300 301 String msg = I18NHelper.getMessage(cmpMessages, 302 "GEN.ejbcreate_exception_dup", beanName, findCallingMethodName(), paramList); 304 305 cmpLifecycleLogger.log(Logger.FINER, msg, ex); 306 throw new DuplicateKeyException (msg); 307 } 308 309 318 public static void handleJDODuplicateObjectIdAsEJBException( 319 String beanName, String paramList, JDOException ex) { 320 321 String msg = I18NHelper.getMessage(cmpMessages, 322 "GEN.ejbcreate_exception_dup", beanName, findCallingMethodName(), paramList); 324 325 cmpLifecycleLogger.log(Logger.FINER, msg, ex); 326 throw new EJBException (msg); 327 } 328 329 338 public static void handleJDOObjectNotFoundException( 339 Object primaryKey, String beanName, JDOException ex) 340 throws ObjectNotFoundException { 341 342 String msg = I18NHelper.getMessage(cmpMessages, 343 "GEN.findbypk_exception_notfound", beanName, primaryKey.toString()); 345 346 cmpLifecycleLogger.log(Logger.FINER, msg, ex); 347 throw new ObjectNotFoundException (msg); 348 } 349 350 356 public static void handleUpdateNotAllowedException( 357 String beanName) { 358 String msg = I18NHelper.getMessage(cmpMessages, 359 "GEN.update_not_allowed", beanName, findCallingMethodName()); 361 362 cmpLifecycleLogger.log(Logger.SEVERE, msg); 363 throw new EJBException (msg); 364 } 365 366 375 public static void handleCloneException( 376 Object primaryKey, String beanName, Exception ex) { 377 378 String msg = I18NHelper.getMessage(cmpMessages, 379 "GEN.clone_exception", beanName, primaryKey.toString()); 381 382 cmpLifecycleLogger.log(Logger.SEVERE, msg, ex); 383 throw new EJBException (msg); 384 } 385 386 391 private static String findCallingMethodName() { 392 StackTraceElement [] ste = (new Throwable ()).getStackTrace(); 393 return ste[2].getMethodName(); 394 } 395 } 396 | Popular Tags |