KickJava   Java API By Example, From Geeks To Geeks.

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


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.TargetModuleID JavaDoc;
23 import javax.enterprise.deploy.spi.Target JavaDoc;
24 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
25 import javax.enterprise.deploy.spi.exceptions.TargetException JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * The CLI deployer logic to list modules.
32  *
33  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
34  */

35 public class CommandListModules extends AbstractCommand {
36     public CommandListModules() {
37         super("list-modules", "2. Other Commands", "[--all|--started|--stopped] [target*]",
38                 "Lists the modules available on the specified targets. If " +
39                 "--started or --stopped is specified, only started or stopped modules will " +
40                 "be listed; otherwise all modules will be listed. If no targets are " +
41                 "specified, then modules on all targets will be listed; otherwise only " +
42                 "modules on the specified targets.");
43     }
44
45     public void execute(PrintWriter JavaDoc out, ServerConnection connection, String JavaDoc[] args) throws DeploymentException {
46         List JavaDoc targets = new ArrayList JavaDoc();
47         Boolean JavaDoc started = null;
48         for (int i = 0; i < args.length; i++) {
49             String JavaDoc arg = args[i];
50             if(arg.startsWith("--")) {
51                 if(arg.equals("--all")) {
52                     if(started != null) {
53                         throw new DeploymentSyntaxException("Cannot specify more than one of --all, --started, --stopped");
54                     }
55                 } else if(arg.equals("--started")) {
56                     if(started != null) {
57                         throw new DeploymentSyntaxException("Cannot specify more than one of --all, --started, --stopped");
58                     }
59                     started = Boolean.TRUE;
60                 } else if(arg.equals("--stopped")) {
61                     if(started != null) {
62                         throw new DeploymentSyntaxException("Cannot specify more than one of --all, --started, --stopped");
63                     }
64                     started = Boolean.FALSE;
65                 } else {
66                     throw new DeploymentSyntaxException("Unrecognized option '"+arg+"'");
67                 }
68             } else {
69                 targets.add(arg);
70             }
71         }
72         final DeploymentManager JavaDoc mgr = connection.getDeploymentManager();
73         TargetModuleID JavaDoc[] running = null, notrunning = null;
74         Target JavaDoc[] tlist = identifyTargets(targets, mgr);
75         if(tlist.length == 0) {
76             tlist = mgr.getTargets();
77         }
78         try {
79             if(started == null || started.booleanValue()) {
80                 running = mgr.getRunningModules(null, tlist);
81             }
82             if(started == null || !started.booleanValue()) {
83                 notrunning = mgr.getNonRunningModules(null, tlist);
84             }
85         } catch (TargetException JavaDoc e) {
86             throw new DeploymentException("Unable to query modules", e);
87         } catch (IllegalStateException JavaDoc e) {
88             throw new DeploymentSyntaxException(e.getMessage());
89         }
90         if(running == null) {
91             running = new TargetModuleID JavaDoc[0];
92         }
93         if(notrunning == null) {
94             notrunning = new TargetModuleID JavaDoc[0];
95         }
96
97         int total = running.length+notrunning.length;
98         out.println("Found "+total+" module"+(total != 1 ? "s" : ""));
99         for (int i = 0; i < running.length; i++) {
100             TargetModuleID JavaDoc result = running[i];
101             out.println(" + "+result.getModuleID()+(tlist.length > 1 ? " on "+result.getTarget().getName(): "")+(result.getWebURL() == null ? "" : " @ "+result.getWebURL()));
102             if(result.getChildTargetModuleID() != null) {
103                 for (int j = 0; j < result.getChildTargetModuleID().length; j++) {
104                     TargetModuleID JavaDoc child = result.getChildTargetModuleID()[j];
105                     out.println(" `-> "+child.getModuleID()+(child.getWebURL() == null ? "" : " @ "+child.getWebURL()));
106                 }
107             }
108         }
109         for (int i = 0; i < notrunning.length; i++) {
110             TargetModuleID JavaDoc result = notrunning[i];
111             out.println(" "+result.getModuleID()+(tlist.length > 1 ? " on "+result.getTarget().getName(): ""));
112             if(result.getChildTargetModuleID() != null) {
113                 for (int j = 0; j < result.getChildTargetModuleID().length; j++) {
114                     TargetModuleID JavaDoc child = result.getChildTargetModuleID()[j];
115                     out.println(" `-> "+child.getModuleID());
116                 }
117             }
118         }
119     }
120 }
121
Popular Tags