001 /** 002 * 003 * Copyright 2003-2004 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package javax.activation; 019 020 import java.beans.Beans; 021 import java.io.Externalizable; 022 import java.io.IOException; 023 import java.io.ObjectInputStream; 024 025 /** 026 * @version $Rev: 123383 $ $Date: 2004-12-26 19:11:00 -0800 (Sun, 26 Dec 2004) $ 027 */ 028 public class CommandInfo { 029 private final String commandName; 030 private final String commandClass; 031 032 /** 033 * Constructor for a CommandInfo 034 * 035 * @param commandName the command name 036 * @param commandClass the name of the command's implementation class 037 */ 038 public CommandInfo(String commandName, String commandClass) { 039 this.commandName = commandName; 040 this.commandClass = commandClass; 041 } 042 043 /** 044 * Return the command name. 045 * 046 * @return the command name 047 */ 048 public String getCommandName() { 049 return commandName; 050 } 051 052 /** 053 * Return the implementation class name. 054 * 055 * @return the name of the command's implementation class; may be null 056 */ 057 public String getCommandClass() { 058 return commandClass; 059 } 060 061 /** 062 * Instantiate and return a command JavaBean. 063 * The bean is created using Beans.instantiate(loader, commandClass). 064 * If the new bean implements CommandObject then its setCommandContext(String, DataHandler) 065 * method is called. 066 * Otherwise if it implements Externalizable and the supplied DataHandler is not null 067 * then its readExternal(ObjectInputStream) method is called with a stream obtained from 068 * DataHandler.getInputStream(). 069 * 070 * @param dh a DataHandler that provides the data to be passed to the command 071 * @param loader the ClassLoader to be used to instantiate the command 072 * @return a new command instance 073 * @throws IOException if there was a problem initializing the command 074 * @throws ClassNotFoundException if the command class could not be found 075 */ 076 public Object getCommandObject(DataHandler dh, ClassLoader loader) throws IOException, ClassNotFoundException { 077 Object bean = Beans.instantiate(loader, commandClass); 078 if (bean instanceof CommandObject) { 079 ((CommandObject) bean).setCommandContext(commandName, dh); 080 } else if (bean instanceof Externalizable && dh != null) { 081 ((Externalizable) bean).readExternal(new ObjectInputStream(dh.getInputStream())); 082 } 083 return bean; 084 } 085 }