Wed, 27 Apr 2016 15:32:20 -0700
Added tag jdk8u102-b05 for changeset 1f512419ae33
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package javax.activation; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.*; |
aoqi@0 | 29 | import java.beans.Beans; |
aoqi@0 | 30 | |
aoqi@0 | 31 | /** |
aoqi@0 | 32 | * The CommandInfo class is used by CommandMap implementations to |
aoqi@0 | 33 | * describe the results of command requests. It provides the requestor |
aoqi@0 | 34 | * with both the verb requested, as well as an instance of the |
aoqi@0 | 35 | * bean. There is also a method that will return the name of the |
aoqi@0 | 36 | * class that implements the command but <i>it is not guaranteed to |
aoqi@0 | 37 | * return a valid value</i>. The reason for this is to allow CommandMap |
aoqi@0 | 38 | * implmentations that subclass CommandInfo to provide special |
aoqi@0 | 39 | * behavior. For example a CommandMap could dynamically generate |
aoqi@0 | 40 | * JavaBeans. In this case, it might not be possible to create an |
aoqi@0 | 41 | * object with all the correct state information solely from the class |
aoqi@0 | 42 | * name. |
aoqi@0 | 43 | * |
aoqi@0 | 44 | * @since 1.6 |
aoqi@0 | 45 | */ |
aoqi@0 | 46 | |
aoqi@0 | 47 | public class CommandInfo { |
aoqi@0 | 48 | private String verb; |
aoqi@0 | 49 | private String className; |
aoqi@0 | 50 | |
aoqi@0 | 51 | /** |
aoqi@0 | 52 | * The Constructor for CommandInfo. |
aoqi@0 | 53 | * @param verb The command verb this CommandInfo decribes. |
aoqi@0 | 54 | * @param className The command's fully qualified class name. |
aoqi@0 | 55 | */ |
aoqi@0 | 56 | public CommandInfo(String verb, String className) { |
aoqi@0 | 57 | this.verb = verb; |
aoqi@0 | 58 | this.className = className; |
aoqi@0 | 59 | } |
aoqi@0 | 60 | |
aoqi@0 | 61 | /** |
aoqi@0 | 62 | * Return the command verb. |
aoqi@0 | 63 | * |
aoqi@0 | 64 | * @return the command verb. |
aoqi@0 | 65 | */ |
aoqi@0 | 66 | public String getCommandName() { |
aoqi@0 | 67 | return verb; |
aoqi@0 | 68 | } |
aoqi@0 | 69 | |
aoqi@0 | 70 | /** |
aoqi@0 | 71 | * Return the command's class name. <i>This method MAY return null in |
aoqi@0 | 72 | * cases where a CommandMap subclassed CommandInfo for its |
aoqi@0 | 73 | * own purposes.</i> In other words, it might not be possible to |
aoqi@0 | 74 | * create the correct state in the command by merely knowing |
aoqi@0 | 75 | * its class name. <b>DO NOT DEPEND ON THIS METHOD RETURNING |
aoqi@0 | 76 | * A VALID VALUE!</b> |
aoqi@0 | 77 | * |
aoqi@0 | 78 | * @return The class name of the command, or <i>null</i> |
aoqi@0 | 79 | */ |
aoqi@0 | 80 | public String getCommandClass() { |
aoqi@0 | 81 | return className; |
aoqi@0 | 82 | } |
aoqi@0 | 83 | |
aoqi@0 | 84 | /** |
aoqi@0 | 85 | * Return the instantiated JavaBean component. |
aoqi@0 | 86 | * <p> |
aoqi@0 | 87 | * Begin by instantiating the component with |
aoqi@0 | 88 | * <code>Beans.instantiate()</code>. |
aoqi@0 | 89 | * <p> |
aoqi@0 | 90 | * If the bean implements the <code>javax.activation.CommandObject</code> |
aoqi@0 | 91 | * interface, call its <code>setCommandContext</code> method. |
aoqi@0 | 92 | * <p> |
aoqi@0 | 93 | * If the DataHandler parameter is null, then the bean is |
aoqi@0 | 94 | * instantiated with no data. NOTE: this may be useful |
aoqi@0 | 95 | * if for some reason the DataHandler that is passed in |
aoqi@0 | 96 | * throws IOExceptions when this method attempts to |
aoqi@0 | 97 | * access its InputStream. It will allow the caller to |
aoqi@0 | 98 | * retrieve a reference to the bean if it can be |
aoqi@0 | 99 | * instantiated. |
aoqi@0 | 100 | * <p> |
aoqi@0 | 101 | * If the bean does NOT implement the CommandObject interface, |
aoqi@0 | 102 | * this method will check if it implements the |
aoqi@0 | 103 | * java.io.Externalizable interface. If it does, the bean's |
aoqi@0 | 104 | * readExternal method will be called if an InputStream |
aoqi@0 | 105 | * can be acquired from the DataHandler.<p> |
aoqi@0 | 106 | * |
aoqi@0 | 107 | * @param dh The DataHandler that describes the data to be |
aoqi@0 | 108 | * passed to the command. |
aoqi@0 | 109 | * @param loader The ClassLoader to be used to instantiate the bean. |
aoqi@0 | 110 | * @return The bean |
aoqi@0 | 111 | * @see java.beans.Beans#instantiate |
aoqi@0 | 112 | * @see javax.activation.CommandObject |
aoqi@0 | 113 | */ |
aoqi@0 | 114 | public Object getCommandObject(DataHandler dh, ClassLoader loader) |
aoqi@0 | 115 | throws IOException, ClassNotFoundException { |
aoqi@0 | 116 | Object new_bean = null; |
aoqi@0 | 117 | |
aoqi@0 | 118 | // try to instantiate the bean |
aoqi@0 | 119 | new_bean = java.beans.Beans.instantiate(loader, className); |
aoqi@0 | 120 | |
aoqi@0 | 121 | // if we got one and it is a CommandObject |
aoqi@0 | 122 | if (new_bean != null) { |
aoqi@0 | 123 | if (new_bean instanceof CommandObject) { |
aoqi@0 | 124 | ((CommandObject)new_bean).setCommandContext(verb, dh); |
aoqi@0 | 125 | } else if (new_bean instanceof Externalizable) { |
aoqi@0 | 126 | if (dh != null) { |
aoqi@0 | 127 | InputStream is = dh.getInputStream(); |
aoqi@0 | 128 | if (is != null) { |
aoqi@0 | 129 | ((Externalizable)new_bean).readExternal( |
aoqi@0 | 130 | new ObjectInputStream(is)); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | } |
aoqi@0 | 133 | } |
aoqi@0 | 134 | } |
aoqi@0 | 135 | |
aoqi@0 | 136 | return new_bean; |
aoqi@0 | 137 | } |
aoqi@0 | 138 | } |