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 org.apache.geronimo.common.DeploymentException;
21
22 import javax.enterprise.deploy.spi.status.ProgressObject;
23 import javax.enterprise.deploy.spi.status.ProgressListener;
24 import javax.enterprise.deploy.spi.status.ProgressEvent;
25 import javax.enterprise.deploy.spi.TargetModuleID;
26 import javax.enterprise.deploy.spi.Target;
27 import javax.enterprise.deploy.spi.DeploymentManager;
28 import java.io.PrintWriter;
29 import java.io.OutputStreamWriter;
30 import java.util.List;
31 import java.util.Set;
32 import java.util.HashSet;
33 import java.util.Collection;
34 import java.util.LinkedList;
35
36 /**
37 * Base class for CLI deployer commands. Tracks some simple properties and
38 * has common utility methods.
39 *
40 * @version $Rev: 405624 $ $Date: 2006-05-09 21:08:49 -0700 (Tue, 09 May 2006) $
41 */
42 public abstract class AbstractCommand implements DeployCommand {
43 private String command;
44 private String group;
45 private String helpArgumentList;
46 private String helpText;
47 private PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
48
49 public AbstractCommand(String command, String group, String helpArgumentList, String helpText) {
50 this.command = command;
51 this.group = group;
52 this.helpArgumentList = helpArgumentList;
53 this.helpText = helpText;
54 }
55
56 public String getCommandName() {
57 return command;
58 }
59
60 public String getHelpArgumentList() {
61 return helpArgumentList;
62 }
63
64 public String getHelpText() {
65 return helpText;
66 }
67
68 public String getCommandGroup() {
69 return group;
70 }
71
72 public boolean isLocalOnly() {
73 return false;
74 }
75
76 public void setOut(PrintWriter out) {
77 this.out = out;
78 }
79
80 protected void emit(String message) {
81 out.print(DeployUtils.reformat(message,4,72));
82 out.flush();
83 }
84
85 /**
86 * Busy-waits until the provided <code>ProgressObject</code>
87 * indicates that it's no longer running.
88 *
89 * @param out a <code>PrintWriter</code> value, only used in case
90 * of an <code>InterruptedException</code> to output the stack
91 * trace.
92 * @param po a <code>ProgressObject</code> value
93 */
94 protected void waitForProgress(PrintWriter out, ProgressObject po) {
95 po.addProgressListener(new ProgressListener() {
96 String last = null;
97 public void handleProgressEvent(ProgressEvent event) {
98 String msg = event.getDeploymentStatus().getMessage();
99 if(last != null && !last.equals(msg)) {
100 emit(last);
101 }
102 last = msg;
103 }
104 });
105 while(po.getDeploymentStatus().isRunning()) {
106 try {
107 Thread.sleep(100);
108 } catch (InterruptedException e) {
109 e.printStackTrace(out);
110 }
111 }
112 return;
113 }
114
115 protected static boolean isMultipleTargets(TargetModuleID[] ids) {
116 Set set = new HashSet();
117 for(int i = 0; i < ids.length; i++) {
118 TargetModuleID id = ids[i];
119 set.add(id.getTarget().getName());
120 }
121 return set.size() > 1;
122 }
123
124 protected static Target[] identifyTargets(List targetNames, final DeploymentManager mgr) throws DeploymentException {
125 Target[] tlist = new Target[targetNames.size()];
126 Target[] all = mgr.getTargets();
127 Set found = new HashSet();
128 for (int i = 0; i < tlist.length; i++) {
129 if(found.contains(targetNames.get(i))) {
130 throw new DeploymentException("Target list should not contain duplicates ("+targetNames.get(i)+")");
131 }
132 for (int j = 0; j < all.length; j++) {
133 Target server = all[j];
134 if(server.getName().equals(targetNames.get(i))) {
135 tlist[i] = server;
136 found.add(server.getName());
137 break;
138 }
139 }
140 if(tlist[i] == null) {
141 throw new DeploymentException("No target named '"+targetNames.get(i)+"' was found");
142 }
143 }
144 return tlist;
145 }
146 }