KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > cli > AbstractCommand


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
23 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
24 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
25 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
26 import javax.enterprise.deploy.spi.Target JavaDoc;
27 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.LinkedList JavaDoc;
35
36 /**
37  * Base class for CLI deployer commands. Tracks some simple properties and
38  * has common utility methods.
39  *
40  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
41  */

42 public abstract class AbstractCommand implements DeployCommand {
43     private String JavaDoc command;
44     private String JavaDoc group;
45     private String JavaDoc helpArgumentList;
46     private String JavaDoc helpText;
47     private PrintWriter JavaDoc out = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(System.out));
48
49     public AbstractCommand(String JavaDoc command, String JavaDoc group, String JavaDoc helpArgumentList, String JavaDoc helpText) {
50         this.command = command;
51         this.group = group;
52         this.helpArgumentList = helpArgumentList;
53         this.helpText = helpText;
54     }
55
56     public String JavaDoc getCommandName() {
57         return command;
58     }
59
60     public String JavaDoc getHelpArgumentList() {
61         return helpArgumentList;
62     }
63
64     public String JavaDoc getHelpText() {
65         return helpText;
66     }
67
68     public String JavaDoc getCommandGroup() {
69         return group;
70     }
71
72     public boolean isLocalOnly() {
73         return false;
74     }
75
76     public void setOut(PrintWriter JavaDoc out) {
77         this.out = out;
78     }
79
80     protected void emit(String JavaDoc 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 JavaDoc out, ProgressObject JavaDoc po) {
95         po.addProgressListener(new ProgressListener JavaDoc() {
96             String JavaDoc last = null;
97             public void handleProgressEvent(ProgressEvent JavaDoc event) {
98                 String JavaDoc 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 JavaDoc e) {
109                 e.printStackTrace(out);
110             }
111         }
112         return;
113     }
114
115     protected static boolean isMultipleTargets(TargetModuleID JavaDoc[] ids) {
116         Set JavaDoc set = new HashSet JavaDoc();
117         for(int i = 0; i < ids.length; i++) {
118             TargetModuleID JavaDoc id = ids[i];
119             set.add(id.getTarget().getName());
120         }
121         return set.size() > 1;
122     }
123
124     protected static Target JavaDoc[] identifyTargets(List JavaDoc targetNames, final DeploymentManager JavaDoc mgr) throws DeploymentException {
125         Target JavaDoc[] tlist = new Target JavaDoc[targetNames.size()];
126         Target JavaDoc[] all = mgr.getTargets();
127         Set JavaDoc found = new HashSet JavaDoc();
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 JavaDoc 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 }
147
Popular Tags