01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.geronimo.deployment.cli;
17:
18: import java.io.File;
19: import java.io.PrintWriter;
20:
21: import javax.enterprise.deploy.spi.DeploymentManager;
22:
23: import org.apache.geronimo.cli.deployer.CommandArgs;
24: import org.apache.geronimo.cli.deployer.InstallLibraryCommandArgs;
25: import org.apache.geronimo.common.DeploymentException;
26: import org.apache.geronimo.deployment.plugin.GeronimoDeploymentManager;
27: import org.apache.geronimo.kernel.repository.Artifact;
28: import jline.ConsoleReader;
29:
30: /**
31: * @version $Rev: 602983 $ $Date: 2007-12-10 10:20:09 -0800 (Mon, 10 Dec 2007) $
32: */
33: public class CommandInstallLibrary extends AbstractCommand {
34:
35: public void execute(ConsoleReader consoleReader,
36: ServerConnection connection, CommandArgs commandArgs)
37: throws DeploymentException {
38: if (!(commandArgs instanceof InstallLibraryCommandArgs)) {
39: throw new DeploymentSyntaxException(
40: "CommandArgs has the type ["
41: + commandArgs.getClass() + "]; expected ["
42: + InstallLibraryCommandArgs.class + "]");
43: }
44: InstallLibraryCommandArgs installLibraryCommandArgs = (InstallLibraryCommandArgs) commandArgs;
45: if (installLibraryCommandArgs.getArgs().length == 0) {
46: throw new DeploymentException("Must specify a LibraryFile");
47: }
48: File libFile = new File(installLibraryCommandArgs.getArgs()[0]);
49: if (!libFile.exists() || !libFile.isFile()
50: || !libFile.canRead()) {
51: throw new DeploymentException(
52: "File does not exist or not a normal file or not readable. "
53: + libFile);
54: }
55: DeploymentManager dmgr = connection.getDeploymentManager();
56: if (dmgr instanceof GeronimoDeploymentManager) {
57: GeronimoDeploymentManager mgr = (GeronimoDeploymentManager) dmgr;
58: String groupId = installLibraryCommandArgs.getGroupId();
59: try {
60: Artifact artifact = mgr
61: .installLibrary(libFile, groupId);
62: if (artifact != null) {
63: consoleReader.printString(DeployUtils.reformat(
64: "Installed " + artifact, 4, 72));
65: } else {
66: throw new DeploymentException(
67: "Unable to install library "
68: + installLibraryCommandArgs
69: .getArgs()[0]);
70: }
71: } catch (Exception e) {
72: throw new DeploymentException(
73: "Unable to install library "
74: + installLibraryCommandArgs.getArgs()[0],
75: e);
76: }
77: } else {
78: throw new DeploymentException(
79: "Cannot install library when connected to "
80: + connection.getServerURI());
81: }
82: }
83: }
|