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