View Javadoc

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.TargetModuleID;
23  import javax.enterprise.deploy.spi.Target;
24  import javax.enterprise.deploy.spi.DeploymentManager;
25  import javax.enterprise.deploy.spi.exceptions.TargetException;
26  import java.io.PrintWriter;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  /**
31   * The CLI deployer logic to list modules.
32   *
33   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
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 out, ServerConnection connection, String[] args) throws DeploymentException {
46          List targets = new ArrayList();
47          Boolean started = null;
48          for (int i = 0; i < args.length; i++) {
49              String 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 mgr = connection.getDeploymentManager();
73          TargetModuleID[] running = null, notrunning = null;
74          Target[] 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 e) {
86              throw new DeploymentException("Unable to query modules", e);
87          } catch (IllegalStateException e) {
88              throw new DeploymentSyntaxException(e.getMessage());
89          }
90          if(running == null) {
91              running = new TargetModuleID[0];
92          }
93          if(notrunning == null) {
94              notrunning = new TargetModuleID[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 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 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 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 child = result.getChildTargetModuleID()[j];
115                     out.println("      `-> "+child.getModuleID());
116                 }
117             }
118         }
119     }
120 }