001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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 org.apache.geronimo.deployment.cli;
019    
020    import java.io.File;
021    import java.io.PrintWriter;
022    
023    import javax.enterprise.deploy.spi.DeploymentManager;
024    
025    import org.apache.geronimo.cli.deployer.CommandArgs;
026    import org.apache.geronimo.cli.deployer.InstallLibraryCommandArgs;
027    import org.apache.geronimo.common.DeploymentException;
028    import org.apache.geronimo.deployment.plugin.GeronimoDeploymentManager;
029    import org.apache.geronimo.kernel.repository.Artifact;
030    import jline.ConsoleReader;
031    
032    /**
033     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
034     */
035    public class CommandInstallLibrary extends AbstractCommand {
036    
037        public void execute(ConsoleReader consoleReader, ServerConnection connection, CommandArgs commandArgs) throws DeploymentException {
038            if (!(commandArgs instanceof InstallLibraryCommandArgs)) {
039                throw new DeploymentSyntaxException("CommandArgs has the type [" + commandArgs.getClass() + "]; expected [" + InstallLibraryCommandArgs.class + "]");
040            }
041            InstallLibraryCommandArgs installLibraryCommandArgs = (InstallLibraryCommandArgs)commandArgs;
042            if (installLibraryCommandArgs.getArgs().length == 0) {
043                throw new DeploymentException("Must specify a LibraryFile");
044            }
045            File libFile = new File(installLibraryCommandArgs.getArgs()[0]);
046            if(!libFile.exists() || !libFile.isFile() || !libFile.canRead()) {
047                throw new DeploymentException("File does not exist or not a normal file or not readable. "+libFile);
048            }
049            DeploymentManager dmgr = connection.getDeploymentManager();
050            if(dmgr instanceof GeronimoDeploymentManager) {
051                GeronimoDeploymentManager mgr = (GeronimoDeploymentManager) dmgr;
052                String groupId = installLibraryCommandArgs.getGroupId();
053                try {
054                    Artifact artifact = mgr.installLibrary(libFile, groupId);
055                    if(artifact != null) {
056                        consoleReader.printString(DeployUtils.reformat("Installed "+artifact, 4, 72));
057                    } else {
058                        throw new DeploymentException("Unable to install library "+installLibraryCommandArgs.getArgs()[0]);
059                    }
060                } catch (Exception e) {
061                    throw new DeploymentException("Unable to install library "+installLibraryCommandArgs.getArgs()[0], e);
062                }
063            } else {
064                throw new DeploymentException("Cannot install library using " + dmgr.getClass().getName() + " deployment manager");
065            }
066        }
067    }