View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package javax.activation;
19  
20  import java.beans.Beans;
21  import java.io.Externalizable;
22  import java.io.IOException;
23  import java.io.ObjectInputStream;
24  
25  /**
26   * @version $Rev: 123383 $ $Date: 2004-12-26 19:11:00 -0800 (Sun, 26 Dec 2004) $
27   */
28  public class CommandInfo {
29      private final String commandName;
30      private final String commandClass;
31  
32      /**
33       * Constructor for a CommandInfo
34       *
35       * @param commandName  the command name
36       * @param commandClass the name of the command's implementation class
37       */
38      public CommandInfo(String commandName, String commandClass) {
39          this.commandName = commandName;
40          this.commandClass = commandClass;
41      }
42  
43      /**
44       * Return the command name.
45       *
46       * @return the command name
47       */
48      public String getCommandName() {
49          return commandName;
50      }
51  
52      /**
53       * Return the implementation class name.
54       *
55       * @return the name of the command's implementation class; may be null
56       */
57      public String getCommandClass() {
58          return commandClass;
59      }
60  
61      /**
62       * Instantiate and return a command JavaBean.
63       * The bean is created using Beans.instantiate(loader, commandClass).
64       * If the new bean implements CommandObject then its setCommandContext(String, DataHandler)
65       * method is called.
66       * Otherwise if it implements Externalizable and the supplied DataHandler is not null
67       * then its readExternal(ObjectInputStream) method is called with a stream obtained from
68       * DataHandler.getInputStream().
69       *
70       * @param dh a DataHandler that provides the data to be passed to the command
71       * @param loader the ClassLoader to be used to instantiate the command
72       * @return a new command instance
73       * @throws IOException if there was a problem initializing the command
74       * @throws ClassNotFoundException if the command class could not be found
75       */
76      public Object getCommandObject(DataHandler dh, ClassLoader loader) throws IOException, ClassNotFoundException {
77          Object bean = Beans.instantiate(loader, commandClass);
78          if (bean instanceof CommandObject) {
79              ((CommandObject) bean).setCommandContext(commandName, dh);
80          } else if (bean instanceof Externalizable && dh != null) {
81              ((Externalizable) bean).readExternal(new ObjectInputStream(dh.getInputStream()));
82          }
83          return bean;
84      }
85  }