1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.activation;
  6. import java.io.*;
  7. import java.beans.Beans;
  8. /**
  9. * The CommandInfo class is used by CommandMap implementations to
  10. * describe the results of command requests. It provides the requestor
  11. * with both the verb requested, as well as an instance of the
  12. * bean. There is also a method that will return the name of the
  13. * class that implements the command but <i>it is not guaranteed to
  14. * return a valid value</i>. The reason for this is to allow CommandMap
  15. * implmentations that subclass CommandInfo to provide special
  16. * behavior. For example a CommandMap could dynamically generate
  17. * JavaBeans. In this case, it might not be possible to create an
  18. * object with all the correct state information solely from the class
  19. * name.
  20. */
  21. public class CommandInfo {
  22. private String verb;
  23. private String className;
  24. /**
  25. * The Constructor for CommandInfo.
  26. * @param verb The command verb this CommandInfo decribes.
  27. * @param className The command's fully qualified class name.
  28. */
  29. public CommandInfo(String verb, String className) {
  30. this.verb = verb;
  31. this.className = className;
  32. }
  33. /**
  34. * Return the command verb.
  35. *
  36. * @return the command verb.
  37. */
  38. public String getCommandName() {
  39. return verb;
  40. }
  41. /**
  42. * Return the command's class name. <i>This method MAY return null in
  43. * cases where a CommandMap subclassed CommandInfo for its
  44. * own purposes.</i> In other words, it might not be possible to
  45. * create the correct state in the command by merely knowing
  46. * its class name. <b>DO NOT DEPEND ON THIS METHOD RETURNING
  47. * A VALID VALUE!</b>
  48. *
  49. * @return The class name of the command, or <i>null</i>
  50. */
  51. public String getCommandClass() {
  52. return className;
  53. }
  54. /**
  55. * Return the instantiated JavaBean component.
  56. * <p>
  57. * Begin by instantiating the component with
  58. * <code>Beans.instantiate()</code>.
  59. * <p>
  60. * If the bean implements the <code>javax.activation.CommandObject</code>
  61. * interface, call its <code>setCommandContext</code> method.
  62. * <p>
  63. * If the DataHandler parameter is null, then the bean is
  64. * instantiated with no data. NOTE: this may be useful
  65. * if for some reason the DataHandler that is passed in
  66. * throws IOExceptions when this method attempts to
  67. * access its InputStream. It will allow the caller to
  68. * retrieve a reference to the bean if it can be
  69. * instantiated.
  70. * <p>
  71. * If the bean does NOT implement the CommandObject interface,
  72. * this method will check if it implements the
  73. * java.io.Externalizable interface. If it does, the bean's
  74. * readExternal method will be called if an InputStream
  75. * can be acquired from the DataHandler.<p>
  76. *
  77. * @param dh The DataHandler that describes the data to be
  78. * passed to the command.
  79. * @param loader The ClassLoader to be used to instantiate the bean.
  80. * @return The bean
  81. * @see java.beans.Beans#instantiate
  82. * @see javax.activation.CommandObject
  83. */
  84. public Object getCommandObject(DataHandler dh, ClassLoader loader)
  85. throws IOException, ClassNotFoundException {
  86. Object new_bean = null;
  87. // try to instantiate the bean
  88. new_bean = java.beans.Beans.instantiate(loader, className);
  89. // if we got one and it is a CommandObject
  90. if (new_bean != null) {
  91. if (new_bean instanceof CommandObject) {
  92. ((CommandObject)new_bean).setCommandContext(verb, dh);
  93. } else if (new_bean instanceof Externalizable) {
  94. if (dh != null) {
  95. InputStream is = dh.getInputStream();
  96. if (is != null) {
  97. ((Externalizable)new_bean).readExternal(
  98. new ObjectInputStream(is));
  99. }
  100. }
  101. }
  102. }
  103. return new_bean;
  104. }
  105. }