aoqi@0: /* aoqi@0: * Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package javax.activation; aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.beans.Beans; aoqi@0: aoqi@0: /** aoqi@0: * The CommandInfo class is used by CommandMap implementations to aoqi@0: * describe the results of command requests. It provides the requestor aoqi@0: * with both the verb requested, as well as an instance of the aoqi@0: * bean. There is also a method that will return the name of the aoqi@0: * class that implements the command but it is not guaranteed to aoqi@0: * return a valid value. The reason for this is to allow CommandMap aoqi@0: * implmentations that subclass CommandInfo to provide special aoqi@0: * behavior. For example a CommandMap could dynamically generate aoqi@0: * JavaBeans. In this case, it might not be possible to create an aoqi@0: * object with all the correct state information solely from the class aoqi@0: * name. aoqi@0: * aoqi@0: * @since 1.6 aoqi@0: */ aoqi@0: aoqi@0: public class CommandInfo { aoqi@0: private String verb; aoqi@0: private String className; aoqi@0: aoqi@0: /** aoqi@0: * The Constructor for CommandInfo. aoqi@0: * @param verb The command verb this CommandInfo decribes. aoqi@0: * @param className The command's fully qualified class name. aoqi@0: */ aoqi@0: public CommandInfo(String verb, String className) { aoqi@0: this.verb = verb; aoqi@0: this.className = className; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the command verb. aoqi@0: * aoqi@0: * @return the command verb. aoqi@0: */ aoqi@0: public String getCommandName() { aoqi@0: return verb; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the command's class name. This method MAY return null in aoqi@0: * cases where a CommandMap subclassed CommandInfo for its aoqi@0: * own purposes. In other words, it might not be possible to aoqi@0: * create the correct state in the command by merely knowing aoqi@0: * its class name. DO NOT DEPEND ON THIS METHOD RETURNING aoqi@0: * A VALID VALUE! aoqi@0: * aoqi@0: * @return The class name of the command, or null aoqi@0: */ aoqi@0: public String getCommandClass() { aoqi@0: return className; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the instantiated JavaBean component. aoqi@0: *

aoqi@0: * Begin by instantiating the component with aoqi@0: * Beans.instantiate(). aoqi@0: *

aoqi@0: * If the bean implements the javax.activation.CommandObject aoqi@0: * interface, call its setCommandContext method. aoqi@0: *

aoqi@0: * If the DataHandler parameter is null, then the bean is aoqi@0: * instantiated with no data. NOTE: this may be useful aoqi@0: * if for some reason the DataHandler that is passed in aoqi@0: * throws IOExceptions when this method attempts to aoqi@0: * access its InputStream. It will allow the caller to aoqi@0: * retrieve a reference to the bean if it can be aoqi@0: * instantiated. aoqi@0: *

aoqi@0: * If the bean does NOT implement the CommandObject interface, aoqi@0: * this method will check if it implements the aoqi@0: * java.io.Externalizable interface. If it does, the bean's aoqi@0: * readExternal method will be called if an InputStream aoqi@0: * can be acquired from the DataHandler.

aoqi@0: * aoqi@0: * @param dh The DataHandler that describes the data to be aoqi@0: * passed to the command. aoqi@0: * @param loader The ClassLoader to be used to instantiate the bean. aoqi@0: * @return The bean aoqi@0: * @see java.beans.Beans#instantiate aoqi@0: * @see javax.activation.CommandObject aoqi@0: */ aoqi@0: public Object getCommandObject(DataHandler dh, ClassLoader loader) aoqi@0: throws IOException, ClassNotFoundException { aoqi@0: Object new_bean = null; aoqi@0: aoqi@0: // try to instantiate the bean aoqi@0: new_bean = java.beans.Beans.instantiate(loader, className); aoqi@0: aoqi@0: // if we got one and it is a CommandObject aoqi@0: if (new_bean != null) { aoqi@0: if (new_bean instanceof CommandObject) { aoqi@0: ((CommandObject)new_bean).setCommandContext(verb, dh); aoqi@0: } else if (new_bean instanceof Externalizable) { aoqi@0: if (dh != null) { aoqi@0: InputStream is = dh.getInputStream(); aoqi@0: if (is != null) { aoqi@0: ((Externalizable)new_bean).readExternal( aoqi@0: new ObjectInputStream(is)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return new_bean; aoqi@0: } aoqi@0: }