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