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 org.apache.geronimo.deployment.cli;
19
20 import java.io.File;
21 import java.io.PrintWriter;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.StringTokenizer;
25
26 import javax.enterprise.deploy.spi.DeploymentManager;
27 import javax.enterprise.deploy.spi.Target;
28 import javax.enterprise.deploy.spi.TargetModuleID;
29 import javax.enterprise.deploy.spi.status.ProgressObject;
30
31 import org.apache.geronimo.common.DeploymentException;
32 import org.apache.geronimo.deployment.plugin.jmx.JMXDeploymentManager;
33
34 /**
35 * The CLI deployer logic to distribute.
36 *
37 * @version $Rev: 405624 $ $Date: 2006-05-09 21:08:49 -0700 (Tue, 09 May 2006) $
38 */
39 public class CommandDistribute extends AbstractCommand {
40 public CommandDistribute() {
41 super("distribute", "2. Other Commands", "[--inPlace] [--targets target;target;...] [module] [plan]",
42 "Processes a module and adds it to the server environment, but does "+
43 "not start it or mark it to be started in the future. " +
44 "Normally both a module and plan are passed to the deployer. " +
45 "Sometimes the module contains a plan, or requires no plan, in which case " +
46 "the plan may be omitted. Sometimes the plan references a module already " +
47 "deployed in the Geronimo server environment, in which case a module does " +
48 "not need to be provided.\n" +
49 "If no targets are provided, the module is distributed to all available " +
50 "targets. Geronimo only provides one target (ever), so this is primarily " +
51 "useful when using a different driver.\n" +
52 "If inPlace is provided, the module is not copied to the configuration " +
53 "store of the selected targets. The targets directly use the module.");
54 }
55
56 protected CommandDistribute(String command, String group, String helpArgumentList, String helpText) {
57 super(command, group, helpArgumentList, helpText);
58 }
59
60 protected ProgressObject runCommand(DeploymentManager mgr, PrintWriter out, boolean inPlace, Target[] tlist, File module, File plan) throws DeploymentException {
61 if (inPlace) {
62 if (!(mgr instanceof JMXDeploymentManager)) {
63 throw new DeploymentSyntaxException(
64 "Target DeploymentManager is not a Geronimo one. \n" +
65 "Cannot perform in-place deployment.");
66 }
67 JMXDeploymentManager jmxMgr = (JMXDeploymentManager) mgr;
68 try {
69 jmxMgr.setInPlace(true);
70 return mgr.distribute(tlist, module, plan);
71 } finally {
72 jmxMgr.setInPlace(false);
73 }
74 } else {
75 return mgr.distribute(tlist, module, plan);
76 }
77 }
78
79 protected String getAction() {
80 return "Distributed";
81 }
82
83 public void execute(PrintWriter out, ServerConnection connection, String[] args) throws DeploymentException {
84 if(args.length == 0) {
85 throw new DeploymentSyntaxException("Must specify a module or plan (or both)");
86 }
87
88 BooleanHolder inPlaceHolder = new BooleanHolder();
89 args = processInPlace(args, inPlaceHolder);
90
91 List targets = new ArrayList();
92 args = processTargets(args, targets);
93 if(args.length > 2) {
94 throw new DeploymentSyntaxException("Too many arguments");
95 }
96 File module = null;
97 File plan = null;
98 if(args.length > 0) {
99 File test = new File(args[0]);
100 if(DeployUtils.isJarFile(test) || test.isDirectory()) {
101 if(module != null) {
102 throw new DeploymentSyntaxException("Module and plan cannot both be JAR files or directories!");
103 }
104 module = test;
105 } else {
106 if(plan != null) {
107 throw new DeploymentSyntaxException("Module or plan must be a JAR file or directory!");
108 }
109 plan = test;
110 }
111 }
112 if(args.length > 1) {
113 File test = new File(args[1]);
114 if(DeployUtils.isJarFile(test) || test.isDirectory()) {
115 if(module != null) {
116 throw new DeploymentSyntaxException("Module and plan cannot both be JAR files or directories!");
117 }
118 module = test;
119 } else {
120 if(plan != null) {
121 throw new DeploymentSyntaxException("Module or plan must be a JAR file or directory!");
122 }
123 plan = test;
124 }
125 }
126 if(module != null) {
127 module = module.getAbsoluteFile();
128 }
129 if(plan != null) {
130 plan = plan.getAbsoluteFile();
131 }
132 executeOnline(connection, inPlaceHolder.inPlace, targets, out, module, plan);
133 }
134
135 private void executeOnline(ServerConnection connection, boolean inPlace, List targets, PrintWriter out, File module, File plan) throws DeploymentException {
136 final DeploymentManager mgr = connection.getDeploymentManager();
137 TargetModuleID[] results;
138 boolean multipleTargets;
139 ProgressObject po;
140 if(targets.size() > 0) {
141 Target[] tlist = identifyTargets(targets, mgr);
142 multipleTargets = tlist.length > 1;
143 po = runCommand(mgr, out, inPlace, tlist, module, plan);
144 waitForProgress(out, po);
145 } else {
146 final Target[] tlist = mgr.getTargets();
147 multipleTargets = tlist.length > 1;
148 po = runCommand(mgr, out, inPlace, tlist, module, plan);
149 waitForProgress(out, po);
150 }
151
152
153 results = po.getResultTargetModuleIDs();
154 for (int i = 0; i < results.length; i++) {
155 TargetModuleID result = results[i];
156 out.print(DeployUtils.reformat(getAction()+" "+result.getModuleID()+(multipleTargets ? " to "+result.getTarget().getName() : "")+(result.getWebURL() == null || !getAction().equals("Deployed") ? "" : " @ "+result.getWebURL()), 4, 72));
157 if(result.getChildTargetModuleID() != null) {
158 for (int j = 0; j < result.getChildTargetModuleID().length; j++) {
159 TargetModuleID child = result.getChildTargetModuleID()[j];
160 out.print(DeployUtils.reformat(" `-> "+child.getModuleID()+(child.getWebURL() == null || !getAction().equals("Deployed") ? "" : " @ "+child.getWebURL()),4, 72));
161 }
162 }
163 }
164
165
166
167 if(po.getDeploymentStatus().isFailed()) {
168 throw new DeploymentException("Operation failed: "+po.getDeploymentStatus().getMessage());
169 }
170 }
171
172 private String[] processTargets(String[] args, List targets) {
173 if(args.length >= 2 && args[0].equals("--targets")) {
174 String value = args[1];
175 StringTokenizer tok = new StringTokenizer(value, ";", false);
176 while(tok.hasMoreTokens()) {
177 targets.add(tok.nextToken());
178 }
179 String[] temp = new String[args.length-2];
180 System.arraycopy(args, 2, temp, 0, temp.length);
181 args = temp;
182 }
183 return args;
184 }
185
186 private String[] processInPlace(String[] args, BooleanHolder inPlaceHolder) {
187 if(args.length >= 2 && args[0].equals("--inPlace")) {
188 inPlaceHolder.inPlace = true;
189 String[] temp = new String[args.length - 1];
190 System.arraycopy(args, 1, temp, 0, temp.length);
191 args = temp;
192 }
193 return args;
194 }
195
196 private final class BooleanHolder {
197 public boolean inPlace;
198 }
199 }