1. /*
  2. * @(#)ParserImplBase.java 1.19 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.spi.orb ;
  8. import java.util.Map ;
  9. import java.util.Set ;
  10. import java.util.Iterator ;
  11. import java.util.Properties ;
  12. import java.security.PrivilegedExceptionAction ;
  13. import java.security.PrivilegedActionException ;
  14. import java.security.AccessController ;
  15. import java.lang.reflect.Field ;
  16. import org.omg.CORBA.INTERNAL ;
  17. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  18. import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  19. import com.sun.corba.se.impl.orbutil.ObjectUtility ;
  20. // XXX This could probably be further extended by using more reflection and
  21. // a dynamic proxy that satisfies the interfaces that are inherited by the
  22. // more derived class. Do we want to go that far?
  23. public abstract class ParserImplBase {
  24. private ORBUtilSystemException wrapper ;
  25. protected abstract PropertyParser makeParser() ;
  26. /** Override this method if there is some needed initialization
  27. * that takes place after argument parsing. It is always called
  28. * at the end of setFields.
  29. */
  30. protected void complete()
  31. {
  32. }
  33. public ParserImplBase()
  34. {
  35. // Do nothing in this case: no parsing takes place
  36. wrapper = ORBUtilSystemException.get(
  37. CORBALogDomains.ORB_LIFECYCLE ) ;
  38. }
  39. public void init( DataCollector coll )
  40. {
  41. PropertyParser parser = makeParser() ;
  42. coll.setParser( parser ) ;
  43. Properties props = coll.getProperties() ;
  44. Map map = parser.parse( props ) ;
  45. setFields( map ) ;
  46. }
  47. private Field getAnyField( String name )
  48. {
  49. Field result = null ;
  50. try {
  51. Class cls = this.getClass() ;
  52. result = cls.getDeclaredField( name ) ;
  53. while (result == null) {
  54. cls = cls.getSuperclass() ;
  55. if (cls == null)
  56. break ;
  57. result = cls.getDeclaredField( name ) ;
  58. }
  59. } catch (Exception exc) {
  60. throw wrapper.fieldNotFound( exc, name ) ;
  61. }
  62. if (result == null)
  63. throw wrapper.fieldNotFound( name ) ;
  64. return result ;
  65. }
  66. protected void setFields( Map map )
  67. {
  68. Set entries = map.entrySet() ;
  69. Iterator iter = entries.iterator() ;
  70. while (iter.hasNext()) {
  71. java.util.Map.Entry entry = (java.util.Map.Entry)(iter.next()) ;
  72. final String name = (String)(entry.getKey()) ;
  73. final Object value = entry.getValue() ;
  74. try {
  75. AccessController.doPrivileged(
  76. new PrivilegedExceptionAction() {
  77. public Object run() throws IllegalAccessException,
  78. IllegalArgumentException
  79. {
  80. Field field = getAnyField( name ) ;
  81. field.setAccessible( true ) ;
  82. field.set( ParserImplBase.this, value ) ;
  83. return null ;
  84. }
  85. }
  86. ) ;
  87. } catch (PrivilegedActionException exc) {
  88. // Since exc wraps the actual exception, use exc.getCause()
  89. // instead of exc.
  90. throw wrapper.errorSettingField( exc.getCause(), name,
  91. ObjectUtility.compactObjectToString(value) ) ;
  92. }
  93. }
  94. // Make sure that any extra initialization takes place after all the
  95. // fields are set from the map.
  96. complete() ;
  97. }
  98. }