src/share/jaf_classes/javax/activation/CommandInfo.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaf_classes/javax/activation/CommandInfo.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,138 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package javax.activation;
    1.30 +
    1.31 +import java.io.*;
    1.32 +import java.beans.Beans;
    1.33 +
    1.34 +/**
    1.35 + * The CommandInfo class is used by CommandMap implementations to
    1.36 + * describe the results of command requests. It provides the requestor
    1.37 + * with both the verb requested, as well as an instance of the
    1.38 + * bean. There is also a method that will return the name of the
    1.39 + * class that implements the command but <i>it is not guaranteed to
    1.40 + * return a valid value</i>. The reason for this is to allow CommandMap
    1.41 + * implmentations that subclass CommandInfo to provide special
    1.42 + * behavior. For example a CommandMap could dynamically generate
    1.43 + * JavaBeans. In this case, it might not be possible to create an
    1.44 + * object with all the correct state information solely from the class
    1.45 + * name.
    1.46 + *
    1.47 + * @since 1.6
    1.48 + */
    1.49 +
    1.50 +public class CommandInfo {
    1.51 +    private String verb;
    1.52 +    private String className;
    1.53 +
    1.54 +    /**
    1.55 +     * The Constructor for CommandInfo.
    1.56 +     * @param verb The command verb this CommandInfo decribes.
    1.57 +     * @param className The command's fully qualified class name.
    1.58 +     */
    1.59 +    public CommandInfo(String verb, String className) {
    1.60 +        this.verb = verb;
    1.61 +        this.className = className;
    1.62 +    }
    1.63 +
    1.64 +    /**
    1.65 +     * Return the command verb.
    1.66 +     *
    1.67 +     * @return the command verb.
    1.68 +     */
    1.69 +    public String getCommandName() {
    1.70 +        return verb;
    1.71 +    }
    1.72 +
    1.73 +    /**
    1.74 +     * Return the command's class name. <i>This method MAY return null in
    1.75 +     * cases where a CommandMap subclassed CommandInfo for its
    1.76 +     * own purposes.</i> In other words, it might not be possible to
    1.77 +     * create the correct state in the command by merely knowing
    1.78 +     * its class name. <b>DO NOT DEPEND ON THIS METHOD RETURNING
    1.79 +     * A VALID VALUE!</b>
    1.80 +     *
    1.81 +     * @return The class name of the command, or <i>null</i>
    1.82 +     */
    1.83 +    public String getCommandClass() {
    1.84 +        return className;
    1.85 +    }
    1.86 +
    1.87 +    /**
    1.88 +     * Return the instantiated JavaBean component.
    1.89 +     * <p>
    1.90 +     * Begin by instantiating the component with
    1.91 +     * <code>Beans.instantiate()</code>.
    1.92 +     * <p>
    1.93 +     * If the bean implements the <code>javax.activation.CommandObject</code>
    1.94 +     * interface, call its <code>setCommandContext</code> method.
    1.95 +     * <p>
    1.96 +     * If the DataHandler parameter is null, then the bean is
    1.97 +     * instantiated with no data. NOTE: this may be useful
    1.98 +     * if for some reason the DataHandler that is passed in
    1.99 +     * throws IOExceptions when this method attempts to
   1.100 +     * access its InputStream. It will allow the caller to
   1.101 +     * retrieve a reference to the bean if it can be
   1.102 +     * instantiated.
   1.103 +     * <p>
   1.104 +     * If the bean does NOT implement the CommandObject interface,
   1.105 +     * this method will check if it implements the
   1.106 +     * java.io.Externalizable interface. If it does, the bean's
   1.107 +     * readExternal method will be called if an InputStream
   1.108 +     * can be acquired from the DataHandler.<p>
   1.109 +     *
   1.110 +     * @param dh        The DataHandler that describes the data to be
   1.111 +     *                  passed to the command.
   1.112 +     * @param loader    The ClassLoader to be used to instantiate the bean.
   1.113 +     * @return The bean
   1.114 +     * @see java.beans.Beans#instantiate
   1.115 +     * @see javax.activation.CommandObject
   1.116 +     */
   1.117 +    public Object getCommandObject(DataHandler dh, ClassLoader loader)
   1.118 +                        throws IOException, ClassNotFoundException {
   1.119 +        Object new_bean = null;
   1.120 +
   1.121 +        // try to instantiate the bean
   1.122 +        new_bean = java.beans.Beans.instantiate(loader, className);
   1.123 +
   1.124 +        // if we got one and it is a CommandObject
   1.125 +        if (new_bean != null) {
   1.126 +            if (new_bean instanceof CommandObject) {
   1.127 +                ((CommandObject)new_bean).setCommandContext(verb, dh);
   1.128 +            } else if (new_bean instanceof Externalizable) {
   1.129 +                if (dh != null) {
   1.130 +                    InputStream is = dh.getInputStream();
   1.131 +                    if (is != null) {
   1.132 +                        ((Externalizable)new_bean).readExternal(
   1.133 +                                               new ObjectInputStream(is));
   1.134 +                    }
   1.135 +                }
   1.136 +            }
   1.137 +        }
   1.138 +
   1.139 +        return new_bean;
   1.140 +    }
   1.141 +}

mercurial