1 16 17 21 22 package javax.jdo; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.InputStream ; 28 import java.io.IOException ; 29 30 import java.lang.reflect.Method ; 31 import java.lang.reflect.InvocationTargetException ; 32 33 import java.util.Map ; 34 import java.util.Properties ; 35 36 import java.security.AccessController ; 37 import java.security.PrivilegedAction ; 38 39 import javax.jdo.spi.I18NHelper; 40 import javax.jdo.spi.PersistenceCapable; 41 import javax.jdo.spi.StateManager; 43 import javax.naming.Context ; 44 import javax.naming.InitialContext ; 45 import javax.naming.NamingException ; 46 47 import javax.rmi.PortableRemoteObject ; 48 49 50 64 public class JDOHelper extends Object { 65 66 68 private final static I18NHelper msg = I18NHelper.getInstance ("javax.jdo.Bundle"); 70 80 public static PersistenceManager getPersistenceManager(Object pc) { 81 return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoGetPersistenceManager():null; 82 } 83 84 97 public static void makeDirty(Object pc, String fieldName) { 98 if (pc instanceof PersistenceCapable) 99 ((PersistenceCapable)pc).jdoMakeDirty(fieldName); 100 } 101 102 129 public static Object getObjectId(Object pc) { 130 return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoGetObjectId():null; 131 } 132 133 140 public static Object getTransactionalObjectId(Object pc) { 141 return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoGetTransactionalObjectId():null; 142 } 143 144 150 public static Object getVersion (Object pc) { 151 return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoGetVersion():null; 152 } 153 166 public static boolean isDirty(Object pc) { 167 return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsDirty():false; 168 } 169 170 181 public static boolean isTransactional(Object pc) { 182 return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsTransactional():false; 183 } 184 185 198 public static boolean isPersistent(Object pc) { 199 return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsPersistent():false; 200 } 201 202 216 public static boolean isNew(Object pc) { 217 return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsNew():false; 218 } 219 220 233 public static boolean isDeleted(Object pc) { 234 return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsDeleted():false; 235 } 236 237 249 public static boolean isDetached(Object pc) { 250 return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsDetached():false; 251 } 252 253 260 public static PersistenceManagerFactory getPersistenceManagerFactory 261 (Map props) { 262 ClassLoader cl = getContextClassLoader(); 263 return getPersistenceManagerFactory (props, cl); 264 } 265 266 302 public static PersistenceManagerFactory getPersistenceManagerFactory 303 (Map props, ClassLoader cl) { 304 String pmfClassName = (String ) props.get ("javax.jdo.PersistenceManagerFactoryClass"); if (pmfClassName == null) { 306 throw new JDOFatalUserException (msg.msg("EXC_GetPMFNoClassNameProperty")); } 308 try { 309 Class pmfClass = cl.loadClass (pmfClassName); 310 Method pmfMethod = pmfClass.getMethod("getPersistenceManagerFactory", new Class [] {Map .class}); 312 return (PersistenceManagerFactory) pmfMethod.invoke (null, new Object [] {props}); 313 } catch (ClassNotFoundException cnfe) { 314 throw new JDOFatalUserException (msg.msg("EXC_GetPMFClassNotFound", pmfClassName), cnfe); } catch (IllegalAccessException iae) { 316 throw new JDOFatalUserException (msg.msg("EXC_GetPMFIllegalAccess", pmfClassName), iae); } catch (NoSuchMethodException nsme) { 318 throw new JDOFatalInternalException (msg.msg("EXC_GetPMFNoSuchMethod"), nsme); } catch (InvocationTargetException ite) { 320 Throwable nested = ite.getTargetException(); 321 if (nested instanceof JDOException) { 322 throw (JDOException)nested; 323 } else throw new JDOFatalInternalException (msg.msg("EXC_GetPMFUnexpectedException"), ite); } catch (NullPointerException e) { 325 throw new JDOFatalInternalException (msg.msg("EXC_GetPMFNullPointerException", pmfClassName), e); } catch (ClassCastException e) { 327 throw new JDOFatalInternalException (msg.msg("EXC_GetPMFClassCastException", pmfClassName), e); } catch (Exception e) { 329 throw new JDOFatalInternalException (msg.msg("EXC_GetPMFUnexpectedException"), e); } 331 } 332 333 345 public static PersistenceManagerFactory getPersistenceManagerFactory 346 (String propsResource) { 347 return getPersistenceManagerFactory (propsResource, 348 getContextClassLoader()); 349 } 350 351 364 public static PersistenceManagerFactory getPersistenceManagerFactory 365 (String propsResource, ClassLoader loader) { 366 return getPersistenceManagerFactory(propsResource, loader, loader); 367 } 368 369 383 public static PersistenceManagerFactory getPersistenceManagerFactory 384 (String propsResource, ClassLoader propsLoader, ClassLoader pmfLoader) { 385 386 if (propsResource == null) 387 throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullResource")); if (propsLoader == null) 389 throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullPropsLoader")); if (pmfLoader == null) 391 throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullPMFLoader")); 393 Properties props = new Properties (); 394 InputStream in = null; 395 try { 396 in = propsLoader.getResourceAsStream (propsResource); 397 if (in == null) 398 throw new JDOFatalUserException 399 (msg.msg ("EXC_GetPMFNoResource", propsResource, propsLoader)); props.load (in); 401 } catch (IOException ioe) { 402 throw new JDOFatalUserException 403 (msg.msg ("EXC_GetPMFIOExceptionRsrc", propsResource), ioe); } 405 finally { 406 if (in != null) 407 try { 408 in.close (); 409 } catch (IOException ioe) { } 410 } 411 412 return getPersistenceManagerFactory (props, pmfLoader); 413 } 414 415 416 428 public static PersistenceManagerFactory getPersistenceManagerFactory 429 (File propsFile) { 430 return getPersistenceManagerFactory (propsFile, 431 getContextClassLoader()); 432 } 433 434 447 public static PersistenceManagerFactory getPersistenceManagerFactory 448 (File propsFile, ClassLoader loader) { 449 if (propsFile == null) 450 throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullFile")); if (loader == null) 452 throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullLoader")); Properties props = new Properties (); 454 InputStream in = null; 455 try { 456 in = new FileInputStream (propsFile); 457 props.load (in); 458 } catch (FileNotFoundException fnfe) { 459 throw new JDOFatalUserException 460 (msg.msg ("EXC_GetPMFNoFile", propsFile, loader), fnfe); } catch (IOException ioe) { 462 throw new JDOFatalUserException 463 (msg.msg ("EXC_GetPMFIOExceptionFile", propsFile), ioe); } finally { 465 if (in != null) 466 try { 467 in.close (); 468 } catch (IOException ioe) { } 469 } 470 return getPersistenceManagerFactory (props, loader); 471 } 472 473 487 public static PersistenceManagerFactory getPersistenceManagerFactory 488 (String jndiLocation, Context context) { 489 return getPersistenceManagerFactory (jndiLocation, context, 490 getContextClassLoader()); 491 } 492 493 494 508 public static PersistenceManagerFactory getPersistenceManagerFactory 509 (String jndiLocation, Context context, ClassLoader loader) { 510 if (jndiLocation == null) 511 throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullJndiLoc")); if (loader == null) 513 throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullLoader")); try { 515 if (context == null) 516 context = new InitialContext (); 517 518 Object o = context.lookup (jndiLocation); 519 return (PersistenceManagerFactory) PortableRemoteObject.narrow 520 (o, PersistenceManagerFactory.class); 521 } catch (NamingException ne) { 522 throw new JDOFatalUserException 523 (msg.msg ("EXC_GetPMFNamingException", jndiLocation, loader), ne); } 525 } 526 527 539 public static PersistenceManagerFactory getPersistenceManagerFactory 540 (InputStream stream) { 541 return getPersistenceManagerFactory (stream, 542 getContextClassLoader()); 543 } 544 545 557 public static PersistenceManagerFactory getPersistenceManagerFactory 558 (InputStream stream, ClassLoader loader) { 559 if (stream == null) 560 throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullStream")); if (loader == null) 562 throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullLoader")); Properties props = new Properties (); 564 try { 565 props.load (stream); 566 } catch (IOException ioe) { 567 throw new JDOFatalUserException 568 (msg.msg ("EXC_GetPMFIOExceptionStream"), ioe); } 570 return getPersistenceManagerFactory (props, loader); 571 } 572 573 578 private static ClassLoader getContextClassLoader() { 579 return (ClassLoader )AccessController.doPrivileged( 580 new PrivilegedAction () { 581 public Object run () { 582 return Thread.currentThread().getContextClassLoader(); 583 } 584 } 585 ); 586 } 587 } 588 | Popular Tags |