1 16 17 21 22 package javax.jdo.spi; 23 24 import java.lang.reflect.Constructor ; 25 26 import java.text.DateFormat ; 27 import java.text.ParsePosition ; 28 import java.text.SimpleDateFormat ; 29 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.Collections ; 33 import java.util.Currency ; 34 import java.util.Date ; 35 import java.util.HashMap ; 36 import java.util.HashSet ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.Locale ; 40 import java.util.Map ; 41 import java.util.WeakHashMap ; 42 43 import javax.jdo.JDOException; 44 import javax.jdo.JDOFatalInternalException; 45 import javax.jdo.JDOFatalUserException; 46 import javax.jdo.JDOUserException; 47 import javax.jdo.spi.JDOPermission; 48 49 65 public class JDOImplHelper extends java.lang.Object { 66 67 73 private static Map registeredClasses = Collections.synchronizedMap(new HashMap ()); 74 75 78 private static Map authorizedStateManagerClasses = new WeakHashMap (); 79 80 82 private static List listeners = new ArrayList (); 83 84 86 private static JDOImplHelper jdoImplHelper = new JDOImplHelper(); 87 88 90 private final static I18NHelper msg = I18NHelper.getInstance ("javax.jdo.Bundle"); 92 94 private static String dateFormatPattern; 95 96 98 private static DateFormat dateFormat; 99 100 102 static { 103 jdoImplHelper.registerDateFormat(DateFormat.getDateTimeInstance()); 104 } 105 106 107 private JDOImplHelper() { 108 } 109 110 116 public static JDOImplHelper getInstance() 117 throws SecurityException { 118 SecurityManager sec = System.getSecurityManager(); 119 if (sec != null) { 120 sec.checkPermission (JDOPermission.GET_METADATA); 122 } 123 return jdoImplHelper; 124 } 125 126 132 public String [] getFieldNames (Class pcClass) { 133 Meta meta = getMeta (pcClass); 134 return meta.getFieldNames(); 135 } 136 137 142 public Class [] getFieldTypes (Class pcClass) { 143 Meta meta = getMeta (pcClass); 144 return meta.getFieldTypes(); 145 } 146 147 152 public byte[] getFieldFlags (Class pcClass) { 153 Meta meta = getMeta (pcClass); 154 return meta.getFieldFlags(); 155 } 156 157 162 public Class getPersistenceCapableSuperclass (Class pcClass) { 163 Meta meta = getMeta (pcClass); 164 return meta.getPersistenceCapableSuperclass(); 165 } 166 167 168 175 public PersistenceCapable newInstance (Class pcClass, StateManager sm) { 176 Meta meta = getMeta (pcClass); 177 PersistenceCapable pcInstance = meta.getPC(); 178 return pcInstance == null?null:pcInstance.jdoNewInstance(sm); 179 } 180 181 191 public PersistenceCapable newInstance 192 (Class pcClass, StateManager sm, Object oid) { 193 Meta meta = getMeta (pcClass); 194 PersistenceCapable pcInstance = meta.getPC(); 195 return pcInstance == null?null:pcInstance.jdoNewInstance(sm, oid); 196 } 197 198 203 public Object newObjectIdInstance (Class pcClass) { 204 Meta meta = getMeta (pcClass); 205 PersistenceCapable pcInstance = meta.getPC(); 206 return pcInstance == null?null:pcInstance.jdoNewObjectIdInstance(); 207 } 208 209 215 public Object newObjectIdInstance (Class pcClass, Object obj) { 216 Meta meta = getMeta (pcClass); 217 PersistenceCapable pcInstance = meta.getPC(); 218 return (pcInstance == null)?null:pcInstance.jdoNewObjectIdInstance(obj); 219 } 220 221 238 public void copyKeyFieldsToObjectId 239 (Class pcClass, PersistenceCapable.ObjectIdFieldSupplier fm, Object oid) { 240 Meta meta = getMeta (pcClass); 241 PersistenceCapable pcInstance = meta.getPC(); 242 if (pcInstance == null) { 243 throw new JDOFatalInternalException ( 244 msg.msg("ERR_AbstractClassNoIdentity", pcClass.getName())); } 246 pcInstance.jdoCopyKeyFieldsToObjectId(fm, oid); 247 } 248 249 266 public void copyKeyFieldsFromObjectId 267 (Class pcClass, PersistenceCapable.ObjectIdFieldConsumer fm, Object oid) { 268 Meta meta = getMeta (pcClass); 269 PersistenceCapable pcInstance = meta.getPC(); 270 if (pcInstance == null) { 271 throw new JDOFatalInternalException ( 272 msg.msg("ERR_AbstractClassNoIdentity", pcClass.getName())); } 274 pcInstance.jdoCopyKeyFieldsFromObjectId(fm, oid); 275 } 276 277 290 public static void registerClass (Class pcClass, 291 String [] fieldNames, Class [] fieldTypes, 292 byte[] fieldFlags, Class persistenceCapableSuperclass, 293 PersistenceCapable pc) { 294 if (pcClass == null) 295 throw new NullPointerException (msg.msg("ERR_NullClass")); Meta meta = new Meta (fieldNames, fieldTypes, 297 fieldFlags, persistenceCapableSuperclass, pc); 298 registeredClasses.put (pcClass, meta); 299 300 synchronized (listeners) { 302 if (!listeners.isEmpty()) { 303 RegisterClassEvent event = new RegisterClassEvent( 304 jdoImplHelper, pcClass, fieldNames, fieldTypes, 305 fieldFlags, persistenceCapableSuperclass); 306 for (Iterator i = listeners.iterator(); i.hasNext();) { 307 RegisterClassListener crl = 308 (RegisterClassListener)i.next(); 309 if (crl != null) { 310 crl.registerClass(event); 311 } 312 } 313 } 314 } 315 } 316 317 325 public void unregisterClasses (ClassLoader cl) 326 { 327 SecurityManager sec = System.getSecurityManager(); 328 if (sec != null) { 329 sec.checkPermission (JDOPermission.MANAGE_METADATA); 331 } 332 synchronized(registeredClasses) { 333 for (Iterator i = registeredClasses.keySet().iterator(); i.hasNext();) { 334 Class pcClass = (Class )i.next(); 335 if ((pcClass != null) && (pcClass.getClassLoader() == cl)) { 343 i.remove(); 346 } 347 } 348 } 349 } 350 351 358 public void unregisterClass (Class pcClass) 359 { 360 if (pcClass == null) 361 throw new NullPointerException (msg.msg("ERR_NullClass")); SecurityManager sec = System.getSecurityManager(); 363 if (sec != null) { 364 sec.checkPermission (JDOPermission.MANAGE_METADATA); 366 } 367 registeredClasses.remove(pcClass); 368 } 369 370 374 public void addRegisterClassListener (RegisterClassListener crl) { 375 HashSet alreadyRegisteredClasses = null; 376 synchronized (listeners) { 377 listeners.add(crl); 378 alreadyRegisteredClasses = new HashSet (registeredClasses.keySet()); 384 } 385 for (Iterator it = alreadyRegisteredClasses.iterator(); it.hasNext();) { 388 Class pcClass = (Class )it.next(); 389 Meta meta = getMeta (pcClass); 390 RegisterClassEvent event = new RegisterClassEvent( 391 this, pcClass, meta.getFieldNames(), meta.getFieldTypes(), 392 meta.getFieldFlags(), meta.getPersistenceCapableSuperclass()); 393 crl.registerClass (event); 394 } 395 } 396 397 401 public void removeRegisterClassListener (RegisterClassListener crl) { 402 synchronized (listeners) { 403 listeners.remove(crl); 404 } 405 } 406 407 412 public Collection getRegisteredClasses() { 413 return Collections.unmodifiableCollection(registeredClasses.keySet()); 414 } 415 416 420 private static Meta getMeta (Class pcClass) { 421 Meta ret = (Meta) registeredClasses.get (pcClass); 422 if (ret == null) { 423 throw new JDOFatalUserException( 424 msg.msg ("ERR_NoMetadata", pcClass.getName())); } 426 return ret; 427 } 428 429 438 public static void registerAuthorizedStateManagerClass (Class smClass) 439 throws SecurityException { 440 if (smClass == null) 441 throw new NullPointerException (msg.msg("ERR_NullClass")); SecurityManager sm = System.getSecurityManager(); 443 if (sm != null) { 444 sm.checkPermission(JDOPermission.SET_STATE_MANAGER); 445 } 446 synchronized (authorizedStateManagerClasses) { 447 authorizedStateManagerClasses.put(smClass, null); 448 } 449 } 450 451 460 public static void registerAuthorizedStateManagerClasses (Collection smClasses) 461 throws SecurityException { 462 SecurityManager sm = System.getSecurityManager(); 463 if (sm != null) { 464 sm.checkPermission(JDOPermission.SET_STATE_MANAGER); 465 synchronized (authorizedStateManagerClasses) { 466 for (Iterator it = smClasses.iterator(); it.hasNext();) { 467 Object smClass = it.next(); 468 if (!(smClass instanceof Class )) { 469 throw new ClassCastException ( 470 msg.msg("ERR_StateManagerClassCast", smClass.getClass().getName())); 472 } 473 registerAuthorizedStateManagerClass((Class )it.next()); 474 } 475 } 476 } 477 } 478 479 491 public static void checkAuthorizedStateManager (StateManager sm) { 492 checkAuthorizedStateManagerClass(sm.getClass()); 493 } 494 495 501 public static void checkAuthorizedStateManagerClass (Class smClass) { 502 final SecurityManager scm = System.getSecurityManager(); 503 if (scm == null) { 504 return; 506 } 507 synchronized(authorizedStateManagerClasses) { 508 if (authorizedStateManagerClasses.containsKey(smClass)) { 509 return; 510 } 511 } 512 scm.checkPermission(JDOPermission.SET_STATE_MANAGER); 514 } 515 516 523 public interface StringConstructor { 524 530 public Object construct(String s); 531 } 532 533 539 static Map stringConstructorMap = new HashMap (); 540 541 550 public Object registerStringConstructor(Class cls, StringConstructor sc) { 551 return stringConstructorMap.put(cls, sc); 552 } 553 554 556 static { 557 JDOImplHelper helper = getInstance(); 558 if (isClassLoadable("java.util.Currency")) { 559 helper.registerStringConstructor(Currency .class, new StringConstructor() { 560 public Object construct(String s) { 561 try { 562 return Currency.getInstance(s); 563 } catch (IllegalArgumentException ex) { 564 throw new javax.jdo.JDOUserException( 565 msg.msg("EXC_CurrencyStringConstructorIllegalArgument", s), ex); } catch (Exception ex) { 567 throw new JDOUserException( 568 msg.msg("EXC_CurrencyStringConstructorException"), ex); } 570 } 571 }); 572 } 573 helper.registerStringConstructor(Locale .class, new StringConstructor() { 574 public Object construct(String s) { 575 try { 576 return new Locale (s); 577 } catch (Exception ex) { 578 throw new JDOUserException( 579 msg.msg("EXC_LocaleStringConstructorException"), ex); } 581 } 582 }); 583 helper.registerStringConstructor(Date .class, new StringConstructor() { 584 public synchronized Object construct(String s) { 585 ParsePosition pp = new ParsePosition (0); 586 Date result = dateFormat.parse(s, pp); 587 if (result == null) { 588 throw new JDOUserException ( 589 msg.msg("EXC_DateStringConstructor", new Object [] {s, new Integer (pp.getErrorIndex()), dateFormatPattern})); 591 } 592 return result; 593 } 594 }); 595 } 596 597 600 public static boolean isClassLoadable(String className) { 601 try { 602 Class.forName(className); 603 return true; 604 } catch (ClassNotFoundException ex) { 605 return false; 606 } 607 } 608 609 619 public Object construct(String className, String keyString) { 620 synchronized(stringConstructorMap) { 621 try { 622 Class keyClass = Class.forName(className); 623 StringConstructor stringConstructor = 624 (StringConstructor) stringConstructorMap.get(keyClass); 625 if (stringConstructor == null) { 626 Constructor keyConstructor = 627 keyClass.getConstructor(new Class []{String .class}); 628 return keyConstructor.newInstance(new Object []{keyString}); 629 } else { 630 return stringConstructor.construct(keyString); 631 } 632 } catch (JDOException ex) { 633 throw ex; 634 } catch (Exception ex) { 635 640 throw new JDOUserException( 641 msg.msg("EXC_ObjectIdentityStringConstruction", new Object [] {ex.toString(), className, keyString}), ex); 643 } 644 } 645 } 646 647 654 public synchronized void registerDateFormat(DateFormat df) { 655 dateFormat = df; 656 if (df instanceof SimpleDateFormat ) { 657 dateFormatPattern = ((SimpleDateFormat )df).toPattern(); 658 } else { 659 dateFormatPattern = msg.msg("MSG_unknown"); } 661 } 662 663 671 static class Meta { 672 673 680 Meta (String [] fieldNames, Class [] fieldTypes, byte[] fieldFlags, 681 Class persistenceCapableSuperclass, PersistenceCapable pc) { 682 this.fieldNames = fieldNames; 683 this.fieldTypes = fieldTypes; 684 this.fieldFlags = fieldFlags; 685 this.persistenceCapableSuperclass = persistenceCapableSuperclass; 686 this.pc = pc; 687 } 688 689 693 String fieldNames[]; 694 695 698 String [] getFieldNames() { 699 return fieldNames; 700 } 701 702 706 Class fieldTypes[]; 707 708 711 Class [] getFieldTypes() { 712 return fieldTypes; 713 } 714 715 719 byte fieldFlags[]; 720 721 724 byte[] getFieldFlags() { 725 return fieldFlags; 726 } 727 728 730 Class persistenceCapableSuperclass; 731 732 735 Class getPersistenceCapableSuperclass() { 736 return persistenceCapableSuperclass; 737 } 738 741 PersistenceCapable pc; 742 743 746 PersistenceCapable getPC() { 747 return pc; 748 } 749 750 753 public String toString() { 754 return "Meta-" + pc.getClass().getName(); } 756 } 757 } 758 | Popular Tags |