1. /*
  2. * @(#)PrefixParserAction.java 1.17 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.impl.orb ;
  8. import org.omg.CORBA.INITIALIZE ;
  9. import java.util.Properties ;
  10. import java.util.List ;
  11. import java.util.LinkedList ;
  12. import java.util.Iterator ;
  13. import java.lang.reflect.Array ;
  14. import com.sun.corba.se.spi.orb.Operation ;
  15. import com.sun.corba.se.spi.orb.StringPair ;
  16. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  17. import com.sun.corba.se.impl.orbutil.ObjectUtility ;
  18. import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  19. public class PrefixParserAction extends ParserActionBase {
  20. private Class componentType ;
  21. private ORBUtilSystemException wrapper ;
  22. public PrefixParserAction( String propertyName,
  23. Operation operation, String fieldName, Class componentType )
  24. {
  25. super( propertyName, true, operation, fieldName ) ;
  26. this.componentType = componentType ;
  27. this.wrapper = ORBUtilSystemException.get(
  28. CORBALogDomains.ORB_LIFECYCLE ) ;
  29. }
  30. /** For each String s that matches the prefix given by getPropertyName(),
  31. * apply getOperation() to { suffix( s ), value }
  32. * and add the result to an Object[]
  33. * which forms the result of apply. Returns null if there are no
  34. * matches.
  35. */
  36. public Object apply( Properties props )
  37. {
  38. String prefix = getPropertyName() ;
  39. int prefixLength = prefix.length() ;
  40. if (prefix.charAt( prefixLength - 1 ) != '.') {
  41. prefix += '.' ;
  42. prefixLength++ ;
  43. }
  44. List matches = new LinkedList() ;
  45. // Find all keys in props that start with propertyName
  46. Iterator iter = props.keySet().iterator() ;
  47. while (iter.hasNext()) {
  48. String key = (String)(iter.next()) ;
  49. if (key.startsWith( prefix )) {
  50. String suffix = key.substring( prefixLength ) ;
  51. String value = props.getProperty( key ) ;
  52. StringPair data = new StringPair( suffix, value ) ;
  53. Object result = getOperation().operate( data ) ;
  54. matches.add( result ) ;
  55. }
  56. }
  57. int size = matches.size() ;
  58. if (size > 0) {
  59. // Convert the list into an array of the proper type.
  60. // An Object[] as a result does NOT work. Also report
  61. // any errors carefully, as errors here or in parsers that
  62. // use this Operation often show up at ORB.init().
  63. Object result = null ;
  64. try {
  65. result = Array.newInstance( componentType, size ) ;
  66. } catch (Throwable thr) {
  67. throw wrapper.couldNotCreateArray( thr,
  68. getPropertyName(), componentType,
  69. new Integer( size ) ) ;
  70. }
  71. Iterator iter2 = matches.iterator() ;
  72. int ctr = 0 ;
  73. while (iter2.hasNext()) {
  74. Object obj = iter2.next() ;
  75. try {
  76. Array.set( result, ctr, obj ) ;
  77. } catch (Throwable thr) {
  78. throw wrapper.couldNotSetArray( thr,
  79. getPropertyName(), new Integer(ctr),
  80. componentType, new Integer(size),
  81. ObjectUtility.compactObjectToString( obj )) ;
  82. }
  83. ctr++ ;
  84. }
  85. return result ;
  86. } else
  87. return null ;
  88. }
  89. }