KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
27 import javax.enterprise.deploy.spi.Target JavaDoc;
28 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
29 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 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 JavaDoc command, String JavaDoc group, String JavaDoc helpArgumentList, String JavaDoc helpText) {
57         super(command, group, helpArgumentList, helpText);
58     }
59
60     protected ProgressObject JavaDoc runCommand(DeploymentManager JavaDoc mgr, PrintWriter JavaDoc out, boolean inPlace, Target JavaDoc[] tlist, File JavaDoc module, File JavaDoc 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 JavaDoc getAction() {
80         return "Distributed";
81     }
82
83     public void execute(PrintWriter JavaDoc out, ServerConnection connection, String JavaDoc[] 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 JavaDoc targets = new ArrayList JavaDoc();
92         args = processTargets(args, targets);
93         if(args.length > 2) {
94             throw new DeploymentSyntaxException("Too many arguments");
95         }
96         File JavaDoc module = null;
97         File JavaDoc plan = null;
98         if(args.length > 0) {
99             File JavaDoc test = new File JavaDoc(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 JavaDoc test = new File JavaDoc(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 JavaDoc targets, PrintWriter JavaDoc out, File JavaDoc module, File JavaDoc plan) throws DeploymentException {
136         final DeploymentManager JavaDoc mgr = connection.getDeploymentManager();
137         TargetModuleID JavaDoc[] results;
138         boolean multipleTargets;
139         ProgressObject JavaDoc po;
140         if(targets.size() > 0) {
141             Target JavaDoc[] 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 JavaDoc[] tlist = mgr.getTargets();
147             multipleTargets = tlist.length > 1;
148             po = runCommand(mgr, out, inPlace, tlist, module, plan);
149             waitForProgress(out, po);
150         }
151
152         // print the results that succeeded
153
results = po.getResultTargetModuleIDs();
154         for (int i = 0; i < results.length; i++) {
155             TargetModuleID JavaDoc 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 JavaDoc 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         // if any results failed then throw so that we'll return non-0
166
// to the operating system
167
if(po.getDeploymentStatus().isFailed()) {
168             throw new DeploymentException("Operation failed: "+po.getDeploymentStatus().getMessage());
169         }
170     }
171
172     private String JavaDoc[] processTargets(String JavaDoc[] args, List JavaDoc targets) {
173         if(args.length >= 2 && args[0].equals("--targets")) {
174             String JavaDoc value = args[1];
175             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(value, ";", false);
176             while(tok.hasMoreTokens()) {
177                 targets.add(tok.nextToken());
178             }
179             String JavaDoc[] temp = new String JavaDoc[args.length-2];
180             System.arraycopy(args, 2, temp, 0, temp.length);
181             args = temp;
182         }
183         return args;
184     }
185     
186     private String JavaDoc[] processInPlace(String JavaDoc[] args, BooleanHolder inPlaceHolder) {
187         if(args.length >= 2 && args[0].equals("--inPlace")) {
188             inPlaceHolder.inPlace = true;
189             String JavaDoc[] temp = new String JavaDoc[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 }
200
Popular Tags