KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > activation > CommandInfo


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)CommandInfo.java 1.11 05/11/16
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.activation;
29
30 import java.io.*;
31 import java.beans.Beans JavaDoc;
32
33 /**
34  * The CommandInfo class is used by CommandMap implementations to
35  * describe the results of command requests. It provides the requestor
36  * with both the verb requested, as well as an instance of the
37  * bean. There is also a method that will return the name of the
38  * class that implements the command but <i>it is not guaranteed to
39  * return a valid value</i>. The reason for this is to allow CommandMap
40  * implmentations that subclass CommandInfo to provide special
41  * behavior. For example a CommandMap could dynamically generate
42  * JavaBeans. In this case, it might not be possible to create an
43  * object with all the correct state information solely from the class
44  * name.
45  */

46
47 public class CommandInfo {
48     private String JavaDoc verb;
49     private String JavaDoc className;
50
51     /**
52      * The Constructor for CommandInfo.
53      * @param verb The command verb this CommandInfo decribes.
54      * @param className The command's fully qualified class name.
55      */

56     public CommandInfo(String JavaDoc verb, String JavaDoc className) {
57     this.verb = verb;
58     this.className = className;
59     }
60
61     /**
62      * Return the command verb.
63      *
64      * @return the command verb.
65      */

66     public String JavaDoc getCommandName() {
67     return verb;
68     }
69
70     /**
71      * Return the command's class name. <i>This method MAY return null in
72      * cases where a CommandMap subclassed CommandInfo for its
73      * own purposes.</i> In other words, it might not be possible to
74      * create the correct state in the command by merely knowing
75      * its class name. <b>DO NOT DEPEND ON THIS METHOD RETURNING
76      * A VALID VALUE!</b>
77      *
78      * @return The class name of the command, or <i>null</i>
79      */

80     public String JavaDoc getCommandClass() {
81     return className;
82     }
83
84     /**
85      * Return the instantiated JavaBean component.
86      * <p>
87      * Begin by instantiating the component with
88      * <code>Beans.instantiate()</code>.
89      * <p>
90      * If the bean implements the <code>javax.activation.CommandObject</code>
91      * interface, call its <code>setCommandContext</code> method.
92      * <p>
93      * If the DataHandler parameter is null, then the bean is
94      * instantiated with no data. NOTE: this may be useful
95      * if for some reason the DataHandler that is passed in
96      * throws IOExceptions when this method attempts to
97      * access its InputStream. It will allow the caller to
98      * retrieve a reference to the bean if it can be
99      * instantiated.
100      * <p>
101      * If the bean does NOT implement the CommandObject interface,
102      * this method will check if it implements the
103      * java.io.Externalizable interface. If it does, the bean's
104      * readExternal method will be called if an InputStream
105      * can be acquired from the DataHandler.<p>
106      *
107      * @param dh The DataHandler that describes the data to be
108      * passed to the command.
109      * @param loader The ClassLoader to be used to instantiate the bean.
110      * @return The bean
111      * @see java.beans.Beans#instantiate
112      * @see javax.activation.CommandObject
113      */

114     public Object JavaDoc getCommandObject(DataHandler JavaDoc dh, ClassLoader JavaDoc loader)
115             throws IOException, ClassNotFoundException JavaDoc {
116     Object JavaDoc new_bean = null;
117
118     // try to instantiate the bean
119
new_bean = java.beans.Beans.instantiate(loader, className);
120
121     // if we got one and it is a CommandObject
122
if (new_bean != null) {
123         if (new_bean instanceof CommandObject JavaDoc) {
124         ((CommandObject JavaDoc)new_bean).setCommandContext(verb, dh);
125         } else if (new_bean instanceof Externalizable) {
126         if (dh != null) {
127             InputStream is = dh.getInputStream();
128             if (is != null) {
129             ((Externalizable)new_bean).readExternal(
130                            new ObjectInputStream(is));
131             }
132         }
133         }
134     }
135
136     return new_bean;
137     }
138 }
139
Popular Tags