1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can obtain
10 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
12 * language governing permissions and limitations under the License.
13 *
14 * When distributing the software, include this License Header Notice in each
15 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
16 * Sun designates this particular file as subject to the "Classpath" exception
17 * as provided by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the License
19 * Header, with the fields enclosed by brackets [] replaced by your own
20 * identifying information: "Portions Copyrighted [year]
21 * [name of copyright owner]"
22 *
23 * Contributor(s):
24 *
25 * If you wish your version of this file to be governed by only the CDDL or
26 * only the GPL Version 2, indicate your decision by adding "[Contributor]
27 * elects to include this software in this distribution under the [CDDL or GPL
28 * Version 2] license." If you don't indicate a single choice of license, a
29 * recipient has the option to distribute your version of this file under
30 * either the CDDL, the GPL Version 2 or to extend the choice of license to
31 * its licensees as provided above. However, if you add GPL Version 2 code
32 * and therefore, elected the GPL Version 2 license, then the option applies
33 * only if the new code is made subject to such option by the copyright
34 * holder.
35 */
36
37 /*
38 * @(#)CommandInfo.java 1.12 07/05/14
39 */
40
41 package javax.activation;
42
43 import java.io;
44 import java.beans.Beans;
45
46 /**
47 * The CommandInfo class is used by CommandMap implementations to
48 * describe the results of command requests. It provides the requestor
49 * with both the verb requested, as well as an instance of the
50 * bean. There is also a method that will return the name of the
51 * class that implements the command but <i>it is not guaranteed to
52 * return a valid value</i>. The reason for this is to allow CommandMap
53 * implmentations that subclass CommandInfo to provide special
54 * behavior. For example a CommandMap could dynamically generate
55 * JavaBeans. In this case, it might not be possible to create an
56 * object with all the correct state information solely from the class
57 * name.
58 */
59
60 public class CommandInfo {
61 private String verb;
62 private String className;
63
64 /**
65 * The Constructor for CommandInfo.
66 * @param verb The command verb this CommandInfo decribes.
67 * @param className The command's fully qualified class name.
68 */
69 public CommandInfo(String verb, String className) {
70 this.verb = verb;
71 this.className = className;
72 }
73
74 /**
75 * Return the command verb.
76 *
77 * @return the command verb.
78 */
79 public String getCommandName() {
80 return verb;
81 }
82
83 /**
84 * Return the command's class name. <i>This method MAY return null in
85 * cases where a CommandMap subclassed CommandInfo for its
86 * own purposes.</i> In other words, it might not be possible to
87 * create the correct state in the command by merely knowing
88 * its class name. <b>DO NOT DEPEND ON THIS METHOD RETURNING
89 * A VALID VALUE!</b>
90 *
91 * @return The class name of the command, or <i>null</i>
92 */
93 public String getCommandClass() {
94 return className;
95 }
96
97 /**
98 * Return the instantiated JavaBean component.
99 * <p>
100 * Begin by instantiating the component with
101 * <code>Beans.instantiate()</code>.
102 * <p>
103 * If the bean implements the <code>javax.activation.CommandObject</code>
104 * interface, call its <code>setCommandContext</code> method.
105 * <p>
106 * If the DataHandler parameter is null, then the bean is
107 * instantiated with no data. NOTE: this may be useful
108 * if for some reason the DataHandler that is passed in
109 * throws IOExceptions when this method attempts to
110 * access its InputStream. It will allow the caller to
111 * retrieve a reference to the bean if it can be
112 * instantiated.
113 * <p>
114 * If the bean does NOT implement the CommandObject interface,
115 * this method will check if it implements the
116 * java.io.Externalizable interface. If it does, the bean's
117 * readExternal method will be called if an InputStream
118 * can be acquired from the DataHandler.<p>
119 *
120 * @param dh The DataHandler that describes the data to be
121 * passed to the command.
122 * @param loader The ClassLoader to be used to instantiate the bean.
123 * @return The bean
124 * @see java.beans.Beans#instantiate
125 * @see javax.activation.CommandObject
126 */
127 public Object getCommandObject(DataHandler dh, ClassLoader loader)
128 throws IOException, ClassNotFoundException {
129 Object new_bean = null;
130
131 // try to instantiate the bean
132 new_bean = java.beans.Beans.instantiate(loader, className);
133
134 // if we got one and it is a CommandObject
135 if (new_bean != null) {
136 if (new_bean instanceof CommandObject) {
137 ((CommandObject)new_bean).setCommandContext(verb, dh);
138 } else if (new_bean instanceof Externalizable) {
139 if (dh != null) {
140 InputStream is = dh.getInputStream();
141 if (is != null) {
142 ((Externalizable)new_bean).readExternal(
143 new ObjectInputStream(is));
144 }
145 }
146 }
147 }
148
149 return new_bean;
150 }
151 }